r369: Improvements in 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.4 2001/01/09 22:31:47 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_H
31 extern "C" {
32 #include <readline.h>
33 #include <history.h>
34 };
35 #elif defined(HAVE_READLINE_READLINE_H)
36 extern "C" {
37 #include <readline/readline.h>
38 #include <readline/history.h>
39 };
40 #endif
41
42
43 // Master shell for all command-line tools
44 // If called as ctsimtext, program will look to next token on command-line as the function name
45 // If linked to ctsimtext, but executed as another name, eg pjrec, then program will use that
46 // linked name as name of function.
47
48 static const char* const g_szIdStr = "$Id: ctsimtext.cpp,v 1.4 2001/01/09 22:31:47 kevin Exp $";
49 static const char* const s_szProgramName = "ctsimtext";
50
51 extern int if1_main (int argc, char* const argv[]);
52 extern int if2_main (int argc, char* const argv[]);
53 extern int ifexport_main (int argc, char* const argv[]);
54 extern int ifinfo_main (int argc, char* const argv[]);
55 extern int phm2if_main (int argc, char* const argv[]);
56 extern int phm2pj_main (int argc, char* const argv[]);
57 extern int pj2if_main (int argc, char* const argv[]);
58 extern int pjinfo_main (int argc, char* const argv[]);
59 extern int pjrec_main (int argc, char* const argv[]);
60
61 static int processCommand (int argc, char* const argv[]);
62 static void convertStringToArgcv (char* szLine, int* piArgc, char*** pppArgv);
63
64
65 void 
66 ctsimtext_usage (const char *program)
67 {
68   std::cout << "usage: " << fileBasename(program) << " ctsim-function-name ctstim-function-parameters...\n";
69   std::cout << "CTSim text shell\n\n";
70   std::cout << "  ifinfo        Image file information\n";
71   std::cout << "  if1           Single image file conversion\n";
72   std::cout << "  if2           Dual image file conversions\n";
73   std::cout << "  pjrec         Projection reconstruction\n";
74   std::cout << "  pjinfo        Projection file information\n";
75   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
76   std::cout << "  phm2pj        Take projections of a phantom object\n";
77   std::cout << "  ifexport      Export an imagefile to a graphics file\n";
78   std::cout << "  pj2if         Convert an projection file into an imagefile\n";
79 }
80
81 void 
82 interactive_usage ()
83 {
84   std::cout << "usage: function-name parameters...\n";
85   std::cout << "Available functions:\n";
86   std::cout << "  ifinfo        Image file information\n";
87   std::cout << "  if1           Single image file conversion\n";
88   std::cout << "  if2           Dual image file conversions\n";
89   std::cout << "  pjrec         Projection reconstruction\n";
90   std::cout << "  pjinfo        Projection file information\n";
91   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
92   std::cout << "  phm2pj        Take projections of a phantom object\n";
93   std::cout << "  ifexport      Export an imagefile to a graphics file\n";
94   std::cout << "  pj2if         Convert an projection file into an imagefile\n";
95   std::cout << "  quit          Quits shell\n";
96   std::cout << "All functions accept --help as parameter for online help\n\n";
97 }
98
99 static bool s_bInteractive = false;
100
101
102 int 
103 ctsimtext_main (int argc, char * argv[])
104 {
105   int iReturn = 0;
106
107   if (argc > 1 && strstr(argv[0], s_szProgramName)) {
108     argv = &argv[1];
109     argc--;
110     iReturn = processCommand (argc, argv);
111   } else if (argc > 1){
112     iReturn = processCommand (argc, argv);
113   } else {
114     s_bInteractive = true;
115     char szPrompt[] = "CTSim> ";
116     std::cout << "CTSim Text Shell (Type \"quit\" to end)\n\n";
117     while (1) {
118 #ifdef HAVE_READLINE
119       char* pszInput = readline (szPrompt);
120       if (! pszInput)
121         break;
122       if (*pszInput != EOS)
123         add_history (pszInput);
124 #else
125       std::cout << szPrompt;
126       std::string strInput;
127       std::cin >> strInput;
128       std::cout << std::flush:;
129       std::cout << "\n";
130       char* pszInput = new char [strInput.length() + 1];
131       strncpy (pszInput, strInput.c_str(), sizeof(pszInput));
132 #endif
133       if (strncasecmp (pszInput, "quit", 4) == 0) {
134         delete pszInput;
135         break;
136       }
137       convertStringToArgcv (pszInput, &argc, &argv);
138       iReturn = processCommand (argc, argv);
139       delete pszInput;
140     }
141   }
142
143   return iReturn;
144 }
145
146 static void
147 convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
148 {
149   char* pCurrentPos = pszLine;
150   int nTokens = 0;
151   std::vector<char*> vecpszToken;
152
153   // Process line
154   bool bInDoubleQuote = false;
155   bool bInSingleQuote = false;
156   bool bInToken = false;
157
158   while (*pCurrentPos) {
159     if (isspace (*pCurrentPos)) {
160       if (! bInToken)
161         *pCurrentPos = 0;
162       else if (bInSingleQuote || bInDoubleQuote)
163         ;
164       else { // in non-quote token
165         *pCurrentPos = 0;
166         bInToken = false;
167       }
168     }
169     else if (*pCurrentPos == '\"') {
170       if (bInSingleQuote) {
171         bInSingleQuote = false;
172         *pCurrentPos = 0;
173       } else {
174         bInSingleQuote = true;
175         if (! bInToken) {
176           bInToken = true;
177           nTokens++;
178           vecpszToken.push_back (pCurrentPos+1);
179         }
180       }
181     } else if (*pCurrentPos == '\'') {
182       if (bInDoubleQuote) {
183         bInDoubleQuote = false;
184         *pCurrentPos = 0;
185       } else {
186         bInDoubleQuote = true;
187         if (! bInToken) {
188           bInToken = true;
189           nTokens++;
190           vecpszToken.push_back (pCurrentPos+1);
191         }
192       }
193     } else if (! bInToken) {   // nonwhite, non-quote character
194       bInToken = true;
195       nTokens++;
196       vecpszToken.push_back (pCurrentPos);
197     }
198
199     pCurrentPos++;
200   }
201
202   *piArgc = nTokens;
203   *pppArgv = new char* [nTokens];
204   for (unsigned int iToken = 0; iToken < vecpszToken.size(); iToken++)
205     (*pppArgv)[iToken] = vecpszToken[iToken];
206 }
207
208 static int 
209 processCommand (int argc, char* const argv[])
210 {
211   const char* const pszFunction = argv[0];
212
213   if (strcasecmp (pszFunction, "if1") == 0)
214     return if1_main (argc, argv);
215   else if (strcasecmp (pszFunction, "if2") == 0)
216     return if2_main (argc, argv);
217   else if (strcasecmp (pszFunction, "ifexport") == 0)
218     return ifexport_main (argc, argv);
219   else if (strcasecmp (pszFunction, "ifinfo") == 0)
220     return ifinfo_main (argc, argv);
221   else if (strcasecmp (pszFunction, "phm2if") == 0)
222     return phm2if_main (argc, argv);
223   else if (strcasecmp (pszFunction, "phm2pj") == 0)
224     return phm2pj_main (argc, argv);
225   else if (strcasecmp (pszFunction, "pj2if") == 0)
226     return pj2if_main (argc, argv);
227   else if (strcasecmp (pszFunction, "pjinfo") == 0)
228     return pjinfo_main (argc, argv);
229   else if (strcasecmp (pszFunction, "pjrec") == 0)
230     return pjrec_main (argc, argv);
231   else {
232     std::cout << "Unknown function name: " << pszFunction << "\n";
233     if (s_bInteractive)
234       interactive_usage();
235     else
236       ctsimtext_usage (s_szProgramName);
237     return 1;
238   }
239 }
240
241 int 
242 main (int argc, char* argv[])
243 {
244   int retval = 1;
245
246   try {
247     retval = ctsimtext_main(argc, argv);
248   } catch (exception e) {
249           std::cerr << "Exception: " << e.what() << std::endl;
250   } catch (...) {
251           std::cerr << "Unknown exception\n";
252   }
253
254   return (retval);
255 }
256