-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphinxutils.h
276 lines (213 loc) · 8.11 KB
/
sphinxutils.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// $Id$
//
//
// Copyright (c) 2001-2015, Andrew Aksyonoff
// Copyright (c) 2008-2015, Sphinx Technologies Inc
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//
/// @file sphinxutils.h
/// Declarations for the stuff shared by all Sphinx utilities.
#ifndef _sphinxutils_
#define _sphinxutils_
#include <ctype.h>
#include <stdarg.h>
//////////////////////////////////////////////////////////////////////////
/// my own isalpha (let's build our own theme park!)
inline int sphIsAlpha ( int c )
{
return ( c>='0' && c<='9' ) || ( c>='a' && c<='z' ) || ( c>='A' && c<='Z' ) || c=='-' || c=='_';
}
/// my own isspace
inline bool sphIsSpace ( int iCode )
{
return iCode==' ' || iCode=='\t' || iCode=='\n' || iCode=='\r';
}
/// check for keyword modifiers
inline bool sphIsModifier ( int iSymbol )
{
return iSymbol=='^' || iSymbol=='$' || iSymbol=='=' || iSymbol=='*';
}
/// all wildcards
inline bool sphIsWild ( char c )
{
return c=='*' || c=='?' || c=='%';
}
/// string splitter, extracts sequences of alphas (as in sphIsAlpha)
void sphSplit ( CSphVector<CSphString> & dOut, const char * sIn );
/// string splitter, splits by the given boundaries
void sphSplit ( CSphVector<CSphString> & dOut, const char * sIn, const char * sBounds );
/// string wildcard matching (case-sensitive, supports * and ? patterns)
bool sphWildcardMatch ( const char * sSstring, const char * sPattern );
//////////////////////////////////////////////////////////////////////////
/// config section (hash of variant values)
class CSphConfigSection : public SmallStringHash_T < CSphVariant >
{
public:
CSphConfigSection ()
: m_iTag ( 0 )
{}
/// get integer option value by key and default value
int GetInt ( const char * sKey, int iDefault=0 ) const
{
CSphVariant * pEntry = (*this)( sKey );
return pEntry ? pEntry->intval() : iDefault;
}
/// get float option value by key and default value
float GetFloat ( const char * sKey, float fDefault=0.0f ) const
{
CSphVariant * pEntry = (*this)( sKey );
return pEntry ? pEntry->floatval() : fDefault;
}
/// get string option value by key and default value
const char * GetStr ( const char * sKey, const char * sDefault="" ) const
{
CSphVariant * pEntry = (*this)( sKey );
return pEntry ? pEntry->strval().cstr() : sDefault;
}
/// get size option (plain int, or with K/M prefix) value by key and default value
int GetSize ( const char * sKey, int iDefault ) const;
int64_t GetSize64 ( const char * sKey, int64_t iDefault ) const;
int m_iTag;
};
/// config section type (hash of sections)
typedef SmallStringHash_T < CSphConfigSection > CSphConfigType;
/// config (hash of section types)
typedef SmallStringHash_T < CSphConfigType > CSphConfig;
/// simple config file
class CSphConfigParser
{
public:
CSphConfig m_tConf;
public:
CSphConfigParser ();
bool Parse ( const char * sFileName, const char * pBuffer = NULL );
// fail-save loading new config over existing.
bool ReParse ( const char * sFileName, const char * pBuffer = NULL );
protected:
CSphString m_sFileName;
int m_iLine;
CSphString m_sSectionType;
CSphString m_sSectionName;
char m_sError [ 1024 ];
int m_iWarnings;
static const int WARNS_THRESH = 5;
protected:
bool IsPlainSection ( const char * sKey );
bool IsNamedSection ( const char * sKey );
bool AddSection ( const char * sType, const char * sSection );
void AddKey ( const char * sKey, char * sValue );
bool ValidateKey ( const char * sKey );
char * GetBufferString ( char * szDest, int iMax, const char * & szSource );
};
bool TryToExec ( char * pBuffer, const char * szFilename, CSphVector<char> & dResult, char * sError, int iErrorLen );
/////////////////////////////////////////////////////////////////////////////
enum
{
// where was TOKENIZER_SBCS=1 once
TOKENIZER_UTF8 = 2,
TOKENIZER_NGRAM = 3,
TOKENIZER_ZHCN_UTF8 = 4
};
/// load config file
const char * sphLoadConfig ( const char * sOptConfig, bool bQuiet, CSphConfigParser & cp );
/// configure tokenizer from index definition section
void sphConfTokenizer ( const CSphConfigSection & hIndex, CSphTokenizerSettings & tSettings );
/// configure dictionary from index definition section
void sphConfDictionary ( const CSphConfigSection & hIndex, CSphDictSettings & tSettings );
/// configure field filter from index definition section
bool sphConfFieldFilter ( const CSphConfigSection & hIndex, CSphFieldFilterSettings & tSettings, CSphString & sError );
/// configure index from index definition section
bool sphConfIndex ( const CSphConfigSection & hIndex, CSphIndexSettings & tSettings, CSphString & sError );
/// try to set dictionary, tokenizer and misc settings for an index (if not already set)
bool sphFixupIndexSettings ( CSphIndex * pIndex, const CSphConfigSection & hIndex, CSphString & sError, bool bTemplateDict=false );
bool sphInitCharsetAliasTable ( CSphString & sError );
enum ESphLogLevel
{
SPH_LOG_FATAL = 0,
SPH_LOG_WARNING = 1,
SPH_LOG_INFO = 2,
SPH_LOG_DEBUG = 3,
SPH_LOG_VERBOSE_DEBUG = 4,
SPH_LOG_VERY_VERBOSE_DEBUG = 5
};
typedef void ( *SphLogger_fn )( ESphLogLevel, const char *, va_list );
void sphWarning ( const char * sFmt, ... ) __attribute__((format(printf,1,2))); //NOLINT
void sphInfo ( const char * sFmt, ... ) __attribute__((format(printf,1,2))); //NOLINT
void sphLogFatal ( const char * sFmt, ... ) __attribute__((format(printf,1,2))); //NOLINT
void sphLogDebug ( const char * sFmt, ... ) __attribute__((format(printf,1,2))); //NOLINT
void sphLogDebugv ( const char * sFmt, ... ) __attribute__((format(printf,1,2))); //NOLINT
void sphLogDebugvv ( const char * sFmt, ... ) __attribute__((format(printf,1,2))); //NOLINT
void sphSetLogger ( SphLogger_fn fnLog );
//////////////////////////////////////////////////////////////////////////
/// how do we properly exit from the crash handler?
#if !USE_WINDOWS
#ifndef NDEBUG
// UNIX debug build, die and dump core
#define CRASH_EXIT { signal ( sig, SIG_DFL ); kill ( getpid(), sig ); }
#else
// UNIX release build, just die
#define CRASH_EXIT { exit ( 2 ); }
#endif
#else
#ifndef NDEBUG
// Windows debug build, show prompt to attach debugger
#define CRASH_EXIT return EXCEPTION_CONTINUE_SEARCH
#else
// Windows release build, just die
#define CRASH_EXIT return EXCEPTION_EXECUTE_HANDLER
#endif
#endif
/// simple write wrapper
/// simplifies partial write checks, and also supresses "fortified" glibc warnings
bool sphWrite ( int iFD, const void * pBuf, size_t iSize );
/// async safe, BUT NOT THREAD SAFE, fprintf
void sphSafeInfo ( int iFD, const char * sFmt, ... );
#if !USE_WINDOWS
/// UNIX backtrace gets printed out to a stream
void sphBacktrace ( int iFD, bool bSafe=false );
#else
/// Windows minidump gets saved to a file
void sphBacktrace ( EXCEPTION_POINTERS * pExc, const char * sFile );
#endif
void sphBacktraceSetBinaryName ( const char * sName );
/// plain backtrace - returns static buffer with the text of the call stack
const char * DoBacktrace ( int iDepth=0, int iSkip=0 );
void sphCheckDuplicatePaths ( const CSphConfig & hConf );
/// set globals from the common config section
void sphConfigureCommon ( const CSphConfig & hConf );
/// my own is chinese
bool sphIsChineseCode ( int iCode );
/// detect chinese chars in a buffer
bool sphDetectChinese ( const BYTE * szBuffer, int iLength );
/// returns ranker name as string
const char * sphGetRankerName ( ESphRankMode eRanker );
class CSphDynamicLibrary : public ISphNoncopyable
{
bool m_bReady; // whether the lib is valid or not
void * m_pLibrary; // internal handle
public:
CSphDynamicLibrary()
: m_bReady ( false )
, m_pLibrary ( NULL )
, m_sError ( "" )
{}
virtual ~CSphDynamicLibrary()
{}
bool Init ( const char* sPath, bool bGlobal=true );
bool LoadSymbol ( const char* sName, void** ppFunc );
bool LoadSymbols ( const char** sNames, void*** pppFuncs, int iNum );
public:
CSphString m_sError;
private:
void FillError ( const char* sMessage=NULL );
};
#endif // _sphinxutils_
//
// $Id$
//