aboutsummaryrefslogtreecommitdiffstats
path: root/src/libnml/inifile/inifile.hh
blob: adb7f48d80d1f1af72642b096bfcee321b46dcc8 (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
/********************************************************************
* Description: inifile.hh
*   Declarations for INI file format functions
*
*   Derived from a work by Fred Proctor & Will Shackleford
*
* Author:
* License: GPL Version 2
* System: Linux
*    
* Copyright (c) 2004 All rights reserved.
*
* Last change: 
********************************************************************/

#ifndef INIFILE_HH
#define INIFILE_HH

#include <inifile.h>
#include <string>
#include <boost/lexical_cast.hpp>

#ifndef __cplusplus
#warning Inclusion of <inifile.hh> from C programs is deprecated.  Include <inifile.h> instead.
#endif

#ifdef __cplusplus
#include <fcntl.h>
class IniFile {
public:
    typedef enum {
        ERR_NONE                = 0x00,
        ERR_NOT_OPEN            = 0x01,
        ERR_SECTION_NOT_FOUND   = 0x02,
        ERR_TAG_NOT_FOUND       = 0x04,
        ERR_CONVERSION          = 0x08,
        ERR_LIMITS              = 0x10,
        ERR_OVER_EXTENDED       = 0x20,
    } ErrorCode;

    class Exception {
    public:
        ErrorCode               errCode;
        const char *            tag;
        const char *            section;
        int                     num;
        unsigned int            lineNo;

        void                    Print(FILE *fp=stderr);
    };


                                IniFile(int errMask=0, FILE *fp=NULL);
                                ~IniFile(void){ Close(); }

    bool                        Open(const char *file);
    bool                        Close(void);
    bool                        IsOpen(void){ return(fp != NULL); }

    const char *                Find(const char *tag, const char *section=NULL,
                                     int num = 1, int *lineno = NULL);

    template<class T>
    ErrorCode                   Find(T *result, T min, T max,
                                     const char *tag,const char *section,
                                     int num=1) {
        ErrorCode errCode;
        T tmp;
        if((errCode = Find(&tmp, tag, section, num)) != ERR_NONE)
            return(errCode);

        if((tmp > max) || (tmp < min)) {
            ThrowException(ERR_LIMITS);
            return(ERR_LIMITS);
        }

        *result = tmp;

        return(ERR_NONE);
    }

    template<class T>
    ErrorCode                   Find(T *result,
                                     const char *tag,const char *section,
                                     int num=1) {
        ErrorCode errCode;
        std::string tmp;
        if((errCode = Find(&tmp, tag, section, num)) != ERR_NONE)
            return(errCode);

        try {
            *result = boost::lexical_cast<T>(tmp);
        } catch (boost::bad_lexical_cast &) {
            ThrowException(ERR_CONVERSION);
            return(ERR_CONVERSION);
        }

        return(ERR_NONE);
    }

    ErrorCode                   Find(std::string *s,
                                     const char *tag,const char *section,
                                     int num=1) {
        const char *tmp = Find(tag, section, num);
        if(!tmp)
            return ERR_TAG_NOT_FOUND; // can't distinguish errors, ugh

        *s = tmp;

        return(ERR_NONE);
    }

    const char *                FindString(char *dest, size_t n,
				     const char *tag, const char *section=NULL,
				     int num = 1, int *lineno = NULL);
    const char *                FindPath(char *dest, size_t n,
				     const char *tag, const char *section=NULL,
				     int num = 1, int *lineno = NULL);
    void                        EnableExceptions(int _errMask){
                                    errMask = _errMask;
                                }

    ErrorCode                   TildeExpansion(const char *file, char *path,
					       size_t n);

protected:
    struct StrIntPair {
        const char             *pStr;
        int                     value;
    };

    struct StrDoublePair {
        const char              *pStr;
        double                   value;
    };


    ErrorCode                   Find(double *result, StrDoublePair *,
                                     const char *tag, const char *section=NULL,
                                     int num = 1, int *lineno = NULL);
    ErrorCode                   Find(int *result, StrIntPair *,
                                     const char *tag, const char *section=NULL,
                                     int num = 1, int *lineno = NULL);


private:
    FILE                        *fp;
    struct flock                lock;
    bool                        owned;

    Exception                   exception;
    int                         errMask;

    unsigned int                lineNo;
    const char *                tag;
    const char *                section;
    int                         num;

    bool                        CheckIfOpen(void);
    bool                        LockFile(void);
    void                        ThrowException(ErrorCode);
    char                        *AfterEqual(const char *string);
    char                        *SkipWhite(const char *string);
};
#endif


#endif
bues.ch cgit interface