More editing. Use gcc, not ld to link. On native Win32 use _unlink(). Use
[platform/upstream/glib.git] / glib / gstring.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Portions Copyright (C) 1999 Tony Gale
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GLib Team and others 1997-1999.  See the AUTHORS
23  * file for a list of people on the GLib Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 /* 
29  * MT safe
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include "glib.h"
45
46 #ifdef NATIVE_WIN32
47 #include <io.h>                 /* For _read */
48 #endif
49
50 #define G_STRING_BLOCK_SIZE 512
51
52 typedef struct _GRealStringChunk GRealStringChunk;
53 typedef struct _GRealString      GRealString;
54
55 struct _GRealStringChunk
56 {
57   GHashTable *const_table;
58   GSList     *storage_list;
59   gint        storage_next;
60   gint        this_size;
61   gint        default_size;
62 };
63
64 struct _GRealString
65 {
66   gchar *str;
67   gint   len;
68   gint   alloc;
69 };
70
71 G_LOCK_DEFINE_STATIC (string_mem_chunk);
72 static GMemChunk *string_mem_chunk = NULL;
73
74 /* Hash Functions.
75  */
76
77 gint
78 g_str_equal (gconstpointer v, gconstpointer v2)
79 {
80   return strcmp ((const gchar*) v, (const gchar*)v2) == 0;
81 }
82
83 /* a char* hash function from ASU */
84 guint
85 g_str_hash (gconstpointer v)
86 {
87   const char *s = (char*)v;
88   const char *p;
89   guint h=0, g;
90
91   for(p = s; *p != '\0'; p += 1) {
92     h = ( h << 4 ) + *p;
93     if ( ( g = h & 0xf0000000 ) ) {
94       h = h ^ (g >> 24);
95       h = h ^ g;
96     }
97   }
98
99   return h /* % M */;
100 }
101
102
103 /* String Chunks.
104  */
105
106 GStringChunk*
107 g_string_chunk_new (gint default_size)
108 {
109   GRealStringChunk *new_chunk = g_new (GRealStringChunk, 1);
110   gint size = 1;
111
112   while (size < default_size)
113     size <<= 1;
114
115   new_chunk->const_table       = NULL;
116   new_chunk->storage_list      = NULL;
117   new_chunk->storage_next      = size;
118   new_chunk->default_size      = size;
119   new_chunk->this_size         = size;
120
121   return (GStringChunk*) new_chunk;
122 }
123
124 void
125 g_string_chunk_free (GStringChunk *fchunk)
126 {
127   GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
128   GSList *tmp_list;
129
130   g_return_if_fail (chunk != NULL);
131
132   if (chunk->storage_list)
133     {
134       for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
135         g_free (tmp_list->data);
136
137       g_slist_free (chunk->storage_list);
138     }
139
140   if (chunk->const_table)
141     g_hash_table_destroy (chunk->const_table);
142
143   g_free (chunk);
144 }
145
146 gchar*
147 g_string_chunk_insert (GStringChunk *fchunk,
148                        const gchar  *string)
149 {
150   GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
151   gint len = strlen (string);
152   char* pos;
153
154   g_return_val_if_fail (chunk != NULL, NULL);
155
156   if ((chunk->storage_next + len + 1) > chunk->this_size)
157     {
158       gint new_size = chunk->default_size;
159
160       while (new_size < len+1)
161         new_size <<= 1;
162
163       chunk->storage_list = g_slist_prepend (chunk->storage_list,
164                                              g_new (char, new_size));
165
166       chunk->this_size = new_size;
167       chunk->storage_next = 0;
168     }
169
170   pos = ((char*)chunk->storage_list->data) + chunk->storage_next;
171
172   strcpy (pos, string);
173
174   chunk->storage_next += len + 1;
175
176   return pos;
177 }
178
179 gchar*
180 g_string_chunk_insert_const (GStringChunk *fchunk,
181                              const gchar  *string)
182 {
183   GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
184   char* lookup;
185
186   g_return_val_if_fail (chunk != NULL, NULL);
187
188   if (!chunk->const_table)
189     chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
190
191   lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
192
193   if (!lookup)
194     {
195       lookup = g_string_chunk_insert (fchunk, string);
196       g_hash_table_insert (chunk->const_table, lookup, lookup);
197     }
198
199   return lookup;
200 }
201
202 /* Strings.
203  */
204 static gint
205 nearest_power (gint num)
206 {
207   gint n = 1;
208
209   while (n < num)
210     n <<= 1;
211
212   return n;
213 }
214
215 static gint
216 nearest_multiple (int num, const int block)
217 {
218   gint n = block;
219
220   while (n < num)
221     n += block;
222
223   return n;
224 }
225
226 static void
227 g_string_maybe_expand (GRealString* string, gint len)
228 {
229   if (string->len + len >= string->alloc)
230     {
231       string->alloc = nearest_power (string->len + len + 1);
232       string->str = g_realloc (string->str, string->alloc);
233     }
234 }
235
236 static void
237 g_string_set_size (GRealString* string, gint size)
238 {
239   if (string->alloc <= size) {
240     string->alloc = nearest_power(size + 1);
241     string->str = g_realloc (string->str, string->alloc);
242   }
243 }
244
245 GString*
246 g_string_sized_new (guint dfl_size)
247 {
248   GRealString *string;
249
250   G_LOCK (string_mem_chunk);
251   if (!string_mem_chunk)
252     string_mem_chunk = g_mem_chunk_new ("string mem chunk",
253                                         sizeof (GRealString),
254                                         1024, G_ALLOC_AND_FREE);
255
256   string = g_chunk_new (GRealString, string_mem_chunk);
257   G_UNLOCK (string_mem_chunk);
258
259   string->alloc = 0;
260   string->len   = 0;
261   string->str   = NULL;
262
263   g_string_maybe_expand (string, MAX (dfl_size, 2));
264   string->str[0] = 0;
265
266   return (GString*) string;
267 }
268
269 GString*
270 g_string_new (const gchar *init)
271 {
272   GString *string;
273
274   string = g_string_sized_new (2);
275
276   if (init)
277     g_string_append (string, init);
278
279   return string;
280 }
281
282 void
283 g_string_free (GString *string,
284                gint free_segment)
285 {
286   g_return_if_fail (string != NULL);
287
288   if (free_segment)
289     g_free (string->str);
290
291   G_LOCK (string_mem_chunk);
292   g_mem_chunk_free (string_mem_chunk, string);
293   G_UNLOCK (string_mem_chunk);
294 }
295
296 GString*
297 g_string_assign (GString *lval,
298                  const gchar *rval)
299 {
300   g_string_truncate (lval, 0);
301   g_string_append (lval, rval);
302
303   return lval;
304 }
305
306 GString*
307 g_string_truncate (GString* fstring,
308                    gint len)
309 {
310   GRealString *string = (GRealString*)fstring;
311
312   g_return_val_if_fail (string != NULL, NULL);
313
314   string->len = len;
315
316   string->str[len] = 0;
317
318   return fstring;
319 }
320
321 GString*
322 g_string_append (GString *fstring,
323                  const gchar *val)
324 {
325   GRealString *string = (GRealString*)fstring;
326   int len;
327
328   g_return_val_if_fail (string != NULL, NULL);
329   g_return_val_if_fail (val != NULL, fstring);
330   
331   len = strlen (val);
332   g_string_maybe_expand (string, len);
333
334   strcpy (string->str + string->len, val);
335
336   string->len += len;
337
338   return fstring;
339 }
340
341 GString*
342 g_string_append_c (GString *fstring,
343                    gchar c)
344 {
345   GRealString *string = (GRealString*)fstring;
346
347   g_return_val_if_fail (string != NULL, NULL);
348   g_string_maybe_expand (string, 1);
349
350   string->str[string->len++] = c;
351   string->str[string->len] = 0;
352
353   return fstring;
354 }
355
356 GString*
357 g_string_prepend (GString *fstring,
358                   const gchar *val)
359 {
360   GRealString *string = (GRealString*)fstring;
361   gint len;
362
363   g_return_val_if_fail (string != NULL, NULL);
364   g_return_val_if_fail (val != NULL, fstring);
365
366   len = strlen (val);
367   g_string_maybe_expand (string, len);
368
369   g_memmove (string->str + len, string->str, string->len);
370
371   strncpy (string->str, val, len);
372
373   string->len += len;
374
375   string->str[string->len] = 0;
376
377   return fstring;
378 }
379
380 GString*
381 g_string_prepend_c (GString *fstring,
382                     gchar    c)
383 {
384   GRealString *string = (GRealString*)fstring;
385
386   g_return_val_if_fail (string != NULL, NULL);
387   g_string_maybe_expand (string, 1);
388
389   g_memmove (string->str + 1, string->str, string->len);
390
391   string->str[0] = c;
392
393   string->len += 1;
394
395   string->str[string->len] = 0;
396
397   return fstring;
398 }
399
400 GString*
401 g_string_insert (GString     *fstring,
402                  gint         pos,
403                  const gchar *val)
404 {
405   GRealString *string = (GRealString*)fstring;
406   gint len;
407
408   g_return_val_if_fail (string != NULL, NULL);
409   g_return_val_if_fail (val != NULL, fstring);
410   g_return_val_if_fail (pos >= 0, fstring);
411   g_return_val_if_fail (pos <= string->len, fstring);
412
413   len = strlen (val);
414   g_string_maybe_expand (string, len);
415
416   g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
417
418   strncpy (string->str + pos, val, len);
419
420   string->len += len;
421
422   string->str[string->len] = 0;
423
424   return fstring;
425 }
426
427 GString *
428 g_string_insert_c (GString *fstring,
429                    gint     pos,
430                    gchar    c)
431 {
432   GRealString *string = (GRealString*)fstring;
433
434   g_return_val_if_fail (string != NULL, NULL);
435   g_return_val_if_fail (pos <= string->len, fstring);
436
437   g_string_maybe_expand (string, 1);
438
439   g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
440
441   string->str[pos] = c;
442
443   string->len += 1;
444
445   string->str[string->len] = 0;
446
447   return fstring;
448 }
449
450 GString*
451 g_string_erase (GString *fstring,
452                 gint pos,
453                 gint len)
454 {
455   GRealString *string = (GRealString*)fstring;
456
457   g_return_val_if_fail (string != NULL, NULL);
458   g_return_val_if_fail (len >= 0, fstring);
459   g_return_val_if_fail (pos >= 0, fstring);
460   g_return_val_if_fail (pos <= string->len, fstring);
461   g_return_val_if_fail (pos + len <= string->len, fstring);
462
463   if (pos + len < string->len)
464     g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
465
466   string->len -= len;
467   
468   string->str[string->len] = 0;
469
470   return fstring;
471 }
472
473 GString*
474 g_string_down (GString *fstring)
475 {
476   GRealString *string = (GRealString*)fstring;
477   gchar *s;
478
479   g_return_val_if_fail (string != NULL, NULL);
480
481   s = string->str;
482
483   while (*s)
484     {
485       *s = tolower (*s);
486       s++;
487     }
488
489   return fstring;
490 }
491
492 GString*
493 g_string_up (GString *fstring)
494 {
495   GRealString *string = (GRealString*)fstring;
496   gchar *s;
497
498   g_return_val_if_fail (string != NULL, NULL);
499
500   s = string->str;
501
502   while (*s)
503     {
504       *s = toupper (*s);
505       s++;
506     }
507
508   return fstring;
509 }
510
511 static void
512 g_string_sprintfa_int (GString     *string,
513                        const gchar *fmt,
514                        va_list      args)
515 {
516   gchar *buffer;
517
518   buffer = g_strdup_vprintf (fmt, args);
519   g_string_append (string, buffer);
520   g_free (buffer);
521 }
522
523 void
524 g_string_sprintf (GString *string,
525                   const gchar *fmt,
526                   ...)
527 {
528   va_list args;
529
530   g_string_truncate (string, 0);
531
532   va_start (args, fmt);
533   g_string_sprintfa_int (string, fmt, args);
534   va_end (args);
535 }
536
537 void
538 g_string_sprintfa (GString *string,
539                    const gchar *fmt,
540                    ...)
541 {
542   va_list args;
543
544   va_start (args, fmt);
545   g_string_sprintfa_int (string, fmt, args);
546   va_end (args);
547 }
548
549 GStringError
550 g_string_readline (GString *dest_str,
551                    gint     max_length,
552                    gint     fd)
553 {
554   gint count=0, retval;
555   gchar c;
556
557   g_return_val_if_fail (dest_str != NULL, G_STRING_ERROR_INVAL);
558   g_return_val_if_fail (max_length > 0, G_STRING_ERROR_INVAL);
559   g_string_truncate(dest_str, 0);
560
561   for (count = 0; count < max_length; count++) {
562     if ( (retval = read(fd, &c, 1)) == 1 ) {
563       if (c == '\r') {
564         continue;
565       }
566       if (c == '\n') {
567         return(G_STRING_ERROR_NONE);
568       }
569       g_string_maybe_expand ((GRealString *) dest_str, 1);
570       dest_str->str[dest_str->len++] = c;
571       dest_str->str[dest_str->len] = 0;
572     } else if (retval == 0) {
573         return(G_STRING_ERROR_NODATA);
574     } else {
575       return(G_STRING_ERROR_READ);
576     }
577   }
578   return(G_STRING_ERROR_LENGTH);
579 }
580
581 GStringError
582 g_string_readline_buffered (GString *dest_str,
583                             GString *buff_str,
584                             gint     max_length,
585                             gint     fd,
586                             gint     match_bare_cr)
587 {
588   guint count, i=0, buff_size;
589
590   g_return_val_if_fail (dest_str != NULL, G_STRING_ERROR_INVAL);
591   g_return_val_if_fail (buff_str != NULL, G_STRING_ERROR_INVAL);
592   g_return_val_if_fail (max_length > 0, G_STRING_ERROR_INVAL);
593
594   /* Make the buffer a multiple of G_STRING_BLOCK_SIZE and
595    * bigger then max_length */
596   buff_size = nearest_multiple(max_length, G_STRING_BLOCK_SIZE);
597   g_string_set_size( (GRealString *) buff_str, buff_size);
598
599   do {
600     /* Allow the buffer to empty before reading more data.
601      * Prevents blocking on read() when data in the buffer */
602
603     if (buff_str->len != 0) {
604       /* Search for a CRLF, CR or LF */
605       for (i = 0; i < max_length-1; i++) {
606
607         /* Look for a CR */
608         if (buff_str->str[i] == '\r') {
609
610           /* Check for CRLF */
611           if (buff_str->str[i+1] == '\n') {
612             buff_str->str[i] = '\0';
613             i++;
614           } else if (match_bare_cr) {
615             buff_str->str[i] = '\0';
616           } else {
617             continue;
618           }
619
620           /* Copy the line to the destination string and
621            * remove it from the buffer */
622           g_string_assign( dest_str, buff_str->str );
623           g_string_erase( buff_str, 0, i+1);
624           return (G_STRING_ERROR_NONE);
625         }
626
627         /* Look for LF */
628         if (buff_str->str[i] == '\n') {
629           buff_str->str[i] = '\0';
630
631           /* Copy the line to the destination string and
632            * remove it from the buffer */
633           g_string_assign( dest_str, buff_str->str );
634           g_string_erase( buff_str, 0, i+1);
635           return (G_STRING_ERROR_NONE);      
636         }
637
638         /* If we hit a '\0' then we've exhausted the buffer */
639         if (buff_str->str[i] == '\0') {
640           break;
641         }
642       }
643     }
644
645     /* Read in a block of data, appending it to the buffer */
646     if ( (count = read(fd, buff_str->str + buff_str->len,
647                        buff_size - buff_str->len - 1)) < 0) {
648       return (G_STRING_ERROR_READ);
649     } else if (count == 0) {
650       return (G_STRING_ERROR_NODATA);
651     } else {
652       /* Fix up the buffer */
653       buff_str->len += count;
654       buff_str->str[buff_str->len] = '\0';
655     }
656
657   } while (i != max_length-1);
658
659   /* If we get here then we have reached max_length */
660   g_string_assign (dest_str, buff_str->str);
661   g_string_truncate (dest_str, max_length-1);
662   g_string_erase (buff_str, 0, max_length-1);
663
664   return (G_STRING_ERROR_LENGTH);
665 }
666
667 GList*
668 g_string_tokenise (GString *string,
669                    gchar   *delims,
670                    gint     max_tokens,
671                    gint     allow_empty)
672 {
673   GList *tokens=NULL;
674   GString *token;
675   gchar *current, *start, c;
676   guint count=1;
677
678   g_return_val_if_fail (string != NULL, NULL);
679   g_return_val_if_fail (delims != NULL, NULL);
680
681   if (max_tokens < 1) {
682     max_tokens = G_MAXINT;
683   }
684
685   current = string->str;
686   while (*current) {
687     /* Remove any leading delimiters */
688     if (!allow_empty) {
689       while ( *current && (strchr(delims, *current) != NULL) ) {
690         current++;
691       }
692     }
693
694     /* If we've reached max_tokens, use the remaining input string
695      * as the last token */
696     if (count == max_tokens) {
697       token = g_string_new(current);
698       tokens = g_list_append(tokens, token);
699       return (tokens);
700     }
701
702     /* Find the extent of the current token */
703     if ( *current ) {
704       start = current;
705       while ( *current && (strchr(delims, *current) == NULL) ) {
706         current++;
707       }
708       c = *current;
709       *current = '\0';
710       token = g_string_new( start );
711       *current = c;
712       tokens = g_list_append(tokens, token);
713       count++;
714       if (*current) {
715         current++;
716       }
717     }
718   }
719
720   return (tokens);
721 }
722
723 void
724 g_string_tokenise_free (GList *tokens,
725                         gint   free_token)
726 {
727
728   if (free_token) {
729     while(tokens) {
730       g_string_free( (GString *) tokens->data, TRUE );
731       tokens = g_list_next(tokens);
732     }
733   }
734
735   g_list_free(tokens);
736 }