Skip to content

Commit c3ac4f5

Browse files
committed
inifile: detect spurious trailing text on numbers in inifiles
this will break inifiles, because before now this text was just ignored by anything that uses the 'typed' interface to InfFile::Find. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
1 parent cc2959d commit c3ac4f5

File tree

2 files changed

+54
-97
lines changed

2 files changed

+54
-97
lines changed

src/libnml/inifile/inifile.cc

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -103,92 +103,6 @@ IniFile::Close()
103103
}
104104

105105

106-
IniFile::ErrorCode
107-
IniFile::Find(int *result, int min, int max,
108-
const char *tag, const char *section, int num)
109-
{
110-
ErrorCode errCode;
111-
int tmp;
112-
113-
if((errCode = Find(&tmp, tag, section, num)) != ERR_NONE)
114-
return(errCode);
115-
116-
if((tmp > max) || (tmp < min))
117-
return(ERR_LIMITS);
118-
119-
*result = tmp;
120-
121-
return(ERR_NONE);
122-
}
123-
124-
125-
IniFile::ErrorCode
126-
IniFile::Find(int *result, const char *tag, const char *section, int num)
127-
{
128-
const char *pStr;
129-
int tmp;
130-
131-
if((pStr = Find(tag, section, num)) == NULL){
132-
// We really need an ErrorCode return from Find() and should be passing
133-
// in a buffer. Just pick a suitable ErrorCode for now.
134-
return(ERR_TAG_NOT_FOUND);
135-
}
136-
137-
if(sscanf(pStr, "%i", &tmp) != 1){
138-
ThrowException(ERR_CONVERSION);
139-
return(ERR_CONVERSION);
140-
}
141-
142-
*result = tmp;
143-
144-
return(ERR_NONE);
145-
}
146-
147-
148-
IniFile::ErrorCode
149-
IniFile::Find(double *result, double min, double max,
150-
const char *tag, const char *section, int num)
151-
{
152-
ErrorCode errCode;
153-
double tmp;
154-
155-
if((errCode = Find(&tmp, tag, section, num)) != ERR_NONE)
156-
return(errCode);
157-
158-
if((tmp > max) || (tmp < min))
159-
return(ERR_LIMITS);
160-
161-
*result = tmp;
162-
163-
return(ERR_NONE);
164-
}
165-
166-
167-
IniFile::ErrorCode
168-
IniFile::Find(double *result, const char *tag, const char *section,
169-
int num, int *lineno)
170-
{
171-
const char *pStr;
172-
double tmp;
173-
174-
if((pStr = Find(tag, section, num)) == NULL){
175-
// We really need an ErrorCode return from Find() and should be passing
176-
// in a buffer. Just pick a suitable ErrorCode for now.
177-
return(ERR_TAG_NOT_FOUND);
178-
}
179-
180-
if(sscanf(pStr, "%lf", &tmp) != 1){
181-
ThrowException(ERR_CONVERSION);
182-
return(ERR_CONVERSION);
183-
}
184-
185-
*result = tmp;
186-
if (lineno)
187-
*lineno = lineNo;
188-
return(ERR_NONE);
189-
}
190-
191-
192106
IniFile::ErrorCode
193107
IniFile::Find(int *result, StrIntPair *pPair,
194108
const char *tag, const char *section, int num, int *lineno)

src/libnml/inifile/inifile.hh

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define INIFILE_HH
1818

1919
#include <inifile.h>
20+
#include <string>
21+
#include <boost/lexical_cast.hpp>
2022

2123
#ifndef __cplusplus
2224
#warning Inclusion of <inifile.hh> from C programs is deprecated. Include <inifile.h> instead.
@@ -53,19 +55,60 @@ public:
5355
bool Open(const char *file);
5456
bool Close(void);
5557
bool IsOpen(void){ return(fp != NULL); }
56-
ErrorCode Find(int *result, int min, int max,
57-
const char *tag,const char *section,
58-
int num=1);
59-
ErrorCode Find(int *result, const char *tag,
60-
const char *section=NULL, int num = 1);
61-
ErrorCode Find(double *result, double min, double max,
62-
const char *tag,const char *section,
63-
int num=1);
64-
ErrorCode Find(double *result, const char *tag,
65-
const char *section=NULL, int num = 1,
66-
int *lineno = NULL);
58+
6759
const char * Find(const char *tag, const char *section=NULL,
6860
int num = 1, int *lineno = NULL);
61+
62+
template<class T>
63+
ErrorCode Find(T *result, T min, T max,
64+
const char *tag,const char *section,
65+
int num=1) {
66+
ErrorCode errCode;
67+
T tmp;
68+
if((errCode = Find(&tmp, tag, section, num)) != ERR_NONE)
69+
return(errCode);
70+
71+
if((tmp > max) || (tmp < min)) {
72+
ThrowException(ERR_LIMITS);
73+
return(ERR_LIMITS);
74+
}
75+
76+
*result = tmp;
77+
78+
return(ERR_NONE);
79+
}
80+
81+
template<class T>
82+
ErrorCode Find(T *result,
83+
const char *tag,const char *section,
84+
int num=1) {
85+
ErrorCode errCode;
86+
std::string tmp;
87+
if((errCode = Find(&tmp, tag, section, num)) != ERR_NONE)
88+
return(errCode);
89+
90+
try {
91+
*result = boost::lexical_cast<T>(tmp);
92+
} catch (boost::bad_lexical_cast &) {
93+
ThrowException(ERR_CONVERSION);
94+
return(ERR_CONVERSION);
95+
}
96+
97+
return(ERR_NONE);
98+
}
99+
100+
ErrorCode Find(std::string *s,
101+
const char *tag,const char *section,
102+
int num=1) {
103+
const char *tmp = Find(tag, section, num);
104+
if(!tmp)
105+
return ERR_TAG_NOT_FOUND; // can't distinguish errors, ugh
106+
107+
*s = tmp;
108+
109+
return(ERR_NONE);
110+
}
111+
69112
const char * FindString(char *dest, size_t n,
70113
const char *tag, const char *section=NULL,
71114
int num = 1, int *lineno = NULL);

0 commit comments

Comments
 (0)