File: help.c

package info (click to toggle)
ctrlproxy 3.0.7-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,668 kB
  • ctags: 2,045
  • sloc: ansic: 15,358; sh: 2,805; xml: 1,078; python: 346; makefile: 302; perl: 18
file content (119 lines) | stat: -rw-r--r-- 2,932 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
	ctrlproxy: A modular IRC proxy
	help: module for access the help system

	(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
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "internals.h"
#include "help.h"
#include "log.h"

#define BUFSIZE 1100

void help_free(help_t *h)
{
	if (h->file != NULL)
#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 8
		g_mapped_file_free(h->file);
#else
		g_free(h->file);
#endif
	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_full(g_str_hash, g_str_equal,
										  g_free, NULL);
	gsize i;
	char *p;

	i = 0;
	while (i < len) {
		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;
}

help_t *help_load_file( const char *helpfile )
{
	help_t *h;
	char *data;
	GError *error = NULL;
	gsize len;
	
	h = g_new0 (help_t, 1);
	
#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);
	}
	
	if (h->file == NULL) {
		log_global(LOG_WARNING, "Unable to open help file `%s': %s", helpfile, 
				  error->message);
		help_free( h );
		return NULL;
	}
#else
	if (!g_file_get_contents(helpfile, &h->file, &len, &error)) {
		log_global(LOG_WARNING, "Unable to open help file `%s': %s", helpfile, 
				  error->message);
		help_free( h );
		return NULL;
	} 
	data = h->file;
#endif

	h->entries = help_build_hash(data, len);

	return h;
}

const char *help_get(help_t *h, const char *string)
{
	if (h == NULL)
		return NULL;
	return g_hash_table_lookup(h->entries, string);
}