PhoenixLecture  2.0.0
Set of tools to make lectures
main.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 <sys/stat.h>
8 #include <sys/types.h>
9 
10 #include "OptionParser.h"
11 #include "psrc_split_lib.h"
12 
13 
21  bool keepComment{false};
23  bool keepTex{false};
25  bool removefirstcomment{false};
27  bool isPlatexMode{false};
30 };
31 
32 bool processFileOrDir(const PPath & outputDir, const PPath & inputFile, const SplitArgument & option);
33 
35 
38  OptionParser parser(true, __PROGRAM_VERSION__);
39  parser.setExampleLongOption("phoenix_srcsplittexdir --input=fileInput.cpp --output=output.ptex --platex");
40  parser.setExampleShortOption("phoenix_srcsplittexdir -i fileInput.cpp -o output.ptex -p");
41 
42  parser.addOption("input", "i", OptionType::FILENAME, true, "name of the input file (source, c, cpp, cmake, py, cu)");
43 
44  PPath defaultOutputDir("./");
45  parser.addOption("output", "o", defaultOutputDir, "output directory for generated files and directories");
46 
47  parser.addOption("keepcomment", "k", OptionType::NONE, false, "keep the comment of the targeted language");
48  parser.addOption("keeptex", "t", OptionType::NONE, false, "keep the ptex comment in the output file");
49  parser.addOption("removefirstcomment", "f", OptionType::NONE, false, "remove the first comment of the file (can be the Licence or whatever)");
50  parser.addOption("platex", "p", OptionType::NONE, false, "activate platex mode (add begin and end around code part)");
51 
52  parser.addOption("ignoredir", "d", OptionType::FILENAME, false, "List of directories to be ignored");
53  return parser;
54 }
55 
57 
63 bool createDirectoryWithFiles(const PPath & outputDir, const PPath & directoryName, const PVecPath & listInputFile, const SplitArgument & option)
64 {
65  bool b(true);
66  PPath currentOutputDir(outputDir/ directoryName.getFileName());
67  if(!currentOutputDir.createDirectory()){
68  std::cerr << "createDirectoryWithFiles : Can't create directory '" << currentOutputDir << "'" << std::endl;
69  return false;
70  }
71  for(PVecPath::const_iterator it(listInputFile.begin()); it != listInputFile.end(); ++it){
72  b &= processFileOrDir(currentOutputDir, *it, option);
73  }
74  return b;
75 }
76 
78 
82 PVecPath pathRemoveIfIgnored(const PVecPath & vecPath, const PVecPath & listIgnoreDir){
83  PVecPath vecOut;
84  for(PVecPath::const_iterator itPath(vecPath.begin()); itPath != vecPath.end(); ++itPath){
85  bool isFound = false;
86  PVecPath::const_iterator it(listIgnoreDir.begin());
87  while(it != listIgnoreDir.end() && !isFound){
88  isFound = itPath->getFileName() == *it;
89  ++it;
90  }
91  if(!isFound){
92  vecOut.push_back(*itPath);
93  }
94  }
95  return vecOut;
96 }
97 
99 
104 bool processFileOrDir(const PPath & outputDir, const PPath & inputFile, const SplitArgument & option){
105  bool isPlatexMode = option.isPlatexMode;
106  std::cout << "createFileFunctionGenerator : begin '" << inputFile <<"'" << std::endl;
107  PPath fileName(inputFile.getFileName());
108  //Get the directory content :
109  PVecPath listFileInDir = inputFile.getAllElementInDir();
110  if(listFileInDir.size() != 0lu){
111  std::cout << "processFileOrDir : get list file in directory '"<<inputFile<<"' :" << std::endl;
112  //Let's remove ignore directories
113  const PVecPath & listIgnoreDir = option.listIgnoreDir;
114  PVecPath vecKeepDir = pathRemoveIfIgnored(listFileInDir, listIgnoreDir);
115 
116  for(PVecPath::iterator it(vecKeepDir.begin()); it != vecKeepDir.end(); ++it){
117  *it = inputFile / (*it);
118  }
119  std::cout << "Number of files = " << vecKeepDir.size() << std::endl;
120 // phoenix_print(vecKeepDir);
121  return createDirectoryWithFiles(outputDir, inputFile, vecKeepDir, option);
122  }
123  std::cout << "processFileOrDir : create file '" << fileName << "'" << std::endl;
124  PPath outputFile(getSplitOutputFileName(outputDir / inputFile.getFileName(), isPlatexMode));
125  //Get the permissions on the input file
126 
127  int fullMode(0);
128  bool hasMode(true);
129  if(!isPlatexMode){
130  struct stat fileStat;
131  if(stat(inputFile.c_str(), &fileStat) < 0){
132  hasMode = false;
133  std::cerr << "processFileOrDir : can't get permission of file '"<<inputFile<<"'" << std::endl;
134  }else{
135  fullMode = fileStat.st_mode;
136  }
137  }
138  bool b(processFile(outputFile, inputFile, option.keepComment, option.keepTex, option.removefirstcomment, isPlatexMode));
139  //Modify the permissions on the output file
140  if(hasMode && !isPlatexMode){
141  if(!outputFile.changeMode(fullMode)){
142  std::cerr << "processFileOrDir : Cannot set mode of file '"<<outputFile<<"'" << std::endl;
143  }
144  }
145  return b;
146 }
147 
149 
152 bool processListFile(const SplitArgument & option){
153  const PVecPath & listInputFile = option.listInputFile;
154  if(listInputFile.size() == 0lu){return true;}
155  bool b(true);
156  for(PVecPath::const_iterator it(listInputFile.begin()); it != listInputFile.end(); ++it){
157  b &= processFileOrDir(option.outputDir, *it, option);
158  }
159  return b;
160 }
161 
162 
163 int main(int argc, char** argv){
164  OptionParser parser = createOptionParser();
165  parser.parseArgument(argc, argv);
166 
167  SplitArgument option;
168 
169  const OptionMode & defaultMode = parser.getDefaultMode();
170  defaultMode.getValue(option.listInputFile, "input");
171 
172  defaultMode.getValue(option.outputDir, "output");
173 
174  option.keepComment = defaultMode.isOptionExist("keepcomment");
175  option.keepTex = defaultMode.isOptionExist("keeptex");
176  option.removefirstcomment = defaultMode.isOptionExist("removefirstcomment");
177  option.isPlatexMode = defaultMode.isOptionExist("platex");
178  defaultMode.getValue(option.listIgnoreDir, "ignoredir");
179  bool b(processListFile(option));
180  return b - 1;
181 }
182 
183 
184 
185 
std::vector< PPath > PVecPath
Definition: PPath.h:75
int main(int argc, char **argv)
Definition: main.cpp:228
OptionParser createOptionParser()
Create the OptionParser of this program.
Definition: main.cpp:17
PVecPath pathRemoveIfIgnored(const PVecPath &vecPath, const PVecPath &listIgnoreDir)
Remove Path if they are ignored.
Definition: main.cpp:82
bool createDirectoryWithFiles(const PPath &outputDir, const PPath &directoryName, const PVecPath &listInputFile, const SplitArgument &option)
Process a list of files or directories and create the output directory.
Definition: main.cpp:63
bool processListFile(const SplitArgument &option)
Process a list of files or directories.
Definition: main.cpp:152
bool processFileOrDir(const PPath &outputDir, const PPath &inputFile, const SplitArgument &option)
Process a file or a directory.
Definition: main.cpp:104
bool processFile(const PPath &inputFile, const PString &command, size_t firstToken, size_t lastToken, size_t incrementToken, const PString &delimitor, bool isNoFail)
Process the file.
Definition: main.cpp:49
Describe a mode in the program arguments.
Definition: OptionMode.h:13
bool isOptionExist(const PString &optionName) const
Say if the given option has been passed to the program.
Definition: OptionMode.cpp:210
bool getValue(T &value, const PString &optionName) const
Get the value of the option.
Parse the options passed to a program.
Definition: OptionParser.h:15
void parseArgument(int argc, char **argv)
Parse the arguments passed to the program.
void addOption(const PString &longOption, const PString &shortOption, OptionType::OptionType optionType, bool isRequired, const PString &docString)
Add an option in the OptionParser.
void setExampleShortOption(const PString &example)
Set the example usage of the program.
void setExampleLongOption(const PString &example)
Set the example usage of the program.
const OptionMode & getDefaultMode() const
Get default mode.
Path of a directory or a file.
Definition: PPath.h:17
bool changeMode(mode_t __mode=S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) const
Change the mode of a file or directory.
Definition: PPath.cpp:526
bool createDirectory(mode_t mode=0755) const
Create the current directory.
Definition: PPath.cpp:331
std::vector< PPath > getAllElementInDir() const
Get the list of all elements in a directory.
Definition: PPath.cpp:503
PPath getFileName() const
Get the name of the file, from last char to /.
Definition: PPath.cpp:172
PPath getSplitOutputFileName(const PPath &outputFile, bool isPlatexMode)
Get the output file name.
arguments of the phoenix_srcsplittexdir program
Definition: main.cpp:15
PVecPath listIgnoreDir
Directory to be ignored.
Definition: main.cpp:29
bool keepComment
true to keep the comments of the targeted language in the output file
Definition: main.cpp:21
PPath outputDir
name of the output directory
Definition: main.cpp:17
bool isPlatexMode
true to add begin and end environnement in the output file, false otherwise
Definition: main.cpp:27
bool removefirstcomment
remove the first comment of the file
Definition: main.cpp:25
bool keepTex
true to keep the ptex comment, false otherwise
Definition: main.cpp:23
PVecPath listInputFile
list of the input files or directories
Definition: main.cpp:19