-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.h
204 lines (161 loc) · 4.78 KB
/
main.h
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
Copyright 2008 Gianni Rossi
This file is part of DBBinder++.
DBBinder++ is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DBBinder++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DBBinder++. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __DBBINDER_MAIN_H
#define __DBBINDER_MAIN_H
#include <template.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
namespace DBBinder
{
/*
Since Google decided to change the namespace between releases,
these typedefs will put everything we need in the DBBinder namespace
*/
#ifdef CTEMPLATE_TEMPLATE_H_
typedef ctemplate::TemplateDictionary TemplateDictionary;
typedef ctemplate::Template Template;
#define DO_NOT_STRIP ctemplate::DO_NOT_STRIP
#else
typedef google::TemplateDictionary TemplateDictionary;
typedef google::Template Template;
#define DO_NOT_STRIP google::DO_NOT_STRIP
#endif
typedef std::vector<std::string> ListString;
enum FileType
{
ftNULL,
ftYAML,
ftXML,
ftSQL,
};
// Options set by command line
extern std::string appName;
extern std::string optOutput;
extern std::string optTemplate;
extern ListString optTemplateDirs;
extern const char* optVersionMajor;
extern const char* optVersionMinor;
extern bool optListDepends;
extern ListString optDepends;
extern bool optExtras;
extern ListString optIncludeFiles;
// Utility funcs and defines
inline std::string stringToLower(const std::string& _string)
{
std::string::size_type sz = _string.length();
std::string result;
result.resize( sz );
for(std::string::size_type i = 0; i < sz; ++i)
result[i] += tolower(_string[i]);
return result;
}
inline std::string stringToUpper(const std::string& _string)
{
std::string::size_type sz = _string.length();
std::string result;
result.resize( sz );
for(std::string::size_type i = 0; i < sz; ++i)
result[i] += toupper(_string[i]);
return result;
}
inline std::string extractFileName(const std::string& _string)
{
std::string::size_type pos = _string.rfind( '/' );
if ( pos != std::string::npos )
return _string.substr( pos + 1 );
else
return _string;
}
std::string cescape(const std::string& _string);
inline ListString stringTok(const std::string &_string, const char _sep)
{
ListString result;
const char *p = _string.c_str();
const char *s = p;
while ( *++p )
{
if ( *p == _sep )
{
result.push_back( std::string( s, p - s ));
++p;
s = p;
}
}
if ( *s )
result.push_back( std::string( s, p - s ));
return result;
}
ListString stringTok(const char* _str);
inline std::string getFilenameRelativeTo(const std::string& _relFileName, const std::string& _fileName)
{
std::string result;
std::string::size_type pos = _relFileName.rfind('/');
if ( pos && pos != std::string::npos )
{
result = _relFileName.substr(0, pos);
result += '/';
}
result += _fileName;
return result;
}
inline bool fileExists(const std::string& fileName)
{
struct stat fs;
return stat(fileName.c_str(), &fs) == 0;
}
enum FileCheckType
{
fctRegularFile = S_IFREG,
fctDirectory = S_IFDIR
};
enum FileCheck
{
fcOK,
fcDoesNotExist,
fcIsNotExpectedType
};
inline FileCheck checkFileExistsAndType(const std::string& fileName, FileCheckType type)
{
struct stat fs;
if ( stat(fileName.c_str(), &fs) )
return fcDoesNotExist;
else
{
if (( fs.st_mode & S_IFMT ) != type )
return fcIsNotExpectedType;
}
return fcOK;
}
}
#ifdef NDEBUG
#define FATAL(MSG) { std::cerr << MSG << std::endl; abort(); }
#define ASSERT(COND, MSG)
#else
#define FATAL(MSG) { std::cerr << MSG << std::endl; assert(false); }
#define ASSERT(COND, MSG) { if ( !COND ) std::cerr << DBBinder::appName << ": " << MSG << std::endl; assert(COND); }
#endif
#define WARNING(MSG) { std::cerr << DBBinder::appName << ": " << MSG << std::endl; }
#define GET_TEXT_OR_ATTR( str, elem, attr ) { str = elem->GetText(false); if ( str.empty() ) elem->GetAttribute(attr, &str, false); }
#include <boost/function.hpp>
#include <boost/foreach.hpp>
#define foreach(X, Y) BOOST_FOREACH(X, Y)
#define UNUSED(VAR) (void)(VAR);
#endif // __DBBINDER_MAIN_H