extensions/spellcheck/hunspell/src/dictmgr.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial