- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathutils_proto.h
329 lines (272 loc) · 9.05 KB
/
utils_proto.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Claudio Valderrama on 25-Dec-2003
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2003 Claudio Valderrama
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* Nickolay Samofatov <nickolay@broadviewsoftware.com>
*/
// =====================================
// Utility functions
#ifndef INCLUDE_UTILS_PROTO_H
#defineINCLUDE_UTILS_PROTO_H
#include<string.h>
#include<type_traits>
#include"../common/classes/fb_string.h"
#include"../common/classes/array.h"
#include"iberror.h"
#include"firebird/Interface.h"
#include"memory_routines.h"
#ifdef SFIO
#include<stdio.h>
#endif
namespacefb_utils
{
char* copy_terminate(char* dest, constchar* src, size_t bufsize);
char* exact_name(char* const name);
inlinevoidexact_name(Firebird::string& str)
{
str.rtrim();
}
char* exact_name_limit(char* const name, size_t bufsize);
boolimplicit_domain(constchar* domain_name);
boolimplicit_integrity(constchar* integ_name);
boolimplicit_pk(constchar* pk_name);
intname_length(const TEXT* const name);
intname_length_limit(const TEXT* const name, size_t bufsize);
boolreadenv(constchar* env_name, Firebird::string& env_value);
boolreadenv(constchar* env_name, Firebird::PathName& env_value);
boolsetenv(constchar* name, constchar* value, bool overwrite);
intsnprintf(char* buffer, size_t count, constchar* format...);
char* cleanup_passwd(char* arg);
inlinechar* get_passwd(char* arg)
{
returncleanup_passwd(arg);
}
typedefchar* arg_string;
// Warning: Only wrappers:
// ********************
// s t r i c m p
// ********************
// Abstraction of incompatible routine names
// for case insensitive comparison.
inlineintstricmp(constchar* a, constchar* b)
{
#if defined(HAVE_STRCASECMP)
return ::strcasecmp(a, b);
#elif defined(HAVE_STRICMP)
return ::stricmp(a, b);
#else
#error dont know how to compare strings case insensitive on this system
#endif
}
// ********************
// s t r n i c m p
// ********************
// Abstraction of incompatible routine names
// for counted length and case insensitive comparison.
inlineintstrnicmp(constchar* a, constchar* b, size_t count)
{
#if defined(HAVE_STRNCASECMP)
return ::strncasecmp(a, b, count);
#elif defined(HAVE_STRNICMP)
return ::strnicmp(a, b, count);
#else
#error dont know how to compare counted length strings case insensitive on this system
#endif
}
#ifdef WIN_NT
boolprefix_kernel_object_name(char* name, size_t bufsize);
boolisGlobalKernelPrefix();
boolprivate_kernel_object_name(char* name, size_t bufsize);
boolprivateNameSpaceReady();
#endif
// Compare the absolute value of two SINT64 numbers.
// Return 0 if they are equal, <0 if n1 < n2 and >0 if n1 > n2.
inlineintabs64Compare(SINT64 n1, SINT64 n2)
{
#ifndef FB_INT64_COMPARE_FAILED
#defineFB_INT64_COMPARE_FAILED1
#endif
#if FB_INT64_COMPARE_FAILED
// avoid compiler bug when comparing minimum INT64
const SINT64 MININT64 = 0x8000000000000000;
if (n1 == MININT64)
return n2 == MININT64 ? 0 : 2;
if (n2 == MININT64)
return -2;
#endif
n1 = n1 > 0 ? -n1 : n1;
n2 = n2 > 0 ? -n2 : n2;
return n1 == n2 ? 0 : n1 < n2 ? 1 : -1;
}
Firebird::PathName get_process_name();
SLONG genUniqueId();
voidgetCwd(Firebird::PathName& pn);
voidinlineinitStatusTo(ISC_STATUS* status, ISC_STATUS to)
{
status[0] = isc_arg_gds;
status[1] = to;
status[2] = isc_arg_end;
}
voidinlineinit_status(ISC_STATUS* status)
{
initStatusTo(status, FB_SUCCESS);
}
voidinlinestatusBadAlloc(ISC_STATUS* status)
{
initStatusTo(status, isc_virmemexh);
}
voidinlinestatusUnknown(ISC_STATUS* status)
{
initStatusTo(status, isc_exception_sigill); // Any better ideas? New error code?
}
voidinlineinit_status(Firebird::CheckStatusWrapper* status)
{
status->init();
}
unsignedintcopyStatus(ISC_STATUS* const to, constunsignedint space,
const ISC_STATUS* const from, constunsignedint count) throw();
voidcopyStatus(Firebird::CheckStatusWrapper* to, const Firebird::IStatus* from) throw();
unsignedintmergeStatus(ISC_STATUS* const to, unsignedint space, const Firebird::IStatus* from) throw();
voidsetIStatus(Firebird::CheckStatusWrapper* to, const ISC_STATUS* from) throw();
unsignedintstatusLength(const ISC_STATUS* const status) throw();
unsignedintsubStatus(const ISC_STATUS* in, unsignedint cin,
const ISC_STATUS* sub, unsignedint csub) throw();
boolcmpStatus(unsignedint len, const ISC_STATUS* a, const ISC_STATUS* b) throw();
const ISC_STATUS* nextCode(const ISC_STATUS* v) throw();
inlineunsignednextArg(const ISC_STATUS v) throw()
{
return v == isc_arg_cstring ? 3 : 2;
}
inlineboolisStr(const ISC_STATUS v) throw()
{
switch (v)
{
case isc_arg_cstring:
case isc_arg_string:
case isc_arg_interpreted:
case isc_arg_sql_state:
returntrue;
}
returnfalse;
}
// Check does vector contain particular code or not
boolcontainsErrorCode(const ISC_STATUS* v, ISC_STATUS code);
enum FetchPassResult {
FETCH_PASS_OK,
FETCH_PASS_FILE_OPEN_ERROR,
FETCH_PASS_FILE_READ_ERROR,
FETCH_PASS_FILE_EMPTY
};
FetchPassResult fetchPassword(const Firebird::PathName& name, constchar*& password);
// Returns current value of performance counter
SINT64 query_performance_counter();
// Returns frequency of performance counter in Hz
SINT64 query_performance_frequency();
voidget_process_times(SINT64 &userTime, SINT64 &sysTime);
voidexactNumericToStr(SINT64 value, int scale, Firebird::string& target, bool append = false);
// Returns true if called from firebird build process (appr. environment is set)
boolbootBuild();
// Add appropriate file prefix.
Firebird::PathName getPrefix(unsigned prefType, constchar* name);
// moves DB path information (from limbo transaction) to another buffer
voidgetDbPathInfo(unsignedint& itemsLength, constunsignedchar*& items,
unsignedint& bufferLength, unsignedchar*& buffer,
Firebird::Array<unsignedchar>& newItemsBuffer, const Firebird::PathName& dbpath);
// returns true if passed info items work with running svc thread
boolisRunningCheck(const UCHAR* items, unsignedint length);
// converts bytes to BASE64 representation
voidbase64(Firebird::string& b64, const Firebird::UCharBuffer& bin);
// generate random string in BASE64 representation
voidrandom64(Firebird::string& randomValue, FB_SIZE_T length);
voidlogAndDie(constchar* text);
// On incorrect sqlType returns dsc_unknown
UCHAR sqlTypeToDscType(SSHORT sqlType);
// Returns next offset value
unsignedsqlTypeToDsc(unsigned prevOffset, unsigned sqlType, unsigned sqlLength,
unsigned* dtype, unsigned* len, unsigned* offset, unsigned* nullOffset);
boolinlineisNetworkError(ISC_STATUS code)
{
return code == isc_network_error ||
code == isc_net_write_err ||
code == isc_net_read_err ||
code == isc_lost_db_connection;
}
// Uppercase/strip string according to login rules
constchar* dpbItemUpper(constchar* s, FB_SIZE_T l, Firebird::string& buf);
// Uppercase/strip string according to login rules
template <typename STR>
voiddpbItemUpper(STR& name)
{
Firebird::string buf;
constchar* up = dpbItemUpper(name.c_str(), name.length(), buf);
if (up)
name = up;
}
// Frequently used actions with clumplets
boolisBpbSegmented(unsigned parLength, constunsignedchar* par);
// Workaround, to be removed with C++ 23
template <typename... T>
constexprbool fb_always_false_v = false;
// Put integer value into info buffer
template<typename T>
inlineunsignedchar* putInfoItemInt(constunsignedchar item, T value,
unsignedchar* ptr, constunsignedchar* end)
{
static_assert(std::is_integral_v<T>, "Integral type expected");
constexprauto len = sizeof(T);
if (ptr + len + 1 + 2 > end)
{
if (ptr < end)
{
*ptr++ = isc_info_truncated;
if (ptr < end)
*ptr++ = isc_info_end;
}
returnnullptr;
}
*ptr++ = item;
*ptr++ = len;
*ptr++ = 0;
ifconstexpr (len == sizeof(SINT64))
put_vax_int64(ptr, value);
elseifconstexpr (len == sizeof(SLONG))
put_vax_long(ptr, value);
elseifconstexpr (len == sizeof(SSHORT))
put_vax_short(ptr, value);
elseifconstexpr (len == sizeof(char))
*ptr = value;
else
static_assert(fb_always_false_v<T>, "unknown data type");
ptr += len;
return ptr;
}
// RAII to call fb_shutdown() in utilities
classFbShutdown
{
public:
FbShutdown(int r)
: reason(r)
{ }
~FbShutdown();
private:
int reason;
};
} // namespace fb_utils
#endif// INCLUDE_UTILS_PROTO_H