#include #include #include #include /* triml */ #include /* toupper */ #include #include #define TRUE 0 #define FALSE -1 /*( UTF 変数 )*/ #define UTF8 1208 #define USA 0 #define JPN 1 #define CHS 2 #define CHT 3 #define JPN5035 8 int encode(char* fromstr, char* tostr, int CNTRY, char* URLPath); char* encodeURIComponentUTF(char* utf8_str); int toUTF8(char* ebcbuf, char* unibuf, int CNTRY); /* UNI_CODE */ int toEBCDIC(char* ebcbuf, char* ascbuf, char* table, /* CVTMOD4 */ char* tbllib, int bSiSo); void main(void){ char fromstr[128], tostr[128], URLPath[128]; printf("** TESTENCD2: UTF8 エンコードのテスト **\n"); getchar(); strcpy(fromstr, "/TEMP/MY#FILE.TMP"); strcpy(URLPath, "http;//www.officequattro.com"); if(encode(fromstr, tostr, JPN5035, URLPath) == FALSE){/* エラー */ printf("HTTPSRV[%d] %s: エンコードでエラーがありました。 ", __LINE__, fromstr); getchar(); exit(-1); }/* エラー */ printf("[%d] 文字列 %s は \n", __LINE__, fromstr); printf("[%d] %s とエンコードされました。 \n", __LINE__, tostr); getchar(); exit(0); } /*****************************************************************/ int encode(char* fromstr, char* tostr, int CNTRY, char* URLPath) /*****************************************************************/ { char utfbuf[2048]; _Decimal(5,0) dclen, outlen = 0; _Decimal(5,0) maxotl = 2048; int len; memset(utfbuf, 0, sizeof(utfbuf)); if((strstr(URLPath, "ea-jp")) != NULL){/* 英語から日本語 */ memset(utfbuf, 0, sizeof(utfbuf)); len = strlen(fromstr); dclen = (_Decimal(5,0))len; QDCXLATE(&dclen, fromstr, "QASCII ", "QSYS ", utfbuf, &maxotl, &outlen, "*JPN ", "N", "*EA "); }/* 英語から日本語 */ else{/* それ以外 */ if(toUTF8(fromstr, utfbuf, CNTRY) == FALSE){/* エラー */ printf("HTTPSRV[%d] UTF8 変換でエラーがありました。 \n%s\n", __LINE__, fromstr); return FALSE; }/* エラー */ }/* それ以外 */ strcpy(tostr, encodeURIComponentUTF(utfbuf)); return TRUE; } /*****************************************/ char* encodeURIComponentUTF(char* utf8_str) /*****************************************/ /* JavaScript の encodeURIComponent() と同じようにエンコードする */ { char *ptr; char ebc_str[5], utf_str[5]; ptr = utf8_str; while(*ptr){/*while*/ #pragma convert(850) if('0' <= *ptr && *ptr <= '9' || 'A' <= *ptr && *ptr <= 'Z' || 'a' <= *ptr && *ptr <= 'z' || *ptr == '-' || *ptr == '_' || *ptr == '.' || *ptr == '!' || *ptr == '。' || *ptr == '*' || *ptr == '\'') #pragma convert(0) {/* エンコードの必要なし */ memcpy(utf_str, ptr, 1); utf_str[1] = 0x00; toEBCDIC(ebc_str, utf_str, "EBCDIC5035", "ASNET.COM ", TRUE); memcpy(ptr, ebc_str, 1); ptr ++; continue; }/* エンコードの必要なし */ else{/* エンコードが必要 */ memmove(ptr + 3, ptr + 1, strlen(ptr + 1) + 1); sprintf(ebc_str, "%%%02X", *ptr); memcpy(ptr, ebc_str, 3); ptr += 3; }/* エンコードが必要 */ }/*while*/ return utf8_str; }