1 /*****************************************************************/
2 /* Substitution maps to replace some unicode characters with */
3 /* multicharacter sequences */
5 /* This file is part of catdoc project */
6 /* (c) Victor Wagner 1998-2003, (c) Alex Ott 2003 */
7 /*****************************************************************/
15 SUBSTMAP spec_chars, replacements;
16 char *map_path=CHARSETPATH;
17 void map_insert(SUBSTMAP map, int uc, const char *s) ;
18 int longest_sequence=6;/* six is longest character sequence which can be
19 generated by catdoc internally*/
21 /******************************************************************/
22 /* checks for terminator of character sequence. If stop is something
23 * like quote - check for same char. If it is \n, check for any space
24 ********************************************************************/
25 int isstop(char c, char stop) {
32 /************************************************************************/
33 /* Reads substitution map file. */
34 /************************************************************************/
35 SUBSTMAP read_substmap(char* filename) {
37 SUBSTMAP map=calloc(sizeof(char **),256);
38 char *path, line[1024], *p, *q;
41 int escaped, lineno=0, i;
43 path=find_file(filename,add_exe_path(map_path));
49 fprintf(stderr,"Insufficient memory\n");
58 setvbuf(f,input_buffer,_IOFBF,FILE_BUFFER);
61 if (!fgets(line,1024,f)) continue;
65 /* skip leading space */
66 for(p=line;*p && isspace(*p);p++);
67 /* if #, it is comment */
70 *p==0x1A || /* DOS have strange habit of using ^Z as eof */
73 /* read hexadecimal code */
75 if (!isspace(*p)|| uc<0 || uc>0xfffd) {
76 fprintf(stderr,"Error parsing %s(%d)\n",path,lineno);
79 /* skip space between code and sequence */
80 for(;*p && isspace(*p);p++);
103 while (*q && (!isstop(*q,stopchar) || escaped)) {
111 if (*q!=stopchar && !(isspace(*q) && stopchar=='\n')) {
112 fprintf(stderr,"Error parsing %s(%d): unterminated sequence\n",
116 /* HERE SHOULD BE BACKSLASH ESCAPE PROCESSING !!!*/
118 for (q=s,i=0;*p && i<256;q++,i++) {
123 case 'n': *q='\n'; break;
124 case 'r': *q='\r'; break;
125 case 't': *q='\t'; break;
126 case 'b': *q='\b'; break;
127 case '\"': *q='\"'; break;
128 case '\'': *q='\''; break;
129 case '0': *q=strtol(p,&p,8); p--; break;
138 if (i>longest_sequence)
140 map_insert(map,uc,s);
147 /*************************************************************************/
148 /* searches for string in the substituton map. Returns NULL if not found */
149 /*************************************************************************/
150 char * map_subst ( SUBSTMAP map, int uc) {
151 char **p=map[(unsigned)uc >>8];
156 /*************************************************************************/
157 /* inserts string + unicode code into map */
158 /*************************************************************************/
159 void map_insert(SUBSTMAP map, int uc, const char *s) {
160 SUBSTMAP p=map+((unsigned)uc>>8);
163 *p= calloc(sizeof(char*),256);
165 fprintf(stderr,"Insufficient memory\n");
169 (*p)[uc & 0xff] = strdup(s);