forked from MGH-MPEC/CEPAC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCepacUtil.cpp
More file actions
192 lines (176 loc) · 6.59 KB
/
Copy pathCepacUtil.cpp
File metadata and controls
192 lines (176 loc) · 6.59 KB
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
#include "include.h"
/** \brief Empty constructor and destructor, should never create an instance of this class */
CepacUtil::CepacUtil(void)
{
}
/** \brief Empty constructor and destructor, should never create an instance of this class */
CepacUtil::~CepacUtil(void)
{
}
/* Constant string values for CEPAC version and file/directory information */
/** The CEPAC Input Version to match that from the .in file */
const char *CepacUtil::CEPAC_INPUT_VERSION = "20210615";
/** CEPAC version string: the version label commonly used in CEPAC vernacular */
const char *CepacUtil::CEPAC_VERSION_STRING = "50d";
/** The compile date of the most recent release */
const char *CepacUtil::CEPAC_EXECUTABLE_COMPILED_DATE = "2025-01-09";
/** .tmp */
const char *CepacUtil::FILE_EXTENSION_FOR_TEMP = ".tmp";
/** .txt */
const char *CepacUtil::FILE_EXTENSION_FOR_TRACE = ".txt";
/** .out */
const char *CepacUtil::FILE_EXTENSION_FOR_OUTPUT = ".out";
/** .cout */
const char *CepacUtil::FILE_EXTENSION_FOR_COSTS_OUTPUT = ".cout";
/** .orph */
const char *CepacUtil::FILE_EXTENSION_FOR_ORPHAN_OUTPUT = ".orph";
/** .in */
const char *CepacUtil::FILE_EXTENSION_FOR_INPUT = ".in";
/** *.in */
const char *CepacUtil::FILE_EXTENSION_INPUT_SEARCH_STR = "*.in";
/** popstats.out */
const char *CepacUtil::FILE_NAME_SUMMARIES = "popstats.out";
/** Vector of the file names to be run*/
std::vector<std::string> CepacUtil::filesToRun;
/** The inputs directory path */
std::string CepacUtil::inputsDirectory;
/** The output directory path */
std::string CepacUtil::resultsDirectory;
/** True if we're using random seed, false for fixed seed */
bool CepacUtil::useRandomSeedByTime;
/** \brief Random number generator class */
MTRand CepacUtil::mtRand;
/** \brief useCurrentDirectoryForInputs determines the current directory and sets as inputs directory
* Currently handles OS differences for Windows, Linux, and Apple
*/
void CepacUtil::useCurrentDirectoryForInputs() {
#if defined(_WIN32)
char buffer[512];
_getcwd(buffer, 512);
inputsDirectory = buffer;
#elif defined(__linux__)
char buffer[512];
getcwd(buffer, 512);
inputsDirectory = buffer;
#else
char buffer[512];
uint32_t size = sizeof(buffer);
_NSGetExecutablePath(buffer, &size);
inputsDirectory = buffer;
inputsDirectory = inputsDirectory.substr(0, inputsDirectory.find_last_of("\\/"));
#endif
} /* end useCurrentDirectoryForInputs */
/** \brief findInputFiles locates all the .in files in the current directory and adds them
to the filesToRun vector */
void CepacUtil::findInputFiles() {
#if defined(_WIN32)
intptr_t hFile;
struct _finddata_t tFileInfo;
hFile = _findfirst( FILE_EXTENSION_INPUT_SEARCH_STR, &tFileInfo );
int nInputFiles = 0;
string fileName;
//get the list of files that we have to process
filesToRun.clear();
do {
fileName = (char *) tFileInfo.name;
filesToRun.push_back(fileName);
nInputFiles++;
} while ( _findnext ( hFile, &tFileInfo ) == 0 );
_findclose( hFile );
#else
glob_t files;
glob(FILE_EXTENSION_INPUT_SEARCH_STR, GLOB_ERR, NULL, &files);
int nInputFiles = 0;
string fileName;
filesToRun.clear();
//get the list of files that we have to process
int i;
for( i = 0; i < files.gl_pathc; i++) {
fileName = (char *) files.gl_pathv[i];
filesToRun.push_back(fileName);
++nInputFiles;
}
globfree( &files);
#endif
} /* end findInputFiles */
/** \brief createResultsDirectory creates the directory "results" as a subdirectory of the inputs one */
void CepacUtil::createResultsDirectory() {
#if defined(_WIN32)
resultsDirectory = inputsDirectory;
resultsDirectory.append("\\");
resultsDirectory.append("results");
_mkdir(resultsDirectory.c_str());
#else
resultsDirectory = inputsDirectory;
resultsDirectory.append("/");
resultsDirectory.append("results");
mkdir(resultsDirectory.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
#endif
} /* end createResultsDirectory */
/** \brief changeDirectoryToResults changes the working directory to the results one */
void CepacUtil::changeDirectoryToResults() {
#if defined(_WIN32)
_chdir(resultsDirectory.c_str());
#else
chdir(resultsDirectory.c_str());
#endif
} /* end changeDirectoryToResults */
/** \brief changeDirectoryToInputs changes the working directory to the inputs one */
void CepacUtil::changeDirectoryToInputs() {
#if defined(_WIN32)
_chdir(inputsDirectory.c_str());
#else
chdir(inputsDirectory.c_str());
#endif
} /* end changeDirectoryToInputs */
/** \brief getDateString places the current date string in the specified buffer
* \param buffer a pointer to a char array representing the buffer to add the resulting date string to
* \param bufsize an integer representing the size of buffer
**/
void CepacUtil::getDateString(char *buffer, int bufsize) {
#if defined(_WIN32)
_strdate(buffer);
#else
time_t currTime;
time(&currTime);
strftime(buffer, bufsize, "%m/%d/%y",localtime(&currTime));
#endif
} /* end getDateString */
/** \brief getTimeString places the current system time string in the specified buffer
* \param buffer a pointer to a char array representing the buffer to add the resulting time string to
* \param bufsize an integer representing the size of buffer */
void CepacUtil::getTimeString(char *buffer, int bufsize) {
#if defined(_WIN32)
_strtime(buffer);
#else
time_t currTime;
time(&currTime);
strftime(buffer, bufsize, "%H:%M:%S",localtime(&currTime));
#endif
} /* end getTimeString */
/** \brief fileExists returns true if the specified file exists, false otherwise
* \param filename a pointer to a character array representing the name of the file
**/
bool CepacUtil::fileExists(const char *filename) {
FILE *file;
//fopen_s(&file, filename, "r");
file = fopen(filename, "r");
if (!file)
return false;
fclose(file);
return true;
} /* end fileExists */
/** \brief openFile opens the specified file in the given mode
* \param filename a pointer to a character array representing the name of the file
* \param mode a pointer to a character array representing the mode to open the file in: "r" for read, "w" for write, "a" for append, "r+" for reading and writing an existing file, "w+" for reading and writing an empty file, "a+" for reading and appending to a file
**/
FILE *CepacUtil::openFile(const char *filename, const char *mode) {
FILE *file = fopen(filename, mode);
return file;
} /* end openFile */
/** \brief closeFile closes the specified file
* \param filename a pointer to a character array representing the name of the file
**/
void CepacUtil::closeFile(FILE *file) {
fclose(file);
} /* end closeFile */