1 | /***************************************
2 | $Revision: 1.12 $
3 |
4 | Properties module (pr) - this _should_ eventually get merged in with the
5 |
6 | Status: NOT REVUED, NOT TESTED
7 |
8 | +html+ <DL COMPACT>
9 | +html+ <DT>Online References:
10 | +html+ <DD><UL>
11 | +html+ <LI><A HREF=".properties">.properties</A>
12 | +html+ </UL>
13 | +html+ </DL>
14 | +html+ <PRE>
15 | Instructions for use:
16 |
17 | To get a property:
18 | use the PR_get_property("Property.name") function from your other code.
19 | +html+ </PRE>
20 |
21 | ******************/ /******************
22 | Filename : properties.c
23 | Description : Provides a hash table of tokens and their values.
24 | Author : ottrey@ripe.net
25 | Date : 04/03/1999
26 | OSs Tested : Solaris, BSDI, Linux
27 | Input Files : .properties
28 | Related Modules : Used in conjunction with the constants module.
29 | Problems :
30 | To Do : Fix up handling multi-lined properties.
31 | : PR_set() could be cleaned up a little.
32 | Comments :
33 | ******************/ /******************
34 | Copyright (c) 1999,2000,2001,2002 RIPE NCC
35 |
36 | All Rights Reserved
37 |
38 | Permission to use, copy, modify, and distribute this software and its
39 | documentation for any purpose and without fee is hereby granted,
40 | provided that the above copyright notice appear in all copies and that
41 | both that copyright notice and this permission notice appear in
42 | supporting documentation, and that the name of the author not be
43 | used in advertising or publicity pertaining to distribution of the
44 | software without specific, written prior permission.
45 |
46 | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
47 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
48 | AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
49 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
50 | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
51 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
52 | ***************************************/
53 |
54 | #include "rip.h"
55 |
56 | #include <stdio.h>
57 | #include <stdlib.h>
58 | #include <string.h>
59 | #include <glib.h>
60 |
61 | #define COMMENT_CHARACTER #
62 | #define MAX_PROPERTIES 1024
63 |
64 |
65 | /*
66 | * Type defs
67 | */
68 | /*+ Each property has a +*/
69 | typedef struct _Property {
70 | char *token; /*+ Token to be found in properties file. +*/
71 | char *value; /*+ Value to be found in properties file. +*/
72 | } *Property;
73 |
74 |
75 | /*
76 | * Global Variables
77 | */
78 | /*+ Array of Properties +*/
79 | Property Properties[MAX_PROPERTIES];
80 |
81 | /*+ The number of properties. +*/
82 | int Prop_count = 0;
83 |
84 | /*+ The name of properties file. +*/
85 | char *Prop_file_name;
86 |
87 |
88 |
89 | /* PR_to_string() */
90 | /*++++++++++++++++++++++++++++++++++++++
91 | Returns the properties as a string.
92 |
93 | More:
94 | +html+ <PRE>
95 | Authors:
96 | ottrey
97 |
98 | Pre-Conditions:
99 | The properties must be loaded first with load_properties().
100 |
101 | +html+ </PRE><DL COMPACT>
102 | +html+ <DT>Online References:
103 | +html+ <DD><UL>
104 | +html+ </UL></DL>
105 |
106 | ++++++++++++++++++++++++++++++++++++++*/
107 | char *PR_to_string(void) {
108 | char *props;
109 | GString *tmp;
110 | int i=0;
111 |
112 | tmp = g_string_new("Properties = { ");
113 | for(i=0; i< Prop_count; i++) {
114 | g_string_sprintfa(tmp, "[%s]=\"%s\" ",
115 | Properties[i]->token, Properties[i]->value );
116 | }
117 | g_string_append_c(tmp, '}');
118 |
119 | props = UT_strdup(tmp->str);
120 | g_string_free(tmp, TRUE);
121 |
122 | return props;
123 | } /* PR_to_string() */
124 |
125 | /* purge_properties() */
126 | /*++++++++++++++++++++++++++++++++++++++
127 | Purges the old properties.
128 |
129 | More:
130 | +html+ <PRE>
131 | Authors:
132 | ottrey
133 | +html+ </PRE><DL COMPACT>
134 | +html+ <DT>Online References:
135 | +html+ <DD><UL>
136 | +html+ <LI><A HREF="../src/.properties">.properties</A>
137 | +html+ </UL></DL>
138 |
139 | ++++++++++++++++++++++++++++++++++++++*/
140 | static void purge_properties(void) {
141 | int i;
142 |
143 | for(i=0; i < Prop_count; i++) {
144 | UT_free(Properties[i]->value);
145 | UT_free(Properties[i]->token);
146 | UT_free(Properties[i]);
147 | }
148 |
149 | Prop_count = 0;
150 | } /* purge_properties() */
151 |
152 |
153 | /* add_property() */
154 | /*++++++++++++++++++++++++++++++++++++++
155 | Adds a new property to the Properties array.
156 |
157 | More:
158 | +html+ <PRE>
159 | Authors:
160 | ottrey
161 | +html+ </PRE><DL COMPACT>
162 | +html+ <DT>Online References:
163 | +html+ <DD><UL>
164 | +html+ <LI><A HREF=".properties">.properties</A>
165 | +html+ </UL></DL>
166 |
167 | ++++++++++++++++++++++++++++++++++++++*/
168 | static void add_property(const char *token, const char *value) {
169 | Property prop;
170 |
171 | prop = (Property)UT_calloc(1, sizeof(struct _Property));
172 |
173 | prop->token = UT_strdup(token);
174 | prop->value = UT_strdup(value);
175 |
176 | Properties[Prop_count] = prop;
177 |
178 | Prop_count++;
179 | Properties[Prop_count] = NULL;
180 | } /* add_property() */
181 |
182 |
183 | /* PR_set() */
184 | /*++++++++++++++++++++++++++++++++++++++
185 | Sets the properties from the properties file.
186 |
187 | More:
188 | +html+ <PRE>
189 | Authors:
190 | ottrey
191 | +html+ </PRE><DL COMPACT>
192 | +html+ <DT>Online References:
193 | +html+ <DD><UL>
194 | +html+ <LI><A HREF=".properties">.properties</A>
195 | +html+ </UL></DL>
196 |
197 | ++++++++++++++++++++++++++++++++++++++*/
198 | char *PR_set() {
199 | FILE *prop_file;
200 | char prop_line[1024];
201 | char prop_line_more[1024];
202 | char *eql_ptr;
203 | char *token_ptr;
204 | char *token_e_ptr;
205 | char *value_ptr;
206 | char *value_more_ptr;
207 | char *value_e_ptr;
208 | int token_l, value_l;
209 | int more_lines;
210 | char the_token[64];
211 | char the_value[1024];
212 | char result_buff[256];
213 |
214 | prop_file = fopen(Prop_file_name, "r");
215 | if (prop_file == NULL) {
216 | fprintf(stderr, "Error: Can't find properties file: %s\n", Prop_file_name);
217 | sprintf(result_buff, "Error: Can't find properties file: %s", Prop_file_name);
218 | }
219 | else {
220 | purge_properties();
221 |
222 | while (fgets(prop_line, 1024, prop_file) != 0) {
223 | if ( (eql_ptr = strchr(prop_line, '=')) != NULL) {
224 | /* An "=" was found */
225 |
226 | token_ptr = prop_line;
227 | token_e_ptr = eql_ptr-1;
228 |
229 | /* Trim the trailing spaces/tabs off the token. */
230 | while (( *(token_e_ptr) == ' ') || ( *(token_e_ptr) == '\t')) {
231 | token_e_ptr--;
232 | }
233 |
234 | /* Trim the leading spaces/tabs off the token. */
235 | while (( *(token_ptr) == ' ') || ( *(token_ptr) == '\t')) {
236 | token_ptr++;
237 | }
238 |
239 | /* Skip if it's a comment line. */
240 | if (token_ptr[0] == '#' ) {
241 | continue;
242 | }
243 |
244 | /* Assign the token */
245 | token_l = (token_e_ptr - token_ptr) + 1;
246 | strncpy(the_token, token_ptr, token_l);
247 | the_token[token_l] = '\0';
248 |
249 | value_ptr = eql_ptr+1;
250 | value_e_ptr = strchr(prop_line, '\n')-1;
251 |
252 | /* Trim the leading spaces/tabs off the value. */
253 | while (( *(value_ptr) == ' ') || ( *(value_ptr) == '\t')) {
254 | value_ptr++;
255 | }
256 |
257 | /* Trim the trailing spaces/tabs off the value. */
258 | while (( *(value_e_ptr) == ' ') || ( *(value_e_ptr) == '\t')) {
259 | value_e_ptr--;
260 | }
261 |
262 | /* Assign the value */
263 | value_l = (value_e_ptr - value_ptr) + 1;
264 | strncpy(the_value, value_ptr, value_l);
265 | the_value[value_l] = '\0';
266 |
267 | /* If the value goes over the line */
268 | if ((value_e_ptr = strrchr(the_value, '\\')) != NULL) {
269 | *value_e_ptr = ' ';
270 | more_lines = 0;
271 | do {
272 | if (fgets(prop_line_more, 1024, prop_file) != 0) {
273 |
274 | /* Trim the leading spaces/tabs off the line_more. */
275 | value_more_ptr = prop_line_more;
276 | while (( *(value_more_ptr) == ' ') || ( *(value_more_ptr) == '\t')) {
277 | value_more_ptr++;
278 | }
279 |
280 | /* Trim the trailing spaces/tabs off the value. */
281 | if ((value_e_ptr = strrchr(prop_line_more, '\\')) != NULL) {
282 | more_lines = 1;
283 | *value_e_ptr = ' ';
284 | }
285 | else {
286 | more_lines = 0;
287 | }
288 | value_e_ptr = strchr(prop_line_more, '\n');
289 | *value_e_ptr = ' ';
290 | while ((*value_e_ptr == ' ') || (*value_e_ptr == '\t')) {
291 | value_e_ptr--;
292 | }
293 |
294 | *(value_e_ptr+1) = '\0';
295 | strcat(the_value, value_more_ptr);
296 |
297 | }
298 | } while (more_lines == 1);
299 |
300 | value_l = strlen(the_value);
301 | the_value[value_l] = '\0';
302 | }
303 |
304 | add_property(the_token, the_value);
305 | } else {
306 | /* Skip this line */
307 | ;
308 | }
309 | }
310 |
311 | /*
312 | printf("%s\n", PR_to_string() );
313 | */
314 |
315 | fclose(prop_file);
316 |
317 | sprintf(result_buff, "Properties successfully set from %s file. (%d properties)", Prop_file_name, Prop_count);
318 | }
319 |
320 | return UT_strdup(result_buff);
321 | } /* PR_set() */
322 |
323 |
324 | /* PR_load() */
325 | /*++++++++++++++++++++++++++++++++++++++
326 | Sets the properties file name. Then sets the properties with a call to set_properties().
327 |
328 | More:
329 | +html+ <PRE>
330 | Authors:
331 | ottrey
332 | +html+ </PRE><DL COMPACT>
333 | +html+ <DT>Online References:
334 | +html+ <DD><UL>
335 | +html+ <LI><A HREF=".properties">.properties</A>
336 | +html+ </UL></DL>
337 |
338 | ++++++++++++++++++++++++++++++++++++++*/
339 | void PR_load(const char *prop_file_name) {
340 |
341 | Prop_file_name = UT_strdup(prop_file_name);
342 | UT_free(PR_set());
343 |
344 | } /* PR_load() */
345 |
346 |
347 | /* PR_get_property() */
348 | /*++++++++++++++++++++++++++++++++++++++
349 | Sets the properties file name. Then sets the properties with a call to set_properties().
350 |
351 | More:
352 | +html+ <PRE>
353 | Authors:
354 | ottrey
355 | +html+ </PRE><DL COMPACT>
356 | +html+ <DT>Online References:
357 | +html+ <DD><UL>
358 | +html+ <LI><A HREF=".properties">.properties</A>
359 | +html+ </UL></DL>
360 |
361 | ++++++++++++++++++++++++++++++++++++++*/
362 | char *PR_get_property(const char *token, const char *default_value) {
363 | char *value;
364 | int i = 0;
365 |
366 | /* Search through the Properties until the token is found */
367 | while (i < Prop_count) {
368 | if (strcmp(token, Properties[i]->token) == 0) {
369 | break;
370 | }
371 | i++;
372 | }
373 |
374 | if (i == Prop_count) {
375 | /* If token not found return the default value */
376 | if (default_value == NULL) {
377 | value = UT_strdup("");
378 | } else {
379 | value = UT_strdup(default_value);
380 | }
381 | } else {
382 | /* Return the found value */
383 | value = UT_strdup(Properties[i]->value);
384 | }
385 |
386 | return value;
387 |
388 | } /* PR_get_property() */