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
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.
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.
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.
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/.
47 #include <io.h> /* For _read */
50 #define G_STRING_BLOCK_SIZE 512
52 typedef struct _GRealStringChunk GRealStringChunk;
53 typedef struct _GRealString GRealString;
55 struct _GRealStringChunk
57 GHashTable *const_table;
71 G_LOCK_DEFINE_STATIC (string_mem_chunk);
72 static GMemChunk *string_mem_chunk = NULL;
78 g_str_equal (gconstpointer v, gconstpointer v2)
80 return strcmp ((const gchar*) v, (const gchar*)v2) == 0;
83 /* a char* hash function from ASU */
85 g_str_hash (gconstpointer v)
87 const char *s = (char*)v;
91 for(p = s; *p != '\0'; p += 1) {
93 if ( ( g = h & 0xf0000000 ) ) {
107 g_string_chunk_new (gint default_size)
109 GRealStringChunk *new_chunk = g_new (GRealStringChunk, 1);
112 while (size < default_size)
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;
121 return (GStringChunk*) new_chunk;
125 g_string_chunk_free (GStringChunk *fchunk)
127 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
130 g_return_if_fail (chunk != NULL);
132 if (chunk->storage_list)
134 for (tmp_list = chunk->storage_list; tmp_list; tmp_list = tmp_list->next)
135 g_free (tmp_list->data);
137 g_slist_free (chunk->storage_list);
140 if (chunk->const_table)
141 g_hash_table_destroy (chunk->const_table);
147 g_string_chunk_insert (GStringChunk *fchunk,
150 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
151 gint len = strlen (string);
154 g_return_val_if_fail (chunk != NULL, NULL);
156 if ((chunk->storage_next + len + 1) > chunk->this_size)
158 gint new_size = chunk->default_size;
160 while (new_size < len+1)
163 chunk->storage_list = g_slist_prepend (chunk->storage_list,
164 g_new (char, new_size));
166 chunk->this_size = new_size;
167 chunk->storage_next = 0;
170 pos = ((char*)chunk->storage_list->data) + chunk->storage_next;
172 strcpy (pos, string);
174 chunk->storage_next += len + 1;
180 g_string_chunk_insert_const (GStringChunk *fchunk,
183 GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
186 g_return_val_if_fail (chunk != NULL, NULL);
188 if (!chunk->const_table)
189 chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal);
191 lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string);
195 lookup = g_string_chunk_insert (fchunk, string);
196 g_hash_table_insert (chunk->const_table, lookup, lookup);
205 nearest_power (gint num)
216 nearest_multiple (int num, const int block)
227 g_string_maybe_expand (GRealString* string, gint len)
229 if (string->len + len >= string->alloc)
231 string->alloc = nearest_power (string->len + len + 1);
232 string->str = g_realloc (string->str, string->alloc);
237 g_string_set_size (GRealString* string, gint size)
239 if (string->alloc <= size) {
240 string->alloc = nearest_power(size + 1);
241 string->str = g_realloc (string->str, string->alloc);
246 g_string_sized_new (guint dfl_size)
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);
256 string = g_chunk_new (GRealString, string_mem_chunk);
257 G_UNLOCK (string_mem_chunk);
263 g_string_maybe_expand (string, MAX (dfl_size, 2));
266 return (GString*) string;
270 g_string_new (const gchar *init)
274 string = g_string_sized_new (2);
277 g_string_append (string, init);
283 g_string_free (GString *string,
286 g_return_if_fail (string != NULL);
289 g_free (string->str);
291 G_LOCK (string_mem_chunk);
292 g_mem_chunk_free (string_mem_chunk, string);
293 G_UNLOCK (string_mem_chunk);
297 g_string_assign (GString *lval,
300 g_string_truncate (lval, 0);
301 g_string_append (lval, rval);
307 g_string_truncate (GString* fstring,
310 GRealString *string = (GRealString*)fstring;
312 g_return_val_if_fail (string != NULL, NULL);
316 string->str[len] = 0;
322 g_string_append (GString *fstring,
325 GRealString *string = (GRealString*)fstring;
328 g_return_val_if_fail (string != NULL, NULL);
329 g_return_val_if_fail (val != NULL, fstring);
332 g_string_maybe_expand (string, len);
334 strcpy (string->str + string->len, val);
342 g_string_append_c (GString *fstring,
345 GRealString *string = (GRealString*)fstring;
347 g_return_val_if_fail (string != NULL, NULL);
348 g_string_maybe_expand (string, 1);
350 string->str[string->len++] = c;
351 string->str[string->len] = 0;
357 g_string_prepend (GString *fstring,
360 GRealString *string = (GRealString*)fstring;
363 g_return_val_if_fail (string != NULL, NULL);
364 g_return_val_if_fail (val != NULL, fstring);
367 g_string_maybe_expand (string, len);
369 g_memmove (string->str + len, string->str, string->len);
371 strncpy (string->str, val, len);
375 string->str[string->len] = 0;
381 g_string_prepend_c (GString *fstring,
384 GRealString *string = (GRealString*)fstring;
386 g_return_val_if_fail (string != NULL, NULL);
387 g_string_maybe_expand (string, 1);
389 g_memmove (string->str + 1, string->str, string->len);
395 string->str[string->len] = 0;
401 g_string_insert (GString *fstring,
405 GRealString *string = (GRealString*)fstring;
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);
414 g_string_maybe_expand (string, len);
416 g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
418 strncpy (string->str + pos, val, len);
422 string->str[string->len] = 0;
428 g_string_insert_c (GString *fstring,
432 GRealString *string = (GRealString*)fstring;
434 g_return_val_if_fail (string != NULL, NULL);
435 g_return_val_if_fail (pos <= string->len, fstring);
437 g_string_maybe_expand (string, 1);
439 g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
441 string->str[pos] = c;
445 string->str[string->len] = 0;
451 g_string_erase (GString *fstring,
455 GRealString *string = (GRealString*)fstring;
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);
463 if (pos + len < string->len)
464 g_memmove (string->str + pos, string->str + pos + len, string->len - (pos + len));
468 string->str[string->len] = 0;
474 g_string_down (GString *fstring)
476 GRealString *string = (GRealString*)fstring;
479 g_return_val_if_fail (string != NULL, NULL);
493 g_string_up (GString *fstring)
495 GRealString *string = (GRealString*)fstring;
498 g_return_val_if_fail (string != NULL, NULL);
512 g_string_sprintfa_int (GString *string,
518 buffer = g_strdup_vprintf (fmt, args);
519 g_string_append (string, buffer);
524 g_string_sprintf (GString *string,
530 g_string_truncate (string, 0);
532 va_start (args, fmt);
533 g_string_sprintfa_int (string, fmt, args);
538 g_string_sprintfa (GString *string,
544 va_start (args, fmt);
545 g_string_sprintfa_int (string, fmt, args);
550 g_string_readline (GString *dest_str,
554 gint count=0, retval;
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);
561 for (count = 0; count < max_length; count++) {
562 if ( (retval = read(fd, &c, 1)) == 1 ) {
567 return(G_STRING_ERROR_NONE);
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);
575 return(G_STRING_ERROR_READ);
578 return(G_STRING_ERROR_LENGTH);
582 g_string_readline_buffered (GString *dest_str,
588 guint count, i=0, buff_size;
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);
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);
600 /* Allow the buffer to empty before reading more data.
601 * Prevents blocking on read() when data in the buffer */
603 if (buff_str->len != 0) {
604 /* Search for a CRLF, CR or LF */
605 for (i = 0; i < max_length-1; i++) {
608 if (buff_str->str[i] == '\r') {
611 if (buff_str->str[i+1] == '\n') {
612 buff_str->str[i] = '\0';
614 } else if (match_bare_cr) {
615 buff_str->str[i] = '\0';
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);
628 if (buff_str->str[i] == '\n') {
629 buff_str->str[i] = '\0';
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);
638 /* If we hit a '\0' then we've exhausted the buffer */
639 if (buff_str->str[i] == '\0') {
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);
652 /* Fix up the buffer */
653 buff_str->len += count;
654 buff_str->str[buff_str->len] = '\0';
657 } while (i != max_length-1);
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);
664 return (G_STRING_ERROR_LENGTH);
668 g_string_tokenise (GString *string,
675 gchar *current, *start, c;
678 g_return_val_if_fail (string != NULL, NULL);
679 g_return_val_if_fail (delims != NULL, NULL);
681 if (max_tokens < 1) {
682 max_tokens = G_MAXINT;
685 current = string->str;
687 /* Remove any leading delimiters */
689 while ( *current && (strchr(delims, *current) != NULL) ) {
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);
702 /* Find the extent of the current token */
705 while ( *current && (strchr(delims, *current) == NULL) ) {
710 token = g_string_new( start );
712 tokens = g_list_append(tokens, token);
724 g_string_tokenise_free (GList *tokens,
730 g_string_free( (GString *) tokens->data, TRUE );
731 tokens = g_list_next(tokens);