]> git.stg.codes - stg.git/blob - stglibs/dotconfpp.lib/include/stg/dotconfpp.h
Various fixes of issues reported by static analyzers.
[stg.git] / stglibs / dotconfpp.lib / include / stg / dotconfpp.h
1 /*  Copyright (C) 2003 Aleksey Krivoshey <voodoo@foss.kharkov.ua>
2 *
3 *   This program is free software; you can redistribute it and/or modify
4 *   it under the terms of the GNU General Public License as published by
5 *   the Free Software Foundation; either version 2 of the License, or
6 *   (at your option) any later version.
7 *
8 *   This program is distributed in the hope that it will be useful,
9 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *   GNU General Public License for more details.
12 *
13 *   You should have received a copy of the GNU General Public License
14 *   along with this program; if not, write to the Free Software
15 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 */
17
18
19 #ifndef DOTCONFPP_H
20 #define DOTCONFPP_H
21
22 #include <list>
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <strings.h>
28 #include <ctype.h>
29 #include <errno.h>
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <limits.h>
35
36 #include "stg/os_int.h"
37
38 typedef void (* DOTCONFCallback) (void * data, const char * buf);
39
40 class DOTCONFDocument;
41 class AsyncDNSMemPool;
42
43 class DOTCONFDocumentNode
44 {
45 friend class DOTCONFDocument;
46 private:
47     DOTCONFDocumentNode * previousNode;
48     DOTCONFDocumentNode * nextNode;
49     DOTCONFDocumentNode * parentNode;
50     DOTCONFDocumentNode * childNode;
51     char ** values;
52     int valuesCount;
53     char * name;
54     const DOTCONFDocument * document;
55     int lineNum;
56     char * fileName;
57     bool closed;
58
59     void pushValue(char * _value);
60
61 public:
62     DOTCONFDocumentNode();
63     ~DOTCONFDocumentNode();
64
65     const char * getConfigurationFileName() const { return fileName; }
66     int getConfigurationLineNumber() const { return lineNum; }
67
68     const DOTCONFDocumentNode * getNextNode() const { return nextNode; }
69     const DOTCONFDocumentNode * getPreviuosNode() const { return previousNode; }
70     const DOTCONFDocumentNode * getParentNode() const { return parentNode; }
71     const DOTCONFDocumentNode * getChildNode() const { return childNode; }
72     const char * getValue(int index = 0) const;
73     const char * getName() const { return name; }
74     const DOTCONFDocument * getDocument() const { return document; }
75 };
76
77 class DOTCONFDocument
78 {
79 public:
80     enum CaseSensitive { CASESENSITIVE, CASEINSENSITIVE };
81 protected:
82     AsyncDNSMemPool * mempool;    
83 private:
84     DOTCONFDocumentNode * curParent;
85     DOTCONFDocumentNode * curPrev;
86     DOTCONFCallback errorCallback;
87     void * errorCallbackData;
88     int curLine;
89     bool quoted;
90     std::list<DOTCONFDocumentNode *> nodeTree;
91     std::list<char *> requiredOptions;
92     std::list<char *> processedFiles;
93     FILE * file;
94     char * fileName;
95     std::list<char *> words;
96     int (* cmp_func)(const char *, const char *);
97
98     int checkRequiredOptions();
99     int parseLine();
100     int parseFile(DOTCONFDocumentNode * _parent = NULL);
101     int checkConfig(const std::list<DOTCONFDocumentNode *>::iterator & from);
102     int cleanupLine(char * line);
103     char * getSubstitution(char * macro, int lineNum);
104     int macroSubstitute(DOTCONFDocumentNode * tagNode, int valueIndex);
105
106 protected:
107     virtual void error(int lineNum, const char * fileName, const char * fmt, ...);
108
109 public:
110     explicit DOTCONFDocument(CaseSensitive caseSensitivity = CASESENSITIVE);
111     virtual ~DOTCONFDocument();
112
113     void setErrorCallback(DOTCONFCallback _callback, void * _data) { errorCallback = _callback; errorCallbackData = _data; }
114
115     int setContent(const char * _fileName);
116
117     void setRequiredOptionNames(const char ** requiredOptionNames); // !TERMINATE ARRAY WITH NULL
118     const DOTCONFDocumentNode * getFirstNode() const;    
119     const DOTCONFDocumentNode * findNode(const char * nodeName, const DOTCONFDocumentNode * parentNode = NULL, const DOTCONFDocumentNode * startNode = NULL) const;
120 };
121
122 #endif