*** empty log message ***
[csql.git] / include / Util.h
blob11e8804d93e45c356ff06f9b7f15fe985f3677e5
1 /***************************************************************************
2 * *
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
4 * *
5 ***************************************************************************/
7 #ifndef UTIL_H
8 #define UTIL_H
9 #include<ErrorType.h>
10 #include<Debug.h>
11 enum UniqueIDType {
12 STMT_ID=0,
13 TXN_ID
16 enum HashFuncType {
17 HASH_INT=0,
18 HASH_BINARY,
21 class DllExport Util
23 public:
24 static unsigned int hashBinary(char *strVal, int length);
25 static unsigned int hashString(char *strVal);
26 static void trimEnd(char *name)
28 while(*name!='\0')
30 if(*name == ' ') { *name='\0'; break;}
31 name++;
34 static void trimRight(char *name)
36 int len = strlen(name);
37 while(name[len-1] == ' ' && len != 0 )
39 len--;
41 name[len]='\0';
43 static void str_tolower(char *s)
45 while(*s)
47 *s=tolower(*s);
48 s++;
51 static void str_toupper(char *s)
53 while(*s)
55 *s=toupper(*s);
56 s++;
59 static bool isIdentifier(char *name)
61 char *p = name;
62 if (!isalpha(*p)) return false;
63 while (*p != '\0') {
64 if (*p == '_') { p++; continue; }
65 if (!isalnum(*p)) return false;
66 p++;
68 return true;
70 inline static void changeWildcardChar(char *src)
72 char *c = (char *)src;
73 while (*c != '\0') {
74 if (*c == '_') *c = '?';
75 else if(*c == '%') *c = '*';
76 c++;
78 return;
80 inline static void itoa(int n, char s[])
82 int i, sign, j, k ;
83 if ((sign = n) < 0) n = -n;
84 i = 0;
85 do {
86 s[i++] = n % 10 + '0';
87 } while ((n /= 10) > 0);
88 if (sign < 0)
89 s[i++] = '-';
90 s[i] = '\0';
91 char c;
92 for (k = 0, j = i-1; k<j; k++, j--) {
93 c = s[k];
94 s[k] = s[j];
95 s[j] = c;
100 class ListNode
102 public:
103 void *element;
104 ListNode *next;
108 class DllExport ListIterator
110 ListNode *iter;
111 ListNode *start;
112 public:
113 ListIterator(){}
115 ListIterator(ListNode *head) { iter = head; start = head; }
117 bool hasElement()
119 if (iter == NULL) return false; else return true;
122 void reset()
124 iter = start;
126 //isRemove ->the node needs to deleted after returning
127 void* nextElement(bool isRemove = false)
129 if (iter == NULL) return NULL;
130 ListNode *node = iter;
131 iter = iter ->next;
132 return node->element;
134 void* nextElementInQueue()
136 ListNode *node = iter;
137 if(iter->next) {
138 iter = iter ->next;
139 return node->element;
140 } else return NULL;
142 void *getCurrentListNode(){ return iter; }
143 //index start with one, such that 1->first element in list
144 void* getElement(int index)
146 ListNode *localIter = start;
147 if (localIter == NULL) return NULL;
148 for (int i=0; i <index; i++) {
149 localIter = localIter->next;
150 if (localIter == NULL) break;
152 return localIter->element;
156 class Identifier
158 public:
159 char name[IDENTIFIER_LENGTH];
162 class DllExport List
164 ListNode *head;
165 int totalElements;
166 public:
167 List() { head = NULL; totalElements = 0;}
168 List(ListNode *hd) { head = hd; } //Use only for free in metadata
169 DbRetVal append(void *elem)
171 ListNode *newNode = new ListNode();
172 newNode->element = elem;
173 newNode->next = NULL;
174 totalElements++;
175 //If this is the first node, set it as head
176 if (NULL == head) { head = newNode; return OK; }
178 ListNode *iter = head;
179 while (NULL != iter->next) iter = iter->next;
180 iter->next = newNode;
181 return OK;
183 //Warning:Try to avoid using this method while using the iterator.The behavior
184 //is undefined. Instead set flag isRemove to yes and call nextElement of iterator.
185 DbRetVal remove(void *elem, bool err=true)
187 if (NULL == head)
189 if (err)
190 printError(ErrNotExists, "There are no elements in the list. Empty list");
191 return ErrNotExists;
193 ListNode *iter = head, *prev = head;
194 while (iter != NULL)
196 if (elem == iter->element)
198 if (iter == head) {
199 head = iter->next;
200 delete iter;
201 totalElements--;
202 return OK;
204 prev->next = iter->next;
205 delete iter;
206 totalElements--;
207 return OK;
209 prev = iter;
210 iter = iter->next;
212 if (err)
213 printError(ErrNotFound, "There are no elements in the list");
214 return ErrNotFound;
217 //index start with one, such that 1->first element in list
218 void* get(int index)
220 ListNode *localIter = head;
221 if (localIter == NULL) return NULL;
222 for (int i=0; i <index -1; i++) {
223 localIter = localIter->next;
224 if (localIter == NULL) break;
226 return localIter->element;
230 bool exists(void *elem)
232 ListNode *iter = head;
233 while (iter != NULL)
235 if (elem == iter->element)
237 return true;
239 iter = iter->next;
241 return false;
244 ListIterator getIterator()
246 ListIterator iter(head);
247 return iter;
249 void reset()
251 if (NULL == head) return;
252 ListNode *iter = head, *prevIter = head;
253 while (iter->next != NULL)
255 prevIter = iter;
256 iter = iter->next;
257 delete prevIter;
260 delete iter;
261 head = NULL;
262 totalElements = 0;
263 return;
265 void init() { head = NULL; totalElements=0;}
267 DbRetVal addAtMiddle(void *elem, void *prevIter)
269 ListNode *newNode = new ListNode();
270 newNode->element = elem;
271 totalElements++;
272 newNode->next =((ListNode *)prevIter)->next;
273 ((ListNode *)prevIter)->next = newNode;
274 return OK;
276 DbRetVal addAtBegin(void *elem)
278 ListNode *newNode = new ListNode();
279 newNode->element = elem;
280 totalElements++;
281 newNode->next = head;
282 head = newNode;
283 return OK;
285 int size()
287 return totalElements;
291 #define MAX_UNIQUE_ID 10
292 class DllExport GlobalUniqueID
294 void *ptr;
295 public:
296 GlobalUniqueID() { ptr = NULL; }
297 DbRetVal create();
298 DbRetVal open();
299 DbRetVal close() { os::shm_detach(ptr); ptr = NULL; return OK; }
300 DbRetVal destroy();
301 int getID(UniqueIDType type);
304 class DllExport UniqueID
306 int startID;
307 Mutex mutex;
308 public:
309 UniqueID() { startID = 1; mutex.init(); }
312 void setID(int id)
314 startID = id;mutex.init();
318 int getID()
320 //TODO::change mutex to atomic increment instruction
321 if (mutex.getLock(-1, false) != 0) return 0;
322 startID++;
323 mutex.releaseLock(-1, false);
324 return startID;
328 #endif