Global cosmetic change.
[silc.git] / lib / silccore / 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.2  2000/07/05 06:06:35  priikone
24  *      Global cosmetic change.
25  *
26  * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
27  *      Imported from internal CVS/Added Log headers.
28  *
29  *
30  */
31
32 #include "silcincludes.h"
33
34 /* Opens and reads a configuration file to a buffer. The read data is 
35    returned to the ret_buffer argument. */
36
37 void silc_config_open(char *filename, SilcBuffer *ret_buffer)
38 {
39   char *buffer;
40   int filelen;
41
42   buffer = silc_file_read(filename, &filelen);
43   if (buffer == NULL)
44     return;
45
46   /* Buffer don't have EOF, but we'll need it. */
47   buffer[filelen] = EOF;
48
49   *ret_buffer = silc_buffer_alloc(filelen + 1);
50   silc_buffer_pull_tail(*ret_buffer, filelen + 1);
51   silc_buffer_put(*ret_buffer, buffer, filelen + 1);
52
53   SILC_LOG_DEBUG(("Config file `%s' opened", filename));
54 }
55
56 /* Returns next token from a buffer to the dest argument. Returns the
57    length of the token. This is used to take tokens from a configuration
58    line. */
59
60 int silc_config_get_token(SilcBuffer buffer, char **dest)
61 {
62   int len;
63
64   if (strchr(buffer->data, ':')) {
65     len = strcspn(buffer->data, ":");
66     if (len) {
67       *dest = silc_calloc(len + 1, sizeof(char));
68       memset(*dest, 0, len + 1);
69       memcpy(*dest, buffer->data, len);
70     }
71     silc_buffer_pull(buffer, len + 1);
72     return len;
73   }
74
75   return -1;
76 }
77
78 /* Returns number of tokens in a buffer. */
79
80 int silc_config_check_num_token(SilcBuffer buffer)
81 {
82   int len, len2, num;
83
84   if (strchr(buffer->data, ':')) {
85     len = 0;
86     num = 0;
87     while (strchr(buffer->data + len, ':')) {
88       num++;
89       len2 = strcspn(buffer->data + len, ":") + 1;
90       len += len2;
91     }
92
93     return num;
94   }
95
96   return 0;
97 }