1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <stdio.h> #include <stdlib.h> #include <string.h>
#define ARRAY "gbkuni30" #define MAX_LEN 65535 // 2字节最大数 static unsigned short big_buffer[MAX_LEN] = {0};
// 源文件 #define SRC "gbkuni30.txt"
// 生产的头文件 #define DST "gbkuni30_gen.h"
int make_charmap_gb18030() { char buffer[16] = {0}; char* p = NULL; FILE* fp_c = NULL; FILE* fp = NULL; int len = 0; int x1 = 0; int x2 = 0; int i = 0; int max_num = 0; int cnt = 0;
fp = fopen(SRC, "r"); if (fp == NULL) { printf("open file error!!\n"); return -1; }
fseek(fp, 0, SEEK_END); len = ftell(fp); fseek(fp,0,SEEK_SET);
printf("file len: %d\n", len);
fp_c = fopen(DST, "w+"); if (fp_c == NULL) { printf("open file error!!\n"); return -1; }
fprintf(fp_c, "/**********************************************************************************/\n"); fprintf(fp_c, "/* GBK(GB18030) to UNICODE table, powered by Late Lee */\n"); fprintf(fp_c, "/* http://www.latelee.org */\n"); fprintf(fp_c, "/* %s %s */\n", __DATE__, __TIME__); fprintf(fp_c, "/* The source file comes from: */\n"); fprintf(fp_c, "/* http://icu-project.org/repos/icu/data/trunk/charset/source/gb18030/gbkuni30.txt*/\n"); fprintf(fp_c, "/**********************************************************************************/\n");
fprintf(fp_c, "#ifndef __GBK2UNICODE__H\n"); fprintf(fp_c, "#define __GBK2UNICODE__H\n\n");
fprintf(fp_c, ""); fprintf(fp_c, "static unsigned short %s[] = \n{\n", ARRAY);
// 先读取到缓冲区,解析出两个数 while (fgets(buffer, 32, fp) != NULL) { sscanf(buffer, "%x:%x\n", &x1, &x2); //printf("%s", buffer); //printf("%04x %x\n", x1, x2); //fprintf(fp_c, "0x%04x, 0x%x,\n", x1, x2); big_buffer[x2] = x1; if (x2 > max_num) max_num = x2;
}
printf("max num: %d %x\n", max_num, max_num); // 注:为节省存储空间,从0x8140开始存储,查询时需要减去此数 for (i = 0x8140; i < max_num + 1; i++) { //printf("0x%04x\n", big_buffer[i]); fprintf(fp_c, "0x%04x, ", big_buffer[i]); cnt++; if (cnt % 10 == 0) { fprintf(fp_c, " // line num %d \n", cnt / 10 - 1); } } fprintf(fp_c, "\n"); fprintf(fp_c, "};\n\n"); fprintf(fp_c, "#endif //__GBK2UNICODE__H\n"); fprintf(stdout, "Job done!\n");
fclose(fp); fclose(fp_c);
return 0;
}
|