PhoenixLecture  2.0.0
Set of tools to make lectures
psrc_split_lib.cpp
Go to the documentation of this file.
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 
10 
17 void flushCode(std::ofstream & fs, PTypeCode::PTypeCode typeCode, PString & code, bool isPlatexMode, const PString & envName, bool keepTex){
18  if(typeCode == PTypeCode::CODE){
19  if(isPlatexMode){
20  code = code.eraseFirstLastChar("\n");
21  }else{
22  code = code.eraseFirstChar("\n");
23  }
24  if(code != ""){
25  if(isPlatexMode){
26  fs << "\\begin{" << envName << "}" << std::endl;
27  fs << code << std::endl;
28  fs << "\\end{" << envName << "}" << std::endl << std::endl;
29  }else{
30  fs << code;
31  }
32  }
33  code = "";
34  }
35 }
36 
38 
51 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  PString ptexComment(""), code("");
60  while(!parser.isEndOfFile()){
61  //Check if a ptex comment matches
62  if(parser.isMatch(ptexLineCommentBegin)){ //Multiline version
63  PString fullComment(parser.getUntilKeyWithoutPatern(ptexLineCommenEnd));
64  flushCode(fs, typeCode, code, isPlatexMode, envName, keepTex);
65  typeCode = PTypeCode::PTEX_COMMENT;
66  ptexComment += fullComment;
67  }else if(parser.isMatch(ptexMonolineComment)){ //Mono line version
68  PString fullComment(parser.getUntilKeyWithoutPatern("\n"));
69  flushCode(fs, typeCode, code, isPlatexMode, envName, keepTex);
70  typeCode = PTypeCode::PTEX_COMMENT;
71  ptexComment += fullComment;
72  }else if(parser.isMatch(lineCommentBegin)){ //Check if a Standard multiline comment matches
73  PString fullComment(lineCommentBegin + parser.getUntilKeyWithoutPatern(lineCommenEnd) + lineCommenEnd);
74  if(typeCode == PTypeCode::PTEX_COMMENT){ //Flush latex comment if there was one
75  if(keepTex){fs << ptexComment << std::endl;}
76  ptexComment = "";
77  }
78  typeCode = PTypeCode::CODE;
79  if(keepComment){
80  code += fullComment;
81  }
82  }else if(parser.isMatch(monolineComment)){
83  PString fullComment(monolineComment + parser.getUntilKeyWithoutPatern("\n") + "\n");
84  if(typeCode == PTypeCode::PTEX_COMMENT){ //Flush latex comment if there was one
85  if(keepTex){fs << ptexComment << std::endl;}
86  ptexComment = "";
87  }
88  typeCode = PTypeCode::CODE;
89  if(keepComment){
90  code += fullComment;
91  }
92  }else{ //This is simple code
93  if(typeCode == PTypeCode::PTEX_COMMENT){ //Flush latex comment if there was one
94  if(keepTex){fs << ptexComment << std::endl;}
95  ptexComment = "";
96  }
97  typeCode = PTypeCode::CODE;
98  code += parser.getCurrentCh();
99  parser.getNextChar();
100  }
101  }
102  //Check is there is still somme code, standardComment or ptexComment to be saved
103  flushCode(fs, typeCode, code, isPlatexMode, envName, keepTex);
104  if(ptexComment != "" && keepTex){
105  fs << ptexComment << std::endl;
106  }
107 }
108 
109 
111 
123 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  PString ptexLineCommentBegin("");
129  PString ptexLineCommenEnd("");
130  if(lineCommentBegin != "" && lineCommenEnd != ""){
131  ptexLineCommentBegin = lineCommentBegin + "{";
132  ptexLineCommenEnd = "}" + lineCommenEnd;
133  }
134  PString ptexMonolineComment("");
135  if(monolineComment != ""){
136  ptexMonolineComment = monolineComment + "{";
137  }
138 
139  std::ofstream fs;
140  if(!openFileStream(fs, outputFile)){return false;}
141 
142  PFileParser parser;
143  if(!parser.open(inputFile)){
144  std::cerr << "createPTexFile : can't parse input file '"<<inputFile<<"'" << std::endl;
145  return false;
146  }
147  parser.setEscapeChar('\0');
148  parser.setWhiteSpace("");
149  if(removefirstcomment){
150  if(parser.isMatch(lineCommentBegin)){
151  parser.getUntilKeyWithoutPatern(lineCommenEnd);
152  }
153  }
154  splitInputFile(fs, parser, keepComment, keepTex, isPlatexMode, envName, lineCommentBegin, lineCommenEnd, monolineComment,
155  ptexLineCommentBegin, ptexLineCommenEnd, ptexMonolineComment);
156  fs.close();
157  return true;
158 }
159 
161 
165 PPath getSplitOutputFileName(const PPath & outputFile, bool isPlatexMode){
166  PString nameOutputFile(outputFile);
167  PString ext(outputFile.getExtension());
168  if(isPlatexMode && ext != "ptex"){
169  nameOutputFile += ".ptex";
170  }
171  return nameOutputFile;
172 }
173 
175 
183 bool processFile(const PPath & outputFile, const PPath & inputFile,
184  bool keepComment, bool keepTex, bool removefirstcomment, bool isPlatexMode)
185 {
186  if(outputFile == "" || inputFile == ""){
187  std::cerr << "processFile : missing input or output file" << std::endl;
188  return false;
189  }
190  PString nameOutputFile(getSplitOutputFileName(outputFile, isPlatexMode));
191  PString ext(inputFile.getExtension());
192  PString baseFileName(inputFile.getFileName().eraseExtension());
193  if(ext == "c" || ext == "cpp" || ext == "c++" || ext == "h" || ext == "hpp"){
194  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "cpp", "/*", "*/", "//");
195  }else if(ext == "cu"){
196  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "cuda", "/*", "*/", "//");
197  }else if(ext == "cmake" || baseFileName == "CMakeLists"){
198  return createPTexFile(outputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "cmake", "#[[", "]]", "#");
199  }else if(ext == "py"){
200  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "python", "'''", "'''", "#");
201  }else if(ext == "sh"){
202  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "bash", ": '", "'", "#");
203  }else if(baseFileName == "Dockerfile"){
204  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "dockerfile", "", "", "#");
205  }else if(baseFileName == "Singularity" || baseFileName == "Apptainer"){
206  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "apptainer", "", "", "#");
207  }else if(ext == "submit"){
208  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "condor", "", "", "#");
209  }else if(ext == "yml"){
210  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "yml", "", "", "#");
211  } if(ext == "toml"){
212  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "toml", "", "", "#");
213  }else if(ext == "md"){
214  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "markdown", "<!--", "-->", "");
215  }else if(ext == "gnuplot"){
216  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "gnuplot", "", "", "#");
217  }else if(ext == "rs"){
218  return createPTexFile(nameOutputFile, inputFile, keepComment, keepTex, removefirstcomment, isPlatexMode, "rust", "/*", "*/", "//");
219  }else{
220  std::cout << "Copy file '"<<inputFile<<"' into '"<<nameOutputFile<<"'" << std::endl;
221  std::ifstream ifs(inputFile.c_str());
222  std::ofstream fs(nameOutputFile.c_str());
223  fs << ifs.rdbuf();
224  fs.close();
225  return true;
226  }
227 }
228 
229 
230 
classe qui permet de parser des fichiers texte en renvoyant les tokens les uns après les autres
Definition: PFileParser.h:20
bool open(const PPath &fileName)
Fonction qui ouvre le fichier que l'on va parser.
Definition: PFileParser.cpp:24
PString getUntilKeyWithoutPatern(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern exclu.
void setEscapeChar(char escapeChar)
Sets the escape character of the PFileParser.
Definition: PFileParser.cpp:58
void setWhiteSpace(const PString &whiteSpace)
Initialise la liste des caractères blancs.
Definition: PFileParser.cpp:35
bool isMatch(const PString &patern)
Says if the patern match with the current caracters of the PFileParser.
char getCurrentCh() const
Renvoie le caractère courant.
bool isEndOfFile() const
Dit si on est à la fin du fichier.
Definition: PFileParser.cpp:88
char getNextChar()
Fonction qui renvoie le prochain caractère du fichier courant.
Path of a directory or a file.
Definition: PPath.h:17
PPath & eraseExtension()
Erase the extension of the PPath.
Definition: PPath.cpp:292
PString getExtension() const
Get file extension.
Definition: PPath.cpp:252
PPath getFileName() const
Get the name of the file, from last char to /.
Definition: PPath.cpp:172
Extends the std::string.
Definition: PString.h:16
PString eraseFirstLastChar(const PString &vecChar) const
Erase first and last char in a string.
Definition: PString.cpp:545
PString eraseFirstChar(const PString &vecChar) const
Erase first char in a string.
Definition: PString.cpp:502
PTypeCode
Get the type of the parsed text.
bool openFileStream(std::ofstream &fs, const PPath &fileName)
Open a ofstream and says if there is a problem.
void flushCode(std::ofstream &fs, PTypeCode::PTypeCode typeCode, PString &code, bool isPlatexMode, const PString &envName, bool keepTex)
Save the code into the output file.
bool processFile(const PPath &outputFile, const PPath &inputFile, bool keepComment, bool keepTex, bool removefirstcomment, bool isPlatexMode)
Process the input file.
bool createPTexFile(const PPath &outputFile, const PPath &inputFile, bool keepComment, bool keepTex, bool removefirstcomment, bool isPlatexMode, const PString &envName, const PString &lineCommentBegin, const PString &lineCommenEnd, const PString &monolineComment)
Create the PTex file.
PPath getSplitOutputFileName(const PPath &outputFile, bool isPlatexMode)
Get the output file name.
void splitInputFile(std::ofstream &fs, PFileParser &parser, bool keepComment, bool keepTex, bool isPlatexMode, const PString &envName, const PString &lineCommentBegin, const PString &lineCommenEnd, const PString &monolineComment, const PString &ptexLineCommentBegin, const PString &ptexLineCommenEnd, const PString &ptexMonolineComment)
Split the input file into the output file.