62 "evalAttributesDefinition",
63 "cacheAttributesDefinition",
64 "evaluatorControlAttributesDefinition",
65 "evaluatorControlGlobalAttributesDefinition",
66 "pbAttributesDefinition",
67 "runAttributesDefinition" };
75 {
"RESTART_ATTRIBUTE",
false},
76 {
"ALGO_COMPATIBILITY_CHECK",
false},
77 {
"UNIQUE_ENTRY",
true} };
82 for_each(str.begin(), str.end(), [](
char& in){ in = std::toupper(in); });
107 const char *
what (
void )
const noexcept {
return _what.c_str(); }
116 blockOfStrings.insert(0,
" ");
117 blockOfStrings.append(
" ");
124 size_t posPar1 = blockOfStrings.find(
"(" , posBegin );
125 if ( posPar1 != std::string::npos )
127 size_t posBlank = blockOfStrings.rfind(
" " , posPar1 );
128 size_t posPar2 = blockOfStrings.find(
")" ,posBegin );
129 if ( posPar2 != std::string::npos && posBlank < posPar2 )
132 std::string word = blockOfStrings.substr(posBlank+1,posPar1-posBlank-1)+
" ";
135 blockOfStrings.erase(posPar2,1);
136 blockOfStrings.erase(posPar1,1);
139 blockOfStrings.insert(posBlank+1,word);
156 const std::string & blockDelimiterOpen,
157 const std::string & blockDelimiterClose)
162 std::getline(fin, line);
167 throw (
Exception(lineNumber,
"Cannot read file") );
170 if (line.find( blockDelimiterOpen ) != 0)
172 std::string errMsg =
" Cannot find opening delimiter ";
173 errMsg = errMsg.append(blockDelimiterOpen) ;
176 std::string blockOfLines = line +
" \\n ";
180 while ( line.empty() || line.find( blockDelimiterClose ) == std::string::npos )
182 std::getline(fin, line);
187 std::string errMsg =
" Reached end of file without finding end delimiter ";
188 errMsg = errMsg.append(blockDelimiterClose) ;
192 blockOfLines += line +
" \\n ";
197 blockOfLines.erase(0,blockDelimiterOpen.size() );
198 size_t posEnd = blockOfLines.find( blockDelimiterClose );
199 blockOfLines.erase(posEnd);
205 int main(
int argc,
char *argv[])
208 std::string inputDir, outputDir;
211 std::string s = argv[1];
213 if (s[s.size()-1] !=
'/')
225 std::istringstream iss;
226 std::ostringstream oss;
228 std::map<std::string,bool> attributeFlags;
232 std::string attDefFile = inputDir + attDefName +
".txt";
233 std::string attDefHeader = outputDir + attDefName +
".hpp";
242 std::string UpperAttDefName = attDefName ;
243 for (
auto & c: UpperAttDefName) c =
toupper(c);
245 oss <<
"//////////// THIS FILE MUST BE CREATED BY EXECUTING WriteAttributeDefinitionFile ////////////" << std::endl;
246 oss <<
"//////////// DO NOT MODIFY THIS FILE MANUALLY ///////////////////////////////////////////////" << std::endl << std::endl;
248 oss <<
"#ifndef __NOMAD400_"<< UpperAttDefName <<
"__" << std::endl;
249 oss <<
"#define __NOMAD400_"<< UpperAttDefName <<
"__" << std::endl << std::endl;
250 oss <<
"_definition = {" ;
258 errMsg =
" Failed to open file " + attDefFile +
".";
261 fin.open ( attDefFile );
267 if ( !errMsg.empty() )
273 bool firstAttribute =
true;
275 while ( fin.good() && !fin.eof() )
279 if ( firstAttribute )
293 errMsg =
" Failed to read file " + attDefFile +
".";
297 if ( line.size() > 0 && line.find_first_of(
"#")== 0 )
308 if ( ! firstAttribute )
309 oss <<
"," << std::endl;
313 std::string attributeName;
316 iss >> attributeName;
318 if ( attributeName.size()==0)
320 errMsg =
" The first word after a comment must be an attribute name. \n An empty line is not valid.";
323 oss <<
"{ \"" << attributeName <<
"\", ";
329 if ( fin.fail() || line.empty() )
331 errMsg =
"Failed to read file " + attDefFile +
".";
335 std::string attributeType;
339 iss >> attributeType ;
341 if ( attributeType.size()==0 )
343 errMsg =
" The next word after an attribute name must be the attribute type. \n An empty line is not valid.";
346 oss <<
" \"" << attributeType <<
"\", " ;
352 if ( fin.fail() || line.empty() )
354 errMsg =
" Failed to read file " + attDefFile +
".";
358 std::string attributeDefault= line;
363 if (
"std::string" == attributeType ||
"NOMAD::ArrayOfString" == attributeType)
365 if (
"\"\"" == attributeDefault ||
"-" == attributeDefault ||
"N/A" == attributeDefault)
367 attributeDefault.clear();
371 oss <<
" \"" << attributeDefault <<
"\", ";
378 std::string attributeShortInfo =
readBlockOfLines( fin, lineNumber ,R
"f(\()f" , R"f(\))f" );
379 oss << " \"" << attributeShortInfo <<
"\", " ;
387 std::string attributeHelpInfo =
readBlockOfLines( fin , lineNumber , R
"f(\()f", R"f(\))f");
391 bool flagEmpty = attributeDefault.empty() || attributeDefault==
"\"\"";
392 bool flagNoDefault = attributeDefault==
"-";
393 attributeHelpInfo +=
". ";
396 attributeHelpInfo +=
"No default value.";
400 attributeHelpInfo +=
"Default: Empty string.";
404 attributeHelpInfo +=
"Default: " + attributeDefault;
406 attributeHelpInfo +=
"\\n\\n";
408 oss <<
" \"" << attributeHelpInfo <<
"\", ";
415 std::string attributeKeywords =
readBlockOfLines( fin , lineNumber , R
"f(\()f", R"f(\))f");
417 oss << " \"" << attributeKeywords <<
"\" ";
423 std::string attributeFlagName,attributeFlagValue;
428 while ( line.size() > 0 && line.find_first_of(
"#") != 0 )
432 iss >> attributeFlagName >> attributeFlagValue;
434 if ( attributeFlagName.size()==0 || attributeFlagValue.size()==0 )
436 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.";
443 if ( attributeFlags.find(attributeFlagName) != attributeFlags.end() )
448 if ( attributeFlagValue.compare(
"YES")==0 || attributeFlagValue.compare(
"TRUE")==0 )
449 attributeFlags[attributeFlagName] =
true;
450 else if ( attributeFlagValue.compare(
"NO")==0 || attributeFlagValue.compare(
"FALSE")==0 )
451 attributeFlags[attributeFlagName] =
false;
454 errMsg =
" An attribute flag name and value (bool) is expected (ex. RESTART_ATTRIBUTE yes). \n The value must be YES/TRUE or NO/FALSE.";
460 errMsg =
" An attribute flag name and value (bool) is expected (ex. RESTART_ATTRIBUTE yes).\n This flag name: " + attributeFlagName +
" is not registered.";
469 for (
auto attFlag : attributeFlags )
471 oss <<
" , \"" << ((attFlag.second) ?
"true":
"false") <<
"\"";
478 firstAttribute =
false;
482 oss << std::endl << std::endl <<
"#endif" << std::endl;
490 fout.open( attDefHeader );
494 errMsg =
" Failed to open the header file for writing attribute definition.";
506 std::cerr <<
"ERROR: Problem handling file for attribute definition: " << ((flagInFile) ? attDefFile:attDefHeader) <<
" at line " << e.
getLineNumber() <<
". \n" << e.
what() << std::endl << std::endl;