3 @brief Implement osrfStringArray, a vector of character strings.
5 An osrfStringArray is implemented as a thin wrapper around an osrfList. The latter is
6 incorporated bodily in the osrfStringArray structure, not as a pointer, so as to avoid
7 a layer of malloc() and free().
9 Operations on the osrfList are restricted so as not to leave NULL pointers in the middle.
12 #include <opensrf/string_array.h>
15 @brief Create and initialize an osrfStringArray.
16 @param size How many strings to allow space for initially.
17 @return Pointer to a newly created osrfStringArray.
19 If the size parameter is zero, osrfNewStringArray uses a default value. If the initial
20 allocation isn't big enough, more space will be added as needed.
22 The calling code is responsible for freeing the osrfStringArray by calling
23 osrfStringArrayFree().
25 osrfStringArray* osrfNewStringArray(int size) {
26 if(size > STRING_ARRAY_MAX_SIZE)
27 osrfLogError( OSRF_LOG_MARK, "osrfNewStringArray size is too large");
30 OSRF_MALLOC(arr, sizeof(osrfStringArray));
35 arr->list.freeItem = NULL;
37 arr->list.arrsize = 16;
39 arr->list.arrsize = size;
40 OSRF_MALLOC( arr->list.arrlist, arr->list.arrsize * sizeof(void*) );
42 // Nullify all pointers in the array
45 for( i = 0; i < arr->list.arrsize; ++i )
46 arr->list.arrlist[ i ] = NULL;
48 osrfListSetDefaultFree(&arr->list);
53 @brief Add a string to the end of an osrfStringArray.
54 @param arr Pointer to the osrfStringArray to which the string will be added.
55 @param string Pointer to the character string to be added.
57 The string stored is a copy; the original is not affected.
59 If either parameter is NULL, nothing happens.
61 void osrfStringArrayAdd( osrfStringArray* arr, const char* string ) {
62 if(arr == NULL || string == NULL ) return;
63 if( arr->list.size > STRING_ARRAY_MAX_SIZE )
64 osrfLogError( OSRF_LOG_MARK, "osrfStringArrayAdd size is too large" );
65 osrfListPush(&arr->list, strdup(string));
66 arr->size = arr->list.size;
70 @brief Return a pointer to the string stored at a specified position in an osrfStringArray.
71 @param arr Pointer to the osrfStringArray.
72 @param index A zero-based index into the array
73 @return A pointer to the string stored at the specified position. if it exists;
76 The calling code should treat the returned pointer as if it were const. Some day,
79 char* osrfStringArrayGetString( osrfStringArray* arr, int index ) {
81 return OSRF_LIST_GET_INDEX(&arr->list, index);
85 @brief Free an osrfStringArray, and all the strings inside it.
86 @param arr Pointer to the osrfStringArray to be freed.
88 void osrfStringArrayFree(osrfStringArray* arr) {
90 // This function is a sleazy hack designed to avoid the
91 // need to duplicate the code in osrfListFree(). It
94 // 1. The osrfList is the first member of an
95 // osrfStringArray. C guarantees that a pointer
96 // to the one is also a pointer to the other.
98 // 2. The rest of the osrfStringArray owns no memory
99 // and requires no special attention when freeing.
101 // If these facts ever cease to be true, we'll have to
102 // revisit this function.
104 osrfListFree( (osrfList*) arr );
108 @brief Determine whether an osrfStringArray contains a specified string.
109 @param arr Pointer to the osrfStringArray.
110 @param string Pointer to the string to be sought.
111 @return A boolean: 1 if the string is present in the osrfStringArray, or 0 if it isn't.
113 The search is case-sensitive.
115 int osrfStringArrayContains(
116 const osrfStringArray* arr, const char* string ) {
117 if(!(arr && string)) return 0;
119 for( i = 0; i < arr->size; i++ ) {
120 char* str = OSRF_LIST_GET_INDEX(&arr->list, i);
121 if(str && !strcmp(str, string))
129 @brief Remove the first occurrence, if any, of a specified string from an osrfStringArray.
130 @param arr Pointer to the osrfStringArray.
131 @param tstr Pointer to a string to be removed.
133 void osrfStringArrayRemove( osrfStringArray* arr, const char* tstr ) {
134 if(!(arr && tstr)) return;
137 int removed = 0; // boolean
139 for( i = 0; i < arr->size; i++ ) {
140 /* find and remove the string */
141 str = OSRF_LIST_GET_INDEX(&arr->list, i);
142 if(str && !strcmp(str, tstr)) {
143 osrfListRemove(&arr->list, i);
150 return; // Nothing was removed
152 /* disable automatic item freeing on delete, and shift
153 * items down in the array to fill in the gap
155 arr->list.freeItem = NULL;
156 for( ; i < arr->size - 1; i++ )
157 osrfListSet(&arr->list, OSRF_LIST_GET_INDEX(&arr->list, i+1), i);
159 /* remove the last item since it was shifted up */
160 osrfListRemove(&arr->list, i);
162 /* re-enable automatic item freeing in delete */
163 osrfListSetDefaultFree(&arr->list);
168 @brief Tokenize a string into an array of substrings separated by a specified delimiter.
169 @param src A pointer to the string to be parsed.
170 @param delim The delimiter character.
171 @return Pointer to a newly constructed osrfStringArray containing the tokens.
173 A series of consecutive delimiter characters is treated as a single separator.
175 The input string is left unchanged.
177 The calling code is responsible for freeing the osrfStringArray by calling
178 osrfStringArrayFree().
180 osrfStringArray* osrfStringArrayTokenize( const char* src, char delim )
182 // Take the length so that we know how big a buffer we need,
183 // in the worst case. Estimate the number of tokens, assuming
184 // 5 characters per token, and add a few for a pad.
186 if( NULL == src || '\0' == *src ) // Got nothing?
187 return osrfNewStringArray( 1 ); // Return empty array
189 size_t src_len = strlen( src );
190 size_t est_count = src_len / 6 + 5;
191 int in_token = 0; // boolean
192 char buf[ src_len + 1 ];
194 osrfStringArray* arr = osrfNewStringArray( est_count );
197 if( in_token ) { // We're building a token
198 if( *src == delim ) {
200 osrfStringArrayAdd( arr, buf );
203 else if( '\0' == *src ) {
205 osrfStringArrayAdd( arr, buf );
212 else { // We're between tokens
213 if( *src == delim ) {
216 else if( '\0' == *src ) {
220 out = buf; // Start the next one