SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VariadicSQLParser.cpp
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
16 #include "slikenet/VariadicSQLParser.h"
17 #include "slikenet/BitStream.h"
18 #include <stdarg.h>
19
20 using namespace VariadicSQLParser;
21
22 struct TypeMapping
23 {
24 char inputType;
25 const char *type;
26 };
27 const int NUM_TYPE_MAPPINGS=7;
28 TypeMapping typeMappings[NUM_TYPE_MAPPINGS] =
29 {
30 {'i', "int"},
31 {'d', "int"},
32 {'s', "text"},
33 {'b', "bool"},
34 {'f', "numeric"},
35 {'g', "double precision"},
36 {'a', "bytea"},
37 };
38 unsigned int GetTypeMappingIndex(char c)
39 {
40 unsigned int i;
41 for (i=0; i < (unsigned int) NUM_TYPE_MAPPINGS; i++ )
42 if (typeMappings[i].inputType==c)
43 return i;
44 return (unsigned int)-1;
45 }
46 const char* VariadicSQLParser::GetTypeMappingAtIndex(int i)
47 {
48 return typeMappings[i].type;
49 }
50 void VariadicSQLParser::GetTypeMappingIndices( const char *format, DataStructures::List<IndexAndType> &indices )
51 {
52 bool previousCharWasPercentSign;
53 unsigned int i;
54 unsigned int typeMappingIndex;
55 indices.Clear(false, _FILE_AND_LINE_);
56 unsigned int len = (unsigned int) strlen(format);
57 previousCharWasPercentSign=false;
58 for (i=0; i < len; i++)
59 {
60 if (previousCharWasPercentSign==true )
61 {
62 typeMappingIndex = GetTypeMappingIndex(format[i]);
63 if (typeMappingIndex!=(unsigned int) -1)
64 {
65 IndexAndType iat;
66 iat.strIndex=i-1;
67 iat.typeMappingIndex=typeMappingIndex;
68 indices.Insert(iat, _FILE_AND_LINE_ );
69 }
70 }
71
72 previousCharWasPercentSign=format[i]=='%';
73 }
74 }
75 void VariadicSQLParser::ExtractArguments( va_list argptr, const DataStructures::List<IndexAndType> &indices, char ***argumentBinary, int **argumentLengths )
76 {
77 if (indices.Size()==0)
78 return;
79
80 unsigned int i;
81 *argumentBinary= SLNet::OP_NEW_ARRAY<char *>(indices.Size(), _FILE_AND_LINE_);
82 *argumentLengths= SLNet::OP_NEW_ARRAY<int>(indices.Size(), _FILE_AND_LINE_);
83
84 char **paramData=*argumentBinary;
85 int *paramLength=*argumentLengths;
86
87 int variadicArgIndex;
88 for (variadicArgIndex=0, i=0; i < indices.Size(); i++, variadicArgIndex++)
89 {
90 switch (typeMappings[indices[i].typeMappingIndex].inputType)
91 {
92 case 'i':
93 case 'd':
94 {
95 int val = va_arg( argptr, int );
96 paramLength[i]=sizeof(val);
97 paramData[i]=(char*) rakMalloc_Ex(paramLength[i], _FILE_AND_LINE_);
98 memcpy(paramData[i], &val, paramLength[i]);
99 if (SLNet::BitStream::IsNetworkOrder()==false) SLNet::BitStream::ReverseBytesInPlace((unsigned char*) paramData[i], paramLength[i]);
100 }
101 break;
102 case 's':
103 {
104 char* val = va_arg( argptr, char* );
105 paramLength[i]=(int) strlen(val);
106 paramData[i]=(char*) rakMalloc_Ex(paramLength[i]+1, _FILE_AND_LINE_);
107 memcpy(paramData[i], val, paramLength[i]+1);
108 }
109 break;
110 case 'b':
111 {
112 bool val = (va_arg( argptr, int )!=0);
113 paramLength[i]=sizeof(val);
114 paramData[i]=(char*) rakMalloc_Ex(paramLength[i], _FILE_AND_LINE_);
115 memcpy(paramData[i], &val, paramLength[i]);
116 if (SLNet::BitStream::IsNetworkOrder()==false) SLNet::BitStream::ReverseBytesInPlace((unsigned char*) paramData[i], paramLength[i]);
117 }
118 break;
119 /*
120 case 'f':
121 {
122 // On MSVC at least, this only works with double as the 2nd param
123 float val = (float) va_arg( argptr, double );
124 //float val = va_arg( argptr, float );
125 paramLength[i]=sizeof(val);
126 paramData[i]=(char*) rakMalloc_Ex(paramLength[i], _FILE_AND_LINE_);
127 memcpy(paramData[i], &val, paramLength[i]);
128 if (SLNet::BitStream::IsNetworkOrder()==false) SLNet::BitStream::ReverseBytesInPlace((unsigned char*) paramData[i], paramLength[i]);
129 }
130 break;
131 */
132 // On MSVC at least, this only works with double as the 2nd param
133 case 'f':
134 case 'g':
135 {
136 double val = va_arg( argptr, double );
137 paramLength[i]=sizeof(val);
138 paramData[i]=(char*) rakMalloc_Ex(paramLength[i], _FILE_AND_LINE_);
139 memcpy(paramData[i], &val, paramLength[i]);
140 if (SLNet::BitStream::IsNetworkOrder()==false) SLNet::BitStream::ReverseBytesInPlace((unsigned char*) paramData[i], paramLength[i]);
141 }
142 break;
143 case 'a':
144 {
145 char* val = va_arg( argptr, char* );
146 paramLength[i]=va_arg( argptr, unsigned int );
147 paramData[i]=(char*) rakMalloc_Ex(paramLength[i], _FILE_AND_LINE_);
148 memcpy(paramData[i], val, paramLength[i]);
149 }
150 break;
151 }
152 }
153
154 }
155 void VariadicSQLParser::FreeArguments(const DataStructures::List<IndexAndType> &indices, char **argumentBinary, int *argumentLengths)
156 {
157 if (indices.Size()==0)
158 return;
159
160 unsigned int i;
161 for (i=0; i < indices.Size(); i++)
162 rakFree_Ex(argumentBinary[i],_FILE_AND_LINE_);
163 SLNet::OP_DELETE_ARRAY(argumentBinary,_FILE_AND_LINE_);
164 SLNet::OP_DELETE_ARRAY(argumentLengths,_FILE_AND_LINE_);
165 }