Initial import to Tizen
[profile/ivi/sphinxbase.git] / src / sphinx_jsgf2fsg / main.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 2007 Carnegie Mellon University.  All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced 
19  * Research Projects Agency and the National Science Foundation of the 
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37
38 #include <string.h>
39
40 #include <sphinxbase/hash_table.h>
41 #include <sphinxbase/fsg_model.h>
42 #include <sphinxbase/jsgf.h>
43 #include <sphinxbase/err.h>
44 #include <sphinxbase/strfuncs.h>
45
46 static fsg_model_t *
47 get_fsg(jsgf_t *grammar, const char *name)
48 {
49     jsgf_rule_iter_t *itor;
50     logmath_t *lmath = logmath_init(1.0001, 0, 0);
51     fsg_model_t *fsg = NULL;
52
53     for (itor = jsgf_rule_iter(grammar); itor;
54          itor = jsgf_rule_iter_next(itor)) {
55         jsgf_rule_t *rule = jsgf_rule_iter_rule(itor);
56         char const *rule_name = jsgf_rule_name(rule);
57
58         if ((name == NULL && jsgf_rule_public(rule))
59             || (name && strlen(rule_name)-2 == strlen(name) &&
60                 0 == strncmp(rule_name + 1, name, strlen(rule_name) - 2))) {
61             fsg = jsgf_build_fsg_raw(grammar, rule, logmath_retain(lmath), 1.0);
62             jsgf_rule_iter_free(itor);
63             break;
64         }
65     }
66
67     logmath_free(lmath);
68     return fsg;
69 }
70
71 int
72 main(int argc, char *argv[])
73 {
74     jsgf_t *jsgf;
75     fsg_model_t *fsg;
76     int fsm = 0;
77     char *outfile = NULL;
78     char *symfile = NULL;
79
80     if (argc > 1 && 0 == strcmp(argv[1], "-fsm")) {
81         fsm = 1;
82         ++argv;
83     }
84
85     jsgf = jsgf_parse_file(argc > 1 ? argv[1] : NULL, NULL);
86     if (jsgf == NULL) {
87         return 1;
88     }
89     fsg = get_fsg(jsgf, argc > 2 ? argv[2] : NULL);
90
91     if (argc > 3)
92         outfile = argv[3];
93     if (argc > 4)
94         symfile = argv[4];
95
96     if (fsm) {
97         if (outfile)
98             fsg_model_writefile_fsm(fsg, outfile);
99         else
100             fsg_model_write_fsm(fsg, stdout);
101         if (symfile)
102             fsg_model_writefile_symtab(fsg, symfile);
103     }
104     else {
105         if (outfile)
106             fsg_model_writefile(fsg, outfile);
107         else
108             fsg_model_write(fsg, stdout);
109     }
110     fsg_model_free(fsg);
111     jsgf_grammar_free(jsgf);
112
113     return 0;
114 }
115
116
117 /** Silvio Moioli: Windows CE/Mobile entry point added. */
118 #if defined(_WIN32_WCE)
119 #pragma comment(linker,"/entry:mainWCRTStartup")
120 #include <windows.h>
121
122 //Windows Mobile has the Unicode main only
123 int wmain(int32 argc, wchar_t *wargv[]) {
124     char** argv;
125     size_t wlen;
126     size_t len;
127     int i;
128
129     argv = malloc(argc*sizeof(char*));
130     for (i=0; i<argc; i++){
131         wlen = lstrlenW(wargv[i]);
132         len = wcstombs(NULL, wargv[i], wlen);
133         argv[i] = malloc(len+1);
134         wcstombs(argv[i], wargv[i], wlen);
135     }
136
137     //assuming ASCII parameters
138     return main(argc, argv);
139 }
140 #endif