forked from sandialabs/InterSpec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
282 lines (228 loc) · 8.76 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Created by wcjohns on 20110322.
/* InterSpec: an application to analyze spectral gamma radiation data.
Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
(NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
Government retains certain rights in this software.
For questions contact William Johnson via email at wcjohns@sandia.gov, or
alternative emails of interspec@sandia.gov.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "InterSpec_config.h"
#include <string>
#include <iostream>
#include <Wt/WString>
#include <Wt/WApplication>
#include <Wt/WEnvironment>
#include "InterSpec/InterSpec.h"
#include "SpecUtils/Filesystem.h"
#include "InterSpec/InterSpecApp.h"
#include "SpecUtils/SerialToDetectorModel.h"
#include "InterSpec/DataBaseVersionUpgrade.h"
#if( ANDROID )
#include "android/AndroidUtils.hpp"
#endif
#if( USE_SQLITE3_DB )
#include "InterSpec/DataBaseUtils.h"
#endif
#if( BUILD_AS_COMMAND_LINE_CODE_DEVELOPMENT )
#include "testing/developcode.h"
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <stdio.h>
#include <direct.h>
#include <shellapi.h>
#include "SpecUtils/StringAlgo.h"
#endif
//Forward declaration
Wt::WApplication *createApplication( const Wt::WEnvironment &env );
#ifdef _WIN32
void getUtf8Args( int &argc, char ** &argv );
#endif
void processCustomArgs( int argc, char **argv );
int main( int argc, char **argv )
{
#ifdef _WIN32
getUtf8Args( argc, argv );
#endif
#if( BUILD_AS_COMMAND_LINE_CODE_DEVELOPMENT )
return developcode::run_development_code();
#endif
#if( ANDROID )
AndroidUtils::androidbuf stdbuf( AndroidUtils::androidbuf::FromCout );
AndroidUtils::androidbuf errbuf( AndroidUtils::androidbuf::FromCerr );
AndroidUtils::set_anrdoid_cwd( argc, argv );
#endif //#if( ANDROID )
std::cout << std::showbase << std::hex << "Running with Wt version "
<< WT_VERSION << std::dec << ", from executable compiled on "
<< __DATE__ << std::endl;
#if( PERFORM_DEVELOPER_CHECKS )
std::cout << "Developer tests are being performed" << std::endl;
#endif
std::cout << std::endl;
#if( WT_VERSION >= 0x3030300 )
//Make it so WString defaults to assuming std::string or char * are UTF8
// encoded, rather than the system encoding.
Wt::WString::setDefaultEncoding( Wt::UTF8 );
#endif
#if( BUILD_AS_LOCAL_SERVER )
// For development we'll put in some default command line argument that assume the CWD is
// either the base InterSpec code directory, or the CMake build directory.
std::string def_args[] = { argv[0], "--docroot=.", "--http-address=127.0.0.1",
"--http-port=8080", "--config=./data/config/wt_config_localweb.xml", "--accesslog=-",
"--no-compression"
};
const size_t default_argc = sizeof(def_args) / sizeof(def_args[0]);
char *default_argv[default_argc] = { nullptr };
if( argc == 1 )
{
std::cout << "Using default set of command line arguments\n" << std::endl;
for( size_t i = 0; i < default_argc; ++i )
default_argv[i] = &(def_args[i].front());
argc = static_cast<int>( default_argc );
argv = default_argv;
#if(_WIN32)
// I cant get MSVC to set CWD to anywhere besides InterSpec/out/build/x64-Debug/,
// so we'll look for our resources up to three levels up
const std::string targetfile = "InterSpec_resources/InterSpec.css";
std::string pardir = "";
while (pardir.size() < 9)
{
const std::string testfile = SpecUtils::append_path(pardir, targetfile);
if (SpecUtils::is_file(testfile))
{
if(!pardir.empty())
{
std::cout << "Will change cwd to '" << pardir << "'." << std::endl;
if (_chdir(pardir.c_str()))
std::cerr << "Failed to change CWD to '" << pardir << "'" << std::endl;
}
break;
}
pardir = SpecUtils::append_path(pardir, "..");
}//while (pardir.size() < 6)
#endif
// If there is a "user_data" directory in the CWD, we'll set this as the writeable data
// directory to simulate desktop app behavior of saving DRFs and similar
const std::string cwd = SpecUtils::get_working_path();
std::cout << "cwd='" << cwd << "'" << std::endl;
const std::string dev_user_data = SpecUtils::append_path( cwd, "user_data" );
if( SpecUtils::is_directory( dev_user_data ) )
InterSpec::setWritableDataDirectory( dev_user_data );
else
std::cerr << "No '" << dev_user_data << "' - not setting writeable data directory.\n";
}//if( no command line arguments were given ).
#endif //BUILD_AS_LOCAL_SERVER
processCustomArgs( argc, argv );
DataBaseVersionUpgrade::checkAndUpgradeVersion();
// TODO: switch to using InterSpecServer::startServer(), etc
return Wt::WRun( argc, argv, &createApplication );
}//int main( int argc, const char * argv[] )
Wt::WApplication *createApplication( const Wt::WEnvironment &env )
{
return new InterSpecApp( env );
}// Wt::WApplication *createApplication(const Wt::WEnvironment& env)
#ifdef _WIN32
/** Get command line arguments encoded as UTF-8.
This function just leaks the memory
Note that environment variables are not in UTF-8, we could account for this
similar to:
wchar_t *wenvstrings = GetEnvironmentStringsW();
...
*/
void getUtf8Args( int &argc, char ** &argv )
{
LPWSTR *argvw = CommandLineToArgvW( GetCommandLineW(), &argc );
if( !argvw )
{
std::cout << "CommandLineToArgvW failed - good luck" << std::endl;
return ;
}
argv = (char **)malloc(sizeof(char *)*argc);
for( int i = 0; i < argc; ++i)
{
printf("Argument: %d: %ws\n", i, argvw[i]);
const std::string asutf8 = SpecUtils::convert_from_utf16_to_utf8( argvw[i] );
argv[i] = (char *)malloc( sizeof(char)*(asutf8.size()+1) );
strcpy( argv[i], asutf8.c_str() );
}//for( int i = 0; i < argc; ++i)
// Free memory allocated for CommandLineToArgvW arguments.
LocalFree(argvw);
}//void getUtf8Args()
#endif
void processCustomArgs( int argc, char **argv )
{
bool set_serial_num_file = false;
for( int i = 1; i < (argc-1); ++i )
{
#if( BUILD_AS_ELECTRON_APP || IOS || ANDROID || BUILD_AS_OSX_APP || BUILD_AS_LOCAL_SERVER )
if( argv[i] == std::string("--userdatadir") )
{
try
{
InterSpec::setWritableDataDirectory( argv[i+1] );
const std::vector<std::string> serial_db = SpecUtils::ls_files_in_directory( argv[i+1], "serial_to_model.csv" );
if( !serial_db.empty() )
{
SerialToDetectorModel::set_detector_model_input_csv( serial_db[0] );
set_serial_num_file = true;
}
}catch( std::exception & )
{
std::cerr << "Invalid userdatadir ('" << argv[i+1] << "') specified"
<< std::endl;
}
}//if( argv[i] == std::string("--userdatadir") )
#endif //if( not a webapp )
#if( USE_SQLITE3_DB )
if( argv[i] == std::string("--userdb") )
{
try
{
// TODO: make sure the filename is valid and we can write to it, and also let using = sign
DataBaseUtils::setPreferenceDatabaseFile( argv[i+1] );
}catch( std::exception & )
{
std::cerr << "Invalid userdb ('" << argv[i+1] << "') specified" << std::endl;
}
}//if( argv[i] == std::string("--userdatadir") )
#endif
if( argv[i] == std::string("--static-data-dir") )
{
try
{
// Let user use the = sign
InterSpec::setStaticDataDirectory( argv[i+1] );
}catch( std::exception &e )
{
std::cerr << "Invalid static data directory ('" << argv[i+1] << "') specified: " << e.what() << std::endl;
}
}//if( argv[i] == std::string("--userdatadir") )
}//for( int i = 1; i < (argc-1); ++i )
#if( BUILD_AS_LOCAL_SERVER )
if( !set_serial_num_file )
{
const char * const searchdirs[] = { "data", "data_ouo" };
for( const char *dir : searchdirs )
{
const std::vector<std::string> serial_db = SpecUtils::ls_files_in_directory( dir, "serial_to_model.csv" );
if( !serial_db.empty() )
{
SerialToDetectorModel::set_detector_model_input_csv( serial_db[0] );
break;
}
}
}//if( !set_serial_num_file )
#endif
}//void processCustomArgs( int argc, char **argv )