/** * @function ferite_strcasecmp * @declaration int ferite_strcasecmp( char *left, char *right ) * @brief Do a case insensitive strcmp on the two strings. * @param char *left * @param char *right */ int ferite_strcasecmp( char *left, char *right ) { int retv = 0; char *left_copy, *right_copy; FE_ENTER_FUNCTION; left_copy = fstrdup(left); right_copy = fstrdup(right); ferite_lowercase( left_copy ); ferite_lowercase( right_copy ); retv = strcmp( left_copy, right_copy ); ffree_ngc( left_copy ); ffree_ngc( right_copy ); FE_LEAVE_FUNCTION( retv ); }
void output_printf( int type, char *format, ... ) { va_list arguments; va_start( arguments, format ); if( output_closure ) { char *msg = NULL; FeriteBuffer *output_buffer = NULL; FeriteVariable **params = NULL; FeriteFunction *function = ferite_object_get_function( output_script, output_closure, "invoke" ); FeriteVariable *return_value = NULL; output_buffer = ferite_buffer_new(output_script, 0); ferite_buffer_vprintf( output_script, output_buffer, format, &arguments ); msg = ferite_buffer_get( output_script, output_buffer, NULL ); params = ferite_create_parameter_list_from_data( output_script, "lc", type, msg, NULL ); return_value = ferite_call_function( output_script, output_closure, NULL, function, params ); if( return_value ) { ferite_variable_destroy( output_script, return_value ); } ferite_delete_parameter_list( output_script, params ); ferite_buffer_delete(output_script, output_buffer); ffree_ngc(msg); } else { vprintf(format, arguments); printf("\n"); } va_end(arguments); }
/** * @function ferite_lowercase * @declaration char *ferite_lowercase( char *str ) * @brief Convert a string to lowercase * @param char *str The string to convert * @return A string that is a lowercase version of the passed string * @warning This function modifies the calling string. */ char *ferite_lowercase( char *str ) { unsigned int i, strt = 0, in_quote = 0; char *tmpstr = NULL; FE_ENTER_FUNCTION; if( str ) { tmpstr = fmalloc_ngc( strlen(str)+1 ); memset( tmpstr, '\0', strlen(str)+1 ); for( i = 0; i < strlen(str); i++ ) { if( str[i] == '\"' ) in_quote = (in_quote == 0 ? 1 : 0); if( !in_quote ) { if( isupper(*(str+i)) ) tmpstr[strt++] = str[i]+32; else tmpstr[strt++] = str[i]; } else tmpstr[strt++] = str[i]; } strcpy( str, tmpstr ); ffree_ngc(tmpstr); FE_LEAVE_FUNCTION( str ); } FE_LEAVE_FUNCTION( NULL ); }
/** * @function ferite_replace_string * @declaration char *ferite_replace_string( char *str, char *pattern, char *data ) * @brief Replace all occurances of a pattern in a string with another * @param char *str The string to scan * @param char *pattern The pattern to look for * @param char *datat The data to replace with * @return A string that has had the replacements * @warning It is the responsibility of the calling function to free the returned string. */ char *ferite_replace_string( char *str, char *pattern, char *data ) { size_t i = 0, start = 0; char *rstr = NULL, *tmpbuf = NULL; FE_ENTER_FUNCTION; if( str && pattern && data ) { /* empty string -- nothing to replace */ if( !str[0] ) { FE_LEAVE_FUNCTION( fstrdup(str) ); } /* empty pattern -- nothing to replace */ if( !pattern[0] ) { FE_LEAVE_FUNCTION( fstrdup(str) ); } if( !data[0] ) /* empty replacement -- string won't grow */ rstr = fcalloc_ngc( strlen( str ) + 1, sizeof(char) ); else /* none of the strings can have length zero now */ rstr = fcalloc_ngc( strlen( str ) * strlen( pattern ) * strlen( data ) + 1, sizeof(char) ); FUD(("replace_str: replace \"%s\" with \"%s\"\n", pattern, data )); while( ((i=ferite_find_string( str+start, pattern ))+1) ) { strncat( rstr, str+start, i ); strcat( rstr, data ); start = i + start + strlen(pattern); } strcat( rstr, str + start ); tmpbuf = fstrdup( rstr ); ffree_ngc( rstr ); FE_LEAVE_FUNCTION( tmpbuf ); } FE_LEAVE_FUNCTION( NULL ); }
/** * @function ferite_strip_whitespace * @declaration char *ferite_strip_whitespace( char *str ) * @brief Create a new string based upon the data with the whitespace removed * @param char *str The string to remove the whitespace from * @return A new string without whitespace * @description This function will remove whitespace but not from within double quotes. * @warning This function modifies the calling string. */ char *ferite_strip_whitespace( char *str ) { unsigned int i, strt = 0, in_quote = 0; char *tmpstr = NULL; FE_ENTER_FUNCTION; if( str ) { tmpstr = fmalloc_ngc( strlen(str)+1 ); memset( tmpstr, '\0', strlen(str)+1 ); for( i = 0; i < strlen(str); i++ ) { if( str[i] == '\"' ) in_quote = (in_quote == 0 ? 1 : 0); if( in_quote || ! isspace(*(str+i)) ) tmpstr[strt++] = str[i]; } strcpy( str, tmpstr ); ffree_ngc(tmpstr); FE_LEAVE_FUNCTION( str ); } FE_LEAVE_FUNCTION( NULL ); }