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.

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

mercurial