~ubuntu-branches/debian/wheezy/mudlet/wheezy

« back to all changes in this revision

Viewing changes to src/hunspell/dictmgr.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Craig Small
  • Date: 2011-05-14 20:12:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110514201249-184gqx5jjqam02lg
Tags: 2.0-rc5-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
#include <stdlib.h>
3
 
#include <string.h>
4
 
#include <ctype.h>
5
 
#include <stdio.h>
6
 
 
7
 
#include "dictmgr.hxx"
8
 
 
9
 
DictMgr::DictMgr(const char * dictpath, const char * etype) : numdict(0)
10
 
{
11
 
  // load list of etype entries
12
 
  pdentry = (dictentry *)malloc(MAXDICTIONARIES*sizeof(struct dictentry));
13
 
  if (pdentry) {
14
 
     if (parse_file(dictpath, etype)) {
15
 
        numdict = 0;
16
 
        // no dictionary.lst found is okay
17
 
     }
18
 
  }
19
 
}
20
 
 
21
 
 
22
 
DictMgr::~DictMgr() 
23
 
{
24
 
  dictentry * pdict = NULL;
25
 
  if (pdentry) {
26
 
     pdict = pdentry;
27
 
     for (int i=0;i<numdict;i++) {
28
 
        if (pdict->lang) {
29
 
            free(pdict->lang);
30
 
            pdict->lang = NULL;
31
 
        }
32
 
        if (pdict->region) {
33
 
            free(pdict->region);
34
 
            pdict->region=NULL;
35
 
        }
36
 
        if (pdict->filename) {
37
 
            free(pdict->filename);
38
 
            pdict->filename = NULL;
39
 
        }
40
 
        pdict++;
41
 
     }
42
 
     free(pdentry);
43
 
     pdentry = NULL;
44
 
     pdict = NULL;
45
 
  }
46
 
  numdict = 0;
47
 
}
48
 
 
49
 
 
50
 
// read in list of etype entries and build up structure to describe them
51
 
int  DictMgr::parse_file(const char * dictpath, const char * etype)
52
 
{
53
 
 
54
 
    int i;
55
 
    char line[MAXDICTENTRYLEN+1];
56
 
    dictentry * pdict = pdentry;
57
 
 
58
 
    // open the dictionary list file
59
 
    FILE * dictlst;
60
 
    dictlst = fopen(dictpath,"r");
61
 
    if (!dictlst) {
62
 
      return 1;
63
 
    }
64
 
 
65
 
    // step one is to parse the dictionary list building up the 
66
 
    // descriptive structures
67
 
 
68
 
    // read in each line ignoring any that dont start with etype
69
 
    while (fgets(line,MAXDICTENTRYLEN,dictlst)) {
70
 
       mychomp(line);
71
 
 
72
 
       /* parse in a dictionary entry */
73
 
       if (strncmp(line,etype,4) == 0) {
74
 
          if (numdict < MAXDICTIONARIES) {
75
 
             char * tp = line;
76
 
             char * piece;
77
 
             i = 0;
78
 
             while ((piece=mystrsep(&tp,' '))) {
79
 
                if (*piece != '\0') {
80
 
                    switch(i) {
81
 
                       case 0: break;
82
 
                       case 1: pdict->lang = mystrdup(piece); break;
83
 
                       case 2: if (strcmp (piece, "ANY") == 0)
84
 
                                 pdict->region = mystrdup("");
85
 
                               else
86
 
                                 pdict->region = mystrdup(piece);
87
 
                               break;
88
 
                       case 3: pdict->filename = mystrdup(piece); break;
89
 
                       default: break;
90
 
                    }
91
 
                    i++;
92
 
                }
93
 
                free(piece);
94
 
             }
95
 
             if (i == 4) {
96
 
                 numdict++;
97
 
                 pdict++;
98
 
             } else {
99
 
                 switch (i) {
100
 
                    case 3:
101
 
                       free(pdict->region);
102
 
                       pdict->region=NULL;
103
 
                    case 2: //deliberate fallthrough
104
 
                       free(pdict->lang);
105
 
                       pdict->lang=NULL;
106
 
                    default:
107
 
                        break;
108
 
                 }
109
 
                 fprintf(stderr,"dictionary list corruption in line \"%s\"\n",line);
110
 
                 fflush(stderr);
111
 
             }
112
 
          }
113
 
       }
114
 
    }
115
 
    fclose(dictlst);
116
 
    return 0;
117
 
}
118
 
 
119
 
// return text encoding of dictionary
120
 
int DictMgr::get_list(dictentry ** ppentry)
121
 
{
122
 
  *ppentry = pdentry;
123
 
  return numdict;
124
 
}
125
 
 
126
 
 
127
 
 
128
 
// strip strings into token based on single char delimiter
129
 
// acts like strsep() but only uses a delim char and not 
130
 
// a delim string
131
 
 
132
 
char * DictMgr::mystrsep(char ** stringp, const char delim)
133
 
{
134
 
  char * rv = NULL;
135
 
  char * mp = *stringp;
136
 
  size_t n = strlen(mp);
137
 
  if (n > 0) {
138
 
     char * dp = (char *)memchr(mp,(int)((unsigned char)delim),n);
139
 
     if (dp) {
140
 
        *stringp = dp+1;
141
 
        size_t nc = dp - mp; 
142
 
        rv = (char *) malloc(nc+1);
143
 
        if (rv) {
144
 
           memcpy(rv,mp,nc);
145
 
           *(rv+nc) = '\0';
146
 
        }
147
 
     } else {
148
 
       rv = (char *) malloc(n+1);
149
 
       if (rv) {
150
 
          memcpy(rv, mp, n);
151
 
          *(rv+n) = '\0';
152
 
          *stringp = mp + n;
153
 
       }
154
 
     }
155
 
  }
156
 
  return rv;
157
 
}
158
 
 
159
 
 
160
 
// replaces strdup with ansi version
161
 
char * DictMgr::mystrdup(const char * s)
162
 
{
163
 
  char * d = NULL;
164
 
  if (s) {
165
 
     int sl = strlen(s)+1;
166
 
     d = (char *) malloc(sl);
167
 
     if (d) memcpy(d,s,sl);
168
 
  }
169
 
  return d;
170
 
}
171
 
 
172
 
 
173
 
// remove cross-platform text line end characters
174
 
void DictMgr:: mychomp(char * s)
175
 
{
176
 
  int k = strlen(s);
177
 
  if ((k > 0) && ((*(s+k-1)=='\r') || (*(s+k-1)=='\n'))) *(s+k-1) = '\0';
178
 
  if ((k > 1) && (*(s+k-2) == '\r')) *(s+k-2) = '\0';
179
 
}
180