All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Sim2DocBuddy.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 
34 /**
35  @file
36 
37  @author Jean-Christophe FABRE <fabrejc@supagro.inra.fr>
38  */
39 
40 
41 #ifndef __SIM2DOCBUDDY_H___
42 #define __SIM2DOCBUDDY_H___
43 
44 
45 #ifndef __APPLE__
46 
47 // Disabled for compilation errors due to boost.spirit usage under MacOSX
48 // TODO Should be re-enabled later
49 
50 
51 #include <boost/filesystem/path.hpp>
52 #include <map>
53 #include <vector>
54 
55 #include <openfluid/dllexport.hpp>
58 
59 #include <boost/spirit/include/classic_core.hpp>
60 #include <boost/spirit/include/classic_file_iterator.hpp>
61 #include <boost/bind.hpp>
62 
64 
65 using namespace BOOST_SPIRIT_CLASSIC_NS;
66 
67 namespace openfluid { namespace buddies {
68 
69 class BuddiesListener;
70 
71 
73 {
74  private:
75  typedef std::map< std::string,std::vector<std::string> > SignatureData_t;
76 
77  std::string m_CPPFile;
78  std::string m_LatexOutFile;
79 
80  boost::filesystem::path m_InputDirPath;
81  boost::filesystem::path m_OutputDirPath;
82  boost::filesystem::path m_InputFilePath;
83  boost::filesystem::path m_OutputLatexFilePath;
84  boost::filesystem::path m_TplFilePath;
85  boost::filesystem::path m_CProcessedFilePath;
86 
87  openfluid::tools::ExternalProgram m_PDFLatexProgram;
88  openfluid::tools::ExternalProgram m_BibtexProgram;
89  openfluid::tools::ExternalProgram m_Latex2HTMLProgram;
91 
92  std::string m_ExtractedLatexDoc;
93 
94  std::string m_Title;
95  std::string m_NewCommands;
96  std::string m_SimID;
97  std::string m_SimName;
98  std::string m_SimVersion;
99  std::string m_SimStatus;
100  std::vector<std::string> m_SimAuthorsNames;
101  std::vector<std::string> m_SimAuthorsEmails;
102  std::string m_SimDomain;
103  std::string m_SimDescription;
104  std::string m_SimData;
105 
106  std::string m_BeginSignatureTag;
107  std::string m_EndSignatureTag;
108  std::string m_BeginSim2DocTag;
109  std::string m_EndSim2DocTag;
110 
111  std::string m_CurrentKeyValue;
112  std::string m_CurrentBuiltParam;
113 
114  std::string m_HTMLPackageLatexCommand;
115 
116  SignatureData_t m_InVars;
117  SignatureData_t m_OutVars;
118  SignatureData_t m_ModVars;
119  SignatureData_t m_InAttrs;
120  SignatureData_t m_OutAttrs;
121  SignatureData_t m_ParamsData;
122  SignatureData_t m_Events;
123  SignatureData_t m_ExtraFiles;
124 
127 
128  std::string extractBetweenTags(std::string Content, const std::string BeginTag, const std::string EndTag);
129 
130  std::string toLatexFriendly(std::string Content);
131 
132  void addLatexDataCatBegin(std::string& Content, const std::string Title, const std::string ColsFormat);
133 
134  void addLatexDataCatEnd(std::string& Content);
135 
136  std::string extractSignatureLines();
137 
138  std::vector<std::string> searchStringLitterals(std::string StrToParse);
139 
140  void copyDocDirectory();
141 
142  void extractLatexDocFromCPP();
143 
144  void cpreprocessCPP();
145 
146  /** Methods called by simulator signature parser **/
147 
148  /**
149  Builds a signature parameter with parsed elements
150  @param[in] First, Last Pointers to the beginning and the end of the parsed element
151  */
152  void buildParsedParam(const char* First, const char* Last);
153 
154  /**
155  Clears the string containing the parameter built with parsing
156  */
157  void clearParsedParam();
158 
159  /**
160  Stores data parsed in the string parameter
161  @param[out] Str Pointer to the string receiving the value
162  */
163  void storeDataIntoString(std::string *Str);
164 
165  /**
166  Adds data parsed in the list parameter
167  @param[out] List Pointer to the vector receiving the value
168  */
169  void storeDataIntoVector(std::vector<std::string> *List);
170 
171  /**
172  Stores the new key value parsed into SignatureData parameter
173  @param[out] SignatureData Pointer to the map receiving the key value
174  @param[in] State of the attribute or variable stored into the map
175  (required, used, produced, updated)
176  */
177  void storeDataIntoKey(SignatureData_t *SignatureData, const std::string& State);
178 
179  /**
180  Adds data parsed in the SignatureData attribute received as parameter
181  @param[out] SignatureData Pointer to the map receiving the value
182  */
183  void storeDataIntoSignatureData(SignatureData_t *SignatureData);
184 
185  /**
186  Checks data parsed and stores it into status attribute
187  */
188  void storeDataIntoStatus();
189 
190  /**
191  Stores data parsed in a new UnitsClass item
192  @param[out] UpdatedUnitsClass Vector of Units class
193  @param[in] Attr Attribute of the new UnitsClass receiving the value
194  */
195  void storeDataIntoUnitsClass(std::vector<openfluid::ware::SignatureHandledUnitsClassItem> *UpdatedUnitsClass,
196  int Attr);
197 
198  /**
199  Sets fixed scheduling of TimeScheduling attribute
200  @param[in] Val Value of scheduling
201  */
202  void setSchedulingFixed(double Val);
203 
204  /**
205  Sets range scheduling of TimeScheduling attribute
206  @param[in] Max Maximum value of the range
207  */
208  void setSchedulingRange(double Max);
209 
210  /** ********************************** **/
211 
212  /**
213  Applies latex syntax to the attributes
214  */
215  void turnIntoLatexSyntax();
216 
217  void processSignature();
218 
219  void generateLatex();
220 
221  bool isErrorInPDFLatexLog();
222 
223  void buildPDF();
224 
225  void buildHTML();
226 
227  public:
228 
230 
231  ~Sim2DocBuddy();
232 
233  bool run();
234 
235 
236  /**
237  * Grammar class used to parse simulator signature
238  */
239  struct SimSignatureGrammar : public grammar<SimSignatureGrammar>
240  {
241  Sim2DocBuddy *mp_Owner; // Object of the class containing methods to be called
242  SimSignatureGrammar(Sim2DocBuddy *Owner) : mp_Owner(Owner) {}
243 
244 
245  template <typename ScannerT>
246  struct definition
247  {
248  /** List of parsing rules **/
249  rule<ScannerT> blank, linemarker, endLine, escapedQuote, string, varName, element, parameters, signature,
250  IDRule, NameRule, DescriptionRule, VersionRule, StatusRule, DomainRule, AuthorRule,
251  SimulatorParamRule, ProducedVarRule, UpdatedVarRule, RequiredVarRule, UsedVarRule,
252  ProducedAttributeRule, RequiredAttributeRule, UsedAttributeRule,
253  UsedEventsRule, UsedExtraFilesRule, RequiredExtraFilesRule,
254  UpdatedUnitsGraphRule, UpdatedUnitsClassRule;
255 
256  /**
257  * Defines the different rules of content to parse or ignore
258  *
259  * boost::bind is used to execute a method after parsing of an element or a rule.
260  * Its parameters are the method to call, the object with which the method is executed
261  * and the different parameters of this method (_1 and _2 are used to send the parsed element).
262  *
263  * The different parsers which can be used :
264  * http://www.boost.org/doc/libs/1_39_0/libs/spirit/classic/doc/quickref.html
265  */
267  {
268  /** Initial rule **/
269 
270  signature = str_p("BEGIN_SIMULATOR_SIGNATURE") >> *blank >> '(' >> IDRule >> ')' >> endLine
271  >> *(str_p("DECLARE_NAME") >> *blank >> '(' >> NameRule >> ')' >> endLine
272  | str_p("DECLARE_DESCRIPTION") >> *blank >> '(' >> DescriptionRule >> ')' >> endLine
273  | str_p("DECLARE_VERSION") >> *blank >> '(' >> VersionRule >> ')' >> endLine
274  | str_p("DECLARE_SDKVERSION") >> endLine
275  | str_p("DECLARE_STATUS") >> *blank >> '(' >> StatusRule >> ')' >> endLine
276  | str_p("DECLARE_DOMAIN") >> *blank >> '(' >> DomainRule >> ')' >> endLine
277  | str_p("DECLARE_PROCESS") >> *blank >> '(' >> parameters >> ')' >> endLine
278  | str_p("DECLARE_METHOD") >> *blank >> '(' >> parameters >> ')' >> endLine
279  | str_p("DECLARE_AUTHOR") >> *blank >> '(' >> AuthorRule >> ')' >> endLine
280 
281  | str_p("DECLARE_SIMULATOR_PARAM") >> *blank >> '(' >> SimulatorParamRule >> ')' >> endLine
282 
283  | str_p("DECLARE_PRODUCED_VAR") >> *blank >> '(' >> ProducedVarRule >> ')' >> endLine
284  | str_p("DECLARE_UPDATED_VAR") >> *blank >> '(' >> UpdatedVarRule >> ')' >> endLine
285  | str_p("DECLARE_REQUIRED_VAR") >> *blank >> '(' >> RequiredVarRule >> ')' >> endLine
286  | str_p("DECLARE_USED_VAR") >> *blank >> '(' >> UsedVarRule >> ')' >> endLine
287 
288  | str_p("DECLARE_PRODUCED_ATTRIBUTE") >> *blank >> '(' >> ProducedAttributeRule >> ')' >> endLine
289  | str_p("DECLARE_REQUIRED_ATTRIBUTE") >> *blank >> '(' >> RequiredAttributeRule >> ')' >> endLine
290  | str_p("DECLARE_USED_ATTRIBUTE") >> *blank >> '(' >> UsedAttributeRule >> ')' >> endLine
291 
292  | str_p("DECLARE_USED_EVENTS") >> *blank >> '(' >> UsedEventsRule >> ')' >> endLine
293  | str_p("DECLARE_USED_EXTRAFILE") >> *blank >> '(' >> UsedExtraFilesRule >> ')' >> endLine
294  | str_p("DECLARE_REQUIRED_EXTRAFILE") >> *blank >> '(' >> RequiredExtraFilesRule >> ')' >> endLine
295 
296  | str_p("DECLARE_UPDATED_UNITSGRAPH") >> *blank >> '(' >> UpdatedUnitsGraphRule >> ')' >> endLine
297  | str_p("DECLARE_UPDATED_UNITSCLASS") >> *blank >> '(' >> UpdatedUnitsClassRule >> ')' >> endLine
298 
299  | str_p("DECLARE_SCHEDULING_UNDEFINED")[boost::bind(&ware::SignatureTimeScheduling::setAsUndefined,
300  &self.mp_Owner->m_TimeScheduling)] >> endLine
301  | str_p("DECLARE_SCHEDULING_DEFAULT")[boost::bind(&ware::SignatureTimeScheduling::setAsDefaultDeltaT,
302  &self.mp_Owner->m_TimeScheduling)] >> endLine
303  | str_p("DECLARE_SCHEDULING_FIXED") >> *blank >> '(' >> *blank >> real_p[boost::bind(&Sim2DocBuddy::setSchedulingFixed, self.mp_Owner, _1)]
304  >> *blank >> ')' >> endLine
305  | str_p("DECLARE_SCHEDULING_RANGE") >> *blank >> '(' >> *blank >> real_p[boost::bind(&Sim2DocBuddy::setSchedulingFixed, self.mp_Owner, _1)]
306  >> *blank >> ',' >> *blank >> real_p[boost::bind(&Sim2DocBuddy::setSchedulingRange, self.mp_Owner, _1)] >> *blank >> ')' >> endLine
307 
308  | linemarker >> endLine
309  )
310  >> str_p("END_SIMULATOR_SIGNATURE") >> endLine
311  ;
312 
313  /** List of common rules **/
314 
315 
316  /* Ignoring of inserted lines by the preprocessor which mean
317  * the original position of lines in source file.
318  */
319  linemarker = '#' >> *(anychar_p - eol_p) >> eol_p;
320 
321  blank = blank_p | eol_p; // Parse all spaces, tabs and newlines
322  endLine = *(blank | ';');
323  escapedQuote = str_p("\\\"");
324 
325  string = *(escapedQuote | (anychar_p - '"')); // String content composed to all characters
326 
327  varName = *(print_p - ')');
328 
329  // Parameter of a method surrounded by quotes (string) or not (var name)
330  element = (+(*blank >> '"' >> string[boost::bind(&Sim2DocBuddy::buildParsedParam, self.mp_Owner, _1, _2)] >> '"' >> *blank))
331  | (*blank >> varName[boost::bind(&Sim2DocBuddy::buildParsedParam, self.mp_Owner, _1, _2)] >> *blank);
332 
333  // List of parameters of a declaration (here not stored)
334  parameters = element[boost::bind(&Sim2DocBuddy::clearParsedParam, self.mp_Owner)]
335  >> *(',' >> element[boost::bind(&Sim2DocBuddy::clearParsedParam, self.mp_Owner)]);
336 
337 
338 
339  /** List of rules for the different lines of signature **/
340 
341  IDRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoString, self.mp_Owner, &self.mp_Owner->m_SimID)];
342  NameRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoString, self.mp_Owner, &self.mp_Owner->m_SimName)];
343  DescriptionRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoString, self.mp_Owner, &self.mp_Owner->m_SimDescription)];
344  VersionRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoString, self.mp_Owner, &self.mp_Owner->m_SimVersion)];
345 
346  StatusRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoStatus, self.mp_Owner)];
347 
348  DomainRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoString, self.mp_Owner, &self.mp_Owner->m_SimDomain)];
349  AuthorRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoVector, self.mp_Owner, &self.mp_Owner->m_SimAuthorsNames)]
350  >> ',' >> element[boost::bind(&Sim2DocBuddy::storeDataIntoVector, self.mp_Owner, &self.mp_Owner->m_SimAuthorsEmails)];
351 
352  SimulatorParamRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_ParamsData, "")]
353  >> *(',' >> element[boost::bind(&Sim2DocBuddy::storeDataIntoSignatureData, self.mp_Owner, &self.mp_Owner->m_ParamsData)]);
354 
355  ProducedVarRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_OutVars, "produced")]
356  >> *(',' >> element[boost::bind(&Sim2DocBuddy::storeDataIntoSignatureData, self.mp_Owner, &self.mp_Owner->m_OutVars)]);
357  UpdatedVarRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_OutVars, "updated")]
358  >> *(',' >> element[boost::bind(&Sim2DocBuddy::storeDataIntoSignatureData, self.mp_Owner, &self.mp_Owner->m_OutVars)]);
359  RequiredVarRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_InVars, "required")]
360  >> *(',' >> element[boost::bind(&Sim2DocBuddy::storeDataIntoSignatureData, self.mp_Owner, &self.mp_Owner->m_InVars)]);
361  UsedVarRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_InVars, "used")]
362  >> *(',' >> element[boost::bind(&Sim2DocBuddy::storeDataIntoSignatureData, self.mp_Owner, &self.mp_Owner->m_InVars)]);
363 
364  ProducedAttributeRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_OutAttrs, "produced")]
365  >> *(',' >> element[boost::bind(&Sim2DocBuddy::storeDataIntoSignatureData, self.mp_Owner, &self.mp_Owner->m_OutAttrs)]);
366  RequiredAttributeRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_InAttrs, "required")]
367  >> *(',' >> element[boost::bind(&Sim2DocBuddy::storeDataIntoSignatureData, self.mp_Owner, &self.mp_Owner->m_InAttrs)]);
368  UsedAttributeRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_InAttrs, "used")]
369  >> *(',' >> element[boost::bind(&Sim2DocBuddy::storeDataIntoSignatureData, self.mp_Owner, &self.mp_Owner->m_InAttrs)]);
370 
371  UsedEventsRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_Events, "")];
372  UsedExtraFilesRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_ExtraFiles, "used")];
373  RequiredExtraFilesRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoKey, self.mp_Owner, &self.mp_Owner->m_ExtraFiles, "required")];
374 
375  UpdatedUnitsGraphRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoString, self.mp_Owner, &self.mp_Owner->m_UnitsGraph.UpdatedUnitsGraph)];
376  UpdatedUnitsClassRule = element[boost::bind(&Sim2DocBuddy::storeDataIntoUnitsClass, self.mp_Owner, &self.mp_Owner->m_UnitsGraph.UpdatedUnitsClass, 1)]
377  >> ',' >> element[boost::bind(&Sim2DocBuddy::storeDataIntoUnitsClass, self.mp_Owner, &self.mp_Owner->m_UnitsGraph.UpdatedUnitsClass, 2)];
378  }
379 
380  /**
381  * @return Main rule of parsing content
382  */
383  rule<ScannerT> const& start() const { return signature; }
384  };
385  };
386 
387 };
388 
389 
390 } } //namespaces
391 
392 #endif /* __APPLE__ */
393 
394 #endif /* __SIM2DOCBUDDY_HPP__ */
Definition: SimulatorSignature.hpp:384
Sim2DocBuddy * mp_Owner
Definition: Sim2DocBuddy.hpp:241
Definition: OpenFLUIDBuddy.hpp:54
SimSignatureGrammar(Sim2DocBuddy *Owner)
Definition: Sim2DocBuddy.hpp:242
Definition: SimulatorSignature.hpp:369
definition(SimSignatureGrammar const &self)
Definition: Sim2DocBuddy.hpp:266
rule< ScannerT > VersionRule
Definition: Sim2DocBuddy.hpp:249
Definition: ExternalProgram.hpp:54
Definition: BuddiesListener.hpp:55
Header of ...
Definition: Sim2DocBuddy.hpp:72
rule< ScannerT > const & start() const
Definition: Sim2DocBuddy.hpp:383
#define DLLEXPORT
Definition: dllexport.hpp:51