Release 3.0.3.
[jelmer/ctrlproxy.git] / src / help.c
index 3ed543616307966f1c0de5219f2856b8427551b2..4d902c0a0aeb07344c756e303c5964fffc1dcc03 100644 (file)
@@ -2,7 +2,7 @@
        ctrlproxy: A modular IRC proxy
        help: module for access the help system
 
-       (c) 2006 Jelmer Vernooij <jelmer@nl.linux.org>
+       (c) 2006-2007 Jelmer Vernooij <jelmer@nl.linux.org>
        (c) 2004 Wilmer van der Gaast <wilmer@gaast.net>
 
        This program is free software; you can redistribute it and/or modify
 
 void help_free(help_t *h)
 {
-#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 8
-       g_mapped_file_free(h->file);
+       if (h->file != NULL)
+#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 8
+               g_mapped_file_free(h->file);
 #else
-       g_free(h->file);
+               g_free(h->file);
 #endif
-       g_hash_table_destroy(h->entries);
+       if (h->entries != NULL)
+               g_hash_table_destroy(h->entries);
        g_free(h);
 }
 
 GHashTable *help_build_hash(char *data, gsize len)
 {
-       GHashTable *h = g_hash_table_new(g_str_hash, g_str_equal);
-       gsize i, k;
+       GHashTable *h = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                                                                 g_free, NULL);
+       gsize i;
        char *p;
 
        i = 0;
        while (i < len) {
-               if (data[i] == '?') {
-                       /* Key starts here */
-                       k = i+1;
-                       p = g_strstr_len(data+k, len-k, "\n%\n");
-                       if (p == NULL) {
-                               log_global(LOG_WARNING, "Error parsing help file");
-                               g_hash_table_destroy(h);
-                               return NULL;
-                       }
-                       p[1] = 0;
-                       i+=strlen(data+i)+2;
-                       p = g_strstr_len(data+k, len-k, "\n");
-                       p[0] = 0;
-                       g_hash_table_insert(h, data+k, p+1);
-               } else {
+               if (data[i] != '?') {
                        log_global(LOG_WARNING, "Unknown character '0x%02x' in help file", 
                                           data[i]);
                        g_hash_table_destroy(h);
                        return NULL;
                }
+               /* Key starts here */
+               p = g_strstr_len(data+i, len-i, "\n");
+               if (p == NULL) {
+                       log_global(LOG_WARNING, "Error parsing help file");
+                       g_hash_table_destroy(h);
+                       return NULL;
+               }
+               g_hash_table_insert(h, g_strndup(data+i+1, p-(data+i)-1), p+1);
+               p = g_strstr_len(data+i, len-i, "\n%\n");
+               if (p == NULL) {
+                       log_global(LOG_WARNING, "Error parsing help file");
+                       g_hash_table_destroy(h);
+                       return NULL;
+               }
+               i = p-data+3;
        }
        
        return h;
@@ -79,8 +83,8 @@ help_t *help_load_file( const char *helpfile )
        
        h = g_new0 (help_t, 1);
        
-#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 8
-       h->file = g_mapped_file_new(helpfile, TRUE, &error);
+#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 8
+       h->file = g_mapped_file_new(helpfile, FALSE, &error);
        if (h->file != NULL) {
                len = g_mapped_file_get_length(h->file);
                data = g_mapped_file_get_contents(h->file);
@@ -98,8 +102,8 @@ help_t *help_load_file( const char *helpfile )
                                  error->message);
                help_free( h );
                return NULL;
-
        } 
+       data = h->file;
 #endif
 
        h->entries = help_build_hash(data, len);