summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDom Lachowicz <domlachowicz@gmail.com>2003-08-28 04:43:06 +0000
committerDom Lachowicz <domlachowicz@gmail.com>2003-08-28 04:43:06 +0000
commitfe9aaff015d9d65d38dd36f1161c1a006e8b97d6 (patch)
tree7699bc19c236a5883a570f9cb9865198b4f89d67
parenta134a55d9b5b3b5b8f3f7ed26916c9c036f4d37c (diff)
downloadenchant-fe9aaff015d9d65d38dd36f1161c1a006e8b97d6.tar.gz
beginnings of an ispell-compatible-ish command line program
git-svn-id: svn+ssh://svn.abisource.com/svnroot/enchant/trunk@20825 bcba8976-2d24-0410-9c9c-aab3bd5fdfd6
-rw-r--r--tests/Makefile.am9
-rw-r--r--tests/enchant-ispell.c186
2 files changed, 193 insertions, 2 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f118cc8..6a693d7 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,4 +1,4 @@
-INCLUDES=-I$(top_srcdir)/src
+INCLUDES=-I$(top_srcdir)/src $(ENCHANT_CFLAGS)
DEPS= $(top_builddir)/src/libenchant.la
ldadd= $(top_builddir)/src/libenchant.la $(ENCHANT_LIBS)
@@ -9,7 +9,7 @@ else
cxx_progs =
endif
-noinst_PROGRAMS = test-enchant $(cxx_progs)
+noinst_PROGRAMS = enchant-ispell test-enchant $(cxx_progs)
bin_PROGRAMS= enchant-lsmod
test_enchant_SOURCES = test-enchant.c
@@ -17,6 +17,11 @@ test_enchant_LDFLAGS =
test_enchant_DEPENDENCIES = $(DEPS)
test_enchant_LDADD = $(ldadd)
+enchant_ispell_SOURCES = enchant-ispell.c
+enchant_ispell_LDFLAGS =
+enchant_ispell_DEPENDENCIES = $(DEPS)
+enchant_ispell_LDADD = $(ldadd)
+
test_enchantxx_SOURCES = test-enchantxx.cpp
test_enchantxx_LDFLAGS =
test_enchantxx_DEPENDENCIES = $(DEPS)
diff --git a/tests/enchant-ispell.c b/tests/enchant-ispell.c
new file mode 100644
index 0000000..452ee04
--- /dev/null
+++ b/tests/enchant-ispell.c
@@ -0,0 +1,186 @@
+/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/* enchant
+ * Copyright (C) 2003 Dom Lachowicz
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * In addition, as a special exception, Dom Lachowicz
+ * gives permission to link the code of this program with
+ * the non-LGPL Spelling Provider libraries (eg: a MSFT Office
+ * spell checker backend) and distribute linked combinations including
+ * the two. You must obey the GNU General Public License in all
+ * respects for all of the code used other than said providers. If you modify
+ * this file, you may extend this exception to your version of the
+ * file, but you are not obligated to do so. If you do not wish to
+ * do so, delete this exception statement from your version.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+
+#include "enchant.h"
+
+typedef enum
+ {
+ MODE_NONE,
+ MODE_VERSION,
+ MODE_A,
+ MODE_L,
+ MODE_FILE
+ } IspellMode_t;
+
+static void
+print_version (FILE * to)
+{
+ fprintf (to, "@(#) International Ispell Version 3.1.20 (but really Enchant %s)\n", VERSION);
+}
+
+static void
+print_help (FILE * to, const char * prog)
+{
+ fprintf (to, "Usage: %s [options] -a|-l|-v[v]|<file>\n", prog);
+}
+
+static char *
+consume_next_word (FILE * in, size_t * start_pos)
+{
+ /* TODO: the word parsing routine - use glib's unicode functions */
+ *start_pos = 0;
+ return NULL;
+}
+
+static int
+parse_file (FILE * in, FILE * out, IspellMode_t mode)
+{
+ EnchantBroker * broker;
+ EnchantDict * dict;
+
+ const gchar * lang;
+
+ char * word = NULL;
+ size_t start_pos = 0, word_count = 0;
+
+ if (mode == MODE_A)
+ print_version (out);
+
+ lang = g_getenv ("LANG");
+ if (!lang || !strcmp (lang, "C"))
+ lang = "en";
+
+ broker = enchant_broker_init ();
+ dict = enchant_broker_request_dict (broker, lang);
+ if (!dict) {
+ fprintf (stderr, "Couldn't create a dictionary for %s\n", lang);
+ enchant_broker_term (broker);
+ }
+
+ while ((word = consume_next_word (in, &start_pos)) != NULL) {
+ word_count++;
+ if (mode == MODE_A) {
+ if (enchant_dict_check (dict, word, strlen (word)) == 0)
+ fprintf (out, "*\n");
+ else {
+ size_t n_suggs;
+ char ** suggs;
+
+ suggs = enchant_dict_suggest (dict, word,
+ strlen (word), &n_suggs);
+ if (!n_suggs || !suggs)
+ fprintf (out, "# %s %ld\n", word, start_pos);
+ else {
+ size_t i = 0;
+
+ fprintf (out, "& %s %ld %ld:", word, n_suggs, start_pos);
+
+ for (i = 0; i < n_suggs; i++) {
+ if (i != (n_suggs - 1))
+ fprintf (out, " %s,", word);
+ else
+ fprintf (out, " %s\n", word);
+ }
+ }
+ }
+ }
+ else if (mode == MODE_L) {
+ if (enchant_dict_check (dict, word, strlen (word)) != 0)
+ fprintf (out, "%s\n", word);
+ }
+
+ g_free (word);
+ }
+
+ enchant_broker_release_dict (broker, dict);
+ enchant_broker_term (broker);
+
+ return 0;
+}
+
+int main (int argc, char ** argv)
+{
+ IspellMode_t mode = MODE_NONE;
+
+ char * arg, * file = NULL;
+ int i, rval = 0;
+
+ FILE * fp = stdin;
+
+ for (i = 1; i < argc; i++) {
+ arg = argv[i];
+ if (arg[0] == '-') {
+ if (strlen (arg) == 2) {
+ if (arg[1] == 'a')
+ mode = MODE_A;
+ else if (arg[1] == 'l')
+ mode = MODE_L;
+ else if (arg[1] == 'v')
+ mode = MODE_VERSION;
+ }
+ else if (strlen (arg) > 2) {
+ fprintf (stderr, "-%c does not take any parameters.\n", arg[1]);
+ exit(1);
+ }
+ else
+ file = arg;
+ }
+ else
+ file = arg;
+ }
+
+ if (mode == MODE_VERSION) {
+ print_version (stdout);
+ }
+ else if (mode == MODE_NONE && !file) {
+ print_help (stdout, argv[0]);
+ }
+ else {
+ if (file) {
+ fp = fopen (file, "r");
+ if (!fp) {
+ fprintf (stderr, "Couldn't open '%s' to spellcheck\n", file);
+ exit (1);
+ }
+ }
+
+ rval = parse_file (fp, stdout, mode);
+
+ if (file)
+ fclose (fp);
+ }
+
+ return rval;
+}