| OLD | NEW |
| (Empty) | |
| 1 #include "license.hunspell" |
| 2 #include "license.myspell" |
| 3 |
| 4 #ifndef MOZILLA_CLIENT |
| 5 #include <cstdlib> |
| 6 #include <cstring> |
| 7 #include <cstdio> |
| 8 #else |
| 9 #include <stdlib.h> |
| 10 #include <string.h> |
| 11 #include <stdio.h> |
| 12 #endif |
| 13 |
| 14 #include "replist.hxx" |
| 15 #include "csutil.hxx" |
| 16 |
| 17 RepList::RepList(int n) { |
| 18 dat = (replentry **) malloc(sizeof(replentry *) * n); |
| 19 if (dat == 0) size = 0; else size = n; |
| 20 pos = 0; |
| 21 } |
| 22 |
| 23 RepList::~RepList() |
| 24 { |
| 25 for (int i = 0; i < pos; i++) { |
| 26 free(dat[i]->pattern); |
| 27 free(dat[i]->pattern2); |
| 28 free(dat[i]); |
| 29 } |
| 30 free(dat); |
| 31 } |
| 32 |
| 33 int RepList::get_pos() { |
| 34 return pos; |
| 35 } |
| 36 |
| 37 replentry * RepList::item(int n) { |
| 38 return dat[n]; |
| 39 } |
| 40 |
| 41 int RepList::near(const char * word) { |
| 42 int p1 = 0; |
| 43 int p2 = pos; |
| 44 while ((p2 - p1) > 1) { |
| 45 int m = (p1 + p2) / 2; |
| 46 // fprintf(stderr, "m: %d p1: %d p2: %d dat: %s\n", m, p1, p2, dat[m]->patt
ern); |
| 47 int c = strcmp(word, dat[m]->pattern); |
| 48 if (c <= 0) { |
| 49 if (c < 0) p2 = m; else p1 = p2 = m; |
| 50 } else p1 = m; |
| 51 } |
| 52 // fprintf(stderr, "NEAR: %s (word: %s)\n", dat[p1]->pattern, word); |
| 53 return p1; |
| 54 } |
| 55 |
| 56 int RepList::match(const char * word, int n) { |
| 57 if (strncmp(word, dat[n]->pattern, strlen(dat[n]->pattern)) == 0) return str
len(dat[n]->pattern); |
| 58 return 0; |
| 59 } |
| 60 |
| 61 int RepList::add(char * pat1, char * pat2) { |
| 62 if (pos >= size || pat1 == NULL || pat2 == NULL) return 1; |
| 63 replentry * r = (replentry *) malloc(sizeof(replentry)); |
| 64 if (r == NULL) return 1; |
| 65 r->pattern = mystrrep(pat1, "_", " "); |
| 66 r->pattern2 = mystrrep(pat2, "_", " "); |
| 67 dat[pos++] = r; |
| 68 for (int i = pos - 1; i > 0; i--) { |
| 69 r = dat[i]; |
| 70 if (strcmp(r->pattern, dat[i - 1]->pattern) < 0) { |
| 71 dat[i] = dat[i - 1]; |
| 72 dat[i - 1] = r; |
| 73 } else break; |
| 74 } |
| 75 return 0; |
| 76 } |
| 77 |
| 78 int RepList::conv(const char * word, char * dest) { |
| 79 int stl = 0; |
| 80 int change = 0; |
| 81 // for (int i = 0; i < pos; i++) fprintf(stderr, "%d. %s\n", i, dat[i]->patte
rn); |
| 82 for (int i = 0; i < strlen(word); i++) { |
| 83 int n = near(word + i); |
| 84 int l = match(word + i, n); |
| 85 if (l) { |
| 86 strcpy(dest + stl, dat[n]->pattern2); |
| 87 stl += strlen(dat[n]->pattern2); |
| 88 i += l - 1; |
| 89 change = 1; |
| 90 } else dest[stl++] = word[i]; |
| 91 } |
| 92 dest[stl] = '\0'; |
| 93 // fprintf(stderr, "i: %s o: %s change: %d\n", word, dest, change); |
| 94 return change; |
| 95 } |
| OLD | NEW |