Created SILC Client Libary by moving stuff from silc/ directory.
[silc.git] / lib / silcutil / silcconfig.c
1 /*
2
3   silcconfig.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /*
21  * $Id$
22  * $Log$
23  * Revision 1.1  2000/09/13 17:45:16  priikone
24  *      Splitted SILC core library. Core library includes now only
25  *      SILC protocol specific stuff. New utility library includes the
26  *      old stuff from core library that is more generic purpose stuff.
27  *
28  * Revision 1.2  2000/07/05 06:06:35  priikone
29  *      Global cosmetic change.
30  *
31  * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
32  *      Imported from internal CVS/Added Log headers.
33  *
34  *
35  */
36
37 #include "silcincludes.h"
38
39 /* Opens and reads a configuration file to a buffer. The read data is 
40    returned to the ret_buffer argument. */
41
42 void silc_config_open(char *filename, SilcBuffer *ret_buffer)
43 {
44   char *buffer;
45   int filelen;
46
47   buffer = silc_file_read(filename, &filelen);
48   if (buffer == NULL)
49     return;
50
51   /* Buffer don't have EOF, but we'll need it. */
52   buffer[filelen] = EOF;
53
54   *ret_buffer = silc_buffer_alloc(filelen + 1);
55   silc_buffer_pull_tail(*ret_buffer, filelen + 1);
56   silc_buffer_put(*ret_buffer, buffer, filelen + 1);
57
58   SILC_LOG_DEBUG(("Config file `%s' opened", filename));
59 }
60
61 /* Returns next token from a buffer to the dest argument. Returns the
62    length of the token. This is used to take tokens from a configuration
63    line. */
64
65 int silc_config_get_token(SilcBuffer buffer, char **dest)
66 {
67   int len;
68
69   if (strchr(buffer->data, ':')) {
70     len = strcspn(buffer->data, ":");
71     if (len) {
72       *dest = silc_calloc(len + 1, sizeof(char));
73       memset(*dest, 0, len + 1);
74       memcpy(*dest, buffer->data, len);
75     }
76     silc_buffer_pull(buffer, len + 1);
77     return len;
78   }
79
80   return -1;
81 }
82
83 /* Returns number of tokens in a buffer. */
84
85 int silc_config_check_num_token(SilcBuffer buffer)
86 {
87   int len, len2, num;
88
89   if (strchr(buffer->data, ':')) {
90     len = 0;
91     num = 0;
92     while (strchr(buffer->data + len, ':')) {
93       num++;
94       len2 = strcspn(buffer->data + len, ":") + 1;
95       len += len2;
96     }
97
98     return num;
99   }
100
101   return 0;
102 }