Refactor: CString optimizations
[TortoiseGit.git] / ext / hunspell / dictmgr.cxx
blobba014407e745f0a488d1f3cbc3d492b77412f442
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <stdio.h>
7 #include "dictmgr.hxx"
9 DictMgr::DictMgr(const char * dictpath, const char * etype) : numdict(0)
11 // load list of etype entries
12 pdentry = (dictentry *)malloc(MAXDICTIONARIES*sizeof(struct dictentry));
13 if (pdentry) {
14 if (parse_file(dictpath, etype)) {
15 numdict = 0;
16 // no dictionary.lst found is okay
22 DictMgr::~DictMgr()
24 dictentry * pdict = NULL;
25 if (pdentry) {
26 pdict = pdentry;
27 for (int i=0;i<numdict;i++) {
28 if (pdict->lang) {
29 free(pdict->lang);
30 pdict->lang = NULL;
32 if (pdict->region) {
33 free(pdict->region);
34 pdict->region=NULL;
36 if (pdict->filename) {
37 free(pdict->filename);
38 pdict->filename = NULL;
40 pdict++;
42 free(pdentry);
43 pdentry = NULL;
44 pdict = NULL;
46 numdict = 0;
50 // read in list of etype entries and build up structure to describe them
51 int DictMgr::parse_file(const char * dictpath, const char * etype)
54 int i;
55 char line[MAXDICTENTRYLEN+1];
56 dictentry * pdict = pdentry;
58 // open the dictionary list file
59 FILE * dictlst;
60 dictlst = fopen(dictpath,"r");
61 if (!dictlst) {
62 return 1;
65 // step one is to parse the dictionary list building up the
66 // descriptive structures
68 // read in each line ignoring any that dont start with etype
69 while (fgets(line,MAXDICTENTRYLEN,dictlst)) {
70 mychomp(line);
72 /* parse in a dictionary entry */
73 if (strncmp(line,etype,4) == 0) {
74 if (numdict < MAXDICTIONARIES) {
75 char * tp = line;
76 char * piece;
77 i = 0;
78 while ((piece=mystrsep(&tp,' '))) {
79 if (*piece != '\0') {
80 switch(i) {
81 case 0: break;
82 case 1: pdict->lang = mystrdup(piece); break;
83 case 2: if (strcmp (piece, "ANY") == 0)
84 pdict->region = mystrdup("");
85 else
86 pdict->region = mystrdup(piece);
87 break;
88 case 3: pdict->filename = mystrdup(piece); break;
89 default: break;
91 i++;
93 free(piece);
95 if (i == 4) {
96 numdict++;
97 pdict++;
98 } else {
99 switch (i) {
100 case 3:
101 free(pdict->region);
102 pdict->region=NULL;
103 case 2: //deliberate fallthrough
104 free(pdict->lang);
105 pdict->lang=NULL;
106 default:
107 break;
109 fprintf(stderr,"dictionary list corruption in line \"%s\"\n",line);
110 fflush(stderr);
115 fclose(dictlst);
116 return 0;
119 // return text encoding of dictionary
120 int DictMgr::get_list(dictentry ** ppentry)
122 *ppentry = pdentry;
123 return numdict;
128 // strip strings into token based on single char delimiter
129 // acts like strsep() but only uses a delim char and not
130 // a delim string
132 char * DictMgr::mystrsep(char ** stringp, const char delim)
134 char * rv = NULL;
135 char * mp = *stringp;
136 size_t n = strlen(mp);
137 if (n > 0) {
138 char * dp = (char *)memchr(mp,(int)((unsigned char)delim),n);
139 if (dp) {
140 *stringp = dp+1;
141 size_t nc = dp - mp;
142 rv = (char *) malloc(nc+1);
143 if (rv) {
144 memcpy(rv,mp,nc);
145 *(rv+nc) = '\0';
147 } else {
148 rv = (char *) malloc(n+1);
149 if (rv) {
150 memcpy(rv, mp, n);
151 *(rv+n) = '\0';
152 *stringp = mp + n;
156 return rv;
160 // replaces strdup with ansi version
161 char * DictMgr::mystrdup(const char * s)
163 char * d = NULL;
164 if (s) {
165 int sl = strlen(s)+1;
166 d = (char *) malloc(sl);
167 if (d) memcpy(d,s,sl);
169 return d;
173 // remove cross-platform text line end characters
174 void DictMgr:: mychomp(char * s)
176 int k = strlen(s);
177 if ((k > 0) && ((*(s+k-1)=='\r') || (*(s+k-1)=='\n'))) *(s+k-1) = '\0';
178 if ((k > 1) && (*(s+k-2) == '\r')) *(s+k-2) = '\0';