1 /***************************************************************************
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
5 ***************************************************************************/
18 static unsigned int hashBinary(char *strVal
, int length
);
19 static void trimEnd(char *name
)
23 if(*name
== ' ') { *name
='\0'; break;}
27 static void trimRight(char *name
)
29 int len
= strlen(name
);
30 while(name
[len
-1] == ' ' && len
!= 0 )
36 static void str_tolower(char *s
)
44 static void str_toupper(char *s
)
52 static bool isIdentifier(char *name
)
55 if (!isalpha(*p
)) return false;
57 if (*p
== '_') { p
++; continue; }
58 if (!isalnum(*p
)) return false;
63 inline static void changeWildcardChar(char *src
)
65 char *c
= (char *)src
;
67 if (*c
== '_') *c
= '?';
68 else if(*c
== '%') *c
= '*';
73 inline static void itoa(int n
, char s
[])
76 if ((sign
= n
) < 0) n
= -n
;
79 s
[i
++] = n
% 10 + '0';
80 } while ((n
/= 10) > 0);
85 for (k
= 0, j
= i
-1; k
<j
; k
++, j
--) {
108 ListIterator(ListNode
*head
) { iter
= head
; start
= head
; }
112 if (iter
== NULL
) return false; else return true;
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
;
125 return node
->element
;
127 void* nextElementInQueue()
129 ListNode
*node
= iter
;
132 return node
->element
;
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
;
152 char name
[IDENTIFIER_LENGTH
];
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
;
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
;
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)
183 printError(ErrNotExists
, "There are no elements in the list. Empty list");
186 ListNode
*iter
= head
, *prev
= head
;
189 if (elem
== iter
->element
)
197 prev
->next
= iter
->next
;
206 printError(ErrNotFound
, "There are no elements in the list");
210 //index start with one, such that 1->first element in list
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
;
228 if (elem
== iter
->element
)
237 ListIterator
getIterator()
239 ListIterator
iter(head
);
244 if (NULL
== head
) return;
245 ListNode
*iter
= head
, *prevIter
= head
;
246 while (iter
->next
!= NULL
)
258 void init() { head
= NULL
; totalElements
=0;}
260 DbRetVal
addAtMiddle(void *elem
, void *prevIter
)
262 ListNode
*newNode
= new ListNode();
263 newNode
->element
= elem
;
265 newNode
->next
=((ListNode
*)prevIter
)->next
;
266 ((ListNode
*)prevIter
)->next
= newNode
;
269 DbRetVal
addAtBegin(void *elem
)
271 ListNode
*newNode
= new ListNode();
272 newNode
->element
= elem
;
274 newNode
->next
= head
;
280 return totalElements
;
284 #define MAX_UNIQUE_ID 10
289 GlobalUniqueID() { ptr
= NULL
; }
292 DbRetVal
close() { os::shm_detach(ptr
); ptr
= NULL
; return OK
; }
294 int getID(UniqueIDType type
);
302 UniqueID() { startID
= 1; mutex
.init(); }
307 startID
= id
;mutex
.init();
313 //TODO::change mutex to atomic increment instruction
314 if (mutex
.getLock(-1, false) != 0) return 0;
316 mutex
.releaseLock(-1, false);