diff options
author | Xavier Roche <xroche@users.noreply.github.com> | 2013-05-14 20:30:28 +0000 |
---|---|---|
committer | Xavier Roche <xroche@users.noreply.github.com> | 2013-05-14 20:30:28 +0000 |
commit | 07f4b003675260004c49755dde413944a16ca2d3 (patch) | |
tree | 11050b4c7fbe1d7c63a38f5f9a6b5ba6d8260ae1 /src/htscharset.c | |
parent | b09def438c5e47305e4a5ab18d8a646a3b004fdc (diff) |
Indenting cleanup for all *.[ch] files, except htsparse.c (too ugly to be automatically indented for now) and /minizip/, /mmsrip/ (external files)
setup:
indent -l80 -lc80 -nhnl -nut -bad -bap -bbo -br -brf -bli2 -brs -bls -br -ss -sai -pmt -nsaw -nsaf -nprs -i2 -ce -npsl -npcs -cs -sob -cdw -nbc -lp
logs:
indent: ./src/htsback.c:157: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsback.c:1417: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsback.c:1826: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsback.c:1833: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsback.c:1981: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsback.c:2685: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsback.c:2747: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsback.c:2861: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsback.c:3128: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsback.c:3512: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htswizard.c:140: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htswizard.c:597: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htswizard.c:598: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htswizard.c:611: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htscoremain.c:97: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htscoremain.c:106: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htscoremain.c:106: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htscoremain.c:256: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htscoremain.c:262: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htscoremain.c:935: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:271: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:272: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:273: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:274: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:275: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:276: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:277: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:278: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:279: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:280: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:281: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:662: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/httrack.c:752: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:413: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:414: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:415: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:416: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:417: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:418: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:419: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:420: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:421: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:422: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsweb.c:423: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.c:188: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.c:507: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.c:508: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.c:509: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.c:510: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.c:511: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.c:512: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.c:580: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.c:581: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.h:115: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.h:246: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.h:247: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.h:248: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.h:249: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.h:250: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.h:251: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.h:314: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/proxy/proxytrack.h:315: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:151: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:158: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:161: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:166: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:174: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:177: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:188: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:243: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:245: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:248: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:257: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htstools.c:647: Warning:old style assignment ambiguity in "=*". Assuming "= *"
indent: ./src/htsbauth.c:364: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htsbauth.c:387: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htscache.c:473: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsserver.h:112: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsserver.h:187: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsjava.c:312: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsjava.c:379: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htsjava.c:407: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsjava.c:472: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htsjava.c:483: Warning:old style assignment ambiguity in "=*". Assuming "= *"
indent: ./src/htslib.c:593: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:809: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:1743: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:1874: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:1896: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:1984: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2085: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2604: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2605: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2606: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2607: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2608: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2609: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2672: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2673: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2906: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2928: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:2996: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:4802: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htslib.c:5353: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:5354: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:5355: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:5357: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:5429: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htslib.c:5435: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htscore.c:208: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htscore.c:277: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htscore.c:279: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htscore.c:357: Warning:old style assignment ambiguity in "=*". Assuming "= *"
indent: ./src/htscore.c:394: Warning:old style assignment ambiguity in "=*". Assuming "= *"
indent: ./src/htscore.c:1544: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htscore.c:3330: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htscore.c:3361: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htshash.c:140: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htshash.c:217: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsname.c:733: Warning:old style assignment ambiguity in "=*". Assuming "= *"
indent: ./src/htsname.c:749: Warning:old style assignment ambiguity in "=*". Assuming "= *"
indent: ./src/htsname.c:933: Warning:old style assignment ambiguity in "=*". Assuming "= *"
indent: ./src/htsname.c:1520: Warning:old style assignment ambiguity in "=*". Assuming "= *"
indent: ./src/htszlib.c:76: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htszlib.c:81: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htscatchurl.c:268: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsinthash.c:156: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htsinthash.c:159: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htsinthash.c:176: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htsinthash.c:179: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htsinthash.c:226: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htsinthash.c:253: Warning:old style assignment ambiguity in "=&". Assuming "= &"
indent: ./src/htsftp.c:169: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsftp.c:177: Warning:old style assignment ambiguity in "=-". Assuming "= -"
indent: ./src/htsfilters.c:67: Warning:old style assignment ambiguity in "=*". Assuming "= *"
indent: ./src/htsfilters.c:80: Warning:old style assignment ambiguity in "=*". Assuming "= *"
Diffstat (limited to 'src/htscharset.c')
-rw-r--r-- | src/htscharset.c | 137 |
1 files changed, 90 insertions, 47 deletions
diff --git a/src/htscharset.c b/src/htscharset.c index 351fa8c..ea96eff 100644 --- a/src/htscharset.c +++ b/src/htscharset.c @@ -17,18 +17,15 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - Important notes: - We hereby ask people using this source NOT to use it in purpose of grabbing emails addresses, or collecting any other private information on persons. This would disgrace our work, and spoil the many hours we spent on it. - Please visit our Website: http://www.httrack.com */ - /* ------------------------------------------------------------ */ /* File: Charset conversion functions */ /* Author: Xavier Roche */ @@ -39,8 +36,10 @@ Please visit our Website: http://www.httrack.com static int hts_isStringAscii(const char *s, size_t size) { size_t i; - for(i = 0 ; i < size ; i++) { + + for(i = 0; i < size; i++) { const unsigned char c = (const unsigned char) s[i]; + if (c >= 0x80) { return 0; } @@ -205,16 +204,19 @@ static const wincodepage_t codepages[] = { /* Get a Windows codepage, by its name. Return 0 upon error. */ UINT hts_getCodepage(const char *name) { int id; + #define IS_ALNUM(C) ( ((C) >= 'A' && (C) <= 'Z') || ((C) >= 'a' && (C) <= 'z') || ((C) >= '0' && (C) <= '9') ) #define CHAR_LOWER(C) ( ((C) >= 'A' && (C) <= 'Z') ? ((C) + 'a' - 'A') : (C) ) - for(id = 0 ; codepages[id].name != NULL ; id++) { + for(id = 0; codepages[id].name != NULL; id++) { int i, j; + /* Compare the two strings, lowercase and alphanum only (ISO88591 == iso-8859-1) */ const char *a = name, *b = codepages[id].name; - for(i = 0, j = 0 ; ; i++, j++) { + + for(i = 0, j = 0;; i++, j++) { /* Skip non-alnum */ - for( ; a[i] != '\0' && !IS_ALNUM(a[i]) ; i++) ; - for( ; b[j] != '\0' && !IS_ALNUM(b[j]) ; j++) ; + for(; a[i] != '\0' && !IS_ALNUM(a[i]); i++) ; + for(; b[j] != '\0' && !IS_ALNUM(b[j]); j++) ; /* Compare */ if (CHAR_LOWER(a[i]) != CHAR_LOWER(b[j])) { break; @@ -233,6 +235,7 @@ UINT hts_getCodepage(const char *name) { static char *strndup(const char *s, size_t size) { char *dest = malloc(size + 1); + if (dest != NULL) { memcpy(dest, s, size); dest[size] = '\0'; @@ -241,14 +244,19 @@ static char *strndup(const char *s, size_t size) { return NULL; } -LPWSTR hts_convertStringToUCS2(const char *s, int size, UINT cp, int* pwsize) { +LPWSTR hts_convertStringToUCS2(const char *s, int size, UINT cp, int *pwsize) { /* Size in wide chars of the output */ const int wsize = MultiByteToWideChar(cp, 0, (LPCSTR) s, size, NULL, 0); + if (wsize > 0) { LPSTR uoutput = NULL; - LPWSTR woutput = malloc((wsize + 1)*sizeof(WCHAR)); - if (woutput != NULL && MultiByteToWideChar(cp, 0, (LPCSTR) s, size, woutput, wsize) == wsize) { - const int usize = WideCharToMultiByte(CP_UTF8, 0, woutput, wsize, NULL, 0, NULL, FALSE); + LPWSTR woutput = malloc((wsize + 1) * sizeof(WCHAR)); + + if (woutput != NULL + && MultiByteToWideChar(cp, 0, (LPCSTR) s, size, woutput, + wsize) == wsize) { + const int usize = + WideCharToMultiByte(CP_UTF8, 0, woutput, wsize, NULL, 0, NULL, FALSE); if (usize > 0) { woutput[wsize] = 0x0; if (pwsize != NULL) @@ -262,16 +270,19 @@ LPWSTR hts_convertStringToUCS2(const char *s, int size, UINT cp, int* pwsize) { return NULL; } -LPWSTR hts_convertUTF8StringToUCS2(const char *s, int size, int* pwsize) { +LPWSTR hts_convertUTF8StringToUCS2(const char *s, int size, int *pwsize) { return hts_convertStringToUCS2(s, size, CP_UTF8, pwsize); } char *hts_convertUCS2StringToCP(LPWSTR woutput, int wsize, UINT cp) { - const int usize = WideCharToMultiByte(cp, 0, woutput, wsize, NULL, 0, NULL, FALSE); + const int usize = + WideCharToMultiByte(cp, 0, woutput, wsize, NULL, 0, NULL, FALSE); if (usize > 0) { - char *const uoutput = malloc((usize + 1)*sizeof(char)); + char *const uoutput = malloc((usize + 1) * sizeof(char)); + if (uoutput != NULL) { - if (WideCharToMultiByte(cp, 0, woutput, wsize, uoutput, usize, NULL, FALSE) == usize) { + if (WideCharToMultiByte + (cp, 0, woutput, wsize, uoutput, usize, NULL, FALSE) == usize) { uoutput[usize] = '\0'; return uoutput; } else { @@ -300,8 +311,10 @@ char *hts_convertStringCPToUTF8(const char *s, size_t size, UINT cp) { /* Size in wide chars of the output */ int wsize; LPWSTR woutput = hts_convertStringToUCS2(s, (int) size, cp, &wsize); + if (woutput != NULL) { char *const uoutput = hts_convertUCS2StringToUTF8(woutput, wsize); + free(woutput); return uoutput; } @@ -325,8 +338,10 @@ char *hts_convertStringCPFromUTF8(const char *s, size_t size, UINT cp) { /* Size in wide chars of the output */ int wsize; LPWSTR woutput = hts_convertStringToUCS2(s, (int) size, CP_UTF8, &wsize); + if (woutput != NULL) { char *const uoutput = hts_convertUCS2StringToCP(woutput, wsize, cp); + free(woutput); return uoutput; } @@ -338,11 +353,13 @@ char *hts_convertStringCPFromUTF8(const char *s, size_t size, UINT cp) { char *hts_convertStringToUTF8(const char *s, size_t size, const char *charset) { const UINT cp = hts_getCodepage(charset); + return hts_convertStringCPToUTF8(s, size, cp); } char *hts_convertStringFromUTF8(const char *s, size_t size, const char *charset) { const UINT cp = hts_getCodepage(charset); + return hts_convertStringCPFromUTF8(s, size, cp); } @@ -355,7 +372,8 @@ char *hts_convertStringSystemToUTF8(const char *s, size_t size) { #include <errno.h> #include <iconv.h> -static char *hts_convertStringToUTF8_(const char *s, size_t size, const char *to, const char *from) { +static char *hts_convertStringToUTF8_(const char *s, size_t size, + const char *to, const char *from) { /* Empty string ? */ if (size == 0) { return strdup(""); @@ -367,8 +385,9 @@ static char *hts_convertStringToUTF8_(const char *s, size_t size, const char *to /* Find codepage */ else { const iconv_t cp = iconv_open(to, from); - if (cp != (iconv_t) -1) { - char *inbuf = (char*) s; + + if (cp != (iconv_t) - 1) { + char *inbuf = (char *) s; size_t inbytesleft = size; size_t outbufCapa = 0; char *outbuf = NULL; @@ -376,7 +395,7 @@ static char *hts_convertStringToUTF8_(const char *s, size_t size, const char *to size_t finalSize; /* Initial size to around the string size */ - for(outbufCapa = 16 ; outbufCapa < size + 1 ; outbufCapa *= 2) ; + for(outbufCapa = 16; outbufCapa < size + 1; outbufCapa *= 2) ; outbuf = malloc(outbufCapa); outbytesleft = outbufCapa; @@ -384,10 +403,12 @@ static char *hts_convertStringToUTF8_(const char *s, size_t size, const char *to while(outbuf != NULL && inbytesleft != 0) { const size_t offset = outbufCapa - outbytesleft; char *outbufCurrent = outbuf + offset; - const size_t ret = iconv(cp, &inbuf, &inbytesleft, &outbufCurrent, &outbytesleft); - if (ret == (size_t) -1) { + const size_t ret = + iconv(cp, &inbuf, &inbytesleft, &outbufCurrent, &outbytesleft); + if (ret == (size_t) - 1) { if (errno == E2BIG) { const size_t used = outbufCapa - outbytesleft; + outbufCapa *= 2; outbuf = realloc(outbuf, outbufCapa); if (outbuf == NULL) { @@ -430,7 +451,8 @@ char *hts_convertStringToUTF8(const char *s, size_t size, const char *charset) { return strdup(""); } /* Already UTF-8 ? */ - if (strcasecmp(charset, "utf-8") == 0 || strcasecmp(charset, "utf8") == 0 || hts_isStringAscii(s, size)) { + if (strcasecmp(charset, "utf-8") == 0 || strcasecmp(charset, "utf8") == 0 + || hts_isStringAscii(s, size)) { return strndup(s, size); } /* Find codepage */ @@ -445,7 +467,8 @@ char *hts_convertStringFromUTF8(const char *s, size_t size, const char *charset) return strdup(""); } /* Already UTF-8 ? */ - if (strcasecmp(charset, "utf-8") == 0 || strcasecmp(charset, "utf8") == 0 || hts_isStringAscii(s, size)) { + if (strcasecmp(charset, "utf-8") == 0 || strcasecmp(charset, "utf8") == 0 + || hts_isStringAscii(s, size)) { return strndup(s, size); } /* Find codepage */ @@ -456,25 +479,32 @@ char *hts_convertStringFromUTF8(const char *s, size_t size, const char *charset) #endif -HTS_STATIC char* hts_getCharsetFromContentType(const char *mime) { +HTS_STATIC char *hts_getCharsetFromContentType(const char *mime) { /* text/html; charset=utf-8 */ const char *const charset = "charset"; char *pos = strstr(mime, charset); + if (pos != NULL) { /* Skip spaces */ int eq = 0; - for(pos += strlen(charset) ; *pos == ' ' || *pos == '=' || *pos == '"' || *pos == '\'' ; pos++) { + + for(pos += strlen(charset); + *pos == ' ' || *pos == '=' || *pos == '"' || *pos == '\''; pos++) { if (*pos == '=') { eq = 1; } } if (eq == 1) { int len; - for(len = 0 ; pos[len] == ' ' || pos[len] == ';' || pos[len] == '"' || *pos == '\'' ; pos++) ; + + for(len = 0; + pos[len] == ' ' || pos[len] == ';' || pos[len] == '"' || *pos == '\''; + pos++) ; if (len != 0) { char *const s = malloc(len + 1); int i; - for(i = 0 ; i < len ; i++) { + + for(i = 0; i < len; i++) { s[i] = pos[i]; } s[len] = '\0'; @@ -503,46 +533,60 @@ static int is_space_or_equal_or_quote(char c) { } size_t hts_stringLengthUTF8(const char *s) { - const unsigned char *const bytes = (const unsigned char*) s; + const unsigned char *const bytes = (const unsigned char *) s; size_t i, len; - for(i = 0, len = 0 ; bytes[i] != '\0' ; i++) { + + for(i = 0, len = 0; bytes[i] != '\0'; i++) { const unsigned char c = bytes[i]; - if (HTS_IS_LEADING_UTF8(c)) { // ASCII or leading byte + + if (HTS_IS_LEADING_UTF8(c)) { // ASCII or leading byte len++; } } return len; } -char* hts_getCharsetFromMeta(const char *html, size_t size) { +char *hts_getCharsetFromMeta(const char *html, size_t size) { int i; + // <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8" > - for(i = 0 ; i < size ; i++) { - if (html[i] == '<' && strncasecmp(&html[i + 1], "meta", 4) == 0 && is_space(html[i + 5]) ) { + for(i = 0; i < size; i++) { + if (html[i] == '<' && strncasecmp(&html[i + 1], "meta", 4) == 0 + && is_space(html[i + 5])) { /* Skip spaces */ - for(i += 5 ; is_space(html[i]) ; i++) ; - if (strncasecmp(&html[i], "HTTP-EQUIV", 10) == 0 && is_space_or_equal(html[i + 10]) ) { - for(i += 10 ; is_space_or_equal_or_quote(html[i]) ; i++) ; + for(i += 5; is_space(html[i]); i++) ; + if (strncasecmp(&html[i], "HTTP-EQUIV", 10) == 0 + && is_space_or_equal(html[i + 10])) { + for(i += 10; is_space_or_equal_or_quote(html[i]); i++) ; if (strncasecmp(&html[i], "CONTENT-TYPE", 12) == 0) { - for(i += 12 ; is_space_or_equal_or_quote(html[i]) ; i++) ; - if (strncasecmp(&html[i], "CONTENT", 7) == 0 && is_space_or_equal(html[i + 7]) ) { - for(i += 7 ; is_space_or_equal_or_quote(html[i]) ; i++) ; + for(i += 12; is_space_or_equal_or_quote(html[i]); i++) ; + if (strncasecmp(&html[i], "CONTENT", 7) == 0 + && is_space_or_equal(html[i + 7])) { + for(i += 7; is_space_or_equal_or_quote(html[i]); i++) ; /* Skip content-type */ - for( ; i < size && html[i] != ';' && html[i] != '"' && html[i] != '\'' ; i++) ; + for(; + i < size && html[i] != ';' && html[i] != '"' && html[i] != '\''; + i++) ; /* Expect charset attribute here */ if (html[i] == ';') { - for(i++ ; is_space(html[i]) ; i++) ; + for(i++; is_space(html[i]); i++) ; /* Look for charset */ - if (strncasecmp(&html[i], "charset", 7) == 0 && is_space_or_equal(html[i + 7])) { + if (strncasecmp(&html[i], "charset", 7) == 0 + && is_space_or_equal(html[i + 7])) { int len; - for(i += 7 ; is_space_or_equal(html[i]) || html[i] == '\'' ; i++) ; + + for(i += 7; is_space_or_equal(html[i]) || html[i] == '\''; + i++) ; /* Charset */ - for(len = 0 ; i + len < size && html[i + len] != '"' && html[i + len] != '\'' && html[i + len] != ' ' ; len++) ; + for(len = 0; + i + len < size && html[i + len] != '"' + && html[i + len] != '\'' && html[i + len] != ' '; len++) ; /* No error ? */ if (len != 0 && i < size) { char *const s = malloc(len + 1); int j; - for(j = 0 ; j < len ; j++) { + + for(j = 0; j < len; j++) { s[j] = html[i + j]; } s[len] = '\0'; @@ -557,4 +601,3 @@ char* hts_getCharsetFromMeta(const char *html, size_t size) { } return NULL; } - |