- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathDynamicStrings.cpp
145 lines (123 loc) · 3.11 KB
/
DynamicStrings.cpp
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
/*
* PROGRAM: Firebird status vector support.
* MODULE: DynamicStrings.cpp
* DESCRIPTION: Dynamically store strings in status vector.
*
* 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 Alex Peshkov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2015 Alex Peshkov <peshkoff at mail dot ru>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
*/
#include"firebird.h"
#include"DynamicStrings.h"
#include"utils_proto.h"
#include"iberror.h"
#include"classes/alloc.h"
#include<string.h>
namespaceFirebird {
unsignedmakeDynamicStrings(unsigned length, ISC_STATUS* const dst, const ISC_STATUS* const src)
{
const ISC_STATUS* end = &src[length];
// allocate space for strings
size_t len = 0;
for (const ISC_STATUS* from = src; from < end; ++from)
{
const ISC_STATUS type = *from++;
if (from == end || type == isc_arg_end)
{
end = from - 1;
break;
}
switch (type)
{
case isc_arg_cstring:
if (from + 1 >= end)
{
end = from - 1;
break;
}
len += *from++;
len++;
break;
case isc_arg_string:
case isc_arg_interpreted:
case isc_arg_sql_state:
len += strlen(reinterpret_cast<constchar*>(*from));
len++;
break;
}
}
char* string = len ? FB_NEW_POOL(*getDefaultMemoryPool()) char[len] : NULL;
ISC_STATUS* to = dst;
// copy status vector saving strings in local buffer
for (const ISC_STATUS* from = src; from < end; ++from)
{
const ISC_STATUS type = *from++;
*to++ = type == isc_arg_cstring ? isc_arg_string : type;
switch (type)
{
case isc_arg_cstring:
fb_assert(string);
*to++ = (ISC_STATUS)(IPTR) string;
memcpy(string, reinterpret_cast<constchar*>(from[1]), from[0]);
string += *from++;
*string++ = '\0';
break;
case isc_arg_string:
case isc_arg_interpreted:
case isc_arg_sql_state:
fb_assert(string);
*to++ = (ISC_STATUS)(IPTR) string;
strcpy(string, reinterpret_cast<constchar*>(*from));
string += strlen(string);
string++;
break;
default:
*to++ = *from;
break;
}
}
*to++ = isc_arg_end;
return (to - dst) - 1;
}
char* findDynamicStrings(unsigned length, ISC_STATUS* ptr) throw()
{
while (length--)
{
const ISC_STATUS type = *ptr++;
if (type == isc_arg_end)
break;
switch (type)
{
case isc_arg_cstring:
fb_assert(false); // CVC: according to the new logic, this case cannot happen
ptr++;
case isc_arg_string:
case isc_arg_interpreted:
case isc_arg_sql_state:
returnreinterpret_cast<char*>(*ptr);
default:
ptr++;
break;
}
}
returnNULL;
}
} // namespace Firebird