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
44 #include "replist.hxx"
47 RepList::RepList(int n
) {
48 dat
= (replentry
**) malloc(sizeof(replentry
*) * n
);
49 if (dat
== 0) size
= 0; else size
= n
;
55 for (int i
= 0; i
< pos
; i
++) {
56 free(dat
[i
]->pattern
);
57 free(dat
[i
]->pattern2
);
63 int RepList::get_pos() {
67 replentry
* RepList::item(int n
) {
71 int RepList::near(const char * word
) {
74 while ((p2
- p1
) > 1) {
75 int m
= (p1
+ p2
) / 2;
76 // fprintf(stderr, "m: %d p1: %d p2: %d dat: %s\n", m, p1, p2, dat[m]->pattern);
77 int c
= strcmp(word
, dat
[m
]->pattern
);
79 if (c
< 0) p2
= m
; else p1
= p2
= m
;
82 // fprintf(stderr, "NEAR: %s (word: %s)\n", dat[p1]->pattern, word);
86 int RepList::match(const char * word
, int n
) {
87 if (strncmp(word
, dat
[n
]->pattern
, strlen(dat
[n
]->pattern
)) == 0) return strlen(dat
[n
]->pattern
);
91 int RepList::add(char * pat1
, char * pat2
) {
92 if (pos
>= size
|| pat1
== NULL
|| pat2
== NULL
) return 1;
93 replentry
* r
= (replentry
*) malloc(sizeof(replentry
));
94 if (r
== NULL
) return 1;
95 r
->pattern
= mystrrep(pat1
, "_", " ");
96 r
->pattern2
= mystrrep(pat2
, "_", " ");
98 for (int i
= pos
- 1; i
> 0; i
--) {
100 if (strcmp(r
->pattern
, dat
[i
- 1]->pattern
) < 0) {
108 int RepList::conv(const char * word
, char * dest
) {
111 // for (int i = 0; i < pos; i++) fprintf(stderr, "%d. %s\n", i, dat[i]->pattern);
112 for (int i
= 0; i
< strlen(word
); i
++) {
113 int n
= near(word
+ i
);
114 int l
= match(word
+ i
, n
);
116 strcpy(dest
+ stl
, dat
[n
]->pattern2
);
117 stl
+= strlen(dat
[n
]->pattern2
);
120 } else dest
[stl
++] = word
[i
];
123 // fprintf(stderr, "i: %s o: %s change: %d\n", word, dest, change);