| 1 |
#ifndef DEBUG |
| 2 |
#include <kpathsea/config.h> |
| 3 |
#include "makejvf.h" |
| 4 |
#endif |
| 5 |
|
| 6 |
#include "usrtable.h" |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#ifdef DEBUG |
| 11 |
#include <string.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
int usertable_replace_max=0,usertable_move_max=0,usertable_charset_max=0; |
| 15 |
struct USERTABLE_REPLACE usertable_replace[MAX_TABLE]; |
| 16 |
struct USERTABLE_MOVE usertable_move[MAX_TABLE]; |
| 17 |
struct USERTABLE_CHARSET usertable_charset[MAX_CHAR_TABLE]; |
| 18 |
|
| 19 |
void get_usertable(char *name) |
| 20 |
{ |
| 21 |
FILE *fp; |
| 22 |
char *tok,*endptr,buf[BUF_SIZE],str0[8],str1[8]; |
| 23 |
int charset_mode=0,l; |
| 24 |
long char_max=-2,ch0,ch1; |
| 25 |
|
| 26 |
fp = fopen(name,"r"); |
| 27 |
if (fp == NULL) { |
| 28 |
fprintf(stderr,"Cannot find %s!\n",name); |
| 29 |
exit(1); |
| 30 |
} |
| 31 |
for (l = 0; fgets(buf, BUF_SIZE, fp) != NULL; l++) { |
| 32 |
if (strncmp(buf, "+", 1) && strncmp(buf, "%", 1)) charset_mode = 0; |
| 33 |
if ((endptr=strchr(buf, '%')) != NULL) strcpy(endptr,"\n"); /* ignore after '%' */ |
| 34 |
if (!strncmp(buf, "\n", 1)) continue; /* ignore empty line */ |
| 35 |
tok = strtok(buf, "\t"); |
| 36 |
if (!strcmp(tok, "REPLACE")) { |
| 37 |
if (usertable_replace_max >= MAX_TABLE) goto buferr; |
| 38 |
usertable_replace[usertable_replace_max].codepoint = strtol(strtok(NULL, "\t\n"), &endptr, 16); |
| 39 |
if (*endptr != '\0') goto taberr; |
| 40 |
usertable_replace[usertable_replace_max].newcodepoint = strtol(strtok(NULL, "\t\n"), &endptr, 16); |
| 41 |
if (*endptr != '\0') goto taberr; |
| 42 |
if (strtok(NULL, "\t\n") != NULL) goto taberr; |
| 43 |
usertable_replace_max++; |
| 44 |
continue; |
| 45 |
} |
| 46 |
if (!strcmp(tok, "MOVE")) { |
| 47 |
if (usertable_move_max >= MAX_TABLE) goto buferr; |
| 48 |
usertable_move[usertable_move_max].codepoint = strtol(strtok(NULL, "\t\n"), &endptr, 16); |
| 49 |
if (*endptr != '\0') goto taberr; |
| 50 |
usertable_move[usertable_move_max].moveright = strtod(strtok(NULL, "\t\n"), &endptr); |
| 51 |
if (*endptr != '\0') goto taberr; |
| 52 |
usertable_move[usertable_move_max].movedown = strtod(strtok(NULL, "\t\n"), &endptr); |
| 53 |
if (*endptr != '\0') goto taberr; |
| 54 |
if (strtok(NULL, "\t\n") != NULL) goto taberr; |
| 55 |
usertable_move_max++; |
| 56 |
continue; |
| 57 |
} |
| 58 |
if ((!strcmp(tok, "+") && charset_mode) || !strcmp(tok, "CHARSET")) { |
| 59 |
charset_mode = 1; |
| 60 |
while ((tok=strtok(NULL, ",\t\n")) != NULL) { |
| 61 |
if ((endptr=strstr(tok,"..")) != NULL) { |
| 62 |
*endptr = '\0'; |
| 63 |
if (sscanf(tok, "%7s",str0) != 1) goto taberr; |
| 64 |
if (sscanf(endptr+2,"%7s",str1) != 1) goto taberr; |
| 65 |
ch0 = strtol(str0, &endptr, 16); |
| 66 |
if (*endptr != '\0' || ch0<=char_max) goto codeerr; |
| 67 |
ch1 = strtol(str1, &endptr, 16); |
| 68 |
if (*endptr != '\0' || ch1<=ch0) goto codeerr; |
| 69 |
} else { |
| 70 |
if (sscanf(tok,"%7s",str0) != 1) goto taberr; |
| 71 |
ch0 = strtol(str0, &endptr, 16); |
| 72 |
if (*endptr != '\0' || ch0<=char_max) goto codeerr; |
| 73 |
ch1 = ch0; |
| 74 |
} |
| 75 |
if (char_max==ch0-1) { |
| 76 |
usertable_charset[usertable_charset_max-1].max = ch1; |
| 77 |
} else { |
| 78 |
if (usertable_charset_max >= MAX_CHAR_TABLE) goto buferr; |
| 79 |
usertable_charset[usertable_charset_max].min = ch0; |
| 80 |
usertable_charset[usertable_charset_max].max = ch1; |
| 81 |
usertable_charset_max++; |
| 82 |
} |
| 83 |
char_max = ch1; |
| 84 |
} |
| 85 |
continue; |
| 86 |
} |
| 87 |
fprintf(stderr, "Unknown setting %s found in %s (line %d)!\n", tok, name, l+1); |
| 88 |
exit(1); |
| 89 |
} |
| 90 |
fclose(fp); |
| 91 |
return; |
| 92 |
taberr: |
| 93 |
fprintf(stderr, "Error in user-defined table file %s (line %d)!\n", name, l+1); |
| 94 |
exit(1); |
| 95 |
buferr: |
| 96 |
fprintf(stderr, "User-defined table in %s is too large!\n", name); |
| 97 |
exit(1); |
| 98 |
codeerr: |
| 99 |
fprintf(stderr, "Character codes must be given in ascending order (line %d)!\n", l+1); |
| 100 |
exit(1); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/* for unit test */ |
| 105 |
/* ex. $ gcc -g -o usrtable.test usrtable.c -DDEBUG */ |
| 106 |
#ifdef DEBUG |
| 107 |
int main() { |
| 108 |
int i; |
| 109 |
char name[]="test_user_table"; |
| 110 |
get_usertable(name); |
| 111 |
|
| 112 |
if (usertable_replace_max>0) { |
| 113 |
printf("REPLACE::\n"); |
| 114 |
for(i=0;i<usertable_replace_max;i++) |
| 115 |
printf("%6d: %06x -> %06x\n", i, usertable_replace[i].codepoint, usertable_replace[i].newcodepoint); |
| 116 |
} |
| 117 |
if (usertable_move_max>0) { |
| 118 |
printf("MOVE::\n"); |
| 119 |
for(i=0;i<usertable_move_max;i++) |
| 120 |
printf("%6d: %06x | %lf, %lf\n", i, usertable_move[i].codepoint, usertable_move[i].moveright, usertable_move[i].movedown); |
| 121 |
} |
| 122 |
if (usertable_charset_max>0) { |
| 123 |
printf("CHARSET::\n"); |
| 124 |
for(i=0;i<usertable_charset_max;i++) |
| 125 |
printf("%6d: %06lx .. %06lx\n", i, usertable_charset[i].min, usertable_charset[i].max); |
| 126 |
} |
| 127 |
return(0); |
| 128 |
} |
| 129 |
#endif |