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 const char* osrfStringArrayGetString( const osrfStringArray* arr, int index ) {
78 return OSRF_LIST_GET_INDEX(&arr->list, index);
82 @brief Free an osrfStringArray, and all the strings inside it.
83 @param arr Pointer to the osrfStringArray to be freed.
85 void osrfStringArrayFree(osrfStringArray* arr) {
87 // This function is a sleazy hack designed to avoid the
88 // need to duplicate the code in osrfListFree(). It
91 // 1. The osrfList is the first member of an
92 // osrfStringArray. C guarantees that a pointer
93 // to the one is also a pointer to the other.
95 // 2. The rest of the osrfStringArray owns no memory
96 // and requires no special attention when freeing.
98 // If these facts ever cease to be true, we'll have to
99 // revisit this function.
101 osrfListFree( (osrfList*) arr );
105 @brief Determine whether an osrfStringArray contains a specified string.
106 @param arr Pointer to the osrfStringArray.
107 @param string Pointer to the string to be sought.
108 @return A boolean: 1 if the string is present in the osrfStringArray, or 0 if it isn't.
110 The search is case-sensitive.
112 int osrfStringArrayContains(
113 const osrfStringArray* arr, const char* string ) {
114 if(!(arr && string)) return 0;
116 for( i = 0; i < arr->size; i++ ) {
117 char* str = OSRF_LIST_GET_INDEX(&arr->list, i);
118 if(str && !strcmp(str, string))
126 @brief Remove the first occurrence, if any, of a specified string from an osrfStringArray.
127 @param arr Pointer to the osrfStringArray.
128 @param tstr Pointer to a string to be removed.
130 void osrfStringArrayRemove( osrfStringArray* arr, const char* tstr ) {
131 if(!(arr && tstr)) return;
134 int removed = 0; // boolean
136 for( i = 0; i < arr->size; i++ ) {
137 /* find and remove the string */
138 str = OSRF_LIST_GET_INDEX(&arr->list, i);
139 if(str && !strcmp(str, tstr)) {
140 osrfListRemove(&arr->list, i);
147 return; // Nothing was removed
149 /* disable automatic item freeing on delete, and shift
150 * items down in the array to fill in the gap
152 arr->list.freeItem = NULL;
153 for( ; i < arr->size - 1; i++ )
154 osrfListSet(&arr->list, OSRF_LIST_GET_INDEX(&arr->list, i+1), i);
156 /* remove the last item since it was shifted up */
157 osrfListRemove(&arr->list, i);
159 /* re-enable automatic item freeing in delete */
160 osrfListSetDefaultFree(&arr->list);
165 @brief Tokenize a string into an array of substrings separated by a specified delimiter.
166 @param src A pointer to the string to be parsed.
167 @param delim The delimiter character.
168 @return Pointer to a newly constructed osrfStringArray containing the tokens.
170 A series of consecutive delimiter characters is treated as a single separator.
172 The input string is left unchanged.
174 The calling code is responsible for freeing the osrfStringArray by calling
175 osrfStringArrayFree().
177 osrfStringArray* osrfStringArrayTokenize( const char* src, char delim )
179 // Take the length so that we know how big a buffer we need,
180 // in the worst case. Estimate the number of tokens, assuming
181 // 5 characters per token, and add a few for a pad.
183 if( NULL == src || '\0' == *src ) // Got nothing?
184 return osrfNewStringArray( 1 ); // Return empty array
186 size_t src_len = strlen( src );
187 size_t est_count = src_len / 6 + 5;
188 int in_token = 0; // boolean
189 char buf[ src_len + 1 ];
191 osrfStringArray* arr = osrfNewStringArray( est_count );
194 if( in_token ) { // We're building a token
195 if( *src == delim ) {
197 osrfStringArrayAdd( arr, buf );
200 else if( '\0' == *src ) {
202 osrfStringArrayAdd( arr, buf );
209 else { // We're between tokens
210 if( *src == delim ) {
213 else if( '\0' == *src ) {
217 out = buf; // Start the next one