zuluCrypt  5.7.1
About: zuluCrypt is a simple but feature rich solution for hard drives encryption. It can manage PLAIN dm-crypt, LUKS, TrueCrypt and VeraCrypt encrypted volumes.
  Fossies Dox: zuluCrypt-5.7.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

String.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2011-2015
4  * name : Francis Banyikwa
5  * email: mhogomchungu@gmail.com
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef STRINGTYPE
21 #define STRINGTYPE
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include <string.h>
28 #include <sys/types.h>
29 #include <stdlib.h>
30 /*
31  * Takes a pointer to a function to be called when memory allocation can not take place
32  * ie if the system has run out of memory and malloc() or realloc() has failed.
33  * This function is optional and "StringVoid" will be returned on memory exaustion if the function
34  * isnt set.
35  */
36 void StringExitOnMemoryExaustion( void (*)( void ) ) ;
37 
38 /*
39  * string_t type is a string handle,all string operation should happen through this handle.
40  */
41 typedef struct StringType * string_t ;
42 
43 /*
44  * create a custom type to represent a string_t pointing to NULL while hiding the pointer nature of string_t
45  * string_t is an opaque handle and NULL assignment "gives unnecessary info" about its nature.
46  */
47 #define StringVoid ( ( string_t ) 0 )
48 
49 typedef char * StringIterator ;
50 
51 /*
52  * Get stl style iterator on the managed string
53  */
55 
56 /*
57  * initialize a handle with a C string
58  */
59 string_t String( const char * cstring ) ;
60 
61 /*
62  * initialize a handle with multiple C strings concatenated together.
63  */
64 string_t String_1( const char * cstring,... ) __attribute__ ( ( sentinel ) ) ;
65 
66 /*
67  * initialize a handle with an empty C string
68  */
69 string_t StringEmpty( void ) ;
70 
71 /*
72  * initialize a handle with a buffer of size s
73  */
74 string_t StringBuffer( size_t s ) ;
75 
76 /*
77  * ininitialize a string handle by memcpy() length characters from a string
78  * a NULL character will be appended to the string.
79  */
80 string_t StringWithSize( const char * string,size_t length ) ;
81 
82 /*
83  * Append a string pointer to by s into a string handled by handle st
84  *
85  * On success,a concatenated string is returned.
86  * On error,NULL is returned and the original string remain intact.
87  */
88 const char * StringAppend( string_t st,const char * s ) ;
89 
90 /*
91  * Append a u_int64_t number to the string
92  */
93 const char * StringAppendInt( string_t,u_int64_t ) ;
94 
95 /*
96  * subsititue all occurance of string str with a number num
97  */
98 const char * StringSubStringWithInt( string_t st,const char * str,u_int64_t num ) ;
99 
100 /*
101  * Append multitple const char * strings to a string handled by handle st.
102  * NOTE: The list must be terminated a NULL character.
103  * example usage
104  * string_t st = String( "abc" ) ;
105  * StringMultipleAppend( st,"def","ghi",NULL ) ;
106  */
107 const char * StringMultipleAppend( string_t st,... ) __attribute__ ( ( sentinel ) ) ;
108 
109 /*
110  * Append a string handled by xt into a string handled by handle st
111  *
112  * On success,a concatenated string is returned.
113  * On error,NULL is returned and the original string remain intact.
114  */
115 const char * StringAppendString( string_t st,string_t xt ) ;
116 
117 /*
118  * check if a string handled by st has str in it
119  * return 1 if str is found
120  * return 0 if str is not found
121  */
122 int StringContains( string_t st,const char * str ) ;
123 
124 /*
125  * check if a string handled by st has str in it
126  * return 0 if str is found
127  * return 1 if str is not found
128  */
129 static __inline__ int StringDoesNotContain( string_t st,const char * str )
130 {
131  return !StringContains( st,str ) ;
132 }
133 
134 /*
135  * Append multiple string_t to a string handled by handle st .
136  * Same requirement as StringMultipleAppend apply
137  * NOTE:The series must be NULL terminated.
138  */
139 const char * StringAppendMultipleString( string_t st,... ) __attribute__ ( ( sentinel ) ) ;
140 
141 /*
142  * Append a char c into a string handled by handle st
143  *
144  * On success,a concatenated string is returned.
145  * On error,NULL is returned and the original string remain intact.
146  */
147 const char * StringAppendChar( string_t st,char c ) ;
148 
149 /*
150  * Prepend a string pointed to by s into a string handled by handle st
151  *
152  * On success,a concatenated string is returned
153  * On error,NULL is returned and the original string remain intact
154  */
155 const char * StringPrepend( string_t st,const char * s ) ;
156 
157 /*
158  * prepend multiple const char * to a string handled by string_t st.
159  * NOTE: entries will be prepended in the order they are presented
160  * NOTE: a list of entries must be NULL terminated
161  * example usage
162  * string_t st = String( "ghi" ) ;
163  * StringMultipleAppend( st,"def","abc",NULL ) ;
164  */
165 const char * StringMultiplePrepend( string_t st,... ) __attribute__ ( ( sentinel ) ) ;
166 
167 /*
168  * prepend multiple string_t to a string handled by string_t st.
169  * NOTE: entries will be prepended in the order they are presented
170  * NOTE: a list of entries must be NULL terminated
171  */
172 const char * StringMultiplePrependString( string_t st,... ) __attribute__ ( ( sentinel ) ) ;
173 
174 /*
175  * Prepend a string_t xt into a string handled by handle st
176  *
177  * On success,a concatenated string is returned
178  * On error,NULL is returned and the original string remain intact
179  */
180 const char * StringPrependString( string_t st,string_t xt ) ;
181 /*
182  * Prepend a char c into a string handled by handle st
183  *
184  * On success,a concatenated string is returned
185  * On error,NULL is returned and the original string remain intact
186  */
187 const char * StringPrependChar( string_t st,char c ) ;
188 
189 /*
190  * Inherit a string pointed to by data and return a string handle to the string on success or NULL on error.
191  * This function should inherit strings only from a dynamically created memory.
192  * The original string remains intact when the function error out.
193  */
194 string_t StringInherit( char ** data ) ;
195 
196 /*
197  * Inherit a string of size s pointed to by data and return a string handle to the string on success or NULL on error.
198  * This function should inherit strings only from a dynamically created memory.
199  * The original string remains intacct when the function error out.
200  * size is the size of the string
201  * length is the size of the buffer holding the string
202  */
203 string_t StringInheritWithSize( char ** data,size_t size,size_t length ) ;
204 
205 /*
206  * Returns a const pointer to a string handled by handle st.
207  * The returned pointer is guaranteed to be valid only if no further operation is performed on the string.
208  */
209 static __inline__ const char * StringContent( string_t st ) ;
210 
211 /*
212  * This function returns a pointer to a string and is guaranteed to be valid as long as StringDelete() is
213  * not called on the string.
214  *
215  * The underlying string can be obtained after the returned double pointer is derefenced.
216  */
217 static __inline__ const char ** StringPointer( string_t st ) ;
218 
219 /*
220  * printf() the string handled by handle st *
221  */
222 void StringPrint( string_t st ) ;
223 
224 /*
225  * printf() the string handled by handle st and followed by a newline
226  * it has the same effect as StringPrint() followed by printf("\n")
227  */
228 void StringPrintLine( string_t st ) ;
229 
230 /*
231  * Returns an editable string copy of a string handled by handle st.
232  * Remember to free it when you are done using it.
233  * NULL is returned when the copy can not be made.
234  */
235 char * StringCopy_1( string_t st ) ;
236 
237 /*
238  * Make a copy of string_t st
239  * return NULL on error.
240  */
242 
243 /*
244  * returns a copy of a string.
245  * remember to free() it when done with it
246  */
247 char * StringCopy_2( const char * ) ;
248 
249 /*
250  * Returns an editable copy of a string made up of upto x characters
251  * Remember to free string when you are done with it.
252  * returns NULL on error. *
253  */
254 char * StringCopy_3( string_t st,size_t x ) ;
255 
256 /*
257  * Write "size" amount of content of a string handled by "st" to a buffer pointed by "buffer" *
258  */
259 void StringReadToBuffer( string_t st,char * buffer,size_t size ) ;
260 
261 /*
262  * Remember to clean after yourself.
263  * Always call this function when you are done with the string handled by handle st.
264  * The function will not attempt to double delete,or delete unassigned handle( NULL handle ),for
265  * this to work,make sure your handles are NULL ininitialized.
266  * NULL is assigned to handle to invalidate it
267  */
268 void StringDelete( string_t * st );
269 
270 /*
271  * convert all upper case characters to lower case
272  */
273 const char * StringToLowerCase( string_t ) ;
274 
275 /*
276  * clear the string before StringDelete() it
277  */
278 void StringClearDelete( string_t * st ) ;
279 
280 /*
281  * Remember to clean after yourself.
282  * delete multitple string_t objects.
283  * NOTE: a list of entries must be NULL terminated
284  * The function will not attempt to double delete,or delete unassigned handle( NULL handle ),for
285  * this to work,make sure your handled are NULL ininitialized.
286  * NULL is assigned to each handle to invalidate them
287  */
288 void StringMultipleDelete( string_t * st,... ) __attribute__ ( ( sentinel ) ) ;
289 
290 /*
291  * input argument: String handle
292  * return value: a pointer to the string managed by the handle.
293  *
294  * This function deletes the handle presented through argument st and returns
295  * a pointer to the string handled by st.
296  *
297  * Remember to free the pointer( using free() ) when you are done with it
298  *
299  * Use this function when you no longer need the handle i.e dont want to do string
300  * manipulation through the handle but want the pointer to the string it managed.
301  */
302 char * StringDeleteHandle( string_t * st ) ;
303 
304 /*
305  * Return the length of the string handled by handle st.
306  */
307 size_t StringLength( string_t st ) ;
308 
309 /*
310  * Return 1 if the the length of the string managed by st equals s.
311  * Return 0 otherwise.
312  */
313 int StringLengthMatch( string_t st,size_t s ) ;
314 
315 /*
316  * Return a character at position p.First position is at "0".
317  *
318  * NOTE: it is your responsibility to make sure p is within range
319  */
320 char StringCharAt( string_t st,size_t p ) ;
321 
322 /*
323  * Returns the last character in the string
324  */
325 char StringCharAtLast( string_t st ) ;
326 
327 /*
328  * return the position of the first occurance of character s starting from position p.
329  *
330  * -1 is returned if the character is not in the string.
331  *
332  * NOTE: first character in the string is at position 0
333  */
334 ssize_t StringIndexOfChar( string_t st,size_t p,char s ) ;
335 
336 /*
337  * return the position of the first occurance of string s starting from position p.
338  *
339  * -1 is returned if s isnt in the string
340  *
341  * NOTE: first character in the string is at position 0
342  */
343 ssize_t StringIndexOfString( string_t st,size_t p,const char * s ) ;
344 
345 /*
346  * Return the position of the last occurance of character s
347  *
348  * -1 is returned if the character isnt in the string
349  */
350 ssize_t StringLastIndexOfChar( string_t st,char s ) ;
351 
352 /*
353  * Returns the last occurance of a string pointed by s
354  *
355  * -1 is returned of the string is not found.
356  *
357  * NOTE: first character in the string is at position 0
358  *
359  */
360 ssize_t StringLastIndexOfString( string_t st,const char * s ) ;
361 
362 /*
363  * Return a const string starting at position p. First position is at 0
364  *
365  * NOTE: it is your responsibility to make sure p is within range.
366  */
367 const char * StringStringAt( string_t st,size_t p ) ;
368 
369 /*
370  * return 1 if a string is a part of stringlist
371  * return 0 otherwise or if StringVoid
372  */
373 int StringOwned( string_t ) ;
374 
375 /*
376  * call mlock() on the string
377  */
378 int StringLock( string_t ) ;
379 
380 /*
381  * call munlock() on the string
382  */
383 int StringUnlock( string_t ) ;
384 
385 /*
386  * Check to see if the string pointer by st ends with string s.
387  *
388  * return 1 is it does
389  *
390  * return 0 if it doesnt
391  */
392 int StringEndsWith( string_t st,const char * s ) ;
393 
394 /*
395  * Check to see if a string e ends with string s.
396  *
397  * return 1 is it does
398  *
399  * return 0 if it doesnt
400  */
401 int StringEndsWith_1( const char * e,const char * s ) ;
402 
403 /*
404  * Check to see if string e ends with atleast one of other arguments.
405  *
406  * return 1 is it does
407  *
408  * return 0 if it doesnt
409  */
410 int StringEndsWithAtLeastOne( const char * e,... ) __attribute__ ( ( sentinel ) ) ;
411 
412 /*
413  * Check to see if a string e ends with a string s.
414  *
415  * return 1 is it does
416  *
417  * return 0 if it doesnt
418  */
420 
421 /*
422  * check to see if the string handled by handle st starts with a string s
423  * return 1 if it does
424  * return 0 if it doesnt
425  */
426 int StringStartsWith( string_t st,const char * s ) ;
427 
428 /*
429  * check to see if the string handled by handle st starts with a string handled by handle xt
430  * return 1 if it does
431  * return 0 if it doesnt
432  */
433 int StringStartsWith_1( string_t st,string_t xt ) ;
434 
435 /*
436  * check if string a starts with string b and ends with string c
437  * return 1 if it does
438  * return 0 if it doesnt
439  */
440 int StringStartsAndEndsWith( const char * a,const char * b,const char * c ) ;
441 
442 /*
443  * check to see the string_t object starts with atleast one of the arbitrary number of cstrings
444  * return 1 if it does
445  * return 0 if it does not
446  *
447  * exampe:
448  * st = String( "abcdef" ) ;
449  * int r = StringStartsWithAtLeastOne( st,"rrr","www","abc",NULL ) ;
450  * r will contain "1" since "abc" argument match the beginning of the string_t object
451  * NOTE:The series must be NULL terminated.
452  */
453 int StringStartsWithAtLeastOne( string_t st,... ) __attribute__ ( ( sentinel ) ) ;
454 
455 /*
456  *Check to see if the string pointer by st ends with char s.
457  *
458  * return 1 is it does
459  *
460  * return 0 if it doesnt *
461  */
462 int StringEndsWithChar( string_t st,char s ) ;
463 
464 /*
465  * Insert a string s from position x and returns a pointer to the new string
466  *
467  * on error returns NULL and the original string remains intact
468  */
469 const char * StringInsertString( string_t st,size_t x,const char * s ) ;
470 
471 /*
472  * Insert a char s at position x and returns a pointer to the new string
473  *
474  * on error returns NULL and the original string remains intact
475  */
476 const char * StringInsertChar( string_t st,size_t x,char s ) ;
477 
478 /*
479  * subsititue whatever character is at position x by character s
480  * returns a pointer to the string containing the subsititue
481  */
482 const char * StringSubChar( string_t st,size_t x,char s ) ;
483 
484 /*
485  * start from position x and subsititue all characters in the string by string s.
486  * returns a pointer with the substitution.
487  */
488 const char * StringSubString( string_t st,size_t x,const char * s ) ;
489 
490 /*
491  * remove all character after index x and then insert string s at index.
492  */
493 const char * StringAppendAt( string_t st,size_t x,const char * s ) ;
494 
495 /*
496  * start at position x and remove y character(s) going right and returns a pointer
497  * to the new string or NULL on error and the original string remain intact.
498  * Careful though,make sure you dont delete past the string length
499  */
500 const char * StringRemoveLength( string_t st,size_t x,size_t y ) ;
501 
502 /*
503  * remove all occurances of string s in a string handled by handle st.
504  * return a pointer to the resulting string on success and NULL on error and the original string
505  * remain intact
506  *
507  */
508 const char * StringRemoveString( string_t st,const char * s ) ;
509 
510 /*
511  * starting at position p,remove all occurances of string s in a string handled by handle st.
512  * return a pointer to the resulting string on success and NULL on error and the original string
513  * remain intact
514  *
515  */
516 const char * StringRemoveStringPos( string_t st,const char * s,size_t p ) ;
517 
518 /*
519  * remove the first x characters counting from the right ie,remove the last x characters from the string
520  * Returns a pointer to the result,NULL on error and the original string remain intact
521  */
522 const char * StringRemoveRight( string_t st,size_t x ) ;
523 
524 /*
525  * Clear the string by writing '\0' using memset() essentiall wipe out all of its current content.
526  * This is a bit more expensive but useful if you want to start afresh with an empty string.
527  */
528 void StringClear( string_t st ) ;
529 
530 /*
531  * forget about all content in the string and start afresh as if the string is not allocated.
532  * The old data is still there and will be overwritten by new data as the string get filled up
533  */
534 void StringReset( string_t st ) ;
535 
536 /*
537  * remove the first x characters from the string counting from the left
538  * Returns a pointer to the result,NULL on error and the original string remain intact
539  */
540 const char * StringRemoveLeft( string_t st,size_t x ) ;
541 
542 /*
543  * remove all occurances numbers in the string,ie '0','1','2' etc
544  */
545 const char * StringRemoveDigits( string_t ) ;
546 
547 /*
548  * Return a sub string starting at position x and made up of y characters
549  *
550  * NULL is returned if the operation fail.
551  *
552  * Remember to free the returned string when done with it. *
553  */
554 string_t StringMidString( string_t st,size_t x,size_t y ) ;
555 
556 /*
557  * replace all occurance of string x by string y
558  * return a pointer to the resulting string on success or NULL on error and the original string
559  * remain intact.
560  */
561 const char * StringReplaceString( string_t st,const char * x,const char * y ) ;
562 
563 /*
564  * replace the string managed by string_t object by string str
565  * return a pointer to the new string managed by string_t object or NULL if either of the argument is NULL
566  */
567 const char * StringReplace( string_t,const char * str ) ;
568 
569 /*
570  * starting at position p,replace all occurance of string x by string y
571  * return a pointer to the resulting string on success or NULL on error and the original string
572  * remain intact.
573  */
574 const char * StringReplaceStringPos( string_t st,const char * x,const char * y,size_t p ) ;
575 
576 /*
577  * replace all occurance of char x by char y
578  * Return a const pointer to a modified string
579  */
580 const char * StringReplaceChar( string_t st,char x,char y ) ;
581 
582 /*
583  * start at index z,replace the first occurance of character x by character y
584  * Only the first character will be subsituted if found
585  * NULL is returned if substitution could not take place
586  * modified string is returned if substitution took place
587  */
588 const char * StringReplaceChar_1( string_t st,size_t z,char x,char y ) ;
589 
590 /*
591  * starting at position p,replace all occurance of char x by char y
592  * Return a const pointer to a modified string
593  */
594 const char * StringReplaceCharPos( string_t st,char x,char y,size_t p ) ;
595 
596 /*
597  * Replace all characters in y by x in a string handled by st
598  */
599 const char * StringReplaceCharString( string_t st,char x,const char * y ) ;
600 
601 /*
602  * Starting at position,Replace all characters in y by x in a string handled by st
603  */
604 const char * StringReplaceCharStringPos( string_t st,char x,const char * y,size_t p ) ;
605 
606 /*
607  * convert a number z into a string and store the result into array x of size y.
608  *
609  * return value: a pointer to the beginning of the result(use this pointer and not x).
610  *
611  * NOTE: Its your responsibility to make sure the resulting string fit into array x.
612  * If z has N digits,then the array must be atleast N+1 in size,null character
613  * takes the last spot.
614  */
615 
616 char * StringIntToString_1( char * x,size_t y,u_int64_t z ) ;
617 
618 /*
619  * convert a number z into a string
620  */
621 string_t StringIntToString( u_int64_t ) ;
622 
623 /*
624  * convert a string made up of digits to a u_int64_t value
625  * eg:
626  * u_int64_t x = StringConvertToInt( "43542" ) ;
627  */
628 u_int64_t StringConvertToInt( const char * ) ;
629 
630 /*
631  * Compare a string handled by handle x to a string handled by handle y.
632  * return 1 if they are equal
633  * return 0 if they are not equal
634  */
636 
637 /*
638  * Compare a string handled by handle x to a string pointer to by y.
639  * return 1 if they are equal
640  * return 0 if they are not equal
641  */
642 int StringsAreEqual_2( string_t x,const char * y ) ;
643 
644 /*
645  * return 1 if x holds an empty string
646  * return 0 if x holds a non empty string
647  * return 0 if x is StringVoid
648  */
649 int StringIsEmpty( string_t x ) ;
650 
651 /*
652  * Compare a string handled by handle x to a string pointer to by y.
653  * return 0 if they are equal
654  * return 1 if they are not equal
655  */
656 static __inline__ int StringsAreNotEqual_2( string_t x,const char * y )
657 {
658  return !StringsAreEqual_2( x,y ) ;
659 }
660 
661 /*
662  * compare string managed by x with a series of cstrings and return true if atleast one of them match
663  * NOTE:the series of strings must be NULL terminated
664  * eg.
665  * string_t st = String( "abc" ) ;
666  * int equal = StringMatchOneAtLeastOne( st,"def","ugf",NULL ) ;
667  */
668 int StringAtLeastOneMatch( string_t x,... ) __attribute__ ( ( sentinel ) ) ;
669 
670 /*
671  * compare cstring x with a series of cstrings and return true if atleast one of them match
672  * NOTE:the series of strings must be NULL terminated
673  * eg.
674  * int equal = StringMatchOneAtLeastOne_1( "def","ugf","rrt",NULL ) ;
675  */
676 int StringAtLeastOneMatch_1( const char * x,... ) __attribute__ ( ( sentinel ) ) ;
677 
678 /*
679  * Insert character x infront of every character that appear in string y in a string handled by handle st.
680  * Retun a poiter to the final string on success and NULL on error and the original string remain intact
681  */
682 const char * StringInsertCharString( string_t st,char x,const char * y ) ;
683 
684 /*
685  * Starting at position p,insert character x infront of every character that appear in string y in a string handled by handle st.
686  * Retun a poiter to the final string on success and NULL on error and the original string remain intact
687  */
688 const char * StringInsertCharStringPos( string_t st,char x,const char * y,size_t p ) ;
689 
690 /*
691  * Insert character x infront of every character y in a string handled by handle st.
692  * Retun a poiter to the final string on success and NULL on error and the original string remain intact
693  */
694 const char * StringInsertCharChar( string_t st,char x,char y ) ;
695 
696 /*
697  * Crop off the first x elements and the last y elements from the string handled by handle st
698  *
699  * Return a pointer to the cropped string on success and NULL on error and the original string remain intact.
700  * NULL is also returned if cropping will result in less that zero characters in the string
701  */
702 const char * StringCrop( string_t st,size_t x,size_t y ) ;
703 
704 /*
705  * getchar() until a newline or EOF character is reached and put the characters to a string_t object
706  * The function basically get its content from the terminal ( stdin ) .
707  * NULL is returned if sufficient memory can not be optained to hold terminal content.
708  */
710 
711 /*
712  * getchar() of upto s characters or until a newline or EOF character is reached and put the characters to a string_t object
713  * The function basically get its content from the terminal ( stdin ) .
714  * NULL is returned if sufficient memory can not be optained to hold terminal content.
715  */
716 string_t StringGetFromTerminal_1( size_t s ) ;
717 
718 /*
719  * Turn echo off and get string from the terminal.
720  * This function is ideal for reading passphrases from the terminal.
721  * 1 is returned when character echoing can not be turned off.
722  * 2 is returned if sufficient memory can not be optained to hold terminal content.
723  * 0 is returned on success.
724  */
726 
727 /*
728  * Turn echo off and get a string of upto s characters from the terminal.
729  * This function is ideal for reading passphrases from the terminal.
730  * 1 is returned when character echoing can not be turned off.
731  * 2 is returned if sufficient memory can not be optained to hold terminal content.
732  * 0 is returned on success.
733  */
734 int StringSilentlyGetFromTerminal_1( string_t *,size_t s ) ;
735 
736 /*
737  * Open a file given by path and return a string_t handle
738  *
739  * NULL is returned if the file could not be opened for reading or could not get sufficient memory to hold the file.
740  */
741 string_t StringGetFromFile( const char * path ) ;
742 
743 /*
744  * constract a string_t object with size random characters read from "/dev/urandom.
745  * StringVoid is returned on error.
746  */
747 string_t StringRandomString( size_t size ) ;
748 
749 /*
750  * Open a file given by path and return a string_t handle through agrument st with the content of the file .
751  * return value: 0 - opefation succeeded.
752  * 1 - path is invalid.
753  * 2 - could not open file for reading.
754  * 3 - could not allocate memory to host file content
755  */
756 int StringGetFromFile_1( string_t * st,const char * path ) ;
757 
758 /*
759  * Open a file given by path and return a string_t handle through agrument st with the content of the file .
760  * Start reading the file from offset byte and read length bytes.
761  * return value: 0 - opefation succeeded.
762  * 1 - path is invalid.
763  * 2 - could not open file for reading.
764  * 3 - could not allocate memory to host file content
765  */
766 int StringGetFromFile_3( string_t * st,const char * path,size_t offset,size_t length ) ;
767 
768 /*
769  * Open a file given by path and return a string_t handle with hhe content of the file .
770  * return value through status:
771  * 0 - opefation succeeded.
772  * 1 - path is invalid.
773  * 2 - could not open file for reading.
774  * 3 - could not allocate memory to host file content
775  *
776  * This function does the same thing the previous one does,the different is how return values are returned.
777  */
778 string_t StringGetFromFile_2( const char * path,int * status ) ;
779 
780 /*
781  * reads a maximum of "length" bytes from a file given by "path" argument starting at "offset" offset
782  * and then lock the memory buffer.
783  * return values:
784  * 0 - success
785  * 1 - file operation failed
786  * 2 - memory operation failed
787  *
788  * The mlock()ed string object will be returned through str argument
789  * negative value of length means reads the entire file content
790  *
791  * warning memory may or may not lock
792  */
793 int StringGetFromFileMemoryLocked( string_t * str,const char * path,size_t offset,ssize_t length ) ;
794 
795 /*
796  * White the string managed by handle st to a file given by path and return the number of bytes written. *
797  */
798 
799 #define CREATE 1 /*if the file does not exist,create it,if the file exist,trancate it*/
800 #define APPEND 2 /* if the file exist,append it*/
801 void StringWriteToFile( string_t st,const char * path,int mode ) ;
802 
803 /*
804  * Open a virtual file given by path return a string_t handle with the content of the file.
805  * Virtual files are like those in /proc,you cab read stuff from them but their sizes are always zero
806  */
807 string_t StringGetFromVirtualFile( const char * path ) ;
808 
809 /*
810  * below two functions creates a hash of the string using junkin's one at a time hash algorithm
811  */
812 u_int32_t StringJenkinsOneAtATimeHash( const char * key ) ;
813 
815 
816 /*
817  * a few convenient "safe" functions
818  */
819 
820 /*
821  * why doesnt free() take const void * ????
822  */
823 static __inline__ void StringFree( const void * str )
824 {
825  free( ( void * )str ) ;
826 }
827 
828 /*
829  * safely do free( *dev ) followed by *dev = NULL
830  * this function hence should take only a double pointer.
831  * ie
832  * char * e = malloc(sizeof(char)) ;
833  * ..;
834  * StringFree_1(&e);
835  */
836 static __inline__ void StringFree_1( const void * str )
837 {
838  char ** c ;
839  if( str != NULL ){
840  c = ( char ** ) str ;
841  free( *c ) ;
842  *c = NULL ;
843  }
844 }
845 
846 static __inline__ size_t StringSize( const char * str )
847 {
848  if( str == NULL ){
849  return 0 ;
850  }else{
851  return strlen( str ) ;
852  }
853 }
854 
855 static __inline__ int StringsAreEqual( const char * x,const char * y )
856 {
857  if( x == NULL || y == NULL ){
858  return 0 ;
859  }else{
860  return strcmp( x,y ) == 0 ;
861  }
862 }
863 
864 static __inline__ int StringHasNothing( const char * x )
865 {
866  return x != NULL && *x == '\0' ;
867 }
868 
869 static __inline__ int StringsAreNotEqual( const char * x,const char * y )
870 {
871  if( x == NULL || y == NULL ){
872  return 1 ;
873  }else{
874  return strcmp( x,y ) != 0 ;
875  }
876 }
877 
878 static __inline__ int StringPrefixMatch( const char * x,const char * y,size_t z )
879 {
880  if( x == NULL || y == NULL ){
881  return 0 ;
882  }else{
883  return strncmp( x,y,z ) == 0 ;
884  }
885 }
886 
887 /*
888  * returns true if a atleast one cstring in the series starts with x
889  * NOTE:The series must be NULL terminated.
890  */
891 int StringAtLeastOnePrefixMatch( const char * x,... ) __attribute__ ( ( sentinel ) ) ;
892 
893 /*
894  * returns true if atleast one cstring is a component of x
895  * NOTE:The series must be NULL terminated.
896  */
897 int StringHasAtLeastOneComponent_1( const char * x,... ) __attribute__ ( ( sentinel ) ) ;
898 
899 /*
900  * returns true if atleast one cstring is a component of a string managed by object st
901  * NOTE:The series must be NULL terminated.
902  */
903 int StringHasAtLeastOneComponent( string_t st,... ) __attribute__ ( ( sentinel ) ) ;
904 
905 static __inline__ int StringPrefixEqual( const char * x,const char * y )
906 {
907  if( x == NULL || y == NULL ){
908  return 0 ;
909  }else{
910  return strncmp( x,y,strlen( y ) ) == 0 ;
911  }
912 }
913 
914 static __inline__ int StringPrefixNotEqual( const char * x,const char * y )
915 {
916  if( x == NULL || y == NULL ){
917  return 0 ;
918  }else{
919  return strncmp( x,y,strlen( y ) ) != 0 ;
920  }
921 }
922 
923 static __inline__ ssize_t StringHasComponent_1( const char * x,const char * y )
924 {
925  char * e ;
926  if( x == NULL || y == NULL ){
927  return -1 ;
928  }else{
929  e = strstr( x,y ) ;
930  if( e == NULL ){
931  return -1 ;
932  }else{
933  return e - x ;
934  }
935  }
936 }
937 
938 static __inline__ size_t StringCharCount( const char * x,char y )
939 {
940  size_t s = 0 ;
941  char m ;
942 
943  if( x != NULL ){
944 
945  while( 1 ){
946 
947  m = *x ;
948  x++ ;
949 
950  if( m == y ){
951 
952  s++ ;
953 
954  }else if( m == '\0' ){
955 
956  break ;
957  }
958  }
959  }
960 
961  return s ;
962 }
963 
964 static __inline__ int StringHasComponent( const char * x,const char * y )
965 {
966  if( x == NULL || y == NULL ){
967  return 0 ;
968  }else{
969  return strstr( x,y ) != NULL ;
970  }
971 }
972 
973 static __inline__ int StringHasNoComponent( const char * x,const char * y )
974 {
975  if( x == NULL || y == NULL ){
976  return 0 ;
977  }else{
978  return strstr( x,y ) == NULL ;
979  }
980 }
981 
982 static __inline__ ssize_t StringLastIndexOfChar_1( const char * str,char s )
983 {
984  const char * c ;
985  if( str == NULL ){
986  return -1 ;
987  }else{
988  c = strrchr( str,s ) ;
989  if( c == NULL ){
990  return -1 ;
991  }else{
992  return c - str ;
993  }
994  }
995 }
996 
997 static __inline__ ssize_t StringFirstIndexOfChar_1( const char * str,char s )
998 {
999  if( str == NULL ){
1000  return -1 ;
1001  }else{
1002  return strchr( str,s ) - str ;
1003  }
1004 }
1005 
1006 static __inline__ const char ** StringPointer( string_t st )
1007 {
1008  return ( const char ** ) st ;
1009 }
1010 
1011 static __inline__ const char * StringContent( string_t st )
1012 {
1013  const char ** e = StringPointer( st ) ;
1014 
1015  if( e == NULL ){
1016  return NULL ;
1017  }else{
1018  return *e ;
1019  }
1020 }
1021 
1022 #ifdef __cplusplus
1023 }
1024 #endif
1025 
1026 #endif
StringHasAtLeastOneComponent_1
int StringHasAtLeastOneComponent_1(const char *x,...)
Definition: String.c:1528
StringCharAt
char StringCharAt(string_t st, size_t p)
Definition: String.c:848
StringLock
int StringLock(string_t)
Definition: String.c:144
StringContains
int StringContains(string_t st, const char *str)
Definition: String.c:420
StringAppend
const char * StringAppend(string_t st, const char *s)
Definition: String.c:1052
StringAppendInt
const char * StringAppendInt(string_t, u_int64_t)
Definition: String.c:1410
StringAppendChar
const char * StringAppendChar(string_t st, char c)
Definition: String.c:1205
StringGetFromTerminal_1
string_t StringGetFromTerminal_1(size_t s)
Definition: String.c:1675
StringClear
void StringClear(string_t st)
Definition: String.c:618
StringGetIterators
void StringGetIterators(string_t, StringIterator *begin, StringIterator *end)
Definition: String.c:133
StringIndexOfString
ssize_t StringIndexOfString(string_t st, size_t p, const char *s)
Definition: String.c:468
StringType::size
size_t size
Definition: String.c:58
StringStartsAndEndsWith
int StringStartsAndEndsWith(const char *a, const char *b, const char *c)
Definition: String.c:808
StringSilentlyGetFromTerminal_1
int StringSilentlyGetFromTerminal_1(string_t *, size_t s)
Definition: String.c:1752
StringPrint
void StringPrint(string_t st)
Definition: String.c:406
StringMidString
string_t StringMidString(string_t st, size_t x, size_t y)
Definition: String.c:1240
StringStartsWith_1
int StringStartsWith_1(string_t st, string_t xt)
Definition: String.c:830
StringEmpty
string_t StringEmpty(void)
Definition: String.c:292
StringType::length
size_t length
Definition: String.c:62
StringCopy_2
char * StringCopy_2(const char *)
Definition: String.c:725
StringIntToString
string_t StringIntToString(u_int64_t)
Definition: String.c:1404
StringConvertToInt
u_int64_t StringConvertToInt(const char *)
Definition: String.c:1416
StringInsertCharStringPos
const char * StringInsertCharStringPos(string_t st, char x, const char *y, size_t p)
Definition: String.c:1635
StringRemoveStringPos
const char * StringRemoveStringPos(string_t st, const char *s, size_t p)
Definition: String.c:1323
StringInsertCharChar
const char * StringInsertCharChar(string_t st, char x, char y)
Definition: String.c:1645
StringInheritWithSize
string_t StringInheritWithSize(char **data, size_t size, size_t length)
Definition: String.c:429
StringRemoveLeft
const char * StringRemoveLeft(string_t st, size_t x)
Definition: String.c:647
StringLengthMatch
int StringLengthMatch(string_t st, size_t s)
Definition: String.c:687
StringCopy
string_t StringCopy(string_t st)
Definition: String.c:260
StringReplaceCharPos
const char * StringReplaceCharPos(string_t st, char x, char y, size_t p)
Definition: String.c:1360
StringReplaceCharString
const char * StringReplaceCharString(string_t st, char x, const char *y)
Definition: String.c:931
StringsAreEqual_2
int StringsAreEqual_2(string_t x, const char *y)
Definition: String.c:1469
StringAppendAt
const char * StringAppendAt(string_t st, size_t x, const char *s)
Definition: String.c:960
StringRandomString
string_t StringRandomString(size_t size)
Definition: String.c:1771
StringsAreEqual_1
int StringsAreEqual_1(string_t x, string_t y)
Definition: String.c:1460
StringCrop
const char * StringCrop(string_t st, size_t x, size_t y)
Definition: String.c:652
StringPrefixNotEqual
static __inline__ int StringPrefixNotEqual(const char *x, const char *y)
Definition: String.h:914
StringRemoveLength
const char * StringRemoveLength(string_t st, size_t x, size_t y)
Definition: String.c:568
StringLastIndexOfString
ssize_t StringLastIndexOfString(string_t st, const char *s)
Definition: String.c:509
StringDeleteHandle
char * StringDeleteHandle(string_t *st)
Definition: String.c:233
String
string_t String(const char *cstring)
Definition: String.c:318
StringMultipleDelete
void StringMultipleDelete(string_t *st,...)
Definition: String.c:195
StringExitOnMemoryExaustion
void StringExitOnMemoryExaustion(void(*)(void))
Definition: String.c:75
StringJenkinsOneAtATimeHash
u_int32_t StringJenkinsOneAtATimeHash(const char *key)
Definition: String.c:1816
StringStringAt
const char * StringStringAt(string_t st, size_t p)
Definition: String.c:870
StringDoesNotContain
static __inline__ int StringDoesNotContain(string_t st, const char *str)
Definition: String.h:129
StringIndexOfChar
ssize_t StringIndexOfChar(string_t st, size_t p, char s)
Definition: String.c:548
StringHasNothing
static __inline__ int StringHasNothing(const char *x)
Definition: String.h:864
StringsAreNotEqual_2
static __inline__ int StringsAreNotEqual_2(string_t x, const char *y)
Definition: String.h:656
StringHasNoComponent
static __inline__ int StringHasNoComponent(const char *x, const char *y)
Definition: String.h:973
StringCopy_3
char * StringCopy_3(string_t st, size_t x)
Definition: String.c:749
StringIntToString_1
char * StringIntToString_1(char *x, size_t y, u_int64_t z)
Definition: String.c:1444
StringReplaceChar
const char * StringReplaceChar(string_t st, char x, char y)
Definition: String.c:1383
StringGetFromVirtualFile
string_t StringGetFromVirtualFile(const char *path)
Definition: String.c:2050
StringSubStringWithInt
const char * StringSubStringWithInt(string_t st, const char *str, u_int64_t num)
Definition: String.c:1438
StringInherit
string_t StringInherit(char **data)
Definition: String.c:391
StringInsertString
const char * StringInsertString(string_t st, size_t x, const char *s)
Definition: String.c:1218
StringWriteToFile
void StringWriteToFile(string_t st, const char *path, int mode)
Definition: String.c:2013
StringRemoveRight
const char * StringRemoveRight(string_t st, size_t x)
Definition: String.c:634
StringSize
static __inline__ size_t StringSize(const char *str)
Definition: String.h:846
StringGetFromFile
string_t StringGetFromFile(const char *path)
Definition: String.c:2006
StringContent
static const __inline__ char * StringContent(string_t st)
Definition: String.h:1011
StringHasComponent_1
static __inline__ ssize_t StringHasComponent_1(const char *x, const char *y)
Definition: String.h:923
StringType
Definition: String.c:49
StringReplaceChar_1
const char * StringReplaceChar_1(string_t st, size_t z, char x, char y)
Definition: String.c:1365
StringClearDelete
void StringClearDelete(string_t *st)
Definition: String.c:177
StringReadToBuffer
void StringReadToBuffer(string_t st, char *buffer, size_t size)
Definition: String.c:384
StringPrependChar
const char * StringPrependChar(string_t st, char c)
Definition: String.c:1045
StringIsEmpty
int StringIsEmpty(string_t x)
Definition: String.c:1333
StringHasAtLeastOneComponent
int StringHasAtLeastOneComponent(string_t st,...)
Definition: String.c:1552
StringInsertCharString
const char * StringInsertCharString(string_t st, char x, const char *y)
Definition: String.c:1640
StringLength
size_t StringLength(string_t st)
Definition: String.c:678
StringLastIndexOfChar_1
static __inline__ ssize_t StringLastIndexOfChar_1(const char *str, char s)
Definition: String.h:982
StringEndsWith_2
int StringEndsWith_2(string_t e, string_t s)
Definition: String.c:799
StringPrefixMatch
static __inline__ int StringPrefixMatch(const char *x, const char *y, size_t z)
Definition: String.h:878
StringGetFromFile_3
int StringGetFromFile_3(string_t *st, const char *path, size_t offset, size_t length)
Definition: String.c:1868
StringStartsWith
int StringStartsWith(string_t st, const char *s)
Definition: String.c:821
StringGetFromTerminal
string_t StringGetFromTerminal(void)
Definition: String.c:1653
siritask::status
status
Definition: siritask.h:114
StringAtLeastOneMatch
int StringAtLeastOneMatch(string_t x,...)
Definition: String.c:1478
StringBuffer
string_t StringBuffer(size_t s)
Definition: String.c:297
StringOwned
int StringOwned(string_t)
Definition: String.c:86
StringStartsWithAtLeastOne
int StringStartsWithAtLeastOne(string_t st,...)
Definition: String.c:1076
StringToLowerCase
const char * StringToLowerCase(string_t)
Definition: String.c:95
StringRemoveDigits
const char * StringRemoveDigits(string_t)
Definition: String.c:587
StringGetFromFile_2
string_t StringGetFromFile_2(const char *path, int *status)
Definition: String.c:1993
StringCharAtLast
char StringCharAtLast(string_t st)
Definition: String.c:859
StringEndsWith
int StringEndsWith(string_t st, const char *s)
Definition: String.c:781
StringFirstIndexOfChar_1
static __inline__ ssize_t StringFirstIndexOfChar_1(const char *str, char s)
Definition: String.h:997
StringLastIndexOfChar
ssize_t StringLastIndexOfChar(string_t st, char s)
Definition: String.c:488
StringMultiplePrepend
const char * StringMultiplePrepend(string_t st,...)
Definition: String.c:1159
StringEndsWithAtLeastOne
int StringEndsWithAtLeastOne(const char *e,...)
Definition: String.c:1104
StringHasComponent
static __inline__ int StringHasComponent(const char *x, const char *y)
Definition: String.h:964
StringEndsWithChar
int StringEndsWithChar(string_t st, char s)
Definition: String.c:839
StringFree_1
static __inline__ void StringFree_1(const void *str)
Definition: String.h:836
StringInsertChar
const char * StringInsertChar(string_t st, size_t x, char s)
Definition: String.c:953
StringPrintLine
void StringPrintLine(string_t st)
Definition: String.c:413
e
static QString e
Definition: about.cpp:31
StringPrepend
const char * StringPrepend(string_t st, const char *s)
Definition: String.c:1015
StringReplaceCharStringPos
const char * StringReplaceCharStringPos(string_t st, char x, const char *y, size_t p)
Definition: String.c:925
StringsAreEqual
static __inline__ int StringsAreEqual(const char *x, const char *y)
Definition: String.h:855
StringMultiplePrependString
const char * StringMultiplePrependString(string_t st,...)
StringIterator
char * StringIterator
Definition: String.h:49
StringReplaceString
const char * StringReplaceString(string_t st, const char *x, const char *y)
Definition: String.c:1318
StringAppendString
const char * StringAppendString(string_t st, string_t xt)
Definition: String.c:1195
StringCharCount
static __inline__ size_t StringCharCount(const char *x, char y)
Definition: String.h:938
StringGetFromFile_1
int StringGetFromFile_1(string_t *st, const char *path)
Definition: String.c:1858
StringDelete
void StringDelete(string_t *st)
Definition: String.c:162
String_1
string_t String_1(const char *cstring,...)
Definition: String.c:363
StringRemoveString
const char * StringRemoveString(string_t st, const char *s)
Definition: String.c:1328
StringReplaceStringPos
const char * StringReplaceStringPos(string_t st, const char *x, const char *y, size_t p)
Definition: String.c:1313
StringReset
void StringReset(string_t st)
Definition: String.c:626
StringReplace
const char * StringReplace(string_t, const char *str)
Definition: String.c:984
StringSilentlyGetFromTerminal
int StringSilentlyGetFromTerminal(string_t *)
Definition: String.c:1730
StringPrependString
const char * StringPrependString(string_t st, string_t xt)
Definition: String.c:1040
StringUnlock
int StringUnlock(string_t)
Definition: String.c:153
StringAppendMultipleString
const char * StringAppendMultipleString(string_t st,...)
Definition: String.c:1141
StringEndsWith_1
int StringEndsWith_1(const char *e, const char *s)
Definition: String.c:790
StringGetFromFileMemoryLocked
int StringGetFromFileMemoryLocked(string_t *str, const char *path, size_t offset, ssize_t length)
Definition: String.c:1921
StringPointer
static const __inline__ char ** StringPointer(string_t st)
Definition: String.h:1006
StringCopy_1
char * StringCopy_1(string_t st)
Definition: String.c:716
StringAtLeastOneMatch_1
int StringAtLeastOneMatch_1(const char *x,...)
Definition: String.c:1504
StringMultipleAppend
const char * StringMultipleAppend(string_t st,...)
Definition: String.c:1123
StringSubChar
const char * StringSubChar(string_t st, size_t x, char s)
Definition: String.c:881
StringsAreNotEqual
static __inline__ int StringsAreNotEqual(const char *x, const char *y)
Definition: String.h:869
StringJenkinsOneAtATimeHash_1
u_int32_t StringJenkinsOneAtATimeHash_1(string_t)
Definition: String.c:1836
StringSubString
const char * StringSubString(string_t st, size_t x, const char *s)
Definition: String.c:936
StringAtLeastOnePrefixMatch
int StringAtLeastOnePrefixMatch(const char *x,...)
Definition: String.c:1578
StringPrefixEqual
static __inline__ int StringPrefixEqual(const char *x, const char *y)
Definition: String.h:905
StringFree
static __inline__ void StringFree(const void *str)
Definition: String.h:823
StringWithSize
string_t StringWithSize(const char *string, size_t length)
Definition: String.c:453
string_t
struct StringType * string_t
Definition: String.h:41