]> git.stg.codes - stg.git/blob - libs/dotconfpp/include/stg/dotconfpp.h
Port to CMake, get rid of os_int.h.
[stg.git] / libs / dotconfpp / 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 typedef void (* DOTCONFCallback) (void * data, const char * buf);
37
38 class DOTCONFDocument;
39 class AsyncDNSMemPool;
40
41 class DOTCONFDocumentNode
42 {
43 friend class DOTCONFDocument;
44 private:
45     DOTCONFDocumentNode * previousNode;
46     DOTCONFDocumentNode * nextNode;
47     DOTCONFDocumentNode * parentNode;
48     DOTCONFDocumentNode * childNode;
49     char ** values;
50     int valuesCount;
51     char * name;
52     const DOTCONFDocument * document;
53     int lineNum;
54     char * fileName;
55     bool closed;
56
57     void pushValue(char * _value);
58
59 public:
60     DOTCONFDocumentNode();
61     ~DOTCONFDocumentNode();
62
63     const char * getConfigurationFileName() const { return fileName; }
64     int getConfigurationLineNumber() const { return lineNum; }
65
66     const DOTCONFDocumentNode * getNextNode() const { return nextNode; }
67     const DOTCONFDocumentNode * getPreviuosNode() const { return previousNode; }
68     const DOTCONFDocumentNode * getParentNode() const { return parentNode; }
69     const DOTCONFDocumentNode * getChildNode() const { return childNode; }
70     const char * getValue(int index = 0) const;
71     const char * getName() const { return name; }
72     const DOTCONFDocument * getDocument() const { return document; }
73 };
74
75 class DOTCONFDocument
76 {
77 public:
78     enum CaseSensitive { CASESENSITIVE, CASEINSENSITIVE };
79 protected:
80     AsyncDNSMemPool * mempool;    
81 private:
82     DOTCONFDocumentNode * curParent;
83     DOTCONFDocumentNode * curPrev;
84     DOTCONFCallback errorCallback;
85     void * errorCallbackData;
86     int curLine;
87     bool quoted;
88     std::list<DOTCONFDocumentNode *> nodeTree;
89     std::list<char *> requiredOptions;
90     std::list<char *> processedFiles;
91     FILE * file;
92     char * fileName;
93     std::list<char *> words;
94     int (* cmp_func)(const char *, const char *);
95
96     int checkRequiredOptions();
97     int parseLine();
98     int parseFile(DOTCONFDocumentNode * _parent = NULL);
99     int checkConfig(const std::list<DOTCONFDocumentNode *>::iterator & from);
100     int cleanupLine(char * line);
101     char * getSubstitution(char * macro, int lineNum);
102     int macroSubstitute(DOTCONFDocumentNode * tagNode, int valueIndex);
103
104 protected:
105     virtual void error(int lineNum, const char * fileName, const char * fmt, ...);
106
107 public:
108     explicit DOTCONFDocument(CaseSensitive caseSensitivity = CASESENSITIVE);
109     virtual ~DOTCONFDocument();
110
111     void setErrorCallback(DOTCONFCallback _callback, void * _data) { errorCallback = _callback; errorCallbackData = _data; }
112
113     int setContent(const char * _fileName);
114
115     void setRequiredOptionNames(const char ** requiredOptionNames); // !TERMINATE ARRAY WITH NULL
116     const DOTCONFDocumentNode * getFirstNode() const;    
117     const DOTCONFDocumentNode * findNode(const char * nodeName, const DOTCONFDocumentNode * parentNode = NULL, const DOTCONFDocumentNode * startNode = NULL) const;
118 };
119
120 #endif