From cb5105f68f52c00dc834103c2d892ae29695e115 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Mon, 2 Oct 2023 14:22:08 -0600 Subject: [PATCH] Feature #2691 main_v11.1 vx_config_tmp_files (#2695) --- src/basic/vx_config/config.tab.cc | 2 +- src/basic/vx_config/config.tab.yy | 2 +- src/basic/vx_config/config_file.cc | 181 +++++++++++++---------- src/basic/vx_config/config_file.h | 10 +- src/basic/vx_config/config_scanner.ll | 5 - src/basic/vx_config/my_config_scanner.cc | 45 ++---- 6 files changed, 124 insertions(+), 121 deletions(-) diff --git a/src/basic/vx_config/config.tab.cc b/src/basic/vx_config/config.tab.cc index 7c1d9c2c4c..a352c67307 100644 --- a/src/basic/vx_config/config.tab.cc +++ b/src/basic/vx_config/config.tab.cc @@ -134,7 +134,7 @@ extern "C" int configwrap(); char * configtext; -FILE * configin; +stringstream * configin; int LineNumber = 1; diff --git a/src/basic/vx_config/config.tab.yy b/src/basic/vx_config/config.tab.yy index 3544627fef..836a874747 100644 --- a/src/basic/vx_config/config.tab.yy +++ b/src/basic/vx_config/config.tab.yy @@ -59,7 +59,7 @@ extern "C" int configwrap(); char * configtext; -FILE * configin; +stringstream * configin; int LineNumber = 1; diff --git a/src/basic/vx_config/config_file.cc b/src/basic/vx_config/config_file.cc index 490a0d6d9b..83edcebb3f 100644 --- a/src/basic/vx_config/config_file.cc +++ b/src/basic/vx_config/config_file.cc @@ -41,7 +41,7 @@ using namespace std; extern int configparse(); -extern FILE * configin; +extern stringstream * configin; extern int configdebug; @@ -152,6 +152,8 @@ void MetConfig::clear() Filename.clear(); +ConfigStream.clear(); + Dictionary::clear(); Debug = false; @@ -172,6 +174,8 @@ clear(); Filename = c.Filename; +ConfigStream << c.ConfigStream.str(); + Dictionary::assign(c); Debug = c.Debug; @@ -298,104 +302,109 @@ return n; //////////////////////////////////////////////////////////////////////// -bool MetConfig::read(const char * name) +bool MetConfig::read(const char * filename) { -if ( empty(name) ) { +set_buffer_from_file(filename); - mlog << Error << "\nMetConfig::read(const char *) -> " - << "empty filename!\n\n"; - - exit ( 1 ); +return parse_buffer(); } -DictionaryStack DS(*this); -ConcatString temp_filename = get_tmp_dir(); - -temp_filename << "/" << "met_config"; -temp_filename = make_temp_file_name(temp_filename.c_str(), nullptr); - -recursive_envs(name, temp_filename.c_str()); -bison_input_filename = temp_filename.c_str(); +//////////////////////////////////////////////////////////////////////// -dict_stack = &DS; -LineNumber = 1; +bool MetConfig::read_string(const char * config_string) -Column = 1; +{ -is_lhs = true; +set_buffer_from_string(config_string); -Filename.add(bison_input_filename); +return parse_buffer(); -configdebug = (Debug ? 1 : 0); +} -if ( (configin = met_fopen(bison_input_filename, "r")) == nullptr ) { - mlog << Error << "\nMetConfig::read(const char *) -> " - << "unable to open input file \"" << bison_input_filename << "\"\n\n"; +//////////////////////////////////////////////////////////////////////// + + +void MetConfig::set_buffer_from_file(const char * filename) + +{ + +if ( empty(filename) ) { + + mlog << Error << "\nMetConfig::set_buffer_from_file(const char *) -> " + << "empty filename!\n\n"; exit ( 1 ); } -int parse_status; + // Open input config file + +ifstream configfilein; + +met_open(configfilein, filename); -parse_status = configparse(); +if ( ! configfilein ) { -if ( configin ) { + mlog << Error << "\nMetConfig::read(const char *) -> " + << "unable to open input file \"" << filename << "\"\n\n"; - fclose(configin); - configin = (FILE *) nullptr; + exit ( 1 ); } -if ( parse_status != 0 ) { + // Initialize stream and load contents - return false; +ConfigStream.clear(); -} +string line; -if ( DS.n_elements() != 1 ) { +while ( getline(configfilein, line) ) { - mlog << Error << "\nMetConfig::read(const char *) -> " - << "should be only one dictionary left after parsing! ...(" - << DS.n_elements() << ")\n\n"; + recursive_envs(line); - DS.dump(cout); - DS.dump_config_format(cout); + ConfigStream << line << "\n"; - mlog << Error << "\n" - << "parse failed!\n\n"; +} - exit ( 1 ); + // Close the input file + +configfilein.close(); + +bison_input_filename = filename; + +Filename.add(filename); + +return; } - // - // done - // -patch_parents(); +//////////////////////////////////////////////////////////////////////// -bison_input_filename = (const char *) nullptr; -dict_stack = (DictionaryStack *) nullptr; +void MetConfig::set_buffer_from_string(const char * config_string) -LineNumber = 1; +{ -Column = 1; +string line(config_string); -is_lhs = true; +recursive_envs(line); -set_exit_on_warning(); +ConfigStream.clear(); -unlink(temp_filename.c_str()); +ConfigStream << line << "\n"; -return true; +bison_input_filename = "config_string"; + +Filename.add("config_string"); + +return; } @@ -403,51 +412,67 @@ return true; //////////////////////////////////////////////////////////////////////// -bool MetConfig::read_string(const char * s) +bool MetConfig::parse_buffer() { -if ( empty(s) ) { +configin = &ConfigStream; - mlog << Error << "\nMetConfig::read_string(const char *) -> " - << "empty input string!\n\n"; +DictionaryStack DS(*this); - exit ( 1 ); +dict_stack = &DS; -} +LineNumber = 1; - // - // write temporary config file - // default to the current directory - // +Column = 1; -ofstream out; -ConcatString temp_filename = get_tmp_dir(); +is_lhs = true; -temp_filename << "/" << "met_config"; -temp_filename = make_temp_file_name(temp_filename.c_str(), nullptr); - -out.open(temp_filename.c_str()); +configdebug = (Debug ? 1 : 0); -if ( ! out ) { +int parse_status = configparse(); + +if ( parse_status != 0 ) { - mlog << Error << "\nMetConfig::read_string(const char *) -> " - << "unable to open temp file \"" << temp_filename << "\".\n" - << "Set MET_TMP_DIR to specify a temporary directory.\n\n"; + return false; + +} + +if ( DS.n_elements() != 1 ) { + + mlog << Error << "\nMetConfig::parse_buffer(const char *) -> " + << "should be only one dictionary left after parsing! ...(" + << DS.n_elements() << ")\n\n"; + + DS.dump(cout); + DS.dump_config_format(cout); + + mlog << Error << "\n" + << "parse failed!\n\n"; exit ( 1 ); } -out << s << '\n'; + // + // done + // -out.close(); +patch_parents(); -bool status = read(temp_filename.c_str()); +bison_input_filename = (const char *) nullptr; -remove_temp_file(temp_filename); +dict_stack = (DictionaryStack *) nullptr; -return status; +LineNumber = 1; + +Column = 1; + +is_lhs = true; + +set_exit_on_warning(); + +return true; } diff --git a/src/basic/vx_config/config_file.h b/src/basic/vx_config/config_file.h index 7989e98c3a..9cc0292c22 100644 --- a/src/basic/vx_config/config_file.h +++ b/src/basic/vx_config/config_file.h @@ -34,8 +34,16 @@ class MetConfig : public Dictionary { void assign(const MetConfig &); + void set_buffer_from_file(const char *); + + void set_buffer_from_string(const char *); + + bool parse_buffer(); + StringArray Filename; + std::stringstream ConfigStream; + bool Debug; public: @@ -78,7 +86,7 @@ class MetConfig : public Dictionary { bool read(const char * filename); - bool read_string(const char *); + bool read_string(const char * config_string); const DictionaryEntry * lookup(const char * name); diff --git a/src/basic/vx_config/config_scanner.ll b/src/basic/vx_config/config_scanner.ll index c416e7f8ea..515b068416 100644 --- a/src/basic/vx_config/config_scanner.ll +++ b/src/basic/vx_config/config_scanner.ll @@ -525,11 +525,6 @@ const DictionaryEntry * e = dict_stack->lookup(configtext); if ( e && (e->is_number()) && (! is_lhs) ) { - // cout << "=================== id = \"" << configtext << "\" is_lhs = " << (is_lhs ? "true" : "false") << "\n"; - - // cout << "do_id() -> \n"; - // e->dump(cout); - if ( e->type() == IntegerType ) { set_int(configlval.nval, e->i_value()); diff --git a/src/basic/vx_config/my_config_scanner.cc b/src/basic/vx_config/my_config_scanner.cc index 023c4da57a..abebb093b6 100644 --- a/src/basic/vx_config/my_config_scanner.cc +++ b/src/basic/vx_config/my_config_scanner.cc @@ -52,7 +52,7 @@ extern int LineNumber; extern int Column; -extern FILE * configin; +extern stringstream * configin; extern char * configtext; @@ -196,19 +196,10 @@ while ( 1 ) { if ( t < 0 ) continue; - // cout << "next\n"; - if ( t > 0 ) break; } // while - -// cout << "\n\n my configlex() -> returned token " << t; -// -// if ( lexeme[0] ) cout << " ... \"" << lexeme << "\" LineNumber = " << LineNumber; -// -// cout << "\n\n" << flush; - return t; } @@ -233,8 +224,6 @@ while ( 1 ) { if ( c < 0 ) c += 256; - // cout << "c = " << c << " (" << (char) c << ") ... LineNumber = " << LineNumber << ", Column = " << Column << "\n" << flush; - if ( c == eof ) break; if ( char_class[c] != char_class_space ) break; @@ -732,7 +721,7 @@ n = 0; while ( n < max_id_length ) { - c = fgetc(configin); + c = configin->get(); if ( c == EOF ) break; @@ -740,7 +729,7 @@ while ( n < max_id_length ) { if ( c == '\\' ) { - c = fgetc(configin); + c = configin->get(); if ( c == EOF ) break; @@ -782,8 +771,6 @@ if ( (pos > 0) && (lexeme[pos - 1] == '\"') ) { lexeme[pos - 1] = (char) 0; -- while ( replace_env(s) ) { - // cout << "s = \"" << s << "\"\n\n" << flush; - } if ( s.length() >= max_id_length ) { @@ -848,7 +835,7 @@ while ( 1 ) { c1 = c2; - c2 = fgetc(configin); + c2 = configin->get(); } @@ -873,9 +860,9 @@ reading_comment = true; while ( 1 ) { - if ( feof (configin) ) break; + if ( configin->eof() ) break; - c = fgetc(configin); + c = configin->get(); if ( (c == eof) || (c == '\n') ) break; @@ -999,15 +986,15 @@ if ( reading_env ) { // not reading env // -if ( feof(configin) ) return eof; +if ( configin->eof() ) return eof; -c = fgetc(configin); +c = configin->get(); if ( c == '\n' ) { ++LineNumber; Column = 0; } if ( c == '$' ) { - cc = fgetc(configin); + cc = configin->get(); ++Column; @@ -1029,7 +1016,7 @@ if ( c == '$' ) { // SonarQube: to avoid side effect by || operator while (env_pos < max_id_length) { - if ((c = fgetc(configin)) == R_curly) break; + if ((c = configin->get()) == R_curly) break; env_name[env_pos++] = (char) c; } @@ -1098,11 +1085,6 @@ int token(int t) { -// cout << "token " << t << " ... text = "; -// if ( (configtext != 0) && (configtext[0] != 0) ) cout << '\"' << configtext << "\"\n"; -// else cout << "(nul)\n"; -// cout.flush(); - return t; } @@ -1115,8 +1097,6 @@ int do_comp() { -// if ( verbose ) cout << "\n\n ... in do_comp()\n\n" << flush; - int return_value = 0; Column += m_strlen(configtext); @@ -1394,11 +1374,6 @@ out += tmp_env_value; out += s.substr(pos2 + 1); - -// cout << "\n\n out = \"" << (out.c_str()) << "\n\n" << flush; - - - // // done //