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