1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/* This file is part of The New Aspell
* Copyright (C) 2000-2001 by Kevin Atkinson under the GNU LGPL
* license version 2.0 or 2.1. You should have received a copy of the
* LGPL license along with this library if you did not you can find it
* at http://www.gnu.org/.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "aspell.h"
static void print_word_list(AspellSpeller * speller,
const AspellWordList *wl,
char delem)
{
if (wl == 0) {
printf("Error: %s\n", aspell_speller_error_message(speller));
} else {
AspellStringEnumeration * els = aspell_word_list_elements(wl);
const char * word;
while ( (word = aspell_string_enumeration_next(els)) != 0) {
fputs(word, stdout);
putc(delem, stdout);
}
delete_aspell_string_enumeration(els);
}
}
#define check_for_error(speller) \
if (aspell_speller_error(speller) != 0) { \
printf("Error: %s\n", aspell_speller_error_message(speller)); \
break; \
}
#define check_for_config_error(config) \
if (aspell_config_error(config) != 0) { \
printf("Error: %s\n", aspell_config_error_message(config)); \
break; \
}
static void check_document(AspellSpeller * speller, const char * file);
int main(int argc, const char *argv[])
{
AspellCanHaveError * ret;
AspellSpeller * speller;
int have;
char word[81];
char * p;
char * word_end;
AspellConfig * config;
if (argc < 2) {
printf("Usage: %s <language> [<size>|- [[<jargon>|- [<encoding>]]]\n", argv[0]);
return 1;
}
config = new_aspell_config();
aspell_config_replace(config, "lang", argv[1]);
if (argc >= 3 && argv[2][0] != '-' && argv[2][1] != '\0')
aspell_config_replace(config, "size", argv[2]);
if (argc >= 4 && argv[3][0] != '-')
aspell_config_replace(config, "jargon", argv[3]);
if (argc >= 5 && argv[4][0] != '-')
aspell_config_replace(config, "encoding", argv[4]);
ret = new_aspell_speller(config);
delete_aspell_config(config);
if (aspell_error(ret) != 0) {
printf("Error: %s\n",aspell_error_message(ret));
delete_aspell_can_have_error(ret);
return 2;
}
speller = to_aspell_speller(ret);
config = aspell_speller_config(speller);
fputs("Using: ", stdout);
fputs(aspell_config_retrieve(config, "lang"), stdout);
fputs("-", stdout);
fputs(aspell_config_retrieve(config, "jargon"), stdout);
fputs("-", stdout);
fputs(aspell_config_retrieve(config, "size"), stdout);
fputs("-", stdout);
fputs(aspell_config_retrieve(config, "module"), stdout);
fputs("\n\n", stdout);
puts("Type \"h\" for help.\n");
while (fgets(word, 80, stdin) != 0) {
/* remove trailing spaces */
word_end = strchr(word, '\0') - 1;
while (word_end != word && (*word_end == '\n' || *word_end == ' '))
--word_end;
++word_end;
*word_end = '\0';
putchar('\n');
switch (word[0]) {
case '\0':
break;
case 'h':
puts(
"Usage: \n"
" h(elp) help\n"
" c <word> check if a word is the correct spelling\n"
" s <word> print out a list of suggestions for a word\n"
" a <word> add a word to the personal word list\n"
" i <word> ignore a word for the rest of the session\n"
" d <file> spell checks a document\n"
" p dumps the personal word list\n"
" P dumps the session word list\n"
" m dumps the main word list\n"
" o <option> <value> sets a config option\n"
" r <option> retrieves a config option\n"
" l <option> retrieves a config option as a list\n"
" S saves all word lists\n"
" C clear the current session word list\n"
" x quit\n" );
break;
case 'p':
print_word_list(speller,
aspell_speller_personal_word_list(speller), '\n');
break;
case 'P':
print_word_list(speller,
aspell_speller_session_word_list(speller), '\n');
break;
case 'm':
print_word_list(speller,
aspell_speller_main_word_list(speller), '\n');
break;
case 'S':
aspell_speller_save_all_word_lists(speller);
check_for_error(speller);
break;
case 'C':
aspell_speller_clear_session(speller);
check_for_error(speller);
break;
case 'x':
goto END;
case 'c':
if (strlen(word) < 3) {
printf("Usage: %c <word>\n", word[0]);
} else {
have = aspell_speller_check(speller, word + 2, -1);
if (have == 1)
puts("correct");
else if (have == 0)
puts("incorrect");
else
printf("Error: %s\n", aspell_speller_error_message(speller));
}
break;
case 's':
if (strlen(word) < 3) {
printf("Usage: %c <word>\n", word[0]);
} else {
print_word_list(speller,
aspell_speller_suggest(speller, word + 2, -1), '\n');
}
break;
case 'a':
if (strlen(word) < 3) {
printf("Usage: %c <word>\n", word[0]);
} else {
aspell_speller_add_to_personal(speller, word + 2, -1);
check_for_error(speller);
}
break;
case 'i':
if (strlen(word) < 3) {
printf("Usage: %c <word>\n", word[0]);
} else {
aspell_speller_add_to_session(speller, word + 2, -1);
check_for_error(speller);
}
break;
case 'o':
word[80] = '\0'; /* to make sure strchr doesn't run off end of string */
p = strchr(word + 3, ' ');
if (strlen(word) < 3 || p == 0) {
printf("Usage: %c <option> <value>\n", word[0]);
} else {
*p = '\0';
++p;
aspell_config_replace(config, word + 2, p);
check_for_config_error(config);
}
break;
case 'r':
if (strlen(word) < 3) {
printf("Usage: %c <option>\n", word[0]);
} else {
const char * val = aspell_config_retrieve(config, word + 2);
check_for_config_error(config);
if (val)
printf("%s = \"%s\"\n", word + 2, val);
}
break;
case 'l':
if (strlen(word) < 3) {
printf("Usage: %c <option>\n", word[0]);
} else {
AspellStringList * lst = new_aspell_string_list();
AspellMutableContainer * lst0
= aspell_string_list_to_mutable_container(lst);
AspellStringEnumeration * els;
const char * val;
aspell_config_retrieve_list(config, word + 2, lst0);
check_for_config_error(config);
els = aspell_string_list_elements(lst);
printf("%s:\n", word + 2);
while ( (val = aspell_string_enumeration_next(els)) != 0)
printf(" %s\n", val);
delete_aspell_string_enumeration(els);
delete_aspell_string_list(lst);
}
break;
case 'd':
if (strlen(word) < 3) {
printf("Usage: %c <file>\n", word[0]);
} else {
check_document(speller, word + 2);
printf("\n");
}
break;
default:
printf("Unknown Command: %s\n", word);
}
putchar('\n');
}
END:
delete_aspell_speller(speller);
return 0;
}
static void check_document(AspellSpeller * speller, const char * filename)
{
/* For readablity this function does not worry about buffer overrun.
This is meant as an illustrative example only. Please do not
attempt to spell check your documents with this function. */
AspellCanHaveError * ret;
AspellDocumentChecker * checker;
AspellToken token;
FILE * doc, * out;
char line[256], repl[256], checked_filename[256];
int diff;
unsigned int repl_len;
char * word_begin;
/* Open the file */
doc = fopen(filename, "r");
if (doc <= 0) {
printf("Error: Unable to open the file \"%s\" for reading.", filename);
return;
}
/* Open filename.checked for writing the results */
strcpy(checked_filename, filename);
strcat(checked_filename, ".checked");
out = fopen(checked_filename, "w");
if (out <= 0) {
printf("Error: Unable to open the file \"%s\" for writing.",
checked_filename);
fclose(doc);
return;
}
/* Set up the document checker */
ret = new_aspell_document_checker(speller);
if (aspell_error(ret) != 0) {
printf("Error: %s\n",aspell_error_message(ret));
fclose(out);
fclose(doc);
return;
}
checker = to_aspell_document_checker(ret);
while (fgets(line, 256, doc))
{
/* First process the line */
aspell_document_checker_process(checker, line, -1);
diff = 0;
/* Now find the misspellings in the line */
while (token = aspell_document_checker_next_misspelling(checker),
token.len != 0)
{
/* Print out the misspelling and get a replacement from the user */
/* Pay particular attention to how token.offset and diff is used */
word_begin = line + token.offset + diff;
printf("%.*s*%.*s*%s",
(int)(token.offset + diff), line,
(int)token.len, word_begin,
word_begin + token.len);
printf("Suggestions: ");
print_word_list(speller,
aspell_speller_suggest(speller, word_begin, token.len),
' ');
printf("\n");
printf("Replacement? ");
fgets(repl, 256, stdin);
printf("\n");
if (repl[0] == '\n') continue; /* ignore the current misspelling */
repl_len = strlen(repl) - 1;
repl[repl_len] = '\0';
/* Replace the misspelled word with the replacement */
diff += repl_len - token.len;
memmove(word_begin + repl_len, word_begin + token.len,
strlen(word_begin + token.len) + 1);
memcpy(word_begin, repl, repl_len);
}
/* print the line to filename.checked */
fputs(line, out);
}
delete_aspell_document_checker(checker);
fclose(out);
fclose(doc);
printf("Done. Results saved to \"%s\".", checked_filename);
}
|