r368: Additions to ctsimtext
[ctsim.git] / tools / ctsimtext.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          ctsimtext.cpp
5 **   Purpose:       Text mode shell for CTSim
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  Jan 2001
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: ctsimtext.cpp,v 1.3 2001/01/09 20:38:53 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "ct.h"
29
30 #ifdef HAVE_READLINE
31 #include <readline.h>
32 #include <history.h>
33 #endif
34
35
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.
40
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";
43
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[]);
53
54 static int processCommand (int argc, char* const argv[]);
55 static void convertStringToArgcv (char* szLine, int* piArgc, char*** pppArgv);
56
57
58
59 void 
60 ctsimtext_usage (const char *program)
61 {
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";
73 }
74
75
76 int 
77 ctsimtext_main (int argc, char * argv[])
78 {
79   int iReturn = 0;
80
81   if (strcmp(argv[0],s_szProgramName) == 0) {
82     argv = &argv[1];
83     argc--;
84     iReturn = processCommand (argc, argv);
85   } else if (argc > 1){
86     iReturn = processCommand (argc, argv);
87   } else {
88     char szPrompt[] = "CTSim> ";
89     std::string strInput;
90     while (1) {
91       std::cout << "CTSimText Interactive mode\nType quit to end";
92 #ifdef HAVE_READLINE
93       char* pszInput = readline (szPrompt);
94 #else
95       std::cout << szPrompt;
96       std::cin >> strInput;
97       char* pszInput = new char [strInput.length() + 1];
98       strncpy (pszInput, strInput.c_str(), sizeof(pszInput));
99 #endif
100       if (strncasecmp (pszInput, "quit", 4) == 0) {
101         delete pszInput;
102         break;
103       }
104       convertStringToArgcv (pszInput, &argc, &argv);
105       delete pszInput;
106       iReturn = processCommand (argc, argv);
107     }
108   }
109
110   return iReturn;
111 }
112
113 static void
114 convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
115 {
116   char* pCurrentPos = pszLine;
117   int nTokens = 0;
118   std::vector<char*> vecpszToken;
119
120   // Process line
121   bool bInDoubleQuote = false;
122   bool bInSingleQuote = false;
123   bool bInToken = false;
124
125   while (*pCurrentPos) {
126     if (iswhite (*pCurrentPos)) {
127       if (! bInToken)
128         *pCurrentPos = 0;
129       else if (bInSingleQuote || bInDoubleQuote)
130         ;
131       else { // in non-quote token
132         *pCurrentPos = 0;
133         bInToken = false;
134       }
135     }
136     else if (*pCurrentPos = '\"') {
137       if (bInSingleQuote) {
138         bInSingleQuote = false;
139       } else {
140         bInSingleQuote = true;
141         if (! bInToken) {
142           bInToken = true;
143           nTokens++;
144           vecpszToken.push_back (pCurrentPos+1);
145         }
146       }
147     } else if (*pCurrentPos = '\'') {
148       if (bInDoubleQuote) {
149         bInDoubleQuote = false;
150       } else {
151         bInDoubleQuote = true;
152         if (! bInToken) {
153           bInToken = true;
154           nTokens++;
155           vecpszToken.push_back (pCurrentPos+1);
156         }
157       }
158     } else if (! bInToken) {   // nonwhite, non-quote character
159       bInToken = true;
160       nTokens++;
161       vecpszToken.push_back (pCurrentPos);
162     }
163
164     pCurrentPos++;
165   }
166
167   *piArgc = nTokens;
168   pppArgv = new char** [nTokens];
169   for (unsigned int iToken = 0; iToken < vecpszToken.size(); iToken++)
170     (*pppArgv)[iToken] = vecpszToken[iToken];
171 }
172
173 static int 
174 processCommand (int argc, char* const argv[])
175 {
176   const char* const pszFunction = argv[0];
177
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);
196   else {
197     std::cout << "Unknown function name" << pszFunction << "\n\n";
198     ctsimtext_usage (argv[0]);
199     return 1;
200   }
201 }
202
203 int 
204 main (int argc, char* argv[])
205 {
206   int retval = 1;
207
208   try {
209     retval = ctsimtext_main(argc, argv);
210   } catch (exception e) {
211           std::cerr << "Exception: " << e.what() << std::endl;
212   } catch (...) {
213           std::cerr << "Unknown exception\n";
214   }
215
216   return (retval);
217 }
218