|
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 *******/ |
|
34 |
|
35 #include <stdlib.h> |
|
36 #include <string.h> |
|
37 #include <stdio.h> |
|
38 |
|
39 #include "replist.hxx" |
|
40 #include "csutil.hxx" |
|
41 |
|
42 RepList::RepList(int n) { |
|
43 dat = (replentry **) malloc(sizeof(replentry *) * n); |
|
44 if (dat == 0) size = 0; else size = n; |
|
45 pos = 0; |
|
46 } |
|
47 |
|
48 RepList::~RepList() |
|
49 { |
|
50 for (int i = 0; i < pos; i++) { |
|
51 free(dat[i]->pattern); |
|
52 free(dat[i]->pattern2); |
|
53 free(dat[i]); |
|
54 } |
|
55 free(dat); |
|
56 } |
|
57 |
|
58 int RepList::get_pos() { |
|
59 return pos; |
|
60 } |
|
61 |
|
62 replentry * RepList::item(int n) { |
|
63 return dat[n]; |
|
64 } |
|
65 |
|
66 int RepList::near(const char * word) { |
|
67 int p1 = 0; |
|
68 int p2 = pos; |
|
69 while ((p2 - p1) > 1) { |
|
70 int m = (p1 + p2) / 2; |
|
71 int c = strcmp(word, dat[m]->pattern); |
|
72 if (c <= 0) { |
|
73 if (c < 0) p2 = m; else p1 = p2 = m; |
|
74 } else p1 = m; |
|
75 } |
|
76 return p1; |
|
77 } |
|
78 |
|
79 int RepList::match(const char * word, int n) { |
|
80 if (strncmp(word, dat[n]->pattern, strlen(dat[n]->pattern)) == 0) return strlen(dat[n]->pattern); |
|
81 return 0; |
|
82 } |
|
83 |
|
84 int RepList::add(char * pat1, char * pat2) { |
|
85 if (pos >= size || pat1 == NULL || pat2 == NULL) return 1; |
|
86 replentry * r = (replentry *) malloc(sizeof(replentry)); |
|
87 if (r == NULL) return 1; |
|
88 r->pattern = mystrrep(pat1, "_", " "); |
|
89 r->pattern2 = mystrrep(pat2, "_", " "); |
|
90 r->start = false; |
|
91 r->end = false; |
|
92 dat[pos++] = r; |
|
93 for (int i = pos - 1; i > 0; i--) { |
|
94 r = dat[i]; |
|
95 if (strcmp(r->pattern, dat[i - 1]->pattern) < 0) { |
|
96 dat[i] = dat[i - 1]; |
|
97 dat[i - 1] = r; |
|
98 } else break; |
|
99 } |
|
100 return 0; |
|
101 } |
|
102 |
|
103 int RepList::conv(const char * word, char * dest) { |
|
104 int stl = 0; |
|
105 int change = 0; |
|
106 for (size_t i = 0; i < strlen(word); i++) { |
|
107 int n = near(word + i); |
|
108 int l = match(word + i, n); |
|
109 if (l) { |
|
110 strcpy(dest + stl, dat[n]->pattern2); |
|
111 stl += strlen(dat[n]->pattern2); |
|
112 i += l - 1; |
|
113 change = 1; |
|
114 } else dest[stl++] = word[i]; |
|
115 } |
|
116 dest[stl] = '\0'; |
|
117 return change; |
|
118 } |