• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

nasa / trick / 13061540346

30 Jan 2025 09:25PM UTC coverage: 55.92% (+0.04%) from 55.885%
13061540346

Pull #1785

github

web-flow
Merge 87dc1925e into bffb021a4
Pull Request #1785: Frame Performance Tool- jperf

15 of 18 new or added lines in 1 file covered. (83.33%)

121 existing lines in 12 files now uncovered.

12318 of 22028 relevant lines covered (55.92%)

83700.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.62
/trick_source/sim_services/CommandLineArguments/CommandLineArguments.cpp
1
/*
2
   PURPOSE: ( Handle the simulation command line args )
3

4
   REFERENCE: ( Trick Simulation Environment )
5

6
   ASSUMPTIONS AND LIMITATIONS: ( None )
7

8
   CLASS: ( N/A )
9

10
   LIBRARY DEPENDENCY: ( None )
11

12
   PROGRAMMERS: ( Keith Vetter LinCom 6/2003 ) */
13

14

15
#include <iostream>
16
#include <sys/stat.h>
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <string.h>
20
#include <unistd.h>
21
#include <cstring>
22
#include <cerrno>
23
#include <unistd.h>
24
#include <sys/types.h>
25
#include <pwd.h>
26

27
#include "trick/CommandLineArguments.hh"
28
#include "trick/memorymanager_c_intf.h"
29

30
Trick::CommandLineArguments * the_cmd_args ;
31

32
Trick::CommandLineArguments::CommandLineArguments() {
221✔
33
    the_cmd_args = this ;
221✔
34

35
    output_dir = std::string(".") ;
221✔
36
    default_dir = std::string(".") ;
221✔
37

38
    argc = 0;
221✔
39
    argv = NULL;
221✔
40
}
221✔
41

42
int Trick::CommandLineArguments::get_argc() {
715✔
43
    return(argc) ;
715✔
44
}
45

46
char ** Trick::CommandLineArguments::get_argv() {
715✔
47
    return(argv) ;
715✔
48
}
49

50
std::string Trick::CommandLineArguments::get_output_dir() {
×
51
    return(output_dir) ;
×
52
}
53

54
std::string & Trick::CommandLineArguments::get_output_dir_ref() {
1,274✔
55
    return(output_dir) ;
1,274✔
56
}
57

58
std::string Trick::CommandLineArguments::get_user_output_dir() {
×
59
    return(user_output_dir) ;
×
60
}
61

UNCOV
62
std::string & Trick::CommandLineArguments::get_user_output_dir_ref() {
×
UNCOV
63
    return(user_output_dir) ;
×
64
}
65

66
std::string Trick::CommandLineArguments::get_input_file() {
×
67
    return(input_file) ;
×
68
}
69

70
std::string & Trick::CommandLineArguments::get_input_file_ref() {
192,980✔
71
    return(input_file) ;
192,980✔
72
}
73

74
std::string Trick::CommandLineArguments::get_default_dir() {
×
75
    return(default_dir) ;
×
76
}
77

78
std::string & Trick::CommandLineArguments::get_default_dir_ref() {
192,631✔
79
    return(default_dir) ;
192,631✔
80
}
81

82
std::string Trick::CommandLineArguments::get_cmdline_name() {
×
83
    return(cmdline_name) ;
×
84
}
85

86
std::string & Trick::CommandLineArguments::get_cmdline_name_ref() {
192,626✔
87
    return(cmdline_name) ;
192,626✔
88
}
89

90
// Helper function - create a full path with error checking along the way
91
int Trick::CommandLineArguments::create_path(const std::string& dirname) {
7✔
92
    size_t cur_index = 0;
7✔
93

94
    std::string full_dir (dirname);
14✔
95

96
    // These syscalls don't seem to take care of home special character, so do it manually
97
    // I think the shell should handle it before it gets here, but just in case check for it
98
    if (dirname.at(0) == '~') {
7✔
99
        struct passwd *pw = getpwuid(getuid());
×
100
        full_dir = std::string(pw->pw_dir) + dirname.substr(1, dirname.size());
×
101
    }
102

103
    while (cur_index != full_dir.size()) {
32✔
104
        cur_index = full_dir.find('/', cur_index+1);
26✔
105
        if (cur_index == std::string::npos) {
26✔
106
            cur_index = full_dir.size();
6✔
107
        }
108
        std::string cur_dir = full_dir.substr(0, cur_index);
26✔
109
        
110
        struct stat info;
111
        if(stat( cur_dir.c_str(), &info ) != 0) {
26✔
112
            // does not exist - make it
113
            if (mkdir(cur_dir.c_str(), 0775) == -1) {
11✔
114
                std::cerr << "Error creating directory " << cur_dir << std::endl;
×
115
                return 1;
×
116
            }
117
        } else {
118
            // Does exist
119
            if(info.st_mode & S_IFDIR) {
15✔
120
                // Is a directory
121
                if (info.st_mode & S_IWUSR) {
14✔
122
                    // Perfect, nothing to do here
123
                } else {
124
                    // Not writeable
125
                    std::cerr << "Intermediate directory " << cur_dir << " is not writable, unable to create output directory." << std::endl;
×
126
                    return 1;
×
127
                }
128
            } else {
129
                // Does exist, but is a file
130
                std::cerr << "Intermediate directory " << cur_dir << " is not a directory, unable to create output directory." << std::endl;
1✔
131
                return 1;
1✔
132
            }
133
        }
134
    }
135
    return 0;
6✔
136
}
137

