summaryrefslogtreecommitdiff
path: root/src/libnml/inifile/inifile.cc
blob: 64ce24be7ef1c4cd9e885f0ec4eab0545ff83b71 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
   inifile.cc

   INI file reader

   Modification history:

  21-Jan-2004  P.C. Moved across from the original EMC source tree.
   */


#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>		/* FILE *, fopen(), fclose(), NULL */
#include <string.h>		/* strstr() */
#include <ctype.h>		/* isspace() */
#include <stdlib.h>		/* exit() */
#include <stdarg.h>		/* va_list */

#ifdef __cplusplus
}
#endif

#include "inifile.h"
#include "rcs_print.hh"

/********************************************************************
*
* Description: Constructor.
*
* Return Value: None.
*
* Side Effects: File descriptor is set to NULL, so open("file.name")
*               must be used before any search.
*
* Called By: 
*
********************************************************************/
INIFILE::INIFILE()
{
    fp = NULL;
}

/********************************************************************
*
* Description: Constructor with a file name.
*
* Return Value: None
*
* Side Effects:
*
* Called By: 
*
********************************************************************/
INIFILE::INIFILE(const char *path)
{
    if (NULL == (fp = fopen(path, "r"))) {
	fprintf(stderr, "can't open %s\n", path);
    }
}

/********************************************************************
*
* Description: Destructor
*
* Return Value: None
*
* Side Effects: Releases the file descriptor and any memory allocated.
*
* Called By: 
*
********************************************************************/
INIFILE::~INIFILE()
{
    if (NULL != fp) {
	fclose(fp);
    }
}

/********************************************************************
*
* Description: Opens the file for reading.
*
* Return Value: 0 on success
*               -1 on failure - Either file not found, or a permissions err.
*
* Side Effects: If a file was already open, the handle is lost.
*               FIX ME - Check and free any FD first ?
*
* Called By: 
*
********************************************************************/
const int INIFILE::open(const char *path)
{
    if (NULL == (fp = fopen(path, "r"))) {
	return -1;
    }
    return 0;
}

/********************************************************************
*
* Description: Closes the file descriptor..
*
* Return Value: 0 on success
*               -1 if an error occurred.
*
* Side Effects:
*
* Called By: 
*
********************************************************************/
const int INIFILE::close()
{
    int retval = 0;
    if (fp != NULL) {
	retval = fclose(fp);
	fp = NULL;
    }
    return retval;
}

/********************************************************************
*
* Description: Finds the tag in section.
*
* Return Value: First item after an '=' as a string.
*               NULL if not found.
*
* Side Effects:
*
* Called By: 
*
********************************************************************/
const char *INIFILE::find(const char *tag, const char *section)
{
    return iniFind(fp, tag, section);
}

/********************************************************************
*
* Description: section(const char *section, INIFILE_ENTRY array[], int max)
*               given 'section' and array of strings, fills strings
*               with what was found in the section, one line per string.
*               Comments and blank lines are omitted. 'array' is assumed
*               to be allocated, of 'max' entries of size INIFILE_MAX_LINELEN.
*
* Return Value: Returns number of entries found
*               0 if section is there but no entries in it, or
*               -1 if section is not found.
*
* Side Effects:
*
* Called By: 
*
********************************************************************/
int INIFILE::section(const char *section, INIFILE_ENTRY array[], int max)
{
    return iniSection(fp, section, array, max);
}

/********************************************************************
*
* Description: valid()
*               Reports if the file descriptor used in the constructor
*               is valid.
*
* Return Value: 1 if the fd is valid and file open.
*               0 if not valid.
*
* Side Effects:
*
* Called By: 
*
********************************************************************/
const int INIFILE::valid()
{
    if (NULL == fp) {
	return 0;
    }
    return 1;
}