159 std::string inputDir, outputDir;
162 std::string s = argv[1];
164 if (s[s.size()-1] !=
'/')
176 std::istringstream iss;
177 std::ostringstream oss;
179 std::map<std::string,bool> attributeFlags;
183 std::string attDefFile = inputDir + attDefName +
".txt";
184 std::string attDefHeader = outputDir + attDefName +
".hpp";
193 std::string UpperAttDefName = attDefName ;
194 for (
auto & c: UpperAttDefName) c =
toupper(c);
196 oss <<
"//////////// THIS FILE MUST BE CREATED BY EXECUTING WriteAttributeDefinitionFile ////////////" << std::endl;
197 oss <<
"//////////// DO NOT MODIFY THIS FILE MANUALLY ///////////////////////////////////////////////" << std::endl << std::endl;
199 oss <<
"#ifndef __NOMAD400_"<< UpperAttDefName <<
"__" << std::endl;
200 oss <<
"#define __NOMAD400_"<< UpperAttDefName <<
"__" << std::endl << std::endl;
201 oss <<
"_definition = {" ;
209 errMsg =
" Failed to open file " + attDefFile +
".";
212 fin.open ( attDefFile );
218 if ( !errMsg.empty() )
224 bool firstAttribute =
true;
226 while ( fin.good() && !fin.eof() )
230 if ( firstAttribute )
244 errMsg =
" Failed to read file " + attDefFile +
".";
248 if ( line.size() > 0 && line.find_first_of(
"#")== 0 )
259 if ( ! firstAttribute )
260 oss <<
"," << std::endl;
264 std::string attributeName;
267 iss >> attributeName;
269 if ( attributeName.size()==0)
271 errMsg =
" The first word after a comment must be an attribute name. \n An empty line is not valid.";
274 oss <<
"{ \"" << attributeName <<
"\", ";
280 if ( fin.fail() || line.empty() )
282 errMsg =
"Failed to read file " + attDefFile +
".";
286 std::string attributeType;
290 iss >> attributeType ;
292 if ( attributeType.size()==0 )
294 errMsg =
" The next word after an attribute name must be the attribute type. \n An empty line is not valid.";
297 oss <<
" \"" << attributeType <<
"\", " ;
303 if ( fin.fail() || line.empty() )
305 errMsg =
" Failed to read file " + attDefFile +
".";
309 std::string attributeDefault;
312 iss >> attributeDefault ;
316 if (
"std::string" == attributeType)
318 if (
"\"\"" == attributeDefault ||
"-" == attributeDefault ||
"N/A" == attributeDefault)
320 attributeDefault.clear();
324 oss <<
" \"" << attributeDefault <<
"\", ";
331 std::string attributeShortInfo =
readBlockOfLines( fin, lineNumber ,R
"f(\()f" , R"f(\))f" );
332 oss << " \"" << attributeShortInfo <<
"\", " ;
340 std::string attributeHelpInfo =
readBlockOfLines( fin , lineNumber , R
"f(\()f", R"f(\))f");
341 oss << " \"" << attributeHelpInfo <<
"\", ";
348 std::string attributeKeywords =
readBlockOfLines( fin , lineNumber , R
"f(\()f", R"f(\))f");
350 oss << " \"" << attributeKeywords <<
"\" ";
356 std::string attributeFlagName,attributeFlagValue;
361 while ( line.size() > 0 && line.find_first_of(
"#") != 0 )
365 iss >> attributeFlagName >> attributeFlagValue;
367 if ( attributeFlagName.size()==0 || attributeFlagValue.size()==0 )
369 errMsg =
" An attribute flag name and value (bool) is expected (ex. RESTART_ATTRIBUTE yes). \n An empty line or a missing flag value (bool) is not valid.";
376 if ( attributeFlags.find(attributeFlagName) != attributeFlags.end() )
381 if ( attributeFlagValue.compare(
"YES")==0 || attributeFlagValue.compare(
"TRUE")==0 )
382 attributeFlags[attributeFlagName] =
true;
383 else if ( attributeFlagValue.compare(
"NO")==0 || attributeFlagValue.compare(
"FALSE")==0 )
384 attributeFlags[attributeFlagName] =
false;
387 errMsg =
" An attribute flag name and value (bool) is expected (ex. RESTART_ATTRIBUTE yes). \n The value must be YES/TRUE or NO/FALSE.";
393 errMsg =
" An attribute flag name and value (bool) is expected (ex. RESTART_ATTRIBUTE yes).\n This flag name: " + attributeFlagName +
" is not registered.";
402 for (
auto attFlag : attributeFlags )
404 oss <<
" , \"" << ((attFlag.second) ?
"true":
"false") <<
"\"";
411 firstAttribute =
false;
415 oss << std::endl << std::endl <<
"#endif" << std::endl;
423 fout.open( attDefHeader );
427 errMsg =
" Failed to open the header file for writing attribute definition.";
439 std::cerr <<
"ERROR: Problem handling file for attribute definition: " << ((flagInFile) ? attDefFile:attDefHeader) <<
" at line " << e.
getLineNumber() <<
". \n" << e.
what() << std::endl << std::endl;
void toupper(std::string &s)
Put a string into upper cases.
void duplicateParPlurals(std::string &blockOfStrings)
Utility to interpret plural words.
const char * what(void) const noexcept
Access to the error message.
const std::string attributeDefinitionNames[6]
size_t getLineNumber(void) const noexcept
std::string readBlockOfLines(std::ifstream &fin, size_t &lineNumber, const std::string &blockDelimiterOpen, const std::string &blockDelimiterClose)
Utility to read a block of lines.
void toUpperCase(std::string &str)
Toupper utility.
const std::map< std::string, bool > attributeFlagsRegistered
Registered attribute flags and default value.