Add:Core:Support more diacritics
[navit-package] / navit / linguistics.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <glib.h>
4 #include "debug.h"
5 #include "linguistics.h"
6
7 static const char *special[][3]={
8 {"Ä","A","AE"},
9 {"Ö","O","OE"},
10 {"Ü","U","UE"},
11 {"Ő","O"},
12 {"Ű","U"},
13 {"Á","A"},
14 {"Ć","C"},
15 {"É","E"},
16 {"Í","I"},
17 {"Ń","N"},
18 {"Ó","O"},
19 {"Ś","S"},
20 {"Ú","U"},
21 {"Ź","Z"},
22 {"Ą","A"},
23 {"Ę","E"},
24 {"Ż","Z"},
25 {"Ł","L"},
26 {"ä","a","ae"},
27 {"ö","o","oe"},
28 {"ü","u","ue"},
29 {"ő","o"},
30 {"ű","u"},
31 {"á","a"},
32 {"ć","c"},
33 {"é","e"},
34 {"í","i"},
35 {"ń","n"},
36 {"ó","o"},
37 {"ś","s"},
38 {"ú","u"},
39 {"ź","z"},
40 {"ą","a"},
41 {"ę","e"},
42 {"ż","z"},
43 {"ł","l"},
44 {"ß","s","ss"},
45 };
46
47 char *
48 linguistics_expand_special(char *str, int mode)
49 {
50         char *in=str;
51         char *out,*ret;
52         int found=0;
53         out=ret=g_strdup(str);
54         if (!mode) 
55                 return ret;
56         while (*in) {
57                 char *next=g_utf8_find_next_char(in, NULL);
58                 int i,len=next-in;
59                 int match=0;
60                 if (len > 1) {
61                         for (i = 0 ; i < sizeof(special)/sizeof(special[0]); i++) {
62                                 const char *search=special[i][0];
63                                 if (!strncmp(in,search,len)) {
64                                         const char *replace=special[i][mode];
65                                         if (replace) {
66                                                 int replace_len=strlen(replace);
67                                                 dbg_assert(replace_len <= len);
68                                                 dbg(1,"found %s %s %s\n",in,search,replace);
69                                                 strcpy(out, replace);
70                                                 out+=replace_len;
71                                                 match=1;
72                                                 break;
73                                         }
74                                 }
75                         }
76                         in=next;
77                 }
78                 if (match) 
79                         found=1;
80                 else {  
81                         while (len-- > 0) 
82                                 *out++=*in++;
83                 }
84         }
85         *out++='\0';
86         if (!found) {
87                 g_free(ret);
88                 ret=NULL;
89         }
90         return ret;
91 }