7 #include "dictmgr.hxx"
\r
11 DictMgr::DictMgr(const char * dictpath, const char * etype)
\r
13 // load list of etype entries
\r
15 pdentry = (dictentry *)malloc(MAXDICTIONARIES*sizeof(struct dictentry));
\r
17 if (parse_file(dictpath, etype)) {
\r
19 // no dictionary.lst found is okay
\r
27 DictMgr::~DictMgr()
\r
29 dictentry * pdict = NULL;
\r
32 for (int i=0;i<numdict;i++) {
\r
37 if (pdict->region) {
\r
38 free(pdict->region);
\r
41 if (pdict->filename) {
\r
42 free(pdict->filename);
\r
43 pdict->filename = NULL;
\r
55 // read in list of etype entries and build up structure to describe them
\r
56 int DictMgr::parse_file(const char * dictpath, const char * etype)
\r
60 char line[MAXDICTENTRYLEN+1];
\r
61 dictentry * pdict = pdentry;
\r
63 // open the dictionary list file
\r
65 dictlst = fopen(dictpath,"r");
\r
70 // step one is to parse the dictionary list building up the
\r
71 // descriptive structures
\r
73 // read in each line ignoring any that dont start with etype
\r
74 while (fgets(line,MAXDICTENTRYLEN,dictlst)) {
\r
77 /* parse in a dictionary entry */
\r
78 if (strncmp(line,etype,4) == 0) {
\r
79 if (numdict < MAXDICTIONARIES) {
\r
83 while ((piece=mystrsep(&tp,' '))) {
\r
84 if (*piece != '\0') {
\r
87 case 1: pdict->lang = mystrdup(piece); break;
\r
88 case 2: if (strcmp (piece, "ANY") == 0)
\r
89 pdict->region = mystrdup("");
\r
91 pdict->region = mystrdup(piece);
\r
93 case 3: pdict->filename = mystrdup(piece); break;
\r
104 fprintf(stderr,"dictionary list corruption in line \"%s\"\n",line);
\r
114 // return text encoding of dictionary
\r
115 int DictMgr::get_list(dictentry ** ppentry)
\r
117 *ppentry = pdentry;
\r
123 // strip strings into token based on single char delimiter
\r
124 // acts like strsep() but only uses a delim char and not
\r
127 char * DictMgr::mystrsep(char ** stringp, const char delim)
\r
130 char * mp = *stringp;
\r
131 int n = strlen(mp);
\r
133 char * dp = (char *)memchr(mp,(int)((unsigned char)delim),n);
\r
136 int nc = (int)((unsigned long)dp - (unsigned long)mp);
\r
137 rv = (char *) malloc(nc+1);
\r
142 rv = (char *) malloc(n+1);
\r
153 // replaces strdup with ansi version
\r
154 char * DictMgr::mystrdup(const char * s)
\r
158 int sl = strlen(s);
\r
159 d = (char *) malloc(((sl+1) * sizeof(char)));
\r
160 if (d) memcpy(d,s,((sl+1)*sizeof(char)));
\r
166 // remove cross-platform text line end characters
\r
167 void DictMgr:: mychomp(char * s)
\r
170 if ((k > 0) && ((*(s+k-1)=='\r') || (*(s+k-1)=='\n'))) *(s+k-1) = '\0';
\r
171 if ((k > 1) && (*(s+k-2) == '\r')) *(s+k-2) = '\0';
\r