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");
59 setvbuf(f,input_buffer,_IOFBF,FILE_BUFFER);
62 if (!fgets(line,1024,f)) continue;
66 /* skip leading space */
67 for(p=line;*p && isspace(*p);p++);
68 /* if #, it is comment */
71 *p==0x1A || /* DOS have strange habit of using ^Z as eof */
74 /* read hexadecimal code */
76 if (!isspace(*p)|| uc<0 || uc>0xfffd) {
77 fprintf(stderr,"Error parsing %s(%d)\n",path,lineno);
80 /* skip space between code and sequence */
81 for(;*p && isspace(*p);p++);
104 while (*q && (!isstop(*q,stopchar) || escaped)) {
112 if (*q!=stopchar && !(isspace(*q) && stopchar=='\n')) {
113 fprintf(stderr,"Error parsing %s(%d): unterminated sequence\n",
117 /* HERE SHOULD BE BACKSLASH ESCAPE PROCESSING !!!*/
119 for (q=s,i=0;*p && i<256;q++,i++) {
124 case 'n': *q='\n'; break;
125 case 'r': *q='\r'; break;
126 case 't': *q='\t'; break;
127 case 'b': *q='\b'; break;
128 case '\"': *q='\"'; break;
129 case '\'': *q='\''; break;
130 case '0': *q=strtol(p,&p,8); p--; break;
139 if (i>longest_sequence)
141 map_insert(map,uc,s);
148 /*************************************************************************/
149 /* searches for string in the substituton map. Returns NULL if not found */
150 /*************************************************************************/
151 char * map_subst ( SUBSTMAP map, int uc) {
152 char **p=map[(unsigned)uc >>8];
157 /*************************************************************************/
158 /* inserts string + unicode code into map */
159 /*************************************************************************/
160 void map_insert(SUBSTMAP map, int uc, const char *s) {
161 SUBSTMAP p=map+((unsigned)uc>>8);
164 *p= calloc(sizeof(char*),256);
166 fprintf(stderr,"Insufficient memory\n");
170 (*p)[uc & 0xff] = strdup(s);