138
/**
139
@details
140
-# Save the number of command line arguments.
141
-# Save the command line arguments.
142
-# Determine the directory and simulation executable name from the first
143
   command line argument
144
   -# If no directory is specified
145
      -# Save the simulation executable name as the full first argument
146
      -# Get the current working directory
147
      -# Save the current working directory as the default input directory.
148
   -# If a relative or full directory is present in the command line arguments
149
      -# split the command line argument on the last "/" character
150
      -# Save the back half of the split as the simulation executable name
151
      -# Save the current working directory
152
      -# Change the current working directory to the first half of the split
153
         command line argument.
154
      -# Get the current working directory
155
      -# Save the current working directory as the default input directory.
156
      -# Change the current working directory to the saved working directory
157
-# If present, the second argument will be the run directory and the input file.
158
   -# Save the run input file  name as the full second argument
159
   -# Split the second argument on the last "/" character
160
   -# If no directory is specified set the default output and current output
161
      directories to the current directory
162
   -# Else set the default and the current output directories to the first
163
      half of the split
164
-# Search the remaining command line arguments for "-O" or "-OO"
165
   -# If found set the output direcory to the following argument
166
   -# Attempt to create the output directory if it does not exist.
167
      -# Exit if the simulation cannot be created.
168
*/
169
int Trick::CommandLineArguments::process_sim_args(int nargs , char **args) {
177✔
170

171
    char *buf, *buf2;
172
    size_t found ;
173

174
    argc = nargs ;
177✔
175
    argv = (char **)TMM_declare_var_1d("char *", argc) ;
177✔
176
    for (int ii = 0 ; ii < argc ; ii++ ) {
534✔
177
       argv[ii] = TMM_strdup(args[ii]) ;
357✔
178
    }
179

180
    buf = (char *)malloc(4096) ;
177✔
181
    buf2 = (char *)malloc(4096) ;
177✔
182

183
    default_dir = argv[0] ;
177✔
184
    found = default_dir.find_last_of("/") ;
177✔
185
    if ( found == std::string::npos ) {
177✔
186
        cmdline_name = default_dir ;
×
187
        getcwd(buf, (size_t) 256);
×
188
        default_dir = buf ;
×
189
    } else {
190
        cmdline_name = default_dir.substr(found + 1) ;
177✔
191

192
        // save the current directory
193
        getcwd(buf, (size_t) 256);
177✔
194

195
        // change to the default directory
196
        chdir(default_dir.substr(0,found).c_str());
177✔
197

198
        // get the full path default directory
199
        getcwd(buf2, (size_t) 256);
177✔
200
        default_dir = buf2 ;
177✔
201

202
        // change back to the current directory
203
        chdir(buf);
177✔
204
    }
205

206
    free(buf) ;
177✔
207
    free(buf2) ;
177✔
208

209
    if ( argc > 1 ) {
177✔
210

211
    /* First occurnance of "RUN_*" is the input file name: '<Run_dir>/<file_name>'. 
212
       If not found, defaults to first argument */
213
        
214
        input_file = argv[1];
177✔
215
        run_dir = argv[1];
177✔
216

217
        for(int ii = 1; ii < argc; ii++) {
184✔
218
            if(std::string(argv[ii]).find("RUN_") != std::string::npos) {
177✔
219
              input_file = argv[ii];
170✔
220
              run_dir = argv[ii];
170✔
221
              break;
170✔
222
            }
223
        }
224

225
        if (access(input_file.c_str(), F_OK) != 0) {
177✔
226
          input_file = "";
×
227
            if(strcmp(argv[1], "trick_version") && strcmp(argv[1], "sie") && strcmp(argv[1], "-help")  && strcmp(argv[1], "--help") &&
×
228
                strcmp(argv[1], "-h") && strcmp(argv[1], "help")) {
×
229
                std::cerr << "\nERROR: Invalid input file or command line argument." << std::endl;
×
230
                exit(1);
×
231
            }
232
        }
233

234
        found = run_dir.find_last_of("/") ;
177✔
235
        if ( found != std::string::npos ) {
177✔
236
            run_dir.erase(found) ;
177✔
237
        } else {
238
            run_dir = "." ;
×
239
        }
240
        /* check existence of run directory */
241
        if (access(run_dir.c_str(), F_OK) != 0) {
177✔
242
            std::cerr << "\nERROR: while accessing input file directory \"" << run_dir << "\" : " << std::strerror(errno) << std::endl ;
×
243
            exit(1);
×
244
        }
245

246
        output_dir = run_dir ;
177✔
247

248
        for (int ii = 1; ii < argc; ii++) {
356✔
249
            if (!strncmp("-OO", argv[ii], (size_t) 3) || !strncmp("-O", argv[ii], (size_t) 2)) {
179✔
250
                if (ii == ( argc - 1 )) {
1✔
251
                    std::cerr << "\nERROR: No directory specified after -O or -OO argument" << std::endl ;
×
252
                    exit(1) ;
×
253
                }
254
                /* Output data directory */
255
                output_dir = user_output_dir = argv[++ii];
1✔
256
                if (!strncmp("-OO", argv[ii-1], (size_t) 3)) {
1✔
257
                    output_dir = output_dir + "/" + run_dir;
1✔
258
                } 
259
            }
260
        }
261

262
        /* Create output directory if necessary. */
263
        if (access(output_dir.c_str(), F_OK) != 0) {
177✔
264
            if (create_path(output_dir) != 0) {
1✔
265
                std::cerr << "\nERROR: While trying to create output directory \"" << output_dir << "\" : " << std::strerror(errno) << std::endl ;
×
266
                exit(1) ;
×
267
            }
268
        } else {
269
            /* check permissions on output directory */
270
            if (access(output_dir.c_str(), (W_OK|X_OK) ) == -1) {
176✔
271
                std::cerr << "\nERROR: while writing to output directory \"" << output_dir << "\" : " << std::strerror(errno) << std::endl ;
×
272
                exit(2) ;
×
273
            }
274
        }
275
    }
276

277
    return(0) ;
177✔
278

279
}
280

