]> git.lyx.org Git - features.git/blob - src/3rdparty/hunspell/1.3.3/src/hunspell/dictmgr.cxx
add stripped down hunspell 1.3.3
[features.git] / src / 3rdparty / hunspell / 1.3.3 / src / hunspell / 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 #include "csutil.hxx"
9
10 DictMgr::DictMgr(const char * dictpath, const char * etype) : numdict(0)
11 {
12   // load list of etype entries
13   pdentry = (dictentry *)malloc(MAXDICTIONARIES*sizeof(struct dictentry));
14   if (pdentry) {
15      if (parse_file(dictpath, etype)) {
16         numdict = 0;
17         // no dictionary.lst found is okay
18      }
19   }
20 }
21
22
23 DictMgr::~DictMgr() 
24 {
25   dictentry * pdict = NULL;
26   if (pdentry) {
27      pdict = pdentry;
28      for (int i=0;i<numdict;i++) {
29         if (pdict->lang) {
30             free(pdict->lang);
31             pdict->lang = NULL;
32         }
33         if (pdict->region) {
34             free(pdict->region);
35             pdict->region=NULL;
36         }
37         if (pdict->filename) {
38             free(pdict->filename);
39             pdict->filename = NULL;
40         }
41         pdict++;
42      }
43      free(pdentry);
44      pdentry = NULL;
45      pdict = NULL;
46   }
47   numdict = 0;
48 }
49
50
51 // read in list of etype entries and build up structure to describe them
52 int  DictMgr::parse_file(const char * dictpath, const char * etype)
53 {
54
55     int i;
56     char line[MAXDICTENTRYLEN+1];
57     dictentry * pdict = pdentry;
58
59     // open the dictionary list file
60     FILE * dictlst;
61     dictlst = myfopen(dictpath,"r");
62     if (!dictlst) {
63       return 1;
64     }
65
66     // step one is to parse the dictionary list building up the 
67     // descriptive structures
68
69     // read in each line ignoring any that dont start with etype
70     while (fgets(line,MAXDICTENTRYLEN,dictlst)) {
71        mychomp(line);
72
73        /* parse in a dictionary entry */
74        if (strncmp(line,etype,4) == 0) {
75           if (numdict < MAXDICTIONARIES) {
76              char * tp = line;
77              char * piece;
78              i = 0;
79              while ((piece=mystrsep(&tp,' '))) {
80                 if (*piece != '\0') {
81                     switch(i) {
82                        case 0: break;
83                        case 1: pdict->lang = mystrdup(piece); break;
84                        case 2: if (strcmp (piece, "ANY") == 0)
85                                  pdict->region = mystrdup("");
86                                else
87                                  pdict->region = mystrdup(piece);
88                                break;
89                        case 3: pdict->filename = mystrdup(piece); break;
90                        default: break;
91                     }
92                     i++;
93                 }
94                 free(piece);
95              }
96              if (i == 4) {
97                  numdict++;
98                  pdict++;
99              } else {
100                  switch (i) {
101                     case 3:
102                        free(pdict->region);
103                        pdict->region=NULL;
104                        /* FALLTHROUGH */
105                     case 2:
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