GCC Code Coverage Report


Directory: ./
File: tmp_project/PhoenixTex2Html/src/tools/PSRC_SPLIT_LIB/psrc_split_lib.cpp
Date: 2025-03-24 18:12:43
Exec Total Coverage
Lines: 117 126 92.9%
Branches: 234 316 74.1%

Line Branch Exec Source
1 /***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5 ****************************************/
6
7 #include "psrc_split_lib.h"
8
9 ///Save the code into the output file
10 /** @param[out] fs : output file stream
11 * @param typeCode : type code
12 * @param[out] code : code to be saved
13 * @param isPlatexMode : true to add begin and end environnement in the output file, false otherwise
14 * @param envName : name of the associated environement
15 * @param keepTex : true to keep the ptex comment, false otherwise
16 */
17 985 void flushCode(std::ofstream & fs, PTypeCode::PTypeCode typeCode, PString & code, bool isPlatexMode, const PString & envName, bool keepTex){
18
1/2
✓ Branch 0 taken 985 times.
✗ Branch 1 not taken.
985 if(typeCode == PTypeCode::CODE){
19
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 789 times.
985 if(isPlatexMode){
20
2/2
✓ Branch 2 taken 196 times.
✓ Branch 5 taken 196 times.
196 code = code.eraseFirstLastChar("\n");
21 }else{
22
2/2
✓ Branch 2 taken 789 times.
✓ Branch 5 taken 789 times.
789 code = code.eraseFirstChar("\n");
23 }
24
2/2
✓ Branch 1 taken 837 times.
✓ Branch 2 taken 148 times.
985 if(code != ""){
25
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 674 times.
837 if(isPlatexMode){
26 163 fs << "\\begin{" << envName << "}" << std::endl;
27 163 fs << code << std::endl;
28 163 fs << "\\end{" << envName << "}" << std::endl << std::endl;
29 }else{
30 674 fs << code;
31 }
32 }
33 985 code = "";
34 }
35 985 }
36
37 ///Split the input file into the output file
38 /** @param[out] fs : output file stream
39 * @param[out] parser : parser of the input file
40 * @param keepComment : true to keep the comments of the targeted language in the output file
41 * @param keepTex : true to keep the ptex comment, false otherwise
42 * @param isPlatexMode : true to add begin and end environnement in the output file, false otherwise
43 * @param envName : name of the associated environement
44 * @param lineCommentBegin : begining of multiline comments
45 * @param lineCommenEnd : ending of multiline comments
46 * @param monolineComment : monoline comment
47 * @param ptexLineCommentBegin : begining of multiline ptex comments
48 * @param ptexLineCommenEnd : ending of multiline ptex comments
49 * @param ptexMonolineComment : monoline ptex comment
50 */
51 166 void splitInputFile(std::ofstream & fs, PFileParser & parser,
52 bool keepComment, bool keepTex, bool isPlatexMode, const PString & envName,
53 const PString & lineCommentBegin, const PString & lineCommenEnd,
54 const PString & monolineComment,
55 const PString & ptexLineCommentBegin, const PString & ptexLineCommenEnd,
56 const PString & ptexMonolineComment)
57 {
58
2/2
✓ Branch 1 taken 166 times.
✓ Branch 4 taken 166 times.
166 PString ptexComment(""), code("");
59 166 PTypeCode::PTypeCode typeCode(PTypeCode::CODE);
60
3/3
✓ Branch 1 taken 132587 times.
✓ Branch 3 taken 132421 times.
✓ Branch 4 taken 166 times.
132587 while(!parser.isEndOfFile()){
61 //Check if a ptex comment matches
62
3/3
✓ Branch 1 taken 132421 times.
✓ Branch 3 taken 334 times.
✓ Branch 4 taken 132087 times.
132421 if(parser.isMatch(ptexLineCommentBegin)){ //Multiline version
63
1/1
✓ Branch 1 taken 334 times.
334 PString fullComment(parser.getUntilKeyWithoutPatern(ptexLineCommenEnd));
64
1/1
✓ Branch 1 taken 334 times.
334 flushCode(fs, typeCode, code, isPlatexMode, envName, keepTex);
65 334 typeCode = PTypeCode::PTEX_COMMENT;
66
1/1
✓ Branch 1 taken 334 times.
334 ptexComment += fullComment;
67
3/3
✓ Branch 2 taken 132087 times.
✓ Branch 4 taken 485 times.
✓ Branch 5 taken 131602 times.
132421 }else if(parser.isMatch(ptexMonolineComment)){ //Mono line version
68
2/2
✓ Branch 1 taken 485 times.
✓ Branch 4 taken 485 times.
485 PString fullComment(parser.getUntilKeyWithoutPatern("\n"));
69
1/1
✓ Branch 1 taken 485 times.
485 flushCode(fs, typeCode, code, isPlatexMode, envName, keepTex);
70 485 typeCode = PTypeCode::PTEX_COMMENT;
71
1/1
✓ Branch 1 taken 485 times.
485 ptexComment += fullComment;
72
3/3
✓ Branch 2 taken 131602 times.
✓ Branch 4 taken 85 times.
✓ Branch 5 taken 131517 times.
132087 }else if(parser.isMatch(lineCommentBegin)){ //Check if a Standard multiline comment matches
73
4/4
✓ Branch 1 taken 85 times.
✓ Branch 4 taken 85 times.
✓ Branch 7 taken 85 times.
✓ Branch 10 taken 85 times.
170 PString fullComment(lineCommentBegin + parser.getUntilKeyWithoutPatern(lineCommenEnd) + lineCommenEnd);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if(typeCode == PTypeCode::PTEX_COMMENT){ //Flush latex comment if there was one
75 if(keepTex){fs << ptexComment << std::endl;}
76 ptexComment = "";
77 }
78 85 typeCode = PTypeCode::CODE;
79
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 67 times.
85 if(keepComment){
80
1/1
✓ Branch 1 taken 18 times.
18 code += fullComment;
81 }
82
3/3
✓ Branch 2 taken 131517 times.
✓ Branch 4 taken 365 times.
✓ Branch 5 taken 131152 times.
131602 }else if(parser.isMatch(monolineComment)){
83
5/5
✓ Branch 1 taken 365 times.
✓ Branch 4 taken 365 times.
✓ Branch 7 taken 365 times.
✓ Branch 10 taken 365 times.
✓ Branch 13 taken 365 times.
730 PString fullComment(monolineComment + parser.getUntilKeyWithoutPatern("\n") + "\n");
84
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 245 times.
365 if(typeCode == PTypeCode::PTEX_COMMENT){ //Flush latex comment if there was one
85
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 96 times.
✓ Branch 3 taken 24 times.
✓ Branch 6 taken 24 times.
120 if(keepTex){fs << ptexComment << std::endl;}
86
1/1
✓ Branch 1 taken 120 times.
120 ptexComment = "";
87 }
88 365 typeCode = PTypeCode::CODE;
89
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 292 times.
365 if(keepComment){
90
1/1
✓ Branch 1 taken 73 times.
73 code += fullComment;
91 }
92 365 }else{ //This is simple code
93
2/2
✓ Branch 0 taken 699 times.
✓ Branch 1 taken 130453 times.
131152 if(typeCode == PTypeCode::PTEX_COMMENT){ //Flush latex comment if there was one
94
4/4
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 560 times.
✓ Branch 3 taken 139 times.
✓ Branch 6 taken 139 times.
699 if(keepTex){fs << ptexComment << std::endl;}
95
1/1
✓ Branch 1 taken 699 times.
699 ptexComment = "";
96 }
97 131152 typeCode = PTypeCode::CODE;
98
2/2
✓ Branch 1 taken 131152 times.
✓ Branch 4 taken 131152 times.
131152 code += parser.getCurrentCh();
99
1/1
✓ Branch 1 taken 131152 times.
131152 parser.getNextChar();
100 }
101 }
102 //Check is there is still somme code, standardComment or ptexComment to be saved
103
1/1
✓ Branch 1 taken 166 times.
166 flushCode(fs, typeCode, code, isPlatexMode, envName, keepTex);
104
3/7
✓ Branch 1 taken 166 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 166 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 166 times.
166 if(ptexComment != "" && keepTex){
105 fs << ptexComment << std::endl;
106 }
107 166 }
108
109
110 ///Create the PTex file
111 /** @param outputFile : file to be written
112 * @param inputFile : file to be red
113 * @param keepComment : true to keep the comments of the targeted language in the output file
114 * @param keepTex : true to keep the ptex comment, false otherwise
115 * @param removefirstcomment : remove the first comment of the file
116 * @param isPlatexMode : true to add begin and end environnement in the output file, false otherwise
117 * @param envName : name of the associated environement
118 * @param lineCommentBegin : begining of multiline comments
119 * @param lineCommenEnd : ending of multiline comments
120 * @param monolineComment : monoline comment
121 * @return true on success, false otherwise
122 */
123 168 bool createPTexFile(const PPath & outputFile, const PPath & inputFile,
124 bool keepComment, bool keepTex, bool removefirstcomment, bool isPlatexMode,
125 const PString & envName,
126 const PString & lineCommentBegin, const PString & lineCommenEnd, const PString & monolineComment)
127 {
128
1/1
✓ Branch 1 taken 168 times.
168 PString ptexLineCommentBegin("");
129
1/1
✓ Branch 1 taken 168 times.
168 PString ptexLineCommenEnd("");
130
7/8
✓ Branch 1 taken 168 times.
✓ Branch 3 taken 113 times.
✓ Branch 4 taken 55 times.
✓ Branch 6 taken 113 times.
✓ Branch 8 taken 113 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 113 times.
✓ Branch 11 taken 55 times.
168 if(lineCommentBegin != "" && lineCommenEnd != ""){
131
2/2
✓ Branch 1 taken 113 times.
✓ Branch 4 taken 113 times.
113 ptexLineCommentBegin = lineCommentBegin + "{";
132
2/2
✓ Branch 1 taken 113 times.
✓ Branch 4 taken 113 times.
113 ptexLineCommenEnd = "}" + lineCommenEnd;
133 }
134
1/1
✓ Branch 1 taken 168 times.
168 PString ptexMonolineComment("");
135
3/3
✓ Branch 1 taken 168 times.
✓ Branch 3 taken 153 times.
✓ Branch 4 taken 15 times.
168 if(monolineComment != ""){
136
2/2
✓ Branch 1 taken 153 times.
✓ Branch 4 taken 153 times.
153 ptexMonolineComment = monolineComment + "{";
137 }
138
139
1/1
✓ Branch 1 taken 168 times.
168 std::ofstream fs;
140
3/3
✓ Branch 1 taken 168 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 167 times.
168 if(!openFileStream(fs, outputFile)){return false;}
141
142
1/1
✓ Branch 1 taken 167 times.
167 PFileParser parser;
143
3/3
✓ Branch 1 taken 167 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 166 times.
167 if(!parser.open(inputFile)){
144
4/4
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
1 std::cerr << "createPTexFile : can't parse input file '"<<inputFile<<"'" << std::endl;
145 1 return false;
146 }
147
1/1
✓ Branch 1 taken 166 times.
166 parser.setEscapeChar('\0');
148
2/2
✓ Branch 1 taken 166 times.
✓ Branch 4 taken 166 times.
166 parser.setWhiteSpace("");
149
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 133 times.
166 if(removefirstcomment){
150
3/3
✓ Branch 1 taken 33 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 27 times.
33 if(parser.isMatch(lineCommentBegin)){
151
1/1
✓ Branch 1 taken 6 times.
6 parser.getUntilKeyWithoutPatern(lineCommenEnd);
152 }
153 }
154
1/1
✓ Branch 1 taken 166 times.
166 splitInputFile(fs, parser, keepComment, keepTex, isPlatexMode, envName, lineCommentBegin, lineCommenEnd, monolineComment,
155 ptexLineCommentBegin, ptexLineCommenEnd, ptexMonolineComment);
156
1/1
✓ Branch 1 taken 166 times.
166 fs.close();
157 166 return true;
158 168 }
159
160 ///Get the output file name
161 /** @param outputFile : name of the output file
162 * @param isPlatexMode : true if we want to create ptex output files
163 * @return output file name
164 */
165 309 PPath getSplitOutputFileName(const PPath & outputFile, bool isPlatexMode){
166
1/1
✓ Branch 1 taken 309 times.
309 PString nameOutputFile(outputFile);
167
1/1
✓ Branch 1 taken 309 times.
309 PString ext(outputFile.getExtension());
168
7/7
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 249 times.
✓ Branch 3 taken 60 times.
✓ Branch 5 taken 36 times.
✓ Branch 6 taken 24 times.
✓ Branch 7 taken 36 times.
✓ Branch 8 taken 273 times.
309 if(isPlatexMode && ext != "ptex"){
169
1/1
✓ Branch 1 taken 36 times.
36 nameOutputFile += ".ptex";
170 }
171
1/1
✓ Branch 1 taken 309 times.
618 return nameOutputFile;
172 309 }
173
174 ///Process the input file
175 /** @param outputFile : file to be written
176 * @param inputFile : file to be red
177 * @param keepComment : true to keep the comments of the targeted language in the output file
178 * @param keepTex : true to keep the ptex comment, false otherwise
179 * @param removefirstcomment : remove the first comment of the file
180 * @param isPlatexMode : true to add begin and end environnement in the output file, false otherwise
181 * @return true on success, false otherwise
182 */
183 187 bool processFile(const PPath & outputFile, const PPath & inputFile,
184 bool keepComment, bool keepTex, bool removefirstcomment, bool isPlatexMode)
185 {
186
3/6
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 187 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 187 times.
187 if(outputFile == "" || inputFile == ""){
187 std::cerr << "processFile : missing input or output file" << std::endl;
188 return false;
189 }
190
2/2
✓ Branch 1 taken 187 times.
✓ Branch 4 taken 187 times.
187 PString nameOutputFile(getSplitOutputFileName(outputFile, isPlatexMode));
191
1/1
✓ Branch 1 taken 187 times.
187 PString ext(inputFile.getExtension());
192
3/3
✓ Branch 1 taken 187 times.
✓ Branch 4 taken 187 times.
✓ Branch 7 taken 187 times.
187 PString baseFileName(inputFile.getFileName().eraseExtension());
193
9/12
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 171 times.
✓ Branch 5 taken 16 times.
✓ Branch 7 taken 171 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 154 times.
✓ Branch 11 taken 17 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 154 times.
✓ Branch 15 taken 33 times.
✓ Branch 16 taken 154 times.
187 if(ext == "c" || ext == "cpp" || ext == "c++" || ext == "h" || ext == "hpp"){
194
6/6
✓ Branch 1 taken 33 times.
✓ Branch 4 taken 33 times.
✓ Branch 7 taken 33 times.
✓ Branch 10 taken 33 times.
✓ Branch 13 taken 33 times.
✓ Branch 16 taken 33 times.
33 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "cpp", "/*", "*/", "//");
195
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 139 times.
154 }else if(ext == "cu"){
196
6/6
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
✓ Branch 16 taken 15 times.
15 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "cuda", "/*", "*/", "//");
197
5/6
✓ Branch 1 taken 139 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 119 times.
✓ Branch 6 taken 20 times.
✓ Branch 7 taken 119 times.
139 }else if(ext == "cmake" || baseFileName == "CMakeLists"){
198
5/5
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✓ Branch 7 taken 20 times.
✓ Branch 10 taken 20 times.
✓ Branch 13 taken 20 times.
20 return createPTexFile(outputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "cmake", "#[[", "]]", "#");
199
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 104 times.
119 }else if(ext == "py"){
200
6/6
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
✓ Branch 16 taken 15 times.
15 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "python", "'''", "'''", "#");
201
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 89 times.
104 }else if(ext == "sh"){
202
6/6
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
✓ Branch 16 taken 15 times.
15 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "bash", ": '", "'", "#");
203
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 69 times.
89 }else if(baseFileName == "Dockerfile"){
204
6/6
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✓ Branch 7 taken 20 times.
✓ Branch 10 taken 20 times.
✓ Branch 13 taken 20 times.
✓ Branch 16 taken 20 times.
20 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "dockerfile", "", "", "#");
205
6/6
✓ Branch 1 taken 59 times.
✓ Branch 2 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 49 times.
✓ Branch 6 taken 20 times.
✓ Branch 7 taken 49 times.
69 }else if(baseFileName == "Singularity" || baseFileName == "Apptainer"){
206
6/6
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✓ Branch 7 taken 20 times.
✓ Branch 10 taken 20 times.
✓ Branch 13 taken 20 times.
✓ Branch 16 taken 20 times.
20 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "apptainer", "", "", "#");
207
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 }else if(ext == "submit"){
208 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "condor", "", "", "#");
209
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 34 times.
49 }else if(ext == "yml"){
210
6/6
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
✓ Branch 16 taken 15 times.
15 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "yml", "", "", "#");
211
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
34 } if(ext == "toml"){
212 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "toml", "", "", "#");
213
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 19 times.
34 }else if(ext == "md"){
214
6/6
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
✓ Branch 16 taken 15 times.
15 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "markdown", "<!--", "-->", "");
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 }else if(ext == "gnuplot"){
216 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "gnuplot", "", "", "#");
217
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 }else if(ext == "rs"){
218 return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "rust", "/*", "*/", "//");
219 }else{
220
6/6
✓ Branch 1 taken 19 times.
✓ Branch 4 taken 19 times.
✓ Branch 7 taken 19 times.
✓ Branch 10 taken 19 times.
✓ Branch 13 taken 19 times.
✓ Branch 16 taken 19 times.
19 std::cout << "Copy file '"<<inputFile<<"' into '"<<nameOutputFile<<"'" << std::endl;
221
1/1
✓ Branch 2 taken 19 times.
19 std::ifstream ifs(inputFile.c_str());
222
1/1
✓ Branch 2 taken 19 times.
19 std::ofstream fs(nameOutputFile.c_str());
223
2/2
✓ Branch 1 taken 19 times.
✓ Branch 4 taken 19 times.
19 fs << ifs.rdbuf();
224
1/1
✓ Branch 1 taken 19 times.
19 fs.close();
225 19 return true;
226 19 }
227 187 }
228
229
230
231