"Fossies" - the Fresh Open Source Software Archive

Member "aspell-0.60.8/examples/example-c.c" (8 Oct 2019, 9958 Bytes) of package /linux/misc/aspell-0.60.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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