blob: cabc4753cf7f3298ec13888b3c1268c894c83436 (
plain)
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
|
/*
* Routines to access directory of section names in a config file.
* $Id$
* Copyright (c) 1997 by Tycho Softworks.
* For conditions of distribution and use see product license.
*
* Abstract:
* These routines may be used to scan the 'directory' of [] section
* names within a config file, much like the MS-DOS find_first() and
* find_next() directory routines.
*
* Functions:
* this_config() - internal routine to return section name.
* first_config() - goto first named [] section within the config file.
* next_config() - skip to the next named [] section.
*/
#include <other/config.h>
#include <other/string.h>
#include <other/env.h>
#ifndef LBUF
#define LBUF 1024
#endif
/*
* Internal program to support parsing of found section name.
*/
static char *this_config(CONFIG *cfg)
{
char *p;
if(feof(cfg->cfg_fp))
return NULL;
if(cfg->cfg_lbuf[0] != '[')
return NULL;
cfg->cfg_flag = TRUE;
p = strtok(cfg->cfg_lbuf, "[]\n\r");
return p;
}
/*
* Automatically seek the very first [] section within a config file.
*
* Paramaters:
* cfg - config object pointer.
*
* Returns:
* Name string of first [] name in config file.
*
* Exceptions:
* If no [] section in config file, NULL cfg pointer, or file
* error, returns NULL.
*/
char *first_config(CONFIG *cfg)
{
if(!cfg)
return NULL;
fseek(cfg->cfg_fp, 0l, SEEK_SET);
return next_config(cfg);
}
/*
* Skip to the next named [] section within a config file.
*
* Paramaters:
* cfg - config object pointer.
*
* Returns:
* Name string of next named [] section in config file.
*
* Exceptions:
* Returns NULL if cfg invalid or end of file reached.
*
*/
char *next_config(CONFIG *cfg)
{
if(!cfg)
return NULL;
if(feof(cfg->cfg_fp))
return NULL;
for(;;)
{
fgets(cfg->cfg_lbuf, LBUF - 1, cfg->cfg_fp);
if(feof(cfg->cfg_fp))
return NULL;
if(cfg->cfg_lbuf[0] == '[')
return this_config(cfg);
}
}
|