Fixed search with different display and source charsets

This commit is contained in:
Slava Zanko 2009-04-21 14:19:42 +03:00
parent 356b501bdc
commit 76bbeb60b2
3 changed files with 110 additions and 43 deletions

View File

@ -102,27 +102,68 @@ editcmd_get_str_nlen(const char*str, int byte_len)
g_free(tmp); g_free(tmp);
return ret; return ret;
} }
/* Convert to display codepage
* then lowercase string.
* returns newly allocated string
*/
static gchar *
my_lower_case_str (char *str)
{
GString *buff;
gchar *tmp, *tmp_dup, *dup;
int len;
buff = str_convert_to_display (str);
dup = tmp_dup = g_strdup(buff->str);
tmp = buff->str;
len = buff->len;
while (str_tolower (tmp, &dup, &len))
tmp+=str_length_char(tmp);
g_string_free(buff, TRUE);
buff = str_convert_to_input (tmp_dup);
g_free(tmp_dup);
tmp = buff->str;
g_string_free(buff, FALSE);
return tmp;
}
static gchar * static gchar *
my_lower_case (char *ch) my_lower_case (char *ch)
{ {
gchar *tmp, *tmp1; gchar *tmp, *tmp_dup, *dup;
size_t size = 7; GString *buff;
int len;
tmp = ch; buff = str_nconvert_to_display (ch, 6);
tmp1 = tmp = g_malloc0(sizeof(gchar)*7); dup = tmp_dup = g_malloc0(sizeof(gchar)*7);
str_tolower (ch, &tmp, &size); tmp = buff->str;
return tmp1; len = buff->len;
str_tolower (tmp, &dup, &len);
g_string_free(buff, TRUE);
buff = str_convert_to_input (tmp_dup);
g_free(tmp_dup);
tmp = buff->str;
g_string_free(buff, FALSE);
return tmp;
} }
static gchar * static gchar *
my_lower_case_static (char *ch) my_lower_case_static (char *ch)
{ {
static gchar tmp[7]; static gchar tmp[7];
gchar *tmp2 = tmp; gchar *tmp1;
size_t size=7;
memset(tmp,0,7); tmp1 = my_lower_case (ch);
str_tolower (ch, &tmp2, &size); memcpy(tmp,tmp1,7);
g_free(tmp1);
return tmp; return tmp;
} }
@ -1492,6 +1533,11 @@ edit_find_string (long start, unsigned char *exp, int *len, long last_byte, edit
char *tmp_exp4; char *tmp_exp4;
gchar *c; gchar *c;
gchar *lower_exp;
lower_exp = my_lower_case_str(exp);
for (p = 0; p < l; p++) /* count conversions... */ for (p = 0; p < l; p++) /* count conversions... */
if (exp[p] == '%') if (exp[p] == '%')
if (exp[++p] != '%') /* ...except for "%%" */ if (exp[++p] != '%') /* ...except for "%%" */
@ -1506,23 +1552,16 @@ edit_find_string (long start, unsigned char *exp, int *len, long last_byte, edit
if (replace_scanf) { if (replace_scanf) {
unsigned char e[MAX_REPL_LEN]; unsigned char e[MAX_REPL_LEN];
if (n >= NUM_REPL_ARGS) if (n >= NUM_REPL_ARGS)
{
g_free(lower_exp);
return -3; return -3;
}
if (replace_case) { if (replace_case) {
for (p = start; p < last_byte && p < start + MAX_REPL_LEN; p++) for (p = start; p < last_byte && p < start + MAX_REPL_LEN; p++)
buf[p - start] = *(*get_byte) (data, p); buf[p - start] = *(*get_byte) (data, p);
} else { } else {
tmp_exp3 = exp; exp = lower_exp;
tmp_exp4 = (char *)tmp_exp3;
tmp_exp1 = tmp_exp2 = g_strdup((gchar *)exp);
tmp_len = str_length((char *)exp);
while (str_tolower(tmp_exp1, (char **) &tmp_exp4, &tmp_len))
tmp_exp1+=str_length_char(tmp_exp1);
g_free(tmp_exp2);
p = start; p = start;
while(p < last_byte && p < start + MAX_REPL_LEN) while(p < last_byte && p < start + MAX_REPL_LEN)
{ {
@ -1543,6 +1582,7 @@ edit_find_string (long start, unsigned char *exp, int *len, long last_byte, edit
if (n == sscanf ((char *) buf, (char *) exp, SCANF_ARGS)) { if (n == sscanf ((char *) buf, (char *) exp, SCANF_ARGS)) {
if (*((int *) sargs[n])) { if (*((int *) sargs[n])) {
*len = *((int *) sargs[n]); *len = *((int *) sargs[n]);
g_free(lower_exp);
return start; return start;
} }
} }
@ -1594,6 +1634,7 @@ edit_find_string (long start, unsigned char *exp, int *len, long last_byte, edit
if (found_start <= -2) { /* regcomp/regexec error */ if (found_start <= -2) { /* regcomp/regexec error */
*len = 0; *len = 0;
g_free(lower_exp);
return -3; return -3;
} }
else if (found_start == -1) /* not found: try next line */ else if (found_start == -1) /* not found: try next line */
@ -1605,10 +1646,16 @@ edit_find_string (long start, unsigned char *exp, int *len, long last_byte, edit
continue; continue;
} }
else /* found */ else /* found */
{
g_free(lower_exp);
return (start + offset - q + found_start); return (start + offset - q + found_start);
}
} }
if (once_only) if (once_only)
{
g_free(lower_exp);
return -2; return -2;
}
if (buf[q - 1] != '\n') { /* incomplete line: try to recover */ if (buf[q - 1] != '\n') { /* incomplete line: try to recover */
buf = mbuf + MAX_REPL_LEN / 2; buf = mbuf + MAX_REPL_LEN / 2;
@ -1646,21 +1693,21 @@ edit_find_string (long start, unsigned char *exp, int *len, long last_byte, edit
q++; q++;
} }
if (f == 0) if (f == 0)
{
g_free(lower_exp);
return p; return p;
}
} }
if (once_only) if (once_only)
{
g_free(lower_exp);
return -2; return -2;
}
p+= str_length_char(c); p+= str_length_char(c);
} }
} else { } else {
tmp_exp3 = exp; exp = lower_exp;
tmp_exp1 = tmp_exp2 = g_strdup((gchar *)exp);
tmp_len = strlen((char *) exp);
tmp_exp4 = (char *) tmp_exp3;
while (str_tolower(tmp_exp1, &tmp_exp4, &tmp_len))
tmp_exp1+=str_length_char(tmp_exp1);
g_free(tmp_exp2);
p = start; p = start;
while(p < last_byte -l) while(p < last_byte -l)
{ {
@ -1681,17 +1728,22 @@ edit_find_string (long start, unsigned char *exp, int *len, long last_byte, edit
q++; q++;
} }
if (f == 0){ if (f == 0){
g_free(lower_exp);
return p; return p;
} }
} }
if (once_only) if (once_only)
{
g_free(lower_exp);
return -2; return -2;
}
p+= str_length_char(c); p+= str_length_char(c);
} }
} }
} }
g_free(lower_exp);
return -2; return -2;
} }
@ -2157,7 +2209,7 @@ void edit_search_cmd (WEdit * edit, int again)
#ifdef HAVE_CHARSET #ifdef HAVE_CHARSET
if (exp && *exp){ if (exp && *exp){
GString *tmp = str_convert_from_input (exp); GString *tmp = str_convert_to_input (exp);
if (tmp && tmp->len){ if (tmp && tmp->len){
g_free(exp); g_free(exp);
exp = tmp->str; exp = tmp->str;

View File

@ -178,7 +178,6 @@ init_translation_table (int cpsource, int cpdisplay)
{ {
int i; int i;
iconv_t cd; iconv_t cd;
const char *cpsour, *cpdisp;
/* Fill inpit <-> display tables */ /* Fill inpit <-> display tables */
@ -186,6 +185,7 @@ init_translation_table (int cpsource, int cpdisplay)
for (i = 0; i <= 255; ++i) { for (i = 0; i <= 255; ++i) {
conv_displ[i] = i; conv_displ[i] = i;
conv_input[i] = i; conv_input[i] = i;
cp_source = cp_display;
} }
return NULL; return NULL;
} }
@ -194,16 +194,15 @@ init_translation_table (int cpsource, int cpdisplay)
conv_displ[i] = i; conv_displ[i] = i;
conv_input[i] = i; conv_input[i] = i;
} }
cp_source = (char *) codepages[cpsource].id;
cp_display = cpsour = (char *) codepages[cpsource].id; cp_display = (char *) codepages[cpdisplay].id;
cp_source = cpdisp = (char *) codepages[cpdisplay].id;
/* display <- inpit table */ /* display <- inpit table */
cd = iconv_open (cpdisp, cpsour); cd = iconv_open (cp_display, cp_source);
if (cd == (iconv_t) - 1) { if (cd == (iconv_t) - 1) {
g_snprintf (errbuf, sizeof (errbuf), g_snprintf (errbuf, sizeof (errbuf),
_("Cannot translate from %s to %s"), cpsour, cpdisp); _("Cannot translate from %s to %s"), cp_source, cp_display);
return errbuf; return errbuf;
} }
@ -214,10 +213,10 @@ init_translation_table (int cpsource, int cpdisplay)
/* inpit <- display table */ /* inpit <- display table */
cd = iconv_open (cpsour, cpdisp); cd = iconv_open (cp_source, cp_display);
if (cd == (iconv_t) - 1) { if (cd == (iconv_t) - 1) {
g_snprintf (errbuf, sizeof (errbuf), g_snprintf (errbuf, sizeof (errbuf),
_("Cannot translate from %s to %s"), cpdisp, cpsour); _("Cannot translate from %s to %s"), cp_display, cp_source);
return errbuf; return errbuf;
} }
@ -246,6 +245,13 @@ convert_to_display (char *str)
GString * GString *
str_convert_to_display (char *str) str_convert_to_display (char *str)
{
return str_nconvert_to_display (str, -1);
}
GString *
str_nconvert_to_display (char *str, int len)
{ {
GString *buff; GString *buff;
GIConv conv; GIConv conv;
@ -256,10 +262,10 @@ str_convert_to_display (char *str)
if (cp_display == cp_source) if (cp_display == cp_source)
return g_string_new(str); return g_string_new(str);
conv = str_crt_conv_from (cp_display); conv = str_crt_conv_from (cp_source);
buff = g_string_new(""); buff = g_string_new("");
str_convert (conv, str, buff); str_nconvert (conv, str, len, buff);
return buff; return buff;
} }
@ -276,7 +282,13 @@ convert_from_input (char *str)
} }
GString * GString *
str_convert_from_input (char *str) str_convert_to_input (char *str)
{
return str_nconvert_to_input (str, -1);
}
GString *
str_nconvert_to_input (char *str, int len)
{ {
GString *buff; GString *buff;
GIConv conv; GIConv conv;
@ -287,10 +299,10 @@ str_convert_from_input (char *str)
if (cp_display == cp_source) if (cp_display == cp_source)
return g_string_new(str); return g_string_new(str);
conv = str_crt_conv_to (cp_display); conv = str_crt_conv_to (cp_source);
buff = g_string_new(""); buff = g_string_new("");
str_convert (conv, str, buff); str_nconvert (conv, str, len, buff);
return buff; return buff;
} }

View File

@ -51,8 +51,11 @@ int convert_from_8bit_to_utf_c (const char input_char);
*/ */
int convert_from_8bit_to_utf_c2 (const char input_char); int convert_from_8bit_to_utf_c2 (const char input_char);
GString *str_convert_from_input (char *str); GString *str_convert_to_input (char *str);
GString *str_nconvert_to_input (char *str, int len);
GString *str_convert_to_display (char *str); GString *str_convert_to_display (char *str);
GString *str_nconvert_to_display (char *str, int len);
/* Convert single characters */ /* Convert single characters */
static inline int static inline int