• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*---------------------------------------------------------------------------*
2  *  make_g2g.c  *
3  *                                                                           *
4  *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
5  *                                                                           *
6  *  Licensed under the Apache License, Version 2.0 (the 'License');          *
7  *  you may not use this file except in compliance with the License.         *
8  *                                                                           *
9  *  You may obtain a copy of the License at                                  *
10  *      http://www.apache.org/licenses/LICENSE-2.0                           *
11  *                                                                           *
12  *  Unless required by applicable law or agreed to in writing, software      *
13  *  distributed under the License is distributed on an 'AS IS' BASIS,        *
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15  *  See the License for the specific language governing permissions and      *
16  *  limitations under the License.                                           *
17  *                                                                           *
18  *---------------------------------------------------------------------------*/
19 
20 #include <stdlib.h>
21 
22 #include "pstdio.h"
23 #include "pmemory.h"
24 #include "plog.h"
25 #include "PFile.h"
26 #include "PFileSystem.h"
27 #include "PANSIFileSystem.h"
28 
29 #include "SR_Grammar.h"
30 #include "ESR_CommandLine.h"
31 #include "ESR_Session.h"
32 #include "LCHAR.h"
33 #include "srec_context.h"
34 
35 #define MAX_LINE_LENGTH 256
36 #define MAX_STR_LENGTH   80
37 #define MAX_SEM_RESULTS   3
38 #define MAX_KEYS         30
39 
usage(LCHAR * exename)40 static void usage(LCHAR* exename)
41 {
42   LFPRINTF(stdout,"usage: %s -base <base grammar filename> [-out <output file>] \n",exename);
43 }
44 
main(int argc,char * argv[])45 int main (int argc, char *argv[])
46 {
47   SR_Grammar* grammar = NULL;
48   ESR_ReturnCode rc;
49   LCHAR base[P_PATH_MAX];
50   LCHAR* p;
51   LCHAR outFilename[P_PATH_MAX];
52   size_t len;
53 
54 	/*
55 	 * Initialize portable library.
56 	 * Can't use CHKLOG() before plogInit, so use non-portable methods instead.
57 	 */
58   CHKLOG(rc, PMemInit());
59 /*  CHKLOG(rc, PFileSystemCreate());
60   CHKLOG(rc, PANSIFileSystemCreate());
61   CHKLOG(rc, PANSIFileSystemAddPath(L("/dev/ansi/"), L("/")));*/
62   CHKLOG(rc, PLogInit(NULL, 3));
63 
64   /* Set ANSI file-system as default file-system */
65 /*  CHKLOG(rc, PANSIFileSystemSetDefault(ESR_TRUE));*/
66   /* Set virtual current working directory to native current working directory */
67 /*  len = P_PATH_MAX;
68   CHKLOG(rc, PANSIFileSystemGetcwd(cwd, &len));
69   CHKLOG(rc, PFileSystemChdir(cwd));*/
70 
71   len = P_PATH_MAX;
72   rc = ESR_CommandLineGetValue(argc, (const char **)argv, L("base"), base, &len);
73   if (rc==ESR_NO_MATCH_ERROR)
74     {
75     LFPRINTF(stderr, "ERROR: Mandatory option -base is unspecified\n");
76     return ESR_INVALID_ARGUMENT;
77   }
78   else if (rc!=ESR_SUCCESS)
79     {
80       PLogError(ESR_rc2str(rc));
81       goto CLEANUP;
82     }
83 
84   len = P_PATH_MAX;
85   rc = ESR_CommandLineGetValue(argc, (const char **)argv, L("out"), outFilename, &len);
86   if (rc==ESR_NO_MATCH_ERROR)
87     {
88       LFPRINTF(stderr, "ERROR: Mandatory option -out is unspecified\n");
89       return ESR_INVALID_ARGUMENT;
90     }
91   else if (rc!=ESR_SUCCESS)
92     {
93       PLogError(ESR_rc2str(rc));
94       goto CLEANUP;
95     }
96 
97   if (base==NULL || (LSTRCMP(outFilename, L(""))==0 ))
98     {
99       usage(argv[0]);
100       exit(EXIT_FAILURE);
101     }
102 
103   /* setup the default outfilename if not already set */
104   if (!outFilename[0])
105     {
106       LSTRCPY(outFilename,base);
107       /* get rid of the ',addWords=2000' grammar load param */
108       p = LSTRCHR(outFilename,L(','));
109       if(p) *p = 0;
110       LSTRCAT(outFilename,L(".g2g"));
111     }
112 
113   LFPRINTF(stdout,"Loading grammar %s from text files...\n",base);
114   CHKLOG(rc, SR_GrammarLoad(base, &grammar));
115 
116   LFPRINTF(stdout,"Saving grammar as binary image %s...\n",outFilename);
117   CHKLOG(rc, SR_GrammarSave(grammar, outFilename));
118 
119   LFPRINTF(stdout,"SUCCESS!\n");
120 
121 CLEANUP:
122 
123   if (grammar)
124 		grammar->destroy(grammar);
125   PLogShutdown();
126 /*	PANSIFileSystemDestroy();
127 	PFileSystemDestroy();*/
128   PMemShutdown();
129   return rc;
130 }
131