Tizen 2.1 base
[external/enchant.git] / tests / enchant-lsmod.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* enchant
3  * Copyright (C) 2003 Dom Lachowicz
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02110-1301, USA.
19  *
20  * In addition, as a special exception, Dom Lachowicz
21  * gives permission to link the code of this program with
22  * the non-LGPL Spelling Provider libraries (eg: a MSFT Office
23  * spell checker backend) and distribute linked combinations including
24  * the two.  You must obey the GNU Lesser General Public License in all
25  * respects for all of the code used other than said providers. If you modify
26  * this file, you may extend this exception to your version of the
27  * file, but you are not obligated to do so.  If you do not wish to
28  * do so, delete this exception statement from your version.
29  */
30
31 #include <glib.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "enchant.h"
37 #include "enchant-provider.h"
38
39 static void
40 describe_dict (const char * const lang_tag,
41                const char * const provider_name,
42                const char * const provider_desc,
43                const char * const provider_file,
44                void * user_data)
45 {
46         FILE * out = (FILE *)user_data;
47         fprintf (out, "%s (%s)\n", lang_tag, provider_name);
48 }
49
50 static void
51 enumerate_providers (const char * name,
52                      const char * desc,
53                      const char * file,
54                      void * user_data)
55 {
56         FILE * out = (FILE *)user_data;
57         fprintf (out, "%s (%s)\n", name, desc);
58 }
59
60 static void
61 enumerate_dicts (const char * const lang_tag,
62                  const char * const provider_name,
63                  const char * const provider_desc,
64                  const char * const provider_file,
65                  void * user_data)
66 {
67         FILE * out = (FILE *)user_data;
68         fprintf (out, "%s (%s)\n", lang_tag, provider_name);
69 }
70
71 int
72 main (int argc, char **argv)
73 {
74         EnchantBroker *broker;
75         EnchantDict *dict;
76         char * lang_tag = NULL;
77         
78         int mode = 0, i;
79
80         for (i = 1; i < argc; i++) {
81                 if (!strcmp (argv[i], "-lang")) {
82                         if (i < (argc - 1)) {
83                                 lang_tag = g_strdup (argv[++i]);
84                         } else {
85                                 lang_tag = enchant_get_user_language();
86
87                                 if (!lang_tag || !strcmp (lang_tag, "C")) {
88                                         if (lang_tag) /* lang might be "C" */
89                                                 g_free (lang_tag);
90                                         lang_tag = g_strdup ("en");
91                                 }
92                         }
93                         mode = 1;
94                 } else if (!strcmp (argv[i], "-h") || !strcmp (argv[i], "-?") || !strcmp(argv[i], "-help")) {
95                         printf ("%s [-lang [language_tag]] [-list-dicts] [-h] [-v]\n", argv[0]);
96                         if (lang_tag)
97                                 g_free (lang_tag);
98                         return 0;
99                 } else if (!strcmp (argv[i], "-v") || !strcmp (argv[i], "-version")) {
100                         printf ("%s %s\n", argv[0], VERSION);
101                         if (lang_tag)
102                                 g_free (lang_tag);
103                         return 0;
104                 } else if (!strcmp (argv[i], "-list-dicts")) {
105                         mode = 2;
106                 }
107         }
108         
109         broker = enchant_broker_init ();
110         
111         if (mode == 0) {
112                 enchant_broker_describe (broker, enumerate_providers, stdout);
113         } else if (mode == 1) {
114
115                 if (!lang_tag) {
116                         printf ("Error: language tag not specified and environment variable $LANG not set.\n");
117                         enchant_broker_free (broker);
118                         return 1;
119                 }
120
121                 dict = enchant_broker_request_dict (broker, lang_tag);
122                 
123                 if (!dict) {
124                         printf ("No dictionary available for '%s'.\n", lang_tag);
125
126                         if (lang_tag)
127                                 g_free (lang_tag);
128
129                         enchant_broker_free (broker);
130                         return 1;
131                 } else {
132                         enchant_dict_describe (dict, describe_dict, stdout);
133                         enchant_broker_free_dict (broker, dict);
134                 }
135         } else if (mode == 2) {
136                 enchant_broker_list_dicts (broker, enumerate_dicts, stdout);
137         }
138
139         if (lang_tag)
140                 g_free (lang_tag);
141         
142         enchant_broker_free (broker);
143         
144         return 0;
145 }