changing file creation mode from 777 to 644
[csql.git] / include / Util.h
blob72a9d4c99cffd47eea74932eb720b113d8c46eea
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
15 class Util
17 public:
18 static unsigned int hashBinary(char *strVal, int length);
19 static void trimEnd(char *name)
21 while(*name!='\0')
23 if(*name == ' ') { *name='\0'; break;}
24 name++;
27 static void trimRight(char *name)
29 int len = strlen(name);
30 while(name[len-1] == ' ' && len != 0 )
32 len--;
34 name[len]='\0';
36 static void str_tolower(char *s)
38 while(*s)
40 *s=tolower(*s);
41 s++;
44 static void str_toupper(char *s)
46 while(*s)
48 *s=toupper(*s);
49 s++;
52 static bool isIdentifier(char *name)
54 char *p = name;
55 if (!isalpha(*p)) return false;
56 while (*p != '\0') {
57 if (*p == '_') { p++; continue; }
58 if (!isalnum(*p)) return false;
59 p++;
61 return true;
63 inline static void changeWildcardChar(char *src)
65 char *c = (char *)src;
66 while (*c != '\0') {
67 if (*c == '_') *c = '?';
68 else if(*c == '%') *c = '*';
69 c++;
71 return;
73 inline static void itoa(int n, char s[])
75 int i, sign, j, k ;
76 if ((sign = n) < 0) n = -n;
77 i = 0;
78 do {
79 s[i++] = n % 10 + '0';
80 } while ((n /= 10) > 0);
81 if (sign < 0)
82 s[i++] = '-';
83 s[i] = '\0';
84 char c;
85 for (k = 0, j = i-1; k<j; k++, j--) {
86 c = s[k];
87 s[k] = s[j];
88 s[j] = c;
93 class ListNode
95 public:
96 void *element;
97 ListNode *next;
101 class ListIterator
103 ListNode *iter;
104 ListNode *start;
105 public:
106 ListIterator(){}
108 ListIterator(ListNode *head) { iter = head; start = head; }
110 bool hasElement()
112 if (iter == NULL) return false; else return true;
115 void reset()
117 iter = start;
119 //isRemove ->the node needs to deleted after returning
120 void* nextElement(bool isRemove = false)
122 if (iter == NULL) return NULL;
123 ListNode *node = iter;
124 iter = iter ->next;
125 return node->element;
127 void* nextElementInQueue()
129 ListNode *node = iter;
130 if(iter->next) {
131 iter = iter ->next;
132 return node->element;
133 } else return NULL;
135 void *getCurrentListNode(){ return iter; }
136 //index start with one, such that 1->first element in list
137 void* getElement(int index)
139 ListNode *localIter = start;
140 if (localIter == NULL) return NULL;
141 for (int i=0; i <index; i++) {
142 localIter = localIter->next;
143 if (localIter == NULL) break;
145 return localIter->element;
149 class Identifier
151 public:
152 char name[IDENTIFIER_LENGTH];
155 class List
157 ListNode *head;
158 int totalElements;
159 public:
160 List() { head = NULL; totalElements = 0;}
161 List(ListNode *hd) { head = hd; } //Use only for free in metadata
162 DbRetVal append(void *elem)
164 ListNode *newNode = new ListNode();
165 newNode->element = elem;
166 newNode->next = NULL;
167 totalElements++;
168 //If this is the first node, set it as head
169 if (NULL == head) { head = newNode; return OK; }
171 ListNode *iter = head;
172 while (NULL != iter->next) iter = iter->next;
173 iter->next = newNode;
174 return OK;
176 //Warning:Try to avoid using this method while using the iterator.The behavior
177 //is undefined. Instead set flag isRemove to yes and call nextElement of iterator.
178 DbRetVal remove(void *elem, bool err=true)
180 if (NULL == head)
182 if (err)
183 printError(ErrNotExists, "There are no elements in the list. Empty list");
184 return ErrNotExists;
186 ListNode *iter = head, *prev = head;
187 while (iter != NULL)
189 if (elem == iter->element)
191 if (iter == head) {
192 head = iter->next;
193 delete iter;
194 totalElements--;
195 return OK;
197 prev->next = iter->next;
198 delete iter;
199 totalElements--;
200 return OK;
202 prev = iter;
203 iter = iter->next;
205 if (err)
206 printError(ErrNotFound, "There are no elements in the list");
207 return ErrNotFound;
210 //index start with one, such that 1->first element in list
211 void* get(int index)
213 ListNode *localIter = head;
214 if (localIter == NULL) return NULL;
215 for (int i=0; i <index -1; i++) {
216 localIter = localIter->next;
217 if (localIter == NULL) break;
219 return localIter->element;
223 bool exists(void *elem)
225 ListNode *iter = head;
226 while (iter != NULL)
228 if (elem == iter->element)
230 return true;
232 iter = iter->next;
234 return false;
237 ListIterator getIterator()
239 ListIterator iter(head);
240 return iter;
242 void reset()
244 if (NULL == head) return;
245 ListNode *iter = head, *prevIter = head;
246 while (iter->next != NULL)
248 prevIter = iter;
249 iter = iter->next;
250 delete prevIter;
253 delete iter;
254 head = NULL;
255 totalElements = 0;
256 return;
258 void init() { head = NULL; totalElements=0;}
260 DbRetVal addAtMiddle(void *elem, void *prevIter)
262 ListNode *newNode = new ListNode();
263 newNode->element = elem;
264 totalElements++;
265 newNode->next =((ListNode *)prevIter)->next;
266 ((ListNode *)prevIter)->next = newNode;
267 return OK;
269 DbRetVal addAtBegin(void *elem)
271 ListNode *newNode = new ListNode();
272 newNode->element = elem;
273 totalElements++;
274 newNode->next = head;
275 head = newNode;
276 return OK;
278 int size()
280 return totalElements;
284 #define MAX_UNIQUE_ID 10
285 class GlobalUniqueID
287 void *ptr;
288 public:
289 GlobalUniqueID() { ptr = NULL; }
290 DbRetVal create();
291 DbRetVal open();
292 DbRetVal close() { os::shm_detach(ptr); ptr = NULL; return OK; }
293 DbRetVal destroy();
294 int getID(UniqueIDType type);
297 class UniqueID
299 int startID;
300 Mutex mutex;
301 public:
302 UniqueID() { startID = 1; mutex.init(); }
305 void setID(int id)
307 startID = id;mutex.init();
311 int getID()
313 //TODO::change mutex to atomic increment instruction
314 if (mutex.getLock(-1, false) != 0) return 0;
315 startID++;
316 mutex.releaseLock(-1, false);
317 return startID;
321 #endif