1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/spellcheck/hunspell/src/dictmgr.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,213 @@ 1.4 +/******* BEGIN LICENSE BLOCK ******* 1.5 + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 1.6 + * 1.7 + * The contents of this file are subject to the Mozilla Public License Version 1.8 + * 1.1 (the "License"); you may not use this file except in compliance with 1.9 + * the License. You may obtain a copy of the License at 1.10 + * http://www.mozilla.org/MPL/ 1.11 + * 1.12 + * Software distributed under the License is distributed on an "AS IS" basis, 1.13 + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 1.14 + * for the specific language governing rights and limitations under the 1.15 + * License. 1.16 + * 1.17 + * The Initial Developers of the Original Code are Kevin Hendricks (MySpell) 1.18 + * and László Németh (Hunspell). Portions created by the Initial Developers 1.19 + * are Copyright (C) 2002-2005 the Initial Developers. All Rights Reserved. 1.20 + * 1.21 + * Contributor(s): László Németh (nemethl@gyorsposta.hu) 1.22 + * Caolan McNamara (caolanm@redhat.com) 1.23 + * 1.24 + * Alternatively, the contents of this file may be used under the terms of 1.25 + * either the GNU General Public License Version 2 or later (the "GPL"), or 1.26 + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 1.27 + * in which case the provisions of the GPL or the LGPL are applicable instead 1.28 + * of those above. If you wish to allow use of your version of this file only 1.29 + * under the terms of either the GPL or the LGPL, and not to allow others to 1.30 + * use your version of this file under the terms of the MPL, indicate your 1.31 + * decision by deleting the provisions above and replace them with the notice 1.32 + * and other provisions required by the GPL or the LGPL. If you do not delete 1.33 + * the provisions above, a recipient may use your version of this file under 1.34 + * the terms of any one of the MPL, the GPL or the LGPL. 1.35 + * 1.36 + ******* END LICENSE BLOCK *******/ 1.37 + 1.38 +#include <stdlib.h> 1.39 +#include <string.h> 1.40 +#include <ctype.h> 1.41 +#include <stdio.h> 1.42 + 1.43 +#include "dictmgr.hxx" 1.44 + 1.45 +DictMgr::DictMgr(const char * dictpath, const char * etype) : numdict(0) 1.46 +{ 1.47 + // load list of etype entries 1.48 + pdentry = (dictentry *)malloc(MAXDICTIONARIES*sizeof(struct dictentry)); 1.49 + if (pdentry) { 1.50 + if (parse_file(dictpath, etype)) { 1.51 + numdict = 0; 1.52 + // no dictionary.lst found is okay 1.53 + } 1.54 + } 1.55 +} 1.56 + 1.57 + 1.58 +DictMgr::~DictMgr() 1.59 +{ 1.60 + dictentry * pdict = NULL; 1.61 + if (pdentry) { 1.62 + pdict = pdentry; 1.63 + for (int i=0;i<numdict;i++) { 1.64 + if (pdict->lang) { 1.65 + free(pdict->lang); 1.66 + pdict->lang = NULL; 1.67 + } 1.68 + if (pdict->region) { 1.69 + free(pdict->region); 1.70 + pdict->region=NULL; 1.71 + } 1.72 + if (pdict->filename) { 1.73 + free(pdict->filename); 1.74 + pdict->filename = NULL; 1.75 + } 1.76 + pdict++; 1.77 + } 1.78 + free(pdentry); 1.79 + pdentry = NULL; 1.80 + pdict = NULL; 1.81 + } 1.82 + numdict = 0; 1.83 +} 1.84 + 1.85 + 1.86 +// read in list of etype entries and build up structure to describe them 1.87 +int DictMgr::parse_file(const char * dictpath, const char * etype) 1.88 +{ 1.89 + 1.90 + int i; 1.91 + char line[MAXDICTENTRYLEN+1]; 1.92 + dictentry * pdict = pdentry; 1.93 + 1.94 + // open the dictionary list file 1.95 + FILE * dictlst; 1.96 + dictlst = fopen(dictpath,"r"); 1.97 + if (!dictlst) { 1.98 + return 1; 1.99 + } 1.100 + 1.101 + // step one is to parse the dictionary list building up the 1.102 + // descriptive structures 1.103 + 1.104 + // read in each line ignoring any that dont start with etype 1.105 + while (fgets(line,MAXDICTENTRYLEN,dictlst)) { 1.106 + mychomp(line); 1.107 + 1.108 + /* parse in a dictionary entry */ 1.109 + if (strncmp(line,etype,4) == 0) { 1.110 + if (numdict < MAXDICTIONARIES) { 1.111 + char * tp = line; 1.112 + char * piece; 1.113 + i = 0; 1.114 + while ((piece=mystrsep(&tp,' '))) { 1.115 + if (*piece != '\0') { 1.116 + switch(i) { 1.117 + case 0: break; 1.118 + case 1: pdict->lang = mystrdup(piece); break; 1.119 + case 2: if (strcmp (piece, "ANY") == 0) 1.120 + pdict->region = mystrdup(""); 1.121 + else 1.122 + pdict->region = mystrdup(piece); 1.123 + break; 1.124 + case 3: pdict->filename = mystrdup(piece); break; 1.125 + default: break; 1.126 + } 1.127 + i++; 1.128 + } 1.129 + free(piece); 1.130 + } 1.131 + if (i == 4) { 1.132 + numdict++; 1.133 + pdict++; 1.134 + } else { 1.135 + switch (i) { 1.136 + case 3: 1.137 + free(pdict->region); 1.138 + pdict->region=NULL; 1.139 + case 2: //deliberate fallthrough 1.140 + free(pdict->lang); 1.141 + pdict->lang=NULL; 1.142 + default: 1.143 + break; 1.144 + } 1.145 + fprintf(stderr,"dictionary list corruption in line \"%s\"\n",line); 1.146 + fflush(stderr); 1.147 + } 1.148 + } 1.149 + } 1.150 + } 1.151 + fclose(dictlst); 1.152 + return 0; 1.153 +} 1.154 + 1.155 +// return text encoding of dictionary 1.156 +int DictMgr::get_list(dictentry ** ppentry) 1.157 +{ 1.158 + *ppentry = pdentry; 1.159 + return numdict; 1.160 +} 1.161 + 1.162 + 1.163 + 1.164 +// strip strings into token based on single char delimiter 1.165 +// acts like strsep() but only uses a delim char and not 1.166 +// a delim string 1.167 + 1.168 +char * DictMgr::mystrsep(char ** stringp, const char delim) 1.169 +{ 1.170 + char * rv = NULL; 1.171 + char * mp = *stringp; 1.172 + size_t n = strlen(mp); 1.173 + if (n > 0) { 1.174 + char * dp = (char *)memchr(mp,(int)((unsigned char)delim),n); 1.175 + if (dp) { 1.176 + *stringp = dp+1; 1.177 + size_t nc = dp - mp; 1.178 + rv = (char *) malloc(nc+1); 1.179 + if (rv) { 1.180 + memcpy(rv,mp,nc); 1.181 + *(rv+nc) = '\0'; 1.182 + } 1.183 + } else { 1.184 + rv = (char *) malloc(n+1); 1.185 + if (rv) { 1.186 + memcpy(rv, mp, n); 1.187 + *(rv+n) = '\0'; 1.188 + *stringp = mp + n; 1.189 + } 1.190 + } 1.191 + } 1.192 + return rv; 1.193 +} 1.194 + 1.195 + 1.196 +// replaces strdup with ansi version 1.197 +char * DictMgr::mystrdup(const char * s) 1.198 +{ 1.199 + char * d = NULL; 1.200 + if (s) { 1.201 + int sl = strlen(s)+1; 1.202 + d = (char *) malloc(sl); 1.203 + if (d) memcpy(d,s,sl); 1.204 + } 1.205 + return d; 1.206 +} 1.207 + 1.208 + 1.209 +// remove cross-platform text line end characters 1.210 +void DictMgr:: mychomp(char * s) 1.211 +{ 1.212 + int k = strlen(s); 1.213 + if ((k > 0) && ((*(s+k-1)=='\r') || (*(s+k-1)=='\n'))) *(s+k-1) = '\0'; 1.214 + if ((k > 1) && (*(s+k-2) == '\r')) *(s+k-2) = '\0'; 1.215 +} 1.216 +