1 /******* BEGIN LICENSE BLOCK *******
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
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
46 #include "dictmgr.hxx"
48 #ifndef MOZILLA_CLIENT
54 DictMgr::DictMgr(const char * dictpath
, const char * etype
)
56 // load list of etype entries
58 pdentry
= (dictentry
*)malloc(MAXDICTIONARIES
*sizeof(struct dictentry
));
60 if (parse_file(dictpath
, etype
)) {
62 // no dictionary.lst found is okay
72 dictentry
* pdict
= NULL
;
75 for (int i
=0;i
<numdict
;i
++) {
84 if (pdict
->filename
) {
85 free(pdict
->filename
);
86 pdict
->filename
= NULL
;
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
)
103 char line
[MAXDICTENTRYLEN
+1];
104 dictentry
* pdict
= pdentry
;
106 // open the dictionary list file
108 dictlst
= fopen(dictpath
,"r");
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
)) {
120 /* parse in a dictionary entry */
121 if (strncmp(line
,etype
,4) == 0) {
122 if (numdict
< MAXDICTIONARIES
) {
126 while ((piece
=mystrsep(&tp
,' '))) {
127 if (*piece
!= '\0') {
130 case 1: pdict
->lang
= mystrdup(piece
); break;
131 case 2: if (strcmp (piece
, "ANY") == 0)
132 pdict
->region
= mystrdup("");
134 pdict
->region
= mystrdup(piece
);
136 case 3: pdict
->filename
= mystrdup(piece
); break;
147 fprintf(stderr
,"dictionary list corruption in line \"%s\"\n",line
);
157 // return text encoding of dictionary
158 int DictMgr::get_list(dictentry
** ppentry
)
166 // strip strings into token based on single char delimiter
167 // acts like strsep() but only uses a delim char and not
170 char * DictMgr::mystrsep(char ** stringp
, const char delim
)
173 char * mp
= *stringp
;
176 char * dp
= (char *)memchr(mp
,(int)((unsigned char)delim
),n
);
179 int nc
= (int)((unsigned long)dp
- (unsigned long)mp
);
180 rv
= (char *) malloc(nc
+1);
187 rv
= (char *) malloc(n
+1);
200 // replaces strdup with ansi version
201 char * DictMgr::mystrdup(const char * s
)
206 d
= (char *) malloc(((sl
+1) * sizeof(char)));
207 if (d
) memcpy(d
,s
,((sl
+1)*sizeof(char)));
213 // remove cross-platform text line end characters
214 void DictMgr:: mychomp(char * 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';