1 /***************************************************************************
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
5 ***************************************************************************/
24 static unsigned int hashBinary(char *strVal
, int length
);
25 static unsigned int hashString(char *strVal
);
26 static void trimEnd(char *name
)
30 if(*name
== ' ') { *name
='\0'; break;}
34 static void trimRight(char *name
)
36 int len
= strlen(name
);
37 while(name
[len
-1] == ' ' && len
!= 0 )
43 static void str_tolower(char *s
)
51 static void str_toupper(char *s
)
59 static bool isIdentifier(char *name
)
62 if (!isalpha(*p
)) return false;
64 if (*p
== '_') { p
++; continue; }
65 if (!isalnum(*p
)) return false;
70 inline static void changeWildcardChar(char *src
)
72 char *c
= (char *)src
;
74 if (*c
== '_') *c
= '?';
75 else if(*c
== '%') *c
= '*';
80 inline static void itoa(int n
, char s
[])
83 if ((sign
= n
) < 0) n
= -n
;
86 s
[i
++] = n
% 10 + '0';
87 } while ((n
/= 10) > 0);
92 for (k
= 0, j
= i
-1; k
<j
; k
++, j
--) {
115 ListIterator(ListNode
*head
) { iter
= head
; start
= head
; }
119 if (iter
== NULL
) return false; else return true;
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
;
132 return node
->element
;
134 void* nextElementInQueue()
136 ListNode
*node
= iter
;
139 return node
->element
;
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
;
159 char name
[IDENTIFIER_LENGTH
];
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
;
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
;
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)
190 printError(ErrNotExists
, "There are no elements in the list. Empty list");
193 ListNode
*iter
= head
, *prev
= head
;
196 if (elem
== iter
->element
)
204 prev
->next
= iter
->next
;
213 printError(ErrNotFound
, "There are no elements in the list");
217 //index start with one, such that 1->first element in list
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
;
235 if (elem
== iter
->element
)
244 ListIterator
getIterator()
246 ListIterator
iter(head
);
251 if (NULL
== head
) return;
252 ListNode
*iter
= head
, *prevIter
= head
;
253 while (iter
->next
!= NULL
)
265 void init() { head
= NULL
; totalElements
=0;}
267 DbRetVal
addAtMiddle(void *elem
, void *prevIter
)
269 ListNode
*newNode
= new ListNode();
270 newNode
->element
= elem
;
272 newNode
->next
=((ListNode
*)prevIter
)->next
;
273 ((ListNode
*)prevIter
)->next
= newNode
;
276 DbRetVal
addAtBegin(void *elem
)
278 ListNode
*newNode
= new ListNode();
279 newNode
->element
= elem
;
281 newNode
->next
= head
;
287 return totalElements
;
291 #define MAX_UNIQUE_ID 10
296 GlobalUniqueID() { ptr
= NULL
; }
299 DbRetVal
close() { os::shm_detach(ptr
); ptr
= NULL
; return OK
; }
301 int getID(UniqueIDType type
);
309 UniqueID() { startID
= 1; mutex
.init(); }
314 startID
= id
;mutex
.init();
320 //TODO::change mutex to atomic increment instruction
321 if (mutex
.getLock(-1, false) != 0) return 0;
323 mutex
.releaseLock(-1, false);