diff --git a/trick_source/sim_services/Sie/Sie.cpp b/trick_source/sim_services/Sie/Sie.cpp index 8fbb81c8b..cc7ed2b6a 100644 --- a/trick_source/sim_services/Sie/Sie.cpp +++ b/trick_source/sim_services/Sie/Sie.cpp @@ -181,32 +181,44 @@ void Trick::Sie::sie_append_runtime_objs() { } std::string sie_filename = get_runtime_sie_dir() + "/" + "S_sie.resource" ; - sie_out.open(sie_filename.c_str(), std::fstream::in | std::fstream::out) ; - sie_out.seekg(-1, sie_out.end); + const char * comment = "\n"; - const int commentLength = 86; - char buff[commentLength + 1] = {0}; - char last = '\0'; - int pos; - while(memcmp(comment, buff, commentLength) != 0) { - while(last != '!' || sie_out.peek() != '<') { - if(sie_out.bad() || sie_out.fail() || sie_out.eof()) { - std::cerr << "Warning: Cannot add runtime/dynamic allocations to S_sie.resource. S_sie.resource is corrupted, outdated, or missing. Please be sure that SIM_*/S_sie.resource is preserved after build time if needed at runtime for trick-tv or other variable server clients. Please also rerun trick-CP." << std::endl; - return; - } - last = sie_out.peek(); - sie_out.seekg(-1, std::ios::cur); - pos = sie_out.tellg(); - } - sie_out.get(buff, commentLength + 1, '\0'); - sie_out.seekg(pos - 1); + int comment_len = strlen(comment); + + int curr_offset = 1; + int mem_size = 1000000; + char * buff = new char[mem_size + 1]; + char * find_str = NULL; + + // initial read use the whole buffer + sie_out.get(buff, mem_size, '\0'); + find_str = strstr(buff, comment); + + // loop through the file looking for the marker comment. The comment could straddle 2 reads. Copy the last bytes of + // the previous read to start the next search. + while ( !find_str && !sie_out.eof() ) { + curr_offset += mem_size - comment_len - 1; + strncpy(buff, &buff[mem_size-comment_len-1], comment_len); + sie_out.get(&buff[comment_len], mem_size-comment_len, '\0'); + find_str = strstr(buff, comment); } - sie_out.seekg(pos - 1); - sie_out.seekp((int)sie_out.tellg() + commentLength + 1); + + if ( !find_str ) { + std::cerr << "Warning: Cannot add runtime/dynamic allocations to S_sie.resource. S_sie.resource is corrupted, outdated, or missing. Please be sure that SIM_*/S_sie.resource is preserved after build time if needed at runtime for trick-tv or other variable server clients. Please also rerun trick-CP." << std::endl; + delete[] buff; + return; + } + + // calculate the position of the marker in the file and jump to the point after the comment ends. + int pos = curr_offset + (find_str - buff) + comment_len - 1; + sie_out.clear(); + sie_out.seekp(pos); + runtime_objects_print(sie_out); sie_out << "\n"; sie_out.close(); + delete[] buff; } std::string Trick::Sie::get_runtime_sie_dir() {