9 #include "enchant-provider.h"
11 DictMgr::DictMgr(const char * dictpath, const char * etype) : numdict(0)
13 // load list of etype entries
14 pdentry = (dictentry *)malloc(MAXDICTIONARIES*sizeof(struct dictentry));
16 if (parse_file(dictpath, etype)) {
18 // no dictionary.lst found is okay
26 dictentry * pdict = NULL;
29 for (int i=0;i<numdict;i++) {
38 if (pdict->filename) {
39 free(pdict->filename);
40 pdict->filename = NULL;
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)
57 char line[MAXDICTENTRYLEN+1];
58 dictentry * pdict = pdentry;
60 // open the dictionary list file
62 dictlst = enchant_fopen(dictpath,"r");
67 // step one is to parse the dictionary list building up the
68 // descriptive structures
70 // read in each line ignoring any that dont start with etype
71 while (fgets(line,MAXDICTENTRYLEN,dictlst)) {
74 /* parse in a dictionary entry */
75 if (strncmp(line,etype,4) == 0) {
76 if (numdict < MAXDICTIONARIES) {
80 while ((piece=mystrsep(&tp,' '))) {
84 case 1: pdict->lang = mystrdup(piece); break;
85 case 2: if (strcmp (piece, "ANY") == 0)
86 pdict->region = mystrdup("");
88 pdict->region = mystrdup(piece);
90 case 3: pdict->filename = mystrdup(piece); break;
105 case 2: //deliberate fallthrough
111 fprintf(stderr,"dictionary list corruption in line \"%s\"\n",line);
121 // return text encoding of dictionary
122 int DictMgr::get_list(dictentry ** ppentry)
130 // strip strings into token based on single char delimiter
131 // acts like strsep() but only uses a delim char and not
134 char * DictMgr::mystrsep(char ** stringp, const char delim)
137 char * mp = *stringp;
138 size_t n = strlen(mp);
140 char * dp = (char *)memchr(mp,(int)((unsigned char)delim),n);
144 rv = (char *) malloc(nc+1);
150 rv = (char *) malloc(n+1);
162 // replaces strdup with ansi version
163 char * DictMgr::mystrdup(const char * s)
167 int sl = strlen(s)+1;
168 d = (char *) malloc(sl);
169 if (d) memcpy(d,s,sl);
175 // remove cross-platform text line end characters
176 void DictMgr:: mychomp(char * 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';