PhoenixLecture  2.0.0
Set of tools to make lectures
main.cpp File Reference
#include <sys/stat.h>
#include <sys/types.h>
#include "OptionParser.h"
#include "psrc_split_lib.h"
+ Include dependency graph for main.cpp:

Go to the source code of this file.

Classes

struct  SplitArgument
 arguments of the phoenix_srcsplittexdir program More...
 

Functions

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. More...
 
OptionParser createOptionParser ()
 Create the OptionParser of this program. More...
 
int main (int argc, char **argv)
 
PVecPath pathRemoveIfIgnored (const PVecPath &vecPath, const PVecPath &listIgnoreDir)
 Remove Path if they are ignored. More...
 
bool processFileOrDir (const PPath &outputDir, const PPath &inputFile, const SplitArgument &option)
 Process a file or a directory. More...
 
bool processListFile (const SplitArgument &option)
 Process a list of files or directories. More...
 

Function Documentation

◆ createDirectoryWithFiles()

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.

Parameters
outputDir: name of the output directory
directoryName: name of the directory to be created
listInputFile: list of the input files or directories
option: arguments of the program
Returns
true on success, false otherwise

Definition at line 63 of file main.cpp.

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 }
bool processFileOrDir(const PPath &outputDir, const PPath &inputFile, const SplitArgument &option)
Process a file or a directory.
Definition: main.cpp:104
Path of a directory or a file.
Definition: PPath.h:17
PPath getFileName() const
Get the name of the file, from last char to /.
Definition: PPath.cpp:172

References PPath::createDirectory(), PPath::getFileName(), and processFileOrDir().

Referenced by processFileOrDir().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createOptionParser()

OptionParser createOptionParser ( )

Create the OptionParser of this program.

Returns
OptionParser of this program

Definition at line 37 of file main.cpp.

37  {
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 }
Parse the options passed to a program.
Definition: OptionParser.h:15

References OptionParser::addOption(), OptionType::FILENAME, OptionType::NONE, OptionParser::setExampleLongOption(), and OptionParser::setExampleShortOption().

+ Here is the call graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 163 of file main.cpp.

163  {
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 }
OptionParser createOptionParser()
Create the OptionParser of this program.
Definition: main.cpp:17
bool processListFile(const SplitArgument &option)
Process a list of files or directories.
Definition: main.cpp:152
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.
void parseArgument(int argc, char **argv)
Parse the arguments passed to the program.
const OptionMode & getDefaultMode() const
Get default mode.
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

References createOptionParser(), OptionParser::getDefaultMode(), OptionMode::getValue(), OptionMode::isOptionExist(), SplitArgument::isPlatexMode, SplitArgument::keepComment, SplitArgument::keepTex, SplitArgument::listIgnoreDir, SplitArgument::listInputFile, SplitArgument::outputDir, OptionParser::parseArgument(), processListFile(), and SplitArgument::removefirstcomment.

+ Here is the call graph for this function:

◆ pathRemoveIfIgnored()

PVecPath pathRemoveIfIgnored ( const PVecPath vecPath,
const PVecPath listIgnoreDir 
)

Remove Path if they are ignored.

Parameters
vecPath: vector of Path
listIgnoreDir: vector of ignored directories
Returns
cleared vector of Path

Definition at line 82 of file main.cpp.

82  {
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 }
std::vector< PPath > PVecPath
Definition: PPath.h:75

Referenced by processFileOrDir().

+ Here is the caller graph for this function:

◆ processFileOrDir()

bool processFileOrDir ( const PPath outputDir,
const PPath inputFile,
const SplitArgument option 
)

Process a file or a directory.

Parameters
outputDir: current output directory
inputFile: input file or directory
option: arguments of the program
Returns
true on success, false otherwise

Definition at line 104 of file main.cpp.

104  {
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 }
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 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
std::vector< PPath > getAllElementInDir() const
Get the list of all elements in a directory.
Definition: PPath.cpp:503
PPath getSplitOutputFileName(const PPath &outputFile, bool isPlatexMode)
Get the output file name.

References PPath::changeMode(), createDirectoryWithFiles(), PPath::getAllElementInDir(), PPath::getFileName(), getSplitOutputFileName(), SplitArgument::isPlatexMode, SplitArgument::keepComment, SplitArgument::keepTex, SplitArgument::listIgnoreDir, pathRemoveIfIgnored(), processFile(), and SplitArgument::removefirstcomment.

Referenced by createDirectoryWithFiles(), and processListFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ processListFile()

bool processListFile ( const SplitArgument option)

Process a list of files or directories.

Parameters
option: arguments of the program
Returns
true on success, false otherwise

Definition at line 152 of file main.cpp.

152  {
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 }

References SplitArgument::listInputFile, SplitArgument::outputDir, and processFileOrDir().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: