ChangeDyna.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "PreCompiled.h"
00026 #include "ChangeDyna.h"
00027 #include <sstream>
00028 #include <vector>
00029 #include <fstream>
00030 #include <stdexcept>
00031
00032 ChangeDyna::ChangeDyna()
00033 {
00034 m_ProperTime.clear();
00035 }
00036 bool ChangeDyna::Read( const std::string & _filename)
00037 {
00038
00039 std::ifstream input( _filename.c_str() );
00040 std::ifstream input2("CurveTimes.k");
00041 if (!input2.is_open()) return false;
00042 if (!ReadTimes(input2)) return false;
00043 std::ofstream output("dyna2.str");
00044 if ( !input.is_open() )
00045 {
00046 std::cout << "failed to open file" << std::endl;
00047 return false;
00048 }
00049 if ( !ReadCurve( input, output ) )
00050 {
00051 std::cout << "failed to read curve_data" << std::endl;
00052 return false;
00053 }
00054
00055 input.close();
00056 output.close();
00057 input2.close();
00058 return true;
00059 }
00060
00061
00062 bool ChangeDyna::ReadTimes(std::ifstream &input2)
00063 {
00064 input2.seekg(std::ifstream::beg);
00065 std::string line;
00066 unsigned int i=0;
00067 std::pair<float,float> tempPair;
00068 std::stringstream astream1;
00069 do
00070 {
00071 std::getline(input2,line);
00072 if (line.size() == 0) continue;
00073 astream1.str(line);
00074 astream1 >> tempPair.first >> tempPair.second;
00075 m_ProperTime.push_back(tempPair);
00076 astream1.str("");
00077 astream1.clear();
00078
00079 }
00080 while (input2.good());
00081 return true;
00082 }
00083
00084
00085 bool ChangeDyna::ReadCurve(std::ifstream &input,std::ofstream &output)
00086 {
00087 input.seekg( std::ifstream::beg );
00088 std::string line,subline1;
00089 bool found = false;
00090 int current_index;
00091 do
00092 {
00093 std::getline( input, line );
00094 if (line.size() == 0)
00095 {
00096 continue;
00097 }
00098 if (found)
00099 {
00100
00101 if (line.size() > 3 && line.at(0) == '$' && (line.find("nid") == std::string::npos))
00102 {
00103 found = false;
00104 output << line << std::endl;
00105 continue;
00106 }
00107 else if (line.at(0) == '$')
00108 {
00109 output << line << std::endl;
00110 continue;
00111 }
00112 else
00113 {
00114 std::stringstream astream1,astream2;
00115 astream1.precision(20);
00116 astream2.precision(20);
00117 if (line.at(9) != '0')
00118 {
00119
00120 astream1.str(line.substr(10,5));
00121 astream1 >> current_index;
00122
00123 if ((current_index-2) < 0)
00124 return false;
00125 astream2 << m_ProperTime[current_index-2].second;
00126
00127 try
00128 {
00129 ReformatStream(astream2,subline1);
00130 output << line.substr(0,66) << subline1 << line.substr(75,5) << std::endl;
00131 }
00132 catch (const std::out_of_range&)
00133 {
00134 output << line << std::endl;
00135 return false;
00136 }
00137
00138 continue;
00139 }
00140 else
00141 {
00142
00143 astream2 << m_ProperTime[current_index-2].first;
00144 try
00145 {
00146 ReformatStream(astream2,subline1);
00147 output << line.substr(0,31) << subline1 << std::endl;
00148 }
00149 catch (std::out_of_range)
00150 {
00151 output << line << std::endl;
00152 return false;
00153 }
00154
00155 continue;
00156 }
00157 }
00158 }
00159 else
00160 {
00161 std::string search("Velocity/Acceleration/");
00162 if (line.find(search)!=std::string::npos)
00163 found = true;
00164 output << line << std::endl;
00165 continue;
00166 }
00167 }
00168 while ( input.good());
00169 return true;
00170 }
00171
00172
00173 bool ChangeDyna::ReformatStream(const std::stringstream& astream, std::string& astring)
00174 {
00175 astring.clear();
00176 std::string tempstring(astream.str());
00177 unsigned int found=tempstring.find('.');
00178 astring = tempstring.substr(0,found);
00179 astring += '.';
00180
00181 astring += tempstring.substr(found+1,9-astring.size());
00182 while (astring.size() < 9)
00183 astring += '0';
00184 return true;
00185 }