PhoenixLecture  2.0.0
Set of tools to make lectures
main.cpp
Go to the documentation of this file.
1 
2 /***************************************
3  Auteur : Pierre Aubert
4  Mail : pierre.aubert@lapp.in2p3.fr
5  Licence : CeCILL-C
6 ****************************************/
7 
8 #include <stdlib.h>
9 #include <fstream>
10 #include <iostream>
11 
12 #include "OptionParser.h"
13 #include "PFileParser.h"
14 
16 
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 }
38 
40 
49 bool processFile(const PPath & inputFile, const PString & command,
50  size_t firstToken, size_t lastToken, size_t incrementToken, const PString & delimitor, bool isNoFail)
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 }
98 
99 
101 
110 bool processVecFile(const PVecPath & vecInputFile, const PString & command,
111  size_t firstToken, size_t lastToken, size_t incrementToken, const PString & delimitor, bool isNoFail)
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 }
119 
120 
121 int main(int argc, char** argv){
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 }
147 
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
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
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.
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.
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
bool saveFileContent(const PString &content) const
Save a PString in a file.
Definition: PPath.cpp:395
PString getExtension() const
Get file extension.
Definition: PPath.cpp:252
Extends the std::string.
Definition: PString.h:16
PString replace(const PString &pattern, const PString &replaceStr) const
Replace a PString into an other PString.
Definition: PString.cpp:204