1 /*****************************************************************************
5 ** Purpose: Text mode shell for CTSim
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: Jan 2001
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: ctsimtext.cpp,v 1.3 2001/01/09 20:38:53 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
36 // Master shell for all command-line tools
37 // If called as ctsimtext, program will look to next token on command-line as the function name
38 // If linked to ctsimtext, but executed as another name, eg pjrec, then program will use that
39 // linked name as name of function.
41 static const char* const g_szIdStr = "$Id: ctsimtext.cpp,v 1.3 2001/01/09 20:38:53 kevin Exp $";
42 static const char* const s_szProgramName = "ctsimtext";
44 extern int if1_main (int argc, char* const argv[]);
45 extern int if2_main (int argc, char* const argv[]);
46 extern int ifexport_main (int argc, char* const argv[]);
47 extern int ifinfo_main (int argc, char* const argv[]);
48 extern int phm2if_main (int argc, char* const argv[]);
49 extern int phm2pj_main (int argc, char* const argv[]);
50 extern int pj2if_main (int argc, char* const argv[]);
51 extern int pjinfo_main (int argc, char* const argv[]);
52 extern int pjrec_main (int argc, char* const argv[]);
54 static int processCommand (int argc, char* const argv[]);
55 static void convertStringToArgcv (char* szLine, int* piArgc, char*** pppArgv);
60 ctsimtext_usage (const char *program)
62 std::cout << "usage: " << fileBasename(program) << " ctsim-function-name ctstim-function-parameters...\n";
63 std::cout << "CTSim text shell\n\n";
64 std::cout << " ifinfo Image file information\n";
65 std::cout << " if1 Single image file conversion\n";
66 std::cout << " if2 Dual image file conversions\n";
67 std::cout << " pjrec Projection reconstruction\n";
68 std::cout << " pjinfo Projection file information\n";
69 std::cout << " phm2if Convert a geometric phantom into an imagefile\n";
70 std::cout << " phm2pj Take projections of a phantom object\n";
71 std::cout << " ifexport Export an imagefile to a graphics file\n";
72 std::cout << " pj2if Convert an projection file into an imagefile\n";
77 ctsimtext_main (int argc, char * argv[])
81 if (strcmp(argv[0],s_szProgramName) == 0) {
84 iReturn = processCommand (argc, argv);
86 iReturn = processCommand (argc, argv);
88 char szPrompt[] = "CTSim> ";
91 std::cout << "CTSimText Interactive mode\nType quit to end";
93 char* pszInput = readline (szPrompt);
95 std::cout << szPrompt;
97 char* pszInput = new char [strInput.length() + 1];
98 strncpy (pszInput, strInput.c_str(), sizeof(pszInput));
100 if (strncasecmp (pszInput, "quit", 4) == 0) {
104 convertStringToArgcv (pszInput, &argc, &argv);
106 iReturn = processCommand (argc, argv);
114 convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
116 char* pCurrentPos = pszLine;
118 std::vector<char*> vecpszToken;
121 bool bInDoubleQuote = false;
122 bool bInSingleQuote = false;
123 bool bInToken = false;
125 while (*pCurrentPos) {
126 if (iswhite (*pCurrentPos)) {
129 else if (bInSingleQuote || bInDoubleQuote)
131 else { // in non-quote token
136 else if (*pCurrentPos = '\"') {
137 if (bInSingleQuote) {
138 bInSingleQuote = false;
140 bInSingleQuote = true;
144 vecpszToken.push_back (pCurrentPos+1);
147 } else if (*pCurrentPos = '\'') {
148 if (bInDoubleQuote) {
149 bInDoubleQuote = false;
151 bInDoubleQuote = true;
155 vecpszToken.push_back (pCurrentPos+1);
158 } else if (! bInToken) { // nonwhite, non-quote character
161 vecpszToken.push_back (pCurrentPos);
168 pppArgv = new char** [nTokens];
169 for (unsigned int iToken = 0; iToken < vecpszToken.size(); iToken++)
170 (*pppArgv)[iToken] = vecpszToken[iToken];
174 processCommand (int argc, char* const argv[])
176 const char* const pszFunction = argv[0];
178 if (strcasecmp (pszFunction, "if1") == 0)
179 return if1_main (argc, argv);
180 else if (strcasecmp (pszFunction, "if2") == 0)
181 return if2_main (argc, argv);
182 else if (strcasecmp (pszFunction, "ifexport") == 0)
183 return ifexport_main (argc, argv);
184 else if (strcasecmp (pszFunction, "ifinfo") == 0)
185 return ifinfo_main (argc, argv);
186 else if (strcasecmp (pszFunction, "phm2if") == 0)
187 return phm2if_main (argc, argv);
188 else if (strcasecmp (pszFunction, "phm2pj") == 0)
189 return phm2pj_main (argc, argv);
190 else if (strcasecmp (pszFunction, "pj2if") == 0)
191 return pj2if_main (argc, argv);
192 else if (strcasecmp (pszFunction, "pjinfo") == 0)
193 return pjinfo_main (argc, argv);
194 else if (strcasecmp (pszFunction, "pjrec") == 0)
195 return pjrec_main (argc, argv);
197 std::cout << "Unknown function name" << pszFunction << "\n\n";
198 ctsimtext_usage (argv[0]);
204 main (int argc, char* argv[])
209 retval = ctsimtext_main(argc, argv);
210 } catch (exception e) {
211 std::cerr << "Exception: " << e.what() << std::endl;
213 std::cerr << "Unknown exception\n";