updates
[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 /* $Id$ */
21
22 #include "silcincludes.h"
23
24 /* Opens and reads a configuration file to a buffer. The read data is 
25    returned to the ret_buffer argument. */
26
27 void silc_config_open(char *filename, SilcBuffer *ret_buffer)
28 {
29   char *buffer;
30   uint32 filelen;
31
32   buffer = silc_file_readfile(filename, &filelen);
33   if (buffer == NULL)
34     return;
35
36   /* Buffer don't have EOF, but we'll need it. */
37   buffer[filelen] = EOF;
38
39   *ret_buffer = silc_buffer_alloc(filelen + 1);
40   silc_buffer_pull_tail(*ret_buffer, filelen + 1);
41   silc_buffer_put(*ret_buffer, buffer, filelen + 1);
42
43   SILC_LOG_DEBUG(("Config file `%s' opened", filename));
44 }
45
46 /* Returns next token from a buffer to the dest argument. Returns the
47    length of the token. This is used to take tokens from a configuration
48    line. */
49
50 int silc_config_get_token(SilcBuffer buffer, char **dest)
51 {
52   int len;
53
54   if (strchr(buffer->data, ':')) {
55     len = strcspn(buffer->data, ":");
56     if (len) {
57       *dest = silc_calloc(len + 1, sizeof(char));
58       memcpy(*dest, buffer->data, len);
59     }
60     silc_buffer_pull(buffer, len + 1);
61     return len;
62   }
63
64   return -1;
65 }
66
67 /* Returns number of tokens in a buffer. */
68
69 int silc_config_check_num_token(SilcBuffer buffer)
70 {
71   int len, len2, num;
72
73   if (strchr(buffer->data, ':')) {
74     len = 0;
75     num = 0;
76     while (strchr(buffer->data + len, ':')) {
77       num++;
78       len2 = strcspn(buffer->data + len, ":") + 1;
79       len += len2;
80     }
81
82     return num;
83   }
84
85   return 0;
86 }