14 "evalAttributesDefinition",
15 "cacheAttributesDefinition",
16 "evaluatorControlAttributesDefinition",
17 "pbAttributesDefinition",
18 "runAttributesDefinition" };
26 {
"RESTART_ATTRIBUTE",
false},
27 {
"ALGO_COMPATIBILITY_CHECK",
false},
28 {
"UNIQUE_ENTRY",
true} };
33 for_each(str.begin(), str.end(), [](
char& in){ in =
std::toupper(in); });
58 const char *
what (
void ) const noexcept {
return _what.c_str(); }
67 blockOfStrings.insert(0,
" ");
68 blockOfStrings.append(
" ");
75 size_t posPar1 = blockOfStrings.find(
"(" , posBegin );
76 if ( posPar1 != std::string::npos )
78 size_t posBlank = blockOfStrings.rfind(
" " , posPar1 );
79 size_t posPar2 = blockOfStrings.find(
")" ,posBegin );
80 if ( posPar2 != std::string::npos && posBlank < posPar2 )
83 std::string word = blockOfStrings.substr(posBlank+1,posPar1-posBlank-1)+
" ";
86 blockOfStrings.erase(posPar2,1);
87 blockOfStrings.erase(posPar1,1);
90 blockOfStrings.insert(posBlank+1,word);
107 const std::string & blockDelimiterOpen,
108 const std::string & blockDelimiterClose)
113 std::getline(fin, line);
118 throw (
Exception(lineNumber,
"Cannot read file") );
121 if (line.find( blockDelimiterOpen ) != 0)
123 std::string errMsg =
" Cannot find opening delimiter ";
124 errMsg = errMsg.append(blockDelimiterOpen) ;
127 std::string blockOfLines = line +
" \\n ";
131 while ( line.empty() || line.find( blockDelimiterClose ) == std::string::npos )
133 std::getline(fin, line);
138 std::string errMsg =
" Reached end of file without finding end delimiter ";
139 errMsg = errMsg.append(blockDelimiterClose) ;
143 blockOfLines += line +
" \\n ";
148 blockOfLines.erase(0,blockDelimiterOpen.size() );
149 size_t posEnd = blockOfLines.find( blockDelimiterClose );
150 blockOfLines.erase(posEnd);
156 int main(
int argc,
char *argv[])
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;
std::string _what
Error message.
virtual ~Exception(void)
Destructor.
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.
int main(int argc, char *argv[])
void toUpperCase(std::string &str)
Toupper utility.
Exception(size_t line, const std::string &msg)
const std::map< std::string, bool > attributeFlagsRegistered
Registered attribute flags and default value.
size_t _line
Line number at which the exception is thrown.