GCC Code Coverage Report


Directory: ./
File: Core/commandLineOption.cpp
Date: 2024-04-14 07:32:34
Exec Total Coverage
Lines: 49 99 49.5%
Branches: 35 124 28.2%

Line Branch Exec Source
1 // ______ ______ _ _ _____ ______
2 // | ____| ____| | (_)/ ____| | ____|
3 // | |__ | |__ | | _| (___ ___| |__
4 // | __| | __| | | | |\___ \ / __| __|
5 // | | | |____| |____| |____) | (__| |____
6 // |_| |______|______|_|_____/ \___|______|
7 // Finite Elements for Life Sciences and Engineering
8 //
9 // License: LGL2.1 License
10 // FELiScE default license: LICENSE in root folder
11 //
12 // Main authors: David Froger
13 //
14
15 // System includes
16
17 // External includes
18
19 // Project includes
20 #include "Core/commandLineOption.hpp"
21
22 namespace felisce
23 {
24 CommandLineOption::CommandLineOption()
25 {
26 }
27
28 /***********************************************************************************/
29 /***********************************************************************************/
30
31 465 CommandLineOption::CommandLineOption(const int argc, const char** argv, const std::size_t instanceIndex)
32
1/2
✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
465 : mInstanceIndex(instanceIndex)
33 {
34 // Proper initialization of the command line options
35
1/2
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
465 initialize(argc,argv);
36 465 }
37
38 /***********************************************************************************/
39 /***********************************************************************************/
40
41 CommandLineOption::CommandLineOption(int argc, char** argv)
42 : CommandLineOption(argc, const_cast<const char**>(argv))
43 {
44 }
45
46 /***********************************************************************************/
47 /***********************************************************************************/
48
49 CommandLineOption::CommandLineOption(const CommandLineOption& opt)
50 {
51 m_dataFileName = opt.m_dataFileName;
52 m_printHelp = opt.m_printHelp;
53
54 ppcharCopy(opt.m_argcSpecial, opt.m_argvSpecial, m_argcSpecial, m_argvSpecial);
55 ppcharCopy(opt.m_argcFelisce, opt.m_argvFelisce, m_argcFelisce, m_argvFelisce);
56 ppcharCopy(opt.m_argcPetsc, opt.m_argvPetsc, m_argcPetsc, m_argvPetsc);
57 }
58
59 /***********************************************************************************/
60 /***********************************************************************************/
61
62 465 CommandLineOption::~CommandLineOption()
63 {
64 465 ppcharFree(m_argcSpecial, m_argvSpecial);
65 465 ppcharFree(m_argcFelisce, m_argvFelisce);
66 465 ppcharFree(m_argcPetsc , m_argvPetsc);
67 465 }
68
69 //---------------------------------------------------------------------------
70 // Main functions
71 //---------------------------------------------------------------------------
72 465 void CommandLineOption::initialize(const int argc, const char **argv)
73 {
74 // For commodity, create C++-style list of strings instead
75 // of C-style list of strings,
76 465 std::vector<std::string> argvec,
77 465 argvecFelisce,
78 465 argvecPetsc,
79 465 argvecSpecial;
80
1/2
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
465 ppcharToVstring(argc,argv,argvec);
81
82 // Everyone wants the executable name.
83
1/2
✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
465 argvecFelisce.push_back( argvec.front() );
84
1/2
✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
465 argvecPetsc .push_back( argvec.front() );
85
1/2
✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
465 argvecSpecial.push_back( argvec.front() );
86
1/2
✓ Branch 3 taken 465 times.
✗ Branch 4 not taken.
465 argvec.erase( argvec.begin() );
87
88 // Take and consumes options from the front of the argv list
89 // until the list is empty, and put its to appropriate list.
90 465 int checkInfiniteLoop = 0;
91
2/2
✓ Branch 1 taken 461 times.
✓ Branch 2 taken 465 times.
926 while (argvec.size() > 0) {
92 // Infinte loop
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 461 times.
461 if ( checkInfiniteLoop++ > 10000) {
94 FEL_ERROR("Entered into an infinite loop")
95 }
96
97 // Distribute options
98
2/4
✓ Branch 1 taken 461 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 461 times.
✗ Branch 4 not taken.
461 if ( CommandLineOption::transferSpecialOption(argvec, argvecSpecial) ) continue;
99 if ( CommandLineOption::transferFelisceOption(argvec, argvecFelisce) ) continue;
100 if ( CommandLineOption::transferPetscOption (argvec, argvecPetsc ) ) continue;
101
102 // Invalid option
103 std::string msg = "Invalid command line option: ";
104 msg += argvec[0];
105 FEL_ERROR(msg);
106 }
107
108 // Convert back C++-style list of strings to the obligatory C-style list
109 // of strings.
110
1/2
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
465 ppcharFromVstring(argvecSpecial, m_argcSpecial, m_argvSpecial);
111
1/2
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
465 ppcharFromVstring(argvecFelisce, m_argcFelisce, m_argvFelisce);
112
1/2
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
465 ppcharFromVstring(argvecPetsc, m_argcPetsc, m_argvPetsc);
113
114
1/2
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
465 parseSpecialOptions(m_argcSpecial,m_argvSpecial,m_printHelp,m_dataFileName);
115 465 }
116
117 //---------------------------------------------------------------------------
118 // Access functions
119 //---------------------------------------------------------------------------
120 void CommandLineOption::optFelisce(int& argcFel, char**& argvFel)
121 {
122 ppcharCopy(m_argcFelisce, m_argvFelisce, argcFel, argvFel);
123 }
124
125 void CommandLineOption::optPetsc(int& argcPet, char**& argvPet)
126 {
127 ppcharCopy(m_argcPetsc, m_argvPetsc, argcPet, argvPet);
128 }
129
130 //---------------------------------------------------------------------------
131 // Static functions
132 //---------------------------------------------------------------------------
133 461 bool CommandLineOption::transferSpecialOption(
134 std::vector<std::string>& argvec,
135 std::vector<std::string>& argvecSpecial
136 )
137 {
138
1/2
✓ Branch 2 taken 461 times.
✗ Branch 3 not taken.
461 std::string arg = argvec.front();
139
140
2/6
✗ Branch 1 not taken.
✓ Branch 2 taken 461 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 461 times.
✗ Branch 7 not taken.
461 if (arg=="-f" || arg=="--file") {
141
1/2
✓ Branch 1 taken 461 times.
✗ Branch 2 not taken.
461 argvecSpecial.push_back(arg);
142
1/2
✓ Branch 3 taken 461 times.
✗ Branch 4 not taken.
461 argvec.erase( argvec.begin() );
143
144
1/2
✓ Branch 1 taken 461 times.
✗ Branch 2 not taken.
461 if (argvec.size() >= 1) {
145 461 std::size_t counter_to_erase = 0;
146
2/2
✓ Branch 4 taken 491 times.
✓ Branch 5 taken 461 times.
952 for (auto& r_input : argvec) {
147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 491 times.
491 if (r_input.find("--help") != std::string::npos) {
148 break;
149 }
150 491 ++counter_to_erase;
151 }
152
153
1/2
✓ Branch 5 taken 461 times.
✗ Branch 6 not taken.
461 arg = *(argvec.begin() + this->instanceIndex());
154
1/2
✓ Branch 1 taken 461 times.
✗ Branch 2 not taken.
461 argvecSpecial.push_back(arg);
155
2/2
✓ Branch 0 taken 491 times.
✓ Branch 1 taken 461 times.
952 for (std::size_t i = 0; i < counter_to_erase; ++i) {
156
1/2
✓ Branch 3 taken 491 times.
✗ Branch 4 not taken.
491 argvec.erase( argvec.begin() );
157 }
158 461 return true;
159 } else {
160 FEL_ERROR("Missing filename. Syntax: --file FILENAME or -f FILENAME");
161 return false; // to avoid warnings
162 }
163 } else if (arg=="--help") {
164 argvecSpecial.push_back(arg);
165 argvec.erase( argvec.begin() );
166 return true;
167 } else {
168 return false;
169 }
170 461 }
171
172 /***********************************************************************************/
173 /***********************************************************************************/
174
175 bool CommandLineOption::transferFelisceOption(
176 std::vector<std::string>& argvec,
177 std::vector<std::string>& argvecFelisce
178 )
179 {
180 std::string arg = argvec.front();
181
182 std::string prefix = "--felisce-";
183
184 // Option is for Felisce if it starts with --felisce-.
185 if ( startswith(arg,prefix) ) {
186 argvecFelisce.push_back(arg);
187 argvec.erase( argvec.begin() );
188 return true;
189 } else {
190 return false;
191 }
192 }
193
194 /***********************************************************************************/
195 /***********************************************************************************/
196
197 bool CommandLineOption::transferPetscOption(
198 std::vector<std::string>& argvec,
199 std::vector<std::string>& argvecPetsc
200 )
201 {
202 std::string arg = argvec.front();
203
204 argvecPetsc.push_back(arg);
205 argvec.erase( argvec.begin() );
206 return true;
207 }
208
209 /***********************************************************************************/
210 /***********************************************************************************/
211
212 465 void CommandLineOption::parseSpecialOptions(
213 int argcSpecial,
214 char** argvSpecial,
215 bool& printHelp,
216 std::string& dataFileName
217 )
218 {
219
2/4
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 465 times.
✗ Branch 5 not taken.
465 GetPot parser(argcSpecial, argvSpecial);
220
2/4
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 465 times.
✗ Branch 5 not taken.
465 dataFileName = parser.follow("data", 2, "-f", "--file");
221
1/2
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
465 printHelp = parser.search("--help");
222 465 }
223
224 /***********************************************************************************/
225 /***********************************************************************************/
226
227 void CommandLineOption::print(int verbose, std::ostream& outstr) const
228 {
229 if (verbose) {
230 outstr << "Special option:" << std::endl;
231 for (int i = 0; i < m_argcSpecial; ++i)
232 outstr << m_argvSpecial[i] << std::endl;
233
234 outstr << "\nFelisce option:" << std::endl;
235 for (int i = 0; i < m_argcFelisce; ++i)
236 outstr << m_argvFelisce[i] << std::endl;
237
238 outstr << "\nPetsc option:" << std::endl;
239 for (int i = 0; i < m_argcPetsc; ++i)
240 outstr << m_argvPetsc[i] << std::endl;
241 }
242 }
243 }
244