diff --git a/glib/ChangeLog b/glib/ChangeLog index dacb0cfc73..5a0668af5c 100644 --- a/glib/ChangeLog +++ b/glib/ChangeLog @@ -1,3 +1,32 @@ +Thu May 7 08:17:28 1998 Tim Janik + + * glib.h (G_GNUC_PRINTF): + (G_GNUC_SCANF): macros to facilitate the printf/scanf format argument + checking of gcc. + + * gstring.c: const corrections, string!=NULL checks at function entry. + (g_string_down): new function for tolower(3) conversion. + (g_string_up): new function for toupper(3) conversion. + + * gutils.c: const corrections. + (g_strdown): g_string_down() counterpart. + (g_strup): g_string_up() counterpart. + + * gscanner.c (g_scanner_unexp_token): + (g_scanner_error): + (g_scanner_warn): new functions to let a scanner put out warnings + or errors, especially to react on unexpected tokens. + + * gslist.c: + (g_slist_index): find out about about the position of a + certain data pointer. + (g_slist_position): find out about about the position of a + certain node. + + * glist.c: + (g_list_index): find out about about the position of a + certain data pointer. + Thu May 7 05:14:19 1998 Tim Janik * ltmain.sh: added a new commandline flag -postfix similar to -release, diff --git a/glib/gerror.c b/glib/gerror.c index d84ea38ca6..1c4ce5c31c 100644 --- a/glib/gerror.c +++ b/glib/gerror.c @@ -55,7 +55,7 @@ static int do_query (char *prompt); -static void debug (char *progname, int method); +static void debug (const gchar *progname, int method); static void stack_trace (char **); static void stack_trace_sigchld (int); @@ -63,7 +63,7 @@ static void stack_trace_sigchld (int); static int stack_trace_done; void -g_debug (char *progname) +g_debug (const gchar *progname) { char buf[32]; @@ -82,14 +82,16 @@ g_debug (char *progname) } void -g_attach_process (char *progname, int query) +g_attach_process (const gchar *progname, + int query) { if (!query || do_query ("attach to process")) debug (progname, INTERACTIVE); } void -g_stack_trace (char *progname, int query) +g_stack_trace (const gchar *progname, + int query) { if (!query || do_query ("print stack trace")) debug (progname, STACK_TRACE); @@ -114,7 +116,7 @@ do_query (char *prompt) } static void -debug (char *progname, +debug (const char *progname, int method) { pid_t pid; @@ -124,7 +126,7 @@ debug (char *progname, sprintf (buf, "%d", (int) getpid ()); - args[1] = progname; + args[1] = (gchar*) progname; args[2] = buf; switch (method) diff --git a/glib/glib.h b/glib/glib.h index 5fa93487b2..d38253e2cb 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -285,8 +285,22 @@ #endif /* G_DISABLE_CHECKS */ +/* Provide macros to feature the GCC printf format function attribute. + */ +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) +#define G_GNUC_PRINTF( format_idx, arg_idx ) \ + __attribute__((format (printf, format_idx, arg_idx))) +#define G_GNUC_SCANF( format_idx, arg_idx ) \ + __attribute__((format (scanf, format_idx, arg_idx))) +#else /* !__GNUC__ */ +#define G_GNUC_PRINTF( format_idx, arg_idx ) +#define G_GNUC_SCANF( format_idx, arg_idx ) +#endif /* !__GNUC__ */ + + #ifdef __cplusplus extern "C" { +#pragma } #endif /* __cplusplus */ /* Provide type definitions for commonly used types. @@ -344,35 +358,46 @@ typedef unsigned long guint32; #endif /* SIZEOF_INT */ -typedef struct _GList GList; -typedef struct _GSList GSList; -typedef struct _GHashTable GHashTable; -typedef struct _GCache GCache; -typedef struct _GTree GTree; -typedef struct _GTimer GTimer; -typedef struct _GMemChunk GMemChunk; -typedef struct _GListAllocator GListAllocator; -typedef struct _GStringChunk GStringChunk; -typedef struct _GString GString; -typedef struct _GArray GArray; -typedef struct _GDebugKey GDebugKey; +typedef struct _GList GList; +typedef struct _GSList GSList; +typedef struct _GHashTable GHashTable; +typedef struct _GCache GCache; +typedef struct _GTree GTree; +typedef struct _GTimer GTimer; +typedef struct _GMemChunk GMemChunk; +typedef struct _GListAllocator GListAllocator; +typedef struct _GStringChunk GStringChunk; +typedef struct _GString GString; +typedef struct _GArray GArray; +typedef struct _GDebugKey GDebugKey; +typedef struct _GScannerConfig GScannerConfig; +typedef struct _GScanner GScanner; +typedef union _GValue GValue; -typedef void (*GFunc) (gpointer data, gpointer user_data); -typedef void (*GHFunc) (gpointer key, gpointer value, gpointer user_data); -typedef guint (*GHashFunc) (gpointer key); -typedef gint (*GCompareFunc) (gpointer a, gpointer b); -typedef gpointer (*GCacheNewFunc) (gpointer key); -typedef gpointer (*GCacheDupFunc) (gpointer value); -typedef void (*GCacheDestroyFunc) (gpointer value); -typedef gint (*GTraverseFunc) (gpointer key, - gpointer value, - gpointer data); -typedef gint (*GSearchFunc) (gpointer key, - gpointer data); -typedef void (*GErrorFunc) (gchar *str); -typedef void (*GWarningFunc) (gchar *str); -typedef void (*GPrintFunc) (gchar *str); +typedef void (*GFunc) (gpointer data, + gpointer user_data); +typedef void (*GHFunc) (gpointer key, + gpointer value, + gpointer user_data); +typedef guint (*GHashFunc) (gpointer key); +typedef gint (*GCompareFunc) (gpointer a, + gpointer b); +typedef gpointer (*GCacheNewFunc) (gpointer key); +typedef gpointer (*GCacheDupFunc) (gpointer value); +typedef void (*GCacheDestroyFunc) (gpointer value); +typedef gint (*GTraverseFunc) (gpointer key, + gpointer value, + gpointer data); +typedef gint (*GSearchFunc) (gpointer key, + gpointer data); +typedef void (*GErrorFunc) (gchar *str); +typedef void (*GWarningFunc) (gchar *str); +typedef void (*GPrintFunc) (gchar *str); +typedef void (*GScannerMsgFunc) (GScanner *scanner, + gchar *message, + gint error); + struct _GList { @@ -445,9 +470,11 @@ GList* g_list_remove_link (GList *list, GList* g_list_reverse (GList *list); GList* g_list_nth (GList *list, guint n); +GList* g_list_find (GList *list, + gpointer data); gint g_list_position (GList *list, GList *link); -GList* g_list_find (GList *list, +gint g_list_index (GList *list, gpointer data); GList* g_list_last (GList *list); GList* g_list_first (GList *list); @@ -486,6 +513,10 @@ GSList* g_slist_nth (GSList *list, guint n); GSList* g_slist_find (GSList *list, gpointer data); +gint g_slist_position (GSList *list, + GSList *link); +gint g_slist_index (GSList *list, + gpointer data); GSList* g_slist_last (GSList *list); guint g_slist_length (GSList *list); void g_slist_foreach (GSList *list, @@ -648,31 +679,35 @@ gdouble g_timer_elapsed (GTimer *timer, /* Output */ -#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) -void g_error (gchar *format, ...) __attribute__ ((format (printf, 1, 2))); -void g_warning (gchar *format, ...) __attribute__ ((format (printf, 1, 2))); -void g_message (gchar *format, ...) __attribute__ ((format (printf, 1, 2))); -void g_print (gchar *format, ...) __attribute__ ((format (printf, 1, 2))); -#else -void g_error (gchar *format, ...); -void g_warning (gchar *format, ...); -void g_message (gchar *format, ...); -void g_print (gchar *format, ...); -#endif +void g_error (gchar *format, ...) G_GNUC_PRINTF (1, 2); +void g_warning (gchar *format, ...) G_GNUC_PRINTF (1, 2); +void g_message (gchar *format, ...) G_GNUC_PRINTF (1, 2); +void g_print (gchar *format, ...) G_GNUC_PRINTF (1, 2); /* Utility functions */ -gchar* g_strdup (const gchar *str); -gchar* g_strconcat (const gchar *string1, ...); /* NULL terminated */ -gdouble g_strtod (const gchar *nptr, gchar **endptr); -gchar* g_strerror (gint errnum); -gchar* g_strsignal (gint signum); -gint g_strcasecmp (const gchar *s1, const gchar *s2); -#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) -gint g_snprintf (gchar *str, gulong n, gchar const *fmt, ...) __attribute__ ((format (printf, 3, 4))); -#else -gint g_snprintf (gchar *str, gulong n, gchar const *fmt, ...); -#endif +#define G_STR_DELIMITERS "_-|> <." +void g_strdelimit (gchar *string, + const gchar *delimiters, + gchar new_delimiter); +gchar* g_strdup (const gchar *str); +gchar* g_strconcat (const gchar *string1, + ...); /* NULL terminated */ +gdouble g_strtod (const gchar *nptr, + gchar **endptr); +gchar* g_strerror (gint errnum); +gchar* g_strsignal (gint signum); +gint g_strcasecmp (const gchar *s1, + const gchar *s2); +void g_strdown (gchar *string); +void g_strup (gchar *string); +guint g_parse_debug_string (const gchar *string, + GDebugKey *keys, + guint nkeys); +gint g_snprintf (gchar *string, + gulong n, + gchar const *format, + ...) G_GNUC_PRINTF (3, 4); /* We make the assumption that if memmove isn't available, then * bcopy will do the job. This isn't safe everywhere. (bcopy can't @@ -690,13 +725,11 @@ GWarningFunc g_set_warning_handler (GWarningFunc func); GPrintFunc g_set_message_handler (GPrintFunc func); GPrintFunc g_set_print_handler (GPrintFunc func); -guint g_parse_debug_string (const gchar *string, - GDebugKey *keys, - guint nkeys); - -void g_debug (char *progname); -void g_attach_process (char *progname, int query); -void g_stack_trace (char *progname, int query); +void g_debug (const gchar *progname); +void g_attach_process (const gchar *progname, + gint query); +void g_stack_trace (const gchar *progname, + gint query); /* String Chunks @@ -710,47 +743,38 @@ gchar* g_string_chunk_insert_const (GStringChunk *chunk, /* Strings */ -GString* g_string_new (gchar *init); -void g_string_free (GString *string, - gint free_segment); -GString* g_string_assign (GString *lval, - gchar *rval); -GString* g_string_truncate (GString *string, - gint len); -GString* g_string_append (GString *string, - gchar *val); -GString* g_string_append_c (GString *string, - gchar c); -GString* g_string_prepend (GString *string, - gchar *val); -GString* g_string_prepend_c (GString *string, - gchar c); -GString* g_string_insert (GString *fstring, - gint pos, - gchar *val); -GString* g_string_insert_c (GString *fstring, - gint pos, - gchar c); -GString* g_string_erase (GString *fstring, - gint pos, - gint len); -#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) -void g_string_sprintf (GString *string, - gchar *fmt, - ...) __attribute__ ((format (printf, 2, 3))); - -void g_string_sprintfa (GString *string, - gchar *fmt, - ...) __attribute__ ((format (printf, 2, 3))); -#else -void g_string_sprintf (GString *string, - gchar *fmt, - ...); - -void g_string_sprintfa (GString *string, - gchar *fmt, - ...); -#endif +GString* g_string_new (const gchar *init); +void g_string_free (GString *string, + gint free_segment); +GString* g_string_assign (GString *lval, + const gchar *rval); +GString* g_string_truncate (GString *string, + gint len); +GString* g_string_append (GString *string, + const gchar *val); +GString* g_string_append_c (GString *string, + gchar c); +GString* g_string_prepend (GString *string, + const gchar *val); +GString* g_string_prepend_c (GString *string, + gchar c); +GString* g_string_insert (GString *string, + gint pos, + const gchar *val); +GString* g_string_insert_c (GString *string, + gint pos, + gchar c); +GString* g_string_erase (GString *string, + gint pos, + gint len); +GString* g_string_down (GString *string); +GString* g_string_up (GString *string); +void g_string_sprintf (GString *string, + const gchar *format, + ...) G_GNUC_PRINTF (2, 3); +void g_string_sprintfa (GString *string, + const gchar *format, + ...) G_GNUC_PRINTF (2, 3); /* Resizable arrays */ @@ -794,7 +818,6 @@ guint g_direct_hash (gpointer key); /* GScanner: Flexible lexical scanner for general purpose. - * Copyright (C) 1997 Tim Janik */ /* Character sets */ @@ -809,10 +832,6 @@ guint g_direct_hash (gpointer key); "\361\362\363\364\365\366"\ "\370\371\372\373\374\375\376\377" -typedef union _GValue GValue; -typedef struct _GScannerConfig GScannerConfig; -typedef struct _GScanner GScanner; - /* Error types */ typedef enum { @@ -922,6 +941,7 @@ struct _GScanner guint max_parse_errors; /* maintained/used by the g_scanner_*() functions */ + GScannerMsgFunc msg_handler; GScannerConfig *config; GTokenType token; GValue value; @@ -961,6 +981,20 @@ gpointer g_scanner_lookup_symbol (GScanner *scanner, const gchar *symbol); void g_scanner_remove_symbol (GScanner *scanner, const gchar *symbol); +void g_scanner_unexp_token (GScanner *scanner, + GTokenType expected_token, + const gchar *identifier_spec, + const gchar *symbol_spec, + const gchar *symbol_name, + const gchar *message, + gint is_error); +void g_scanner_error (GScanner *scanner, + const gchar *format, + ...) G_GNUC_PRINTF (2,3); +void g_scanner_warn (GScanner *scanner, + const gchar *format, + ...) G_GNUC_PRINTF (2,3); +gint g_scanner_stat_mode (const gchar *filename); diff --git a/glib/glist.c b/glib/glist.c index bd0c29bcff..06cfd54905 100644 --- a/glib/glist.c +++ b/glib/glist.c @@ -300,21 +300,6 @@ g_list_nth (GList *list, return list; } -gint -g_list_position (GList *list, - GList *link) -{ - gint nth; - GList *curlink; - for(nth = 0, curlink = list; curlink; curlink = g_list_next(curlink), nth++) - { - if(curlink == link) return nth; - } - - return -1; -} - - GList* g_list_find (GList *list, gpointer data) @@ -329,6 +314,42 @@ g_list_find (GList *list, return list; } +gint +g_list_position (GList *list, + GList *link) +{ + gint i; + + i = 0; + while (list) + { + if (list == link) + return i; + i++; + list = list->next; + } + + return -1; +} + +gint +g_list_index (GList *list, + gpointer data) +{ + gint i; + + i = 0; + while (list) + { + if (list->data == data) + return i; + i++; + list = list->next; + } + + return -1; +} + GList* g_list_last (GList *list) { diff --git a/glib/gscanner.c b/glib/gscanner.c index a5d359461e..840fa429a0 100644 --- a/glib/gscanner.c +++ b/glib/gscanner.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald * * GScanner: Flexible lexical scanner for general purpose. - * Copyright (C) 1997 Tim Janik + * Copyright (C) 1997, 1998 Tim Janik * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -22,9 +22,12 @@ #define __gscanner_c__ #include +#include #include +#include #include #include +#include #include "glib.h" @@ -32,9 +35,9 @@ /* --- defines --- */ #define to_lower(c) ( \ (guchar) ( \ - ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) + \ - ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) + \ - ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) + \ + ( (((guchar)(c))>='A' && ((guchar)(c))<='Z') * ('a'-'A') ) | \ + ( (((guchar)(c))>=192 && ((guchar)(c))<=214) * (224-192) ) | \ + ( (((guchar)(c))>=216 && ((guchar)(c))<=222) * (248-216) ) | \ ((guchar)(c)) \ ) \ ) @@ -96,6 +99,7 @@ static GScannerConfig g_scanner_config_template = /* --- prototypes --- */ +extern char* g_vsprintf (gchar *fmt, va_list *args, va_list *args2); static GScannerHashVal* g_scanner_lookup_internal (GScanner *scanner, const gchar *symbol); static void g_scanner_get_token_ll (GScanner *scanner, @@ -118,6 +122,9 @@ static guchar g_scanner_peek_next_char(GScanner *scanner); static guchar g_scanner_get_char (GScanner *scanner, guint *line_p, guint *position_p); +static void g_scanner_msg_handler (GScanner *scanner, + gchar *message, + gint is_error); /* --- functions --- */ @@ -154,6 +161,7 @@ g_scanner_new (GScannerConfig *config_templ) scanner->input_name = NULL; scanner->parse_errors = 0; scanner->max_parse_errors = 0; + scanner->msg_handler = g_scanner_msg_handler; scanner->config = g_new0 (GScannerConfig, 1); @@ -224,6 +232,75 @@ g_scanner_destroy (GScanner *scanner) g_free (scanner); } +static void +g_scanner_msg_handler (GScanner *scanner, + gchar *message, + gint is_error) +{ + g_return_if_fail (scanner != NULL); + + fprintf (stdout, "%s:%d: ", scanner->input_name, scanner->line); + if (is_error) + fprintf (stdout, "error: "); + fprintf (stdout, "%s", message); +} + +void +g_scanner_error (GScanner *scanner, + const gchar *format, + ...) +{ + g_return_if_fail (scanner != NULL); + g_return_if_fail (format != NULL); + + scanner->parse_errors++; + + if (scanner->msg_handler) + { + va_list args, args2; + gchar *string; + + va_start (args, format); + va_start (args2, format); + string = g_vsprintf ((gchar*) format, &args, &args2); + va_end (args); + va_end (args2); + + string = g_strdup (string); + + scanner->msg_handler (scanner, string, TRUE); + + g_free (string); + } +} + +void +g_scanner_warn (GScanner *scanner, + const gchar *format, + ...) +{ + g_return_if_fail (scanner != NULL); + g_return_if_fail (format != NULL); + + if (scanner->msg_handler) + { + va_list args, args2; + gchar *string; + + va_start (args, format); + va_start (args2, format); + string = g_vsprintf ((gchar*) format, &args, &args2); + va_end (args); + va_end (args2); + + string = g_strdup (string); + + scanner->msg_handler (scanner, string, FALSE); + + g_free (string); + } +} + void g_scanner_input_file (GScanner *scanner, gint input_fd) @@ -245,7 +322,7 @@ g_scanner_input_file (GScanner *scanner, void g_scanner_input_text (GScanner *scanner, const gchar *text, - guint text_len) + guint text_len) { g_return_if_fail (text != NULL); @@ -530,6 +607,274 @@ g_scanner_get_char (GScanner *scanner, return fchar; } +void +g_scanner_unexp_token (GScanner *scanner, + GTokenType expected_token, + const gchar *identifier_spec, + const gchar *symbol_spec, + const gchar *symbol_name, + const gchar *message, + gint is_error) +{ + register gchar *token_string; + register guint token_string_len; + register gchar *expected_string; + register guint expected_string_len; + register gchar *message_prefix; + register gboolean print_unexp; + void (*msg_handler) (GScanner*, const gchar*, ...); + + g_return_if_fail (scanner != NULL); + + if (is_error) + msg_handler = g_scanner_error; + else + msg_handler = g_scanner_warn; + + token_string_len = 56; + token_string = g_new (gchar, token_string_len + 1); + expected_string_len = 64; + expected_string = g_new (gchar, expected_string_len + 1); + print_unexp = TRUE; + + switch (scanner->token) + { + + case G_TOKEN_EOF: + snprintf (token_string, token_string_len, "end of file"); + break; + + default: /* 1 ... 255 */ + if (scanner->token >= 1 && scanner->token <= 255) + { + if ((scanner->token >= ' ' && scanner->token <= '~') || + strchr (scanner->config->cset_identifier_first, scanner->token) || + strchr (scanner->config->cset_identifier_nth, scanner->token)) + snprintf (token_string, expected_string_len, "character `%c'", scanner->token); + else + snprintf (token_string, expected_string_len, "character `\\%o'", scanner->token); + } + else + snprintf (token_string, token_string_len, "(unknown) token <%d>", scanner->token); + break; + + case G_TOKEN_ERROR: + print_unexp = FALSE; + expected_token = G_TOKEN_NONE; + switch (scanner->value.v_error) + { + case G_ERR_UNEXP_EOF: + snprintf (token_string, token_string_len, "scanner: unexpected end of file"); + break; + + case G_ERR_UNEXP_EOF_IN_STRING: + snprintf (token_string, token_string_len, "scanner: unterminated string constant"); + break; + + case G_ERR_UNEXP_EOF_IN_COMMENT: + snprintf (token_string, token_string_len, "scanner: unterminated comment"); + break; + + case G_ERR_NON_DIGIT_IN_CONST: + snprintf (token_string, token_string_len, "scanner: non digit in constant"); + break; + + case G_ERR_FLOAT_RADIX: + snprintf (token_string, token_string_len, "scanner: invalid radix for floating constant"); + break; + + case G_ERR_FLOAT_MALFORMED: + snprintf (token_string, token_string_len, "scanner: malformed floating constant"); + break; + + case G_ERR_DIGIT_RADIX: + snprintf (token_string, token_string_len, "scanner: digit is beyond radix"); + break; + + case G_ERR_UNKNOWN: + default: + snprintf (token_string, token_string_len, "scanner: unknown error"); + break; + } + break; + + case G_TOKEN_CHAR: + snprintf (token_string, token_string_len, "character `%c'", scanner->value.v_char); + break; + + case G_TOKEN_SYMBOL: + if (expected_token == G_TOKEN_SYMBOL) + print_unexp = FALSE; + if (symbol_name) + snprintf (token_string, + token_string_len, + "%s%s `%s'", + print_unexp ? "" : "invalid ", + symbol_spec, + symbol_name); + else + snprintf (token_string, + token_string_len, + "%s%s", + print_unexp ? "" : "invalid ", + symbol_spec); + break; + + case G_TOKEN_IDENTIFIER: + if (expected_token == G_TOKEN_IDENTIFIER) + print_unexp = FALSE; + snprintf (token_string, + token_string_len, + "%s%s `%s'", + print_unexp ? "" : "invalid ", + identifier_spec, + scanner->value.v_string); + break; + + case G_TOKEN_BINARY: + case G_TOKEN_OCTAL: + case G_TOKEN_INT: + case G_TOKEN_HEX: + snprintf (token_string, token_string_len, "number `%ld'", scanner->value.v_int); + break; + + case G_TOKEN_FLOAT: + snprintf (token_string, token_string_len, "number `%.3f'", scanner->value.v_float); + break; + + case G_TOKEN_STRING: + snprintf (token_string, + token_string_len, + "%sstring constant \"%s\"", + scanner->value.v_string[0] == 0 ? "empty " : "", + scanner->value.v_string); + token_string[token_string_len - 2] = '"'; + token_string[token_string_len - 1] = 0; + break; + + case G_TOKEN_COMMENT_SINGLE: + case G_TOKEN_COMMENT_MULTI: + snprintf (token_string, token_string_len, "comment"); + break; + + case G_TOKEN_NONE: + g_assert_not_reached (); + break; + } + + + switch (expected_token) + { + default: /* 1 ... 255 */ + if (expected_token >= 1 && expected_token <= 255) + { + if ((expected_token >= ' ' && expected_token <= '~') || + strchr (scanner->config->cset_identifier_first, expected_token) || + strchr (scanner->config->cset_identifier_nth, expected_token)) + snprintf (expected_string, expected_string_len, "character `%c'", expected_token); + else + snprintf (expected_string, expected_string_len, "character `\\%o'", expected_token); + } + else + snprintf (expected_string, expected_string_len, "(unknown) token <%d>", expected_token); + break; + + case G_TOKEN_INT: + snprintf (expected_string, expected_string_len, "number (integer)"); + break; + + case G_TOKEN_FLOAT: + snprintf (expected_string, expected_string_len, "number (float)"); + break; + + case G_TOKEN_STRING: + snprintf (expected_string, expected_string_len, "string constant"); + break; + + case G_TOKEN_SYMBOL: + snprintf (expected_string, + expected_string_len, + "%s%s", + scanner->token == G_TOKEN_SYMBOL ? "valid " : "", + symbol_spec); + break; + + case G_TOKEN_IDENTIFIER: + snprintf (expected_string, + expected_string_len, + "%s%s", + scanner->token == G_TOKEN_IDENTIFIER ? "valid " : "", + identifier_spec); + break; + + case G_TOKEN_NONE: + break; + } + + if (message && message[0] != 0) + message_prefix = " - "; + else + { + message_prefix = ""; + message = ""; + } + + if (expected_token != G_TOKEN_NONE) + { + if (print_unexp) + msg_handler (scanner, + "unexpected %s, expected %s%s%s", + token_string, + expected_string, + message_prefix, + message); + else + msg_handler (scanner, + "%s, expected %s%s%s", + token_string, + expected_string, + message_prefix, + message); + } + else + { + if (print_unexp) + msg_handler (scanner, + "unexpected %s%s%s", + token_string, + message_prefix, + message); + else + msg_handler (scanner, + "%s%s%s", + token_string, + message_prefix, + message); + } + + va_end (args); + + g_free (token_string); + g_free (expected_string); +} + +gint +g_scanner_stat_mode (const gchar *filename) +{ + struct stat *stat_buf; + gint st_mode; + + stat_buf = g_new0 (struct stat, 1); + + stat (filename, stat_buf); + + st_mode = stat_buf->st_mode; + + g_free (stat_buf); + + return st_mode; +} + static void g_scanner_free_value (GTokenType *token_p, GValue *value_p) diff --git a/glib/gslist.c b/glib/gslist.c index e98c0ae5a3..e608f131e8 100644 --- a/glib/gslist.c +++ b/glib/gslist.c @@ -303,6 +303,42 @@ g_slist_find (GSList *list, return list; } +gint +g_slist_position (GSList *list, + GSList *link) +{ + gint i; + + i = 0; + while (list) + { + if (list == link) + return i; + i++; + list = list->next; + } + + return -1; +} + +gint +g_slist_index (GSList *list, + gpointer data) +{ + gint i; + + i = 0; + while (list) + { + if (list->data == data) + return i; + i++; + list = list->next; + } + + return -1; +} + GSList* g_slist_last (GSList *list) { diff --git a/glib/gstring.c b/glib/gstring.c index e006415b85..9a7a44ceb5 100644 --- a/glib/gstring.c +++ b/glib/gstring.c @@ -21,6 +21,7 @@ #include #include #include +#include typedef struct _GRealStringChunk GRealStringChunk; @@ -101,6 +102,8 @@ g_string_chunk_free (GStringChunk *fchunk) GRealStringChunk *chunk = (GRealStringChunk*) fchunk; GSList *tmp_list; + g_return_if_fail (chunk != NULL); + if (chunk->storage_list) { GListAllocator *tmp_allocator = g_slist_set_allocator (NULL); @@ -127,6 +130,8 @@ g_string_chunk_insert (GStringChunk *fchunk, gint len = strlen (string); char* pos; + g_return_val_if_fail (chunk != NULL, NULL); + if ((chunk->storage_next + len + 1) > chunk->this_size) { GListAllocator *tmp_allocator = g_slist_set_allocator (NULL); @@ -160,6 +165,8 @@ g_string_chunk_insert_const (GStringChunk *fchunk, GRealStringChunk *chunk = (GRealStringChunk*) fchunk; char* lookup; + g_return_val_if_fail (chunk != NULL, NULL); + if (!chunk->const_table) chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal); @@ -198,7 +205,7 @@ g_string_maybe_expand (GRealString* string, gint len) } GString* -g_string_new (gchar *init) +g_string_new (const gchar *init) { GRealString *string; @@ -220,8 +227,11 @@ g_string_new (gchar *init) } void -g_string_free (GString *string, gint free_segment) +g_string_free (GString *string, + gint free_segment) { + g_return_if_fail (string != NULL); + if (free_segment) g_free (string->str); @@ -230,7 +240,7 @@ g_string_free (GString *string, gint free_segment) GString* g_string_assign (GString *lval, - char *rval) + const gchar *rval) { g_string_truncate (lval, 0); g_string_append (lval, rval); @@ -239,10 +249,13 @@ g_string_assign (GString *lval, } GString* -g_string_truncate (GString* fstring, gint len) +g_string_truncate (GString* fstring, + gint len) { GRealString *string = (GRealString*)fstring; + g_return_val_if_fail (string != NULL, NULL); + string->len = len; string->str[len] = 0; @@ -251,11 +264,13 @@ g_string_truncate (GString* fstring, gint len) } GString* -g_string_append (GString *fstring, gchar *val) +g_string_append (GString *fstring, + const gchar *val) { GRealString *string = (GRealString*)fstring; int len; + g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (val != NULL, fstring); len = strlen (val); @@ -269,10 +284,12 @@ g_string_append (GString *fstring, gchar *val) } GString* -g_string_append_c (GString *fstring, char c) +g_string_append_c (GString *fstring, + gchar c) { GRealString *string = (GRealString*)fstring; + g_return_val_if_fail (string != NULL, NULL); g_string_maybe_expand (string, 1); string->str[string->len++] = c; @@ -282,11 +299,13 @@ g_string_append_c (GString *fstring, char c) } GString* -g_string_prepend (GString *fstring, gchar *val) +g_string_prepend (GString *fstring, + const gchar *val) { GRealString *string = (GRealString*)fstring; gint len; + g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (val != NULL, fstring); len = strlen (val); @@ -304,10 +323,12 @@ g_string_prepend (GString *fstring, gchar *val) } GString* -g_string_prepend_c (GString *fstring, char c) +g_string_prepend_c (GString *fstring, + gchar c) { GRealString *string = (GRealString*)fstring; + g_return_val_if_fail (string != NULL, NULL); g_string_maybe_expand (string, 1); g_memmove (string->str + 1, string->str, string->len); @@ -321,12 +342,15 @@ g_string_prepend_c (GString *fstring, char c) return fstring; } -GString * -g_string_insert (GString *fstring, gint pos, gchar *val) +GString* +g_string_insert (GString *fstring, + gint pos, + const gchar *val) { GRealString *string = (GRealString*)fstring; gint len; + g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (val != NULL, fstring); g_return_val_if_fail (pos >= 0, fstring); g_return_val_if_fail (pos <= string->len, fstring); @@ -346,10 +370,13 @@ g_string_insert (GString *fstring, gint pos, gchar *val) } GString * -g_string_insert_c (GString *fstring, gint pos, gchar c) +g_string_insert_c (GString *fstring, + gint pos, + gchar c) { GRealString *string = (GRealString*)fstring; + g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (pos <= string->len, fstring); g_string_maybe_expand (string, 1); @@ -365,11 +392,14 @@ g_string_insert_c (GString *fstring, gint pos, gchar c) return fstring; } -GString * -g_string_erase (GString *fstring, gint pos, gint len) +GString* +g_string_erase (GString *fstring, + gint pos, + gint len) { GRealString *string = (GRealString*)fstring; + g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (len >= 0, fstring); g_return_val_if_fail (pos >= 0, fstring); g_return_val_if_fail (pos <= string->len, fstring); @@ -385,6 +415,44 @@ g_string_erase (GString *fstring, gint pos, gint len) return fstring; } +GString* +g_string_down (GString *fstring) +{ + GRealString *string = (GRealString*)fstring; + gchar *s; + + g_return_val_if_fail (string != NULL, NULL); + + s = string->str; + + while (*s) + { + *s = tolower (*s); + s++; + } + + return fstring; +} + +GString* +g_string_up (GString *fstring) +{ + GRealString *string = (GRealString*)fstring; + gchar *s; + + g_return_val_if_fail (string != NULL, NULL); + + s = string->str; + + while (*s) + { + *s = toupper (*s); + s++; + } + + return fstring; +} + static int get_length_upper_bound (const gchar* fmt, va_list *args) { @@ -524,7 +592,7 @@ g_vsprintf (const gchar *fmt, static void g_string_sprintfa_int (GString *string, - gchar *fmt, + const gchar *fmt, va_list *args, va_list *args2) { @@ -532,7 +600,9 @@ g_string_sprintfa_int (GString *string, } void -g_string_sprintf (GString *string, gchar *fmt, ...) +g_string_sprintf (GString *string, + const gchar *fmt, + ...) { va_list args, args2; @@ -548,7 +618,9 @@ g_string_sprintf (GString *string, gchar *fmt, ...) } void -g_string_sprintfa (GString *string, gchar *fmt, ...) +g_string_sprintfa (GString *string, + const gchar *fmt, + ...) { va_list args, args2; diff --git a/glib/gutils.c b/glib/gutils.c index 089c204b05..b3ce675422 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -854,11 +854,44 @@ g_snprintf (gchar *str, #endif } +void +g_strdown (gchar *string) +{ + register gchar *s; + + g_return_if_fail (string != NULL); + + s = string; + + while (*s) + { + *s = tolower (*s); + s++; + } +} + +void +g_strup (gchar *string) +{ + register gchar *s; + + g_return_if_fail (string != NULL); + + s = string; + + while (*s) + { + *s = toupper (*s); + s++; + } +} + gint -g_strcasecmp (const gchar *s1, const gchar *s2) +g_strcasecmp (const gchar *s1, + const gchar *s2) { #ifdef HAVE_STRCASECMP - return strcasecmp(s1, s2); + return strcasecmp (s1, s2); #else gint c1, c2; @@ -878,6 +911,25 @@ g_strcasecmp (const gchar *s1, const gchar *s2) #endif } +void +g_strdelimit (gchar *string, + const gchar *delimiters, + gchar new_delim) +{ + register gchar *c; + + g_return_if_fail (string != NULL); + + if (!delimiters) + delimiters = G_STR_DELIMITERS; + + for (c = string; *c; c++) + { + if (strchr (delimiters, *c)) + *c = new_delim; + } +} + guint g_parse_debug_string (const gchar *string, GDebugKey *keys,