Directory: | ./ |
---|---|
File: | tmp_project/PhoenixTex2Html/src/tools/PSRC_SPLIT_PTEX_DIR/main.cpp |
Date: | 2025-03-24 18:12:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 82 | 86 | 95.3% |
Branches: | 121 | 138 | 87.7% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include <sys/stat.h> | ||
8 | #include <sys/types.h> | ||
9 | |||
10 | #include "OptionParser.h" | ||
11 | #include "psrc_split_lib.h" | ||
12 | |||
13 | |||
14 | ///@brief arguments of the phoenix_srcsplittexdir program | ||
15 | struct SplitArgument{ | ||
16 | ///name of the output directory | ||
17 | PPath outputDir{"./"}; | ||
18 | ///list of the input files or directories | ||
19 | PVecPath listInputFile; | ||
20 | ///true to keep the comments of the targeted language in the output file | ||
21 | bool keepComment{false}; | ||
22 | ///true to keep the ptex comment, false otherwise | ||
23 | bool keepTex{false}; | ||
24 | ///remove the first comment of the file | ||
25 | bool removefirstcomment{false}; | ||
26 | ///true to add begin and end environnement in the output file, false otherwise | ||
27 | bool isPlatexMode{false}; | ||
28 | ///Directory to be ignored | ||
29 | PVecPath listIgnoreDir; | ||
30 | }; | ||
31 | |||
32 | bool processFileOrDir(const PPath & outputDir, const PPath & inputFile, const SplitArgument & option); | ||
33 | |||
34 | ///Create the OptionParser of this program | ||
35 | /** @return OptionParser of this program | ||
36 | */ | ||
37 | 57 | OptionParser createOptionParser(){ | |
38 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | OptionParser parser(true, __PROGRAM_VERSION__); |
39 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | parser.setExampleLongOption("phoenix_srcsplittexdir --input=fileInput.cpp --output=output.ptex --platex"); |
40 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | parser.setExampleShortOption("phoenix_srcsplittexdir -i fileInput.cpp -o output.ptex -p"); |
41 | |||
42 |
4/4✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
✓ Branch 7 taken 57 times.
✓ Branch 10 taken 57 times.
|
57 | parser.addOption("input", "i", OptionType::FILENAME, true, "name of the input file (source, c, cpp, cmake, py, cu)"); |
43 | |||
44 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | PPath defaultOutputDir("./"); |
45 |
5/5✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
✓ Branch 7 taken 57 times.
✓ Branch 10 taken 57 times.
✓ Branch 13 taken 57 times.
|
57 | parser.addOption("output", "o", defaultOutputDir, "output directory for generated files and directories"); |
46 | |||
47 |
4/4✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
✓ Branch 7 taken 57 times.
✓ Branch 10 taken 57 times.
|
57 | parser.addOption("keepcomment", "k", OptionType::NONE, false, "keep the comment of the targeted language"); |
48 |
4/4✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
✓ Branch 7 taken 57 times.
✓ Branch 10 taken 57 times.
|
57 | parser.addOption("keeptex", "t", OptionType::NONE, false, "keep the ptex comment in the output file"); |
49 |
4/4✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
✓ Branch 7 taken 57 times.
✓ Branch 10 taken 57 times.
|
57 | parser.addOption("removefirstcomment", "f", OptionType::NONE, false, "remove the first comment of the file (can be the Licence or whatever)"); |
50 |
4/4✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
✓ Branch 7 taken 57 times.
✓ Branch 10 taken 57 times.
|
57 | parser.addOption("platex", "p", OptionType::NONE, false, "activate platex mode (add begin and end around code part)"); |
51 | |||
52 |
4/4✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
✓ Branch 7 taken 57 times.
✓ Branch 10 taken 57 times.
|
57 | parser.addOption("ignoredir", "d", OptionType::FILENAME, false, "List of directories to be ignored"); |
53 | 114 | return parser; | |
54 | 57 | } | |
55 | |||
56 | ///Process a list of files or directories and create the output directory | ||
57 | /** @param outputDir : name of the output directory | ||
58 | * @param directoryName : name of the directory to be created | ||
59 | * @param listInputFile : list of the input files or directories | ||
60 | * @param option : arguments of the program | ||
61 | * @return true on success, false otherwise | ||
62 | */ | ||
63 | 10 | bool createDirectoryWithFiles(const PPath & outputDir, const PPath & directoryName, const PVecPath & listInputFile, const SplitArgument & option) | |
64 | { | ||
65 | 10 | bool b(true); | |
66 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | PPath currentOutputDir(outputDir/ directoryName.getFileName()); |
67 |
2/3✓ Branch 1 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
|
10 | if(!currentOutputDir.createDirectory()){ |
68 | ✗ | std::cerr << "createDirectoryWithFiles : Can't create directory '" << currentOutputDir << "'" << std::endl; | |
69 | ✗ | return false; | |
70 | } | ||
71 |
2/2✓ Branch 3 taken 75 times.
✓ Branch 4 taken 10 times.
|
85 | for(PVecPath::const_iterator it(listInputFile.begin()); it != listInputFile.end(); ++it){ |
72 |
1/1✓ Branch 2 taken 75 times.
|
75 | b &= processFileOrDir(currentOutputDir, *it, option); |
73 | } | ||
74 | 10 | return b; | |
75 | 10 | } | |
76 | |||
77 | ///Remove Path if they are ignored | ||
78 | /** @param vecPath : vector of Path | ||
79 | * @param listIgnoreDir : vector of ignored directories | ||
80 | * @return cleared vector of Path | ||
81 | */ | ||
82 | 10 | PVecPath pathRemoveIfIgnored(const PVecPath & vecPath, const PVecPath & listIgnoreDir){ | |
83 | 10 | PVecPath vecOut; | |
84 |
2/2✓ Branch 4 taken 75 times.
✓ Branch 5 taken 10 times.
|
85 | for(PVecPath::const_iterator itPath(vecPath.begin()); itPath != vecPath.end(); ++itPath){ |
85 | 75 | bool isFound = false; | |
86 | 75 | PVecPath::const_iterator it(listIgnoreDir.begin()); | |
87 |
2/6✗ Branch 2 not taken.
✓ Branch 3 taken 75 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 75 times.
|
75 | while(it != listIgnoreDir.end() && !isFound){ |
88 | ✗ | isFound = itPath->getFileName() == *it; | |
89 | ✗ | ++it; | |
90 | } | ||
91 |
1/2✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
|
75 | if(!isFound){ |
92 |
1/1✓ Branch 2 taken 75 times.
|
75 | vecOut.push_back(*itPath); |
93 | } | ||
94 | } | ||
95 | 10 | return vecOut; | |
96 | } | ||
97 | |||
98 | ///Process a file or a directory | ||
99 | /** @param outputDir : current output directory | ||
100 | * @param inputFile : input file or directory | ||
101 | * @param option : arguments of the program | ||
102 | * @return true on success, false otherwise | ||
103 | */ | ||
104 | 132 | bool processFileOrDir(const PPath & outputDir, const PPath & inputFile, const SplitArgument & option){ | |
105 | 132 | bool isPlatexMode = option.isPlatexMode; | |
106 |
4/4✓ Branch 1 taken 132 times.
✓ Branch 4 taken 132 times.
✓ Branch 7 taken 132 times.
✓ Branch 10 taken 132 times.
|
132 | std::cout << "createFileFunctionGenerator : begin '" << inputFile <<"'" << std::endl; |
107 |
1/1✓ Branch 1 taken 132 times.
|
132 | PPath fileName(inputFile.getFileName()); |
108 | //Get the directory content : | ||
109 |
1/1✓ Branch 1 taken 132 times.
|
132 | PVecPath listFileInDir = inputFile.getAllElementInDir(); |
110 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 122 times.
|
132 | if(listFileInDir.size() != 0lu){ |
111 |
4/4✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
|
10 | std::cout << "processFileOrDir : get list file in directory '"<<inputFile<<"' :" << std::endl; |
112 | //Let's remove ignore directories | ||
113 | 10 | const PVecPath & listIgnoreDir = option.listIgnoreDir; | |
114 |
1/1✓ Branch 1 taken 10 times.
|
10 | PVecPath vecKeepDir = pathRemoveIfIgnored(listFileInDir, listIgnoreDir); |
115 | |||
116 |
2/2✓ Branch 3 taken 75 times.
✓ Branch 4 taken 10 times.
|
85 | for(PVecPath::iterator it(vecKeepDir.begin()); it != vecKeepDir.end(); ++it){ |
117 |
2/2✓ Branch 2 taken 75 times.
✓ Branch 6 taken 75 times.
|
75 | *it = inputFile / (*it); |
118 | } | ||
119 |
3/3✓ Branch 1 taken 10 times.
✓ Branch 5 taken 10 times.
✓ Branch 8 taken 10 times.
|
10 | std::cout << "Number of files = " << vecKeepDir.size() << std::endl; |
120 | // phoenix_print(vecKeepDir); | ||
121 |
1/1✓ Branch 1 taken 10 times.
|
10 | return createDirectoryWithFiles(outputDir, inputFile, vecKeepDir, option); |
122 | 10 | } | |
123 |
4/4✓ Branch 1 taken 122 times.
✓ Branch 4 taken 122 times.
✓ Branch 7 taken 122 times.
✓ Branch 10 taken 122 times.
|
122 | std::cout << "processFileOrDir : create file '" << fileName << "'" << std::endl; |
124 |
3/3✓ Branch 1 taken 122 times.
✓ Branch 4 taken 122 times.
✓ Branch 7 taken 122 times.
|
244 | PPath outputFile(getSplitOutputFileName(outputDir / inputFile.getFileName(), isPlatexMode)); |
125 | //Get the permissions on the input file | ||
126 | |||
127 | 122 | int fullMode(0); | |
128 | 122 | bool hasMode(true); | |
129 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 24 times.
|
122 | if(!isPlatexMode){ |
130 | struct stat fileStat; | ||
131 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 97 times.
|
98 | if(stat(inputFile.c_str(), &fileStat) < 0){ |
132 | 1 | hasMode = false; | |
133 |
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 << "processFileOrDir : can't get permission of file '"<<inputFile<<"'" << std::endl; |
134 | }else{ | ||
135 | 97 | fullMode = fileStat.st_mode; | |
136 | } | ||
137 | } | ||
138 |
1/1✓ Branch 1 taken 122 times.
|
122 | bool b(processFile(outputFile, inputFile, option.keepComment, option.keepTex, option.removefirstcomment, isPlatexMode)); |
139 | //Modify the permissions on the output file | ||
140 |
4/4✓ Branch 0 taken 121 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 97 times.
✓ Branch 3 taken 24 times.
|
122 | if(hasMode && !isPlatexMode){ |
141 |
3/3✓ Branch 1 taken 97 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 96 times.
|
97 | if(!outputFile.changeMode(fullMode)){ |
142 |
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 << "processFileOrDir : Cannot set mode of file '"<<outputFile<<"'" << std::endl; |
143 | } | ||
144 | } | ||
145 | 122 | return b; | |
146 | 132 | } | |
147 | |||
148 | ///Process a list of files or directories | ||
149 | /** @param option : arguments of the program | ||
150 | * @return true on success, false otherwise | ||
151 | */ | ||
152 | 57 | bool processListFile(const SplitArgument & option){ | |
153 | 57 | const PVecPath & listInputFile = option.listInputFile; | |
154 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
|
57 | if(listInputFile.size() == 0lu){return true;} |
155 | 57 | bool b(true); | |
156 |
2/2✓ Branch 3 taken 57 times.
✓ Branch 4 taken 57 times.
|
114 | for(PVecPath::const_iterator it(listInputFile.begin()); it != listInputFile.end(); ++it){ |
157 |
1/1✓ Branch 2 taken 57 times.
|
57 | b &= processFileOrDir(option.outputDir, *it, option); |
158 | } | ||
159 | 57 | return b; | |
160 | } | ||
161 | |||
162 | |||
163 | 57 | int main(int argc, char** argv){ | |
164 |
1/1✓ Branch 1 taken 57 times.
|
57 | OptionParser parser = createOptionParser(); |
165 |
1/1✓ Branch 1 taken 57 times.
|
57 | parser.parseArgument(argc, argv); |
166 | |||
167 |
1/1✓ Branch 1 taken 57 times.
|
57 | SplitArgument option; |
168 | |||
169 |
1/1✓ Branch 1 taken 57 times.
|
57 | const OptionMode & defaultMode = parser.getDefaultMode(); |
170 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | defaultMode.getValue(option.listInputFile, "input"); |
171 | |||
172 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | defaultMode.getValue(option.outputDir, "output"); |
173 | |||
174 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | option.keepComment = defaultMode.isOptionExist("keepcomment"); |
175 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | option.keepTex = defaultMode.isOptionExist("keeptex"); |
176 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | option.removefirstcomment = defaultMode.isOptionExist("removefirstcomment"); |
177 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | option.isPlatexMode = defaultMode.isOptionExist("platex"); |
178 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
|
57 | defaultMode.getValue(option.listIgnoreDir, "ignoredir"); |
179 |
1/1✓ Branch 1 taken 57 times.
|
57 | bool b(processListFile(option)); |
180 | 57 | return b - 1; | |
181 | 57 | } | |
182 | |||
183 | |||
184 | |||
185 | |||
186 |