Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / extensions / spellcheck / hunspell / src / dictmgr.cpp
blob57fed3d905ddce1571a317ec550c830cf6b05c46
1 /******* BEGIN LICENSE BLOCK *******
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Initial Developers of the Original Code are Kevin Hendricks (MySpell)
15 * and László Németh (Hunspell). Portions created by the Initial Developers
16 * are Copyright (C) 2002-2005 the Initial Developers. All Rights Reserved.
18 * Contributor(s): László Németh (nemethl@gyorsposta.hu)
20 * Alternatively, the contents of this file may be used under the terms of
21 * either the GNU General Public License Version 2 or later (the "GPL"), or
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the GPL or the LGPL are applicable instead
24 * of those above. If you wish to allow use of your version of this file only
25 * under the terms of either the GPL or the LGPL, and not to allow others to
26 * use your version of this file under the terms of the MPL, indicate your
27 * decision by deleting the provisions above and replace them with the notice
28 * and other provisions required by the GPL or the LGPL. If you do not delete
29 * the provisions above, a recipient may use your version of this file under
30 * the terms of any one of the MPL, the GPL or the LGPL.
32 ******* END LICENSE BLOCK *******/
34 #ifndef MOZILLA_CLIENT
35 #include <cstdlib>
36 #include <cstring>
37 #include <cstdio>
38 #include <cctype>
39 #else
40 #include <stdlib.h>
41 #include <string.h>
42 #include <stdio.h>
43 #include <ctype.h>
44 #endif
46 #include "dictmgr.hxx"
48 #ifndef MOZILLA_CLIENT
49 #ifndef W32
50 using namespace std;
51 #endif
52 #endif
54 DictMgr::DictMgr(const char * dictpath, const char * etype)
56 // load list of etype entries
57 numdict = 0;
58 pdentry = (dictentry *)malloc(MAXDICTIONARIES*sizeof(struct dictentry));
59 if (pdentry) {
60 if (parse_file(dictpath, etype)) {
61 numdict = 0;
62 // no dictionary.lst found is okay
64 } else {
65 numdict = 0;
70 DictMgr::~DictMgr()
72 dictentry * pdict = NULL;
73 if (pdentry) {
74 pdict = pdentry;
75 for (int i=0;i<numdict;i++) {
76 if (pdict->lang) {
77 free(pdict->lang);
78 pdict->lang = NULL;
80 if (pdict->region) {
81 free(pdict->region);
82 pdict->region=NULL;
84 if (pdict->filename) {
85 free(pdict->filename);
86 pdict->filename = NULL;
88 pdict++;
90 free(pdentry);
91 pdentry = NULL;
92 pdict = NULL;
94 numdict = 0;
98 // read in list of etype entries and build up structure to describe them
99 int DictMgr::parse_file(const char * dictpath, const char * etype)
102 int i;
103 char line[MAXDICTENTRYLEN+1];
104 dictentry * pdict = pdentry;
106 // open the dictionary list file
107 FILE * dictlst;
108 dictlst = fopen(dictpath,"r");
109 if (!dictlst) {
110 return 1;
113 // step one is to parse the dictionary list building up the
114 // descriptive structures
116 // read in each line ignoring any that dont start with etype
117 while (fgets(line,MAXDICTENTRYLEN,dictlst)) {
118 mychomp(line);
120 /* parse in a dictionary entry */
121 if (strncmp(line,etype,4) == 0) {
122 if (numdict < MAXDICTIONARIES) {
123 char * tp = line;
124 char * piece;
125 i = 0;
126 while ((piece=mystrsep(&tp,' '))) {
127 if (*piece != '\0') {
128 switch(i) {
129 case 0: break;
130 case 1: pdict->lang = mystrdup(piece); break;
131 case 2: if (strcmp (piece, "ANY") == 0)
132 pdict->region = mystrdup("");
133 else
134 pdict->region = mystrdup(piece);
135 break;
136 case 3: pdict->filename = mystrdup(piece); break;
137 default: break;
139 i++;
141 free(piece);
143 if (i == 4) {
144 numdict++;
145 pdict++;
146 } else {
147 fprintf(stderr,"dictionary list corruption in line \"%s\"\n",line);
148 fflush(stderr);
153 fclose(dictlst);
154 return 0;
157 // return text encoding of dictionary
158 int DictMgr::get_list(dictentry ** ppentry)
160 *ppentry = pdentry;
161 return numdict;
166 // strip strings into token based on single char delimiter
167 // acts like strsep() but only uses a delim char and not
168 // a delim string
170 char * DictMgr::mystrsep(char ** stringp, const char delim)
172 char * rv = NULL;
173 char * mp = *stringp;
174 int n = strlen(mp);
175 if (n > 0) {
176 char * dp = (char *)memchr(mp,(int)((unsigned char)delim),n);
177 if (dp) {
178 *stringp = dp+1;
179 int nc = (int)((unsigned long)dp - (unsigned long)mp);
180 rv = (char *) malloc(nc+1);
181 if (rv) {
182 memcpy(rv,mp,nc);
183 *(rv+nc) = '\0';
184 return rv;
186 } else {
187 rv = (char *) malloc(n+1);
188 if (rv) {
189 memcpy(rv, mp, n);
190 *(rv+n) = '\0';
191 *stringp = mp + n;
192 return rv;
196 return NULL;
200 // replaces strdup with ansi version
201 char * DictMgr::mystrdup(const char * s)
203 char * d = NULL;
204 if (s) {
205 int sl = strlen(s);
206 d = (char *) malloc(((sl+1) * sizeof(char)));
207 if (d) memcpy(d,s,((sl+1)*sizeof(char)));
209 return d;
213 // remove cross-platform text line end characters
214 void DictMgr:: mychomp(char * s)
216 int k = strlen(s);
217 if ((k > 0) && ((*(s+k-1)=='\r') || (*(s+k-1)=='\n'))) *(s+k-1) = '\0';
218 if ((k > 1) && (*(s+k-2) == '\r')) *(s+k-2) = '\0';