Thread: [Refdb-cvs] CVS: refdb/src strfncs.c,1.15.2.4,1.15.2.5
Status: Beta
Brought to you by:
mhoenicka
From: Markus H. <mho...@us...> - 2005-12-28 01:31:40
|
Update of /cvsroot/refdb/refdb/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28264 Modified Files: Tag: Release_0_9_5_stable strfncs.c Log Message: count_the_flowers(), replace_char_string(), sgml_entitize(): now handle multibyte letters Index: strfncs.c =================================================================== RCS file: /cvsroot/refdb/refdb/src/strfncs.c,v retrieving revision 1.15.2.4 retrieving revision 1.15.2.5 diff -u -U2 -r1.15.2.4 -r1.15.2.5 --- strfncs.c 11 Oct 2005 20:35:02 -0000 1.15.2.4 +++ strfncs.c 28 Dec 2005 01:31:31 -0000 1.15.2.5 @@ -38,5 +38,5 @@ /* forward declarations of local functions */ -static size_t count_the_flowers(char *buffer, char letter); +static size_t count_the_flowers(char *buffer, char *letter, size_t len); static int is_ip(char *address); @@ -680,5 +680,5 @@ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ count_the_flowers() counts the number of occurrences of letter in - buffer + buffer. letter may be a multibyte character static size_t count_the_flowers returns the number of occurrences of letter @@ -688,39 +688,55 @@ char* buffer pointer to the string to scan - char letter the letter to locate + char *letter ptr to the letter to locate + + size_t len the number of bytes that represent letter ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ -static size_t count_the_flowers(char *buffer, char letter) { +static size_t count_the_flowers(char *buffer, char *letter, size_t len) { size_t numletters = 0; - int i; + size_t i; + size_t buflen; + + buflen = strlen(buffer); - for (i = 0; i < strlen(buffer); i++) { - if (buffer[i] == letter) { - numletters++; + if (buflen >= len) { + buflen = buflen-len+1; + + for (i = 0; i < buflen; i++) { + if (!memcmp(buffer+i, letter, len)) { + numletters++; + } } + return numletters; + } + else { + /* string too short to hold the flower */ + return 0; } - return numletters; } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - replace_char_string() replaces the character that buffer points to + replace_char_string() replaces the letter that buffer points to with an insert of arbitrary size. The calling function is responsible to allocate enough space for buffer - so the insertion can be safely executed. + so the insertion can be safely executed. The letter may + be a multibyte character. void replace_char_string has no return value - char* buffer pointer to the string that will be extended + char* buffer pointer to the null-terminated string that will be extended char* insert pointer to the string that will be inserted + size_t len length of the letter to replace in bytes + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ -void replace_char_string(char *buffer, char *insert) { - int len_insert; +void replace_char_string(char *buffer, char *insert, size_t len) { + size_t insert_len; - len_insert = strlen(insert); + insert_len = strlen(insert); - memmove(buffer+len_insert, buffer+1, strlen(buffer)); - memcpy(buffer, insert, len_insert); + memmove(buffer+insert_len, buffer+len, strlen(buffer+len)+1); + memcpy(buffer, insert, insert_len); } @@ -789,9 +805,12 @@ struct charent* the_ents; - struct charent default_ents[4] = { /* default lookup table for entity replacements */ - {(int)'&', "&"}, - {(int)'<', "<"}, - {(int)'>', ">"}, - {0, ""} + struct charent default_ents[7] = { /* default lookup table for entity replacements */ + {"&", 1, "&"}, + {"<", 1, "<"}, + {">", 1, ">"}, + {{226, 128, 148, 0}, 3, "—"}, /* 0x2014 */ + {{226, 128, 152, 0}, 3, "‘"}, /* 0x2018 */ + {{226, 128, 153, 0}, 3, "’"}, /* 0x2019 */ + {"", 0, ""} }; @@ -803,8 +822,8 @@ } - while (the_ents[i].character != 0) { + while (the_ents[i].len != 0) { - numents = count_the_flowers(*buffer, the_ents[i].character); -/* printf("%d\n", numents); */ + numents = count_the_flowers(*buffer, the_ents[i].letter, the_ents[i].len); +/* printf("numents for %s went to %d\n", the_ents[i].letter, numents); */ if (numents > 0) { @@ -816,5 +835,5 @@ } - token = strchr(*buffer, the_ents[i].character); + token = strstr(*buffer, the_ents[i].letter); the_end = &((*buffer)[strlen(*buffer)-1]); @@ -824,5 +843,5 @@ char* next_sp; -/* printf("token went to:%s<<\n", token); */ +/* printf("token went to:%d<<letter went to %s\n", (int)*token, the_ents[i].letter); */ /* replace ampersand only if it does not start an entity */ /* get pointers to the next ampersand and semicolon, if any, @@ -832,8 +851,8 @@ next_sp = strchr(token+1, (int)' '); - if (the_ents[i].character != '&' || compare_ptr(&next_sc, &next_amp) != -1 || compare_ptr(&next_sp, &next_sc) != 1) { - replace_char_string(token, the_ents[i].entity); + if (*(the_ents[i].letter) != '&' || compare_ptr(&next_sc, &next_amp) != -1 || compare_ptr(&next_sp, &next_sc) != 1) { + replace_char_string(token, the_ents[i].entity, the_ents[i].len); } - token = (token + 1 > the_end + 5) ? NULL:strchr(token+1, the_ents[i].character); + token = (token + the_ents[i].len > the_end) ? NULL:strstr(token+the_ents[i].len, the_ents[i].letter); } } @@ -884,5 +903,5 @@ expand with the value of HOME. This way we can call this function with paths that did not go through shell expansion */ - if ((numtilde = count_the_flowers(the_path, '~')) > 1) { + if ((numtilde = count_the_flowers(the_path, "~", 1)) > 1) { free(homepath); free(temp); @@ -892,5 +911,5 @@ myhome = getenv("HOME"); strcpy(homepath, the_path); - replace_char_string(strchr(homepath, (int)'~'), myhome); + replace_char_string(strchr(homepath, (int)'~'), myhome, 1); } else { |