Fix warning
[jelmer/ctrlproxy.git] / src / help.c
1 /*
2         ctrlproxy: A modular IRC proxy
3         help: module for access the help system
4
5         (c) 2006 Jelmer Vernooij <jelmer@nl.linux.org>
6         (c) 2004 Wilmer van der Gaast <wilmer@gaast.net>
7
8         This program is free software; you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation; either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with this program; if not, write to the Free Software
20         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "internals.h"
24 #include "help.h"
25 #include "log.h"
26
27 #define BUFSIZE 1100
28
29 void help_free(help_t *h)
30 {
31 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 8
32         g_mapped_file_free(h->file);
33 #else
34         g_free(h->file);
35 #endif
36         g_hash_table_destroy(h->entries);
37         g_free(h);
38 }
39
40 GHashTable *help_build_hash(char *data, gsize len)
41 {
42         GHashTable *h = g_hash_table_new(g_str_hash, g_str_equal);
43         gsize i, k;
44         char *p;
45
46         i = 0;
47         while (i < len) {
48                 if (data[i] == '?') {
49                         /* Key starts here */
50                         k = i+1;
51                         p = g_strstr_len(data+k, len-k, "\n%\n");
52                         if (p == NULL) {
53                                 log_global(LOG_WARNING, "Error parsing help file");
54                                 g_hash_table_destroy(h);
55                                 return NULL;
56                         }
57                         p[1] = 0;
58                         i+=strlen(data+i)+2;
59                         p = g_strstr_len(data+k, len-k, "\n");
60                         p[0] = 0;
61                         g_hash_table_insert(h, data+k, p+1);
62                 } else {
63                         log_global(LOG_WARNING, "Unknown character '0x%02x' in help file", 
64                                            data[i]);
65                         g_hash_table_destroy(h);
66                         return NULL;
67                 }
68         }
69         
70         return h;
71 }
72
73 help_t *help_load_file( const char *helpfile )
74 {
75         help_t *h;
76         char *data;
77         GError *error = NULL;
78         gsize len;
79         
80         h = g_new0 (help_t, 1);
81         
82 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 8
83         h->file = g_mapped_file_new(helpfile, TRUE, &error);
84         if (h->file != NULL) {
85                 len = g_mapped_file_get_length(h->file);
86                 data = g_mapped_file_get_contents(h->file);
87         }
88         
89         if( h->file == NULL ) {
90                 log_global(LOG_WARNING, "Unable to open help file `%s': %s", helpfile, 
91                                   error->message);
92                 help_free( h );
93                 return NULL;
94         }
95 #else
96         if (!g_file_get_contents(helpfile, &h->file, &len, &error)) {
97                 log_global(LOG_WARNING, "Unable to open help file `%s': %s", helpfile, 
98                                   error->message);
99                 help_free( h );
100                 return NULL;
101         } 
102         data = h->file;
103 #endif
104
105         h->entries = help_build_hash(data, len);
106
107         return h;
108 }
109
110 const char *help_get(help_t *h, const char *string)
111 {
112         if (h == NULL)
113                 return NULL;
114         return g_hash_table_lookup(h->entries, string);
115 }