Search engine: fix return number of found bytes

Some reidentation of code
This commit is contained in:
Slava Zanko 2009-04-23 16:26:14 +03:00
parent a3e6a1147d
commit d9500ec836
2 changed files with 22 additions and 19 deletions

View File

@ -68,9 +68,9 @@ mc_search__recode_str (const char *str, gsize str_len, const char *charset_from,
GIConv conv; GIConv conv;
if (!strcmp(charset_to, charset_from)){ if (!strcmp (charset_to, charset_from)) {
*bytes_written = str_len; *bytes_written = str_len;
return g_strndup(str,str_len); return g_strndup (str, str_len);
} }
conv = g_iconv_open (charset_to, charset_from); conv = g_iconv_open (charset_to, charset_from);
@ -95,7 +95,7 @@ mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len
if (converted_str == NULL) if (converted_str == NULL)
return NULL; return NULL;
tmp_len = converted_str_len+1; tmp_len = converted_str_len + 1;
tmp_str3 = tmp_str1 = g_strdup (converted_str); tmp_str3 = tmp_str1 = g_strdup (converted_str);
@ -146,7 +146,8 @@ mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static mc_search_cond_t * static mc_search_cond_t *
mc_search__cond_struct_new (const char *str, gsize str_len, const char *charset, gboolean case_sentitive) mc_search__cond_struct_new (const char *str, gsize str_len, const char *charset,
gboolean case_sentitive)
{ {
mc_search_cond_t *mc_search_cond; mc_search_cond_t *mc_search_cond;
mc_search_cond = g_malloc0 (sizeof (mc_search_cond_t)); mc_search_cond = g_malloc0 (sizeof (mc_search_cond_t));
@ -163,7 +164,8 @@ mc_search__cond_struct_new (const char *str, gsize str_len, const char *charset,
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static GPtrArray * static GPtrArray *
mc_search__conditions_new (const char *str, gsize str_len, gboolean all_charsets, gboolean case_sentitive) mc_search__conditions_new (const char *str, gsize str_len, gboolean all_charsets,
gboolean case_sentitive)
{ {
GPtrArray *ret; GPtrArray *ret;
ret = g_ptr_array_new (); ret = g_ptr_array_new ();
@ -255,13 +257,13 @@ mc_search__normal_found_cond (mc_search_t * mc_search, int current_chr, gsize se
for (loop1 = 0; loop1 < mc_search->conditions->len; loop1++) { for (loop1 = 0; loop1 < mc_search->conditions->len; loop1++) {
mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (mc_search->conditions, loop1); mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (mc_search->conditions, loop1);
if (search_pos > mc_search_cond->len-1) if (search_pos > mc_search_cond->len - 1)
continue; continue;
if (mc_search->is_case_sentitive) { if (mc_search->is_case_sentitive) {
if ((char) current_chr == mc_search_cond->str->str[search_pos]) if ((char) current_chr == mc_search_cond->str->str[search_pos])
return (search_pos == return (search_pos ==
mc_search_cond->len-1) ? COND__FOUND_CHAR_LAST : COND__FOUND_CHAR; mc_search_cond->len - 1) ? COND__FOUND_CHAR_LAST : COND__FOUND_CHAR;
} else { } else {
GString *upp, *low; GString *upp, *low;
upp = (mc_search_cond->upper) ? mc_search_cond->upper : mc_search_cond->str; upp = (mc_search_cond->upper) ? mc_search_cond->upper : mc_search_cond->str;
@ -270,7 +272,7 @@ mc_search__normal_found_cond (mc_search_t * mc_search, int current_chr, gsize se
if (((char) current_chr == upp->str[search_pos]) if (((char) current_chr == upp->str[search_pos])
|| ((char) current_chr == low->str[search_pos])) || ((char) current_chr == low->str[search_pos]))
return (search_pos == return (search_pos ==
mc_search_cond->len-1) ? COND__FOUND_CHAR_LAST : COND__FOUND_CHAR; mc_search_cond->len - 1) ? COND__FOUND_CHAR_LAST : COND__FOUND_CHAR;
} }
} }
return COND__NOT_ALL_FOUND; return COND__NOT_ALL_FOUND;
@ -311,7 +313,7 @@ mc_search__run_normal (mc_search_t * mc_search, const void *user_data,
case COND__FOUND_CHAR_LAST: case COND__FOUND_CHAR_LAST:
mc_search->normal_offset = current_pos; mc_search->normal_offset = current_pos;
*found_len = search_pos; *found_len = search_pos + 1;
return TRUE; return TRUE;
break; break;
@ -384,7 +386,7 @@ mc_search_free (mc_search_t * mc_search)
g_free (mc_search->error_str); g_free (mc_search->error_str);
if (mc_search->conditions) if (mc_search->conditions)
mc_search__conditions_free (mc_search->conditions); mc_search__conditions_free (mc_search->conditions);
g_free (mc_search); g_free (mc_search);
} }
@ -394,14 +396,14 @@ gboolean
mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search, gsize end_search, mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search, gsize end_search,
gsize * found_len) gsize * found_len)
{ {
gboolean ret=FALSE; gboolean ret = FALSE;
if (!mc_search) if (!mc_search)
return FALSE; return FALSE;
if (!mc_search->conditions) if (!mc_search->conditions)
mc_search->conditions = mc_search__conditions_new (mc_search->original, mc_search->original_len, mc_search->conditions =
mc_search->is_all_charsets, mc_search__conditions_new (mc_search->original, mc_search->original_len,
mc_search->is_case_sentitive); mc_search->is_all_charsets, mc_search->is_case_sentitive);
mc_search->error = MC_SEARCH_E_OK; mc_search->error = MC_SEARCH_E_OK;
if (mc_search->error_str) { if (mc_search->error_str) {
@ -421,8 +423,8 @@ mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_searc
case MC_SEARCH_T_GLOB: case MC_SEARCH_T_GLOB:
default: default:
mc_search->error = MC_SEARCH_E_INPUT; mc_search->error = MC_SEARCH_E_INPUT;
mc_search->error_str = g_strdup (_(STR_E_UNKNOWN_TYPE)); mc_search->error_str = g_strdup (_(STR_E_UNKNOWN_TYPE));
ret = FALSE; ret = FALSE;
break; break;
} }
return ret; return ret;

View File

@ -3,7 +3,7 @@
#include <config.h> #include <config.h>
#include "../src/global.h" /* <glib.h> */ #include "../src/global.h" /* <glib.h> */
/*** typedefs(not structures) and defined constants **********************************************/ /*** typedefs(not structures) and defined constants **********************************************/
@ -93,6 +93,7 @@ mc_search_t *mc_search_new (const gchar * original, gsize original_len);
void mc_search_free (mc_search_t * mc_search); void mc_search_free (mc_search_t * mc_search);
gboolean mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search, gsize end_search, gsize * founded_len); gboolean mc_search_run (mc_search_t * mc_search, const void *user_data, gsize start_search,
gsize end_search, gsize * founded_len);
#endif #endif