281

282
/**
283
@design
284
-# Get the current system date and time.
285
-# Set the subdirectory within the current output directory to
286
   DATA_<year>_<month>_<day>_<hour>_<minute>_<second>
287
   -# Attempt to create the output directory if it does not exist.
288
      -# Exit if the simulation cannot be created.
289
*/
290
int Trick::CommandLineArguments::output_dir_timestamped_on() {
×
291

292
    time_t date ;
293
    struct tm *curr_time ;
294
    char temp_str[256] ;
295

296
    date = time(NULL) ;
×
297
    curr_time = localtime(&date) ;
×
298

299
    snprintf(temp_str, sizeof(temp_str), "DATA_%4d_%02d_%02d_%02d_%02d_%02d",
×
300
            curr_time->tm_year + 1900 , curr_time->tm_mon + 1 , curr_time->tm_mday,
×
301
            curr_time->tm_hour , curr_time->tm_min , curr_time->tm_sec );
302

303
    time_stamp_dir = std::string(temp_str) ;
×
304

305
    if ( ! user_output_dir.empty() ) {
×
306
        output_dir = user_output_dir + "/" + time_stamp_dir ;
×
307
    } else {
308
        output_dir = run_dir + "/" + time_stamp_dir ;
×
309
    }
310

311
    /* Create directories if necessary. */
312
    if (access(output_dir.c_str(), F_OK) != 0) {
×
313
        if (mkdir(output_dir.c_str(), 0775) == -1) {
×
314
            std::cerr << "\nERROR: While trying to create dir \"" << output_dir << "\" Exiting!" << std::endl ;
×
315
            exit(1) ;
×
316
        }
317
    }
318

319
    output_dir_timestamped = 1 ;
×
320
    return(0) ;
×
321
}
322

323
/**
324
@design
325
-# If the user had specified an output directory reset
326
   the output directory to the user specified directory
327
-# Else set the output directory to the run directory.
328
*/
329
int Trick::CommandLineArguments::output_dir_timestamped_off() {
×
330

331
    if ( ! user_output_dir.empty() ) {
×
332
        output_dir = user_output_dir ;
×
333
    } else {
334
        output_dir = run_dir ;
×
335
    }
336

337
    output_dir_timestamped = 0 ;
×
338
    return(0) ;
×
339

340
}
341

342
/**
343
@design
344
-#  Sets the value of output_dir
345
*/
346
void Trick::CommandLineArguments::set_output_dir(std::string output_directory) {
×
347
    output_dir = output_directory;
×
348
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc