PhoenixLecture  2.0.0
Set of tools to make lectures
main.cpp File Reference
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include "OptionParser.h"
#include "PFileParser.h"
+ Include dependency graph for main.cpp:

Go to the source code of this file.

Functions

OptionParser createOptionParser ()
 Create the OptionParser of this program. More...
 
int main (int argc, char **argv)
 
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. More...
 
bool processVecFile (const PVecPath &vecInputFile, const PString &command, size_t firstToken, size_t lastToken, size_t incrementToken, const PString &delimitor, bool isNoFail)
 Process the program. More...
 

Function Documentation

◆ createOptionParser()

OptionParser createOptionParser ( )

Create the OptionParser of this program.

Returns
OptionParser of this program

Definition at line 18 of file main.cpp.

18  {
19  OptionParser parser(true, __PROGRAM_VERSION__);
20  parser.setExampleLongOption("phoenix_dripfile --input=fileInput.txt --command=\"some_program FILENAME\"");
21  parser.setExampleShortOption("phoenix_dripfile -i fileInput1.txt -c \"some_program FILENAME\"");
22 
23  parser.addOption("input", "i", OptionType::FILENAME, true, "list of input files");
24  parser.addOption("command", "c", OptionType::STRING, true, "Command to be executed by the program. The token 'FILENAME' specifies where the generated file has to be passed in the command");
25 
26  size_t firstToken(0lu), lastToken(-1lu), incrementToken(1lu);
27  parser.addOption("firsttoken", "f", firstToken, "Index of the first token to be tested");
28  parser.addOption("lasttoken", "l", lastToken, "Index of the last token to be tested");
29  parser.addOption("incrementtoken", "k", incrementToken, "Increment of the index to be used");
30 
31  PString delimitor("+-*/!:~&|<>.()[]{}\"'%");
32  parser.addOption("delimitor", "d", delimitor, "All delimitor characters to be used to tokenise the input file");
33 
34  parser.addOption("nofail", "x", OptionType::NONE, false, "Does not take account if the tested program failed or not. Always retruns 0");
35 
36  return parser;
37 }
Parse the options passed to a program.
Definition: OptionParser.h:15
Extends the std::string.
Definition: PString.h:16

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

+ Here is the call graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 121 of file main.cpp.

121  {
122  OptionParser parser = createOptionParser();
123  parser.parseArgument(argc, argv);
124 
125  const OptionMode & defaultMode = parser.getDefaultMode();
126  PVecPath vecInputFile;
127  defaultMode.getValue(vecInputFile, "input");
128 
129  PString command("");
130  defaultMode.getValue(command, "command");
131 
132  size_t firstToken(0lu), lastToken(-1lu), incrementToken(1lu);
133  defaultMode.getValue(firstToken, "firsttoken");
134  defaultMode.getValue(lastToken, "lasttoken");
135  defaultMode.getValue(incrementToken, "incrementtoken");
136  if(incrementToken == 0lu){incrementToken = 1lu;} //incrementToken cannot be 0
137 
138  PString delimitor("");
139  defaultMode.getValue(delimitor, "delimitor");
140 
141  bool isNoFail = defaultMode.isOptionExist("nofail");
142 
143  bool b(processVecFile(vecInputFile, command,
144  firstToken, lastToken, incrementToken, delimitor, isNoFail));
145  return b - 1;
146 }
std::vector< PPath > PVecPath
Definition: PPath.h:75
OptionParser createOptionParser()
Create the OptionParser of this program.
Definition: main.cpp:17
bool processVecFile(const PVecPath &vecInputFile, const PString &command, size_t firstToken, size_t lastToken, size_t incrementToken, const PString &delimitor, bool isNoFail)
Process the program.
Definition: main.cpp:110
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.

References createOptionParser(), OptionParser::getDefaultMode(), OptionMode::getValue(), OptionMode::isOptionExist(), OptionParser::parseArgument(), and processVecFile().

+ Here is the call graph for this function:

◆ processFile()

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.

Parameters
inputFile: input file
command: command to be used
firstToken: Index of the first token to be tested
lastToken: Index of the last token to be tested
incrementToken: Increment of the index to be used
delimitor: All delimitor characters to be used to tokenise the input file
isNoFail: true to ignore if the tested program failed or not, false for normal behaviour
Returns
true on success, false otherwise

Definition at line 49 of file main.cpp.

51 {
52  std::cout << "processFile : using file '"<<inputFile<<"'" << std::endl;
53  PString extention(inputFile.getExtension());
54  if(extention != ""){extention = "." + extention;}
55  PPath configFile("temporary_file" + extention);
56  PString updatedCommand(command.replace("FILENAME", configFile));
57  if(command == updatedCommand){
58  std::cerr << "processFile : 'FILENAME' token missing in the command" << std::endl;
59  std::cerr << "\t" << command << std::endl;
60  return false;
61  }
62  std::cout << "Command : '"<<updatedCommand<<"'" << std::endl;
63 
64  PFileParser parser;
65  parser.setSeparator(delimitor);
66  if(!parser.open(inputFile)){return false;}
67 
68  bool b(true);
69  size_t tokenIndex(firstToken);
70  PString body("");
71  for(size_t i(0lu); i < firstToken; ++i){ //Let's create the basic configuration file with the first number of token
72  PString skippedStr("");
73  PString token(parser.getNextToken(skippedStr));
74  body += skippedStr + token;
75  }
76  while(tokenIndex < lastToken && !parser.isEndOfFile()){
77  //Let's create the config file
78  if(!configFile.saveFileContent(body)){
79  std::cerr << "processFile : cannot save file '"<<configFile<<"'" << std::endl;
80  return false;
81  }
82  //Let's call the updatedCommand
83  std::cout << "- token " << tokenIndex << " => ";
84  if(system(updatedCommand.c_str()) != 0 && !isNoFail){
85  std::cout << "FAIL" << std::endl;
86  return false;
87  }
88  std::cout << "OK" << std::endl;
89  for(size_t i(0lu); i < incrementToken; ++i){
90  PString skippedStr("");
91  PString token(parser.getNextToken(skippedStr));
92  body += skippedStr + token;
93  ++tokenIndex;
94  }
95  }
96  return b;
97 }
classe qui permet de parser des fichiers texte en renvoyant les tokens les uns après les autres
Definition: PFileParser.h:20
void setSeparator(const PString &separator)
Initialise la liste des caractères séparateurs.
Definition: PFileParser.cpp:43
bool open(const PPath &fileName)
Fonction qui ouvre le fichier que l'on va parser.
Definition: PFileParser.cpp:24
PString getNextToken()
Get the next token.
bool isEndOfFile() const
Dit si on est à la fin du fichier.
Definition: PFileParser.cpp:88
Path of a directory or a file.
Definition: PPath.h:17
PString getExtension() const
Get file extension.
Definition: PPath.cpp:252
PString replace(const PString &pattern, const PString &replaceStr) const
Replace a PString into an other PString.
Definition: PString.cpp:204

References PPath::getExtension(), PFileParser::getNextToken(), PFileParser::isEndOfFile(), PFileParser::open(), PString::replace(), PPath::saveFileContent(), and PFileParser::setSeparator().

Referenced by main(), processFileOrDir(), and processVecFile().

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

◆ processVecFile()

bool processVecFile ( const PVecPath vecInputFile,
const PString command,
size_t  firstToken,
size_t  lastToken,
size_t  incrementToken,
const PString delimitor,
bool  isNoFail 
)

Process the program.

Parameters
vecInputFile: vector of input files
command: command to be used
firstToken: Index of the first token to be tested
lastToken: Index of the last token to be tested
incrementToken: Increment of the index to be used
delimitor: All delimitor characters to be used to tokenise the input file
isNoFail: true to ignore if the tested program failed or not, false for normal behaviour
Returns
true on success, false otherwise

Definition at line 110 of file main.cpp.

112 {
113  bool b(true);
114  for(PVecPath::const_iterator it(vecInputFile.begin()); it != vecInputFile.end() && b; ++it){
115  b &= processFile(*it, command, firstToken, lastToken, incrementToken, delimitor, isNoFail);
116  }
117  return b;
118 }
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

References processFile().

Referenced by main().

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