Tizen 2.1 base
[external/enchant.git] / src / myspell / dictmgr.cxx
1
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <stdio.h>
6
7 #include "dictmgr.hxx"
8
9 #include "enchant-provider.h"
10
11 DictMgr::DictMgr(const char * dictpath, const char * etype) : numdict(0)
12 {
13   // load list of etype entries
14   pdentry = (dictentry *)malloc(MAXDICTIONARIES*sizeof(struct dictentry));
15   if (pdentry) {
16      if (parse_file(dictpath, etype)) {
17         numdict = 0;
18         // no dictionary.lst found is okay
19      }
20   }
21 }
22
23
24 DictMgr::~DictMgr() 
25 {
26   dictentry * pdict = NULL;
27   if (pdentry) {
28      pdict = pdentry;
29      for (int i=0;i<numdict;i++) {
30         if (pdict->lang) {
31             free(pdict->lang);
32             pdict->lang = NULL;
33         }
34         if (pdict->region) {
35             free(pdict->region);
36             pdict->region=NULL;
37         }
38         if (pdict->filename) {
39             free(pdict->filename);
40             pdict->filename = NULL;
41         }
42         pdict++;
43      }
44      free(pdentry);
45      pdentry = NULL;
46      pdict = NULL;
47   }
48   numdict = 0;
49 }
50
51
52 // read in list of etype entries and build up structure to describe them
53 int  DictMgr::parse_file(const char * dictpath, const char * etype)
54 {
55
56     int i;
57     char line[MAXDICTENTRYLEN+1];
58     dictentry * pdict = pdentry;
59
60     // open the dictionary list file
61     FILE * dictlst;
62     dictlst = enchant_fopen(dictpath,"r");
63     if (!dictlst) {
64       return 1;
65     }
66
67     // step one is to parse the dictionary list building up the 
68     // descriptive structures
69
70     // read in each line ignoring any that dont start with etype
71     while (fgets(line,MAXDICTENTRYLEN,dictlst)) {
72        mychomp(line);
73
74        /* parse in a dictionary entry */
75        if (strncmp(line,etype,4) == 0) {
76           if (numdict < MAXDICTIONARIES) {
77              char * tp = line;
78              char * piece;
79              i = 0;
80              while ((piece=mystrsep(&tp,' '))) {
81                 if (*piece != '\0') {
82                     switch(i) {
83                        case 0: break;
84                        case 1: pdict->lang = mystrdup(piece); break;
85                        case 2: if (strcmp (piece, "ANY") == 0)
86                                  pdict->region = mystrdup("");
87                                else
88                                  pdict->region = mystrdup(piece);
89                                break;
90                        case 3: pdict->filename = mystrdup(piece); break;
91                        default: break;
92                     }
93                     i++;
94                 }
95                 free(piece);
96              }
97              if (i == 4) {
98                  numdict++;
99                  pdict++;
100              } else {
101                  switch (i) {
102                     case 3:
103                        free(pdict->region);
104                        pdict->region=NULL;
105                     case 2: //deliberate fallthrough
106                        free(pdict->lang);
107                        pdict->lang=NULL;
108                     default:
109                         break;
110                  }
111                  fprintf(stderr,"dictionary list corruption in line \"%s\"\n",line);
112                  fflush(stderr);
113              }
114           }
115        }
116     }
117     fclose(dictlst);
118     return 0;
119 }
120
121 // return text encoding of dictionary
122 int DictMgr::get_list(dictentry ** ppentry)
123 {
124   *ppentry = pdentry;
125   return numdict;
126 }
127
128
129
130 // strip strings into token based on single char delimiter
131 // acts like strsep() but only uses a delim char and not 
132 // a delim string
133
134 char * DictMgr::mystrsep(char ** stringp, const char delim)
135 {
136   char * rv = NULL;
137   char * mp = *stringp;
138   size_t n = strlen(mp);
139   if (n > 0) {
140      char * dp = (char *)memchr(mp,(int)((unsigned char)delim),n);
141      if (dp) {
142         *stringp = dp+1;
143         size_t nc = dp - mp; 
144         rv = (char *) malloc(nc+1);
145         if (rv) {
146            memcpy(rv,mp,nc);
147            *(rv+nc) = '\0';
148         }
149      } else {
150        rv = (char *) malloc(n+1);
151        if (rv) {
152           memcpy(rv, mp, n);
153           *(rv+n) = '\0';
154           *stringp = mp + n;
155        }
156      }
157   }
158   return rv;
159 }
160
161
162 // replaces strdup with ansi version
163 char * DictMgr::mystrdup(const char * s)
164 {
165   char * d = NULL;
166   if (s) {
167      int sl = strlen(s)+1;
168      d = (char *) malloc(sl);
169      if (d) memcpy(d,s,sl);
170   }
171   return d;
172 }
173
174
175 // remove cross-platform text line end characters
176 void DictMgr:: mychomp(char * s)
177 {
178   int k = strlen(s);
179   if ((k > 0) && ((*(s+k-1)=='\r') || (*(s+k-1)=='\n'))) *(s+k-1) = '\0';
180   if ((k > 1) && (*(s+k-2) == '\r')) *(s+k-2) = '\0';
181 }
182