📄 vmfileparser.cpp
字号:
try
{
m_v2dData.at(iVariable).push_back(value);
return true;
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[0]; }
return false;
}
bool VMFileParser::AppendData(const int& iVariable, const char* szValue)
{
try
{
m_v2dStrData.at(iVariable).push_back(szValue);
return true;
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[0]; }
return false;
}
bool VMFileParser::AppendData(const int& iVariable, const std::vector<double>& vData)
{
try
{
for_each
(
vData.begin(),
vData.end(),
AddTo(m_v2dData.at(iVariable))
);
return true;
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[0]; }
return false;
}
bool VMFileParser::AppendData(const int& iVariable, const std::vector<std::string>& vszData)
{
try
{
for_each
(
vszData.begin(),
vszData.end(),
AddTo(m_v2dStrData.at(iVariable))
);
return true;
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[0]; }
return false;
}
// Attempts to create a variable with the name specified by szName with a size of
// iSize and an initial_value. Returns true if successful, false if an error is encountered.
bool VMFileParser::CreateVariable(const char* szName, const double& initial_value, const int& iSize)
{
try
{
m_vstrSourceFilenames.push_back("");
m_vstrVariableNames.push_back(szName);
m_v2dData.push_back(vector<double>(iSize, initial_value));
return true;
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[0]; }
return false;
}
// Attempts to create a variable with the name specified by szName with a size of
// iSize and an initial_value. Returns true if successful, false if an error is encountered.
bool VMFileParser::CreateVariable(const char* szName, const std::string& initial_value, const int& iSize)
{
try
{
m_vstrSourceFilenames.push_back("");
m_vstrVariableNames.push_back(szName);
m_v2dStrData.push_back(vector<string>(iSize, initial_value));
return true;
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[0]; }
return false;
}
// Attempts to create a variable with the name specified by szName and the values
// contained in vData. Returns true if successful, false if an error is encountered.
bool VMFileParser::CreateVariable(const char* szName, const std::vector<double>& vData)
{
try
{
m_vstrSourceFilenames.push_back("");
m_vstrVariableNames.push_back(szName);
m_v2dData.push_back(vData);
return true;
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[0]; }
return false;
}
// Attempts to create a variable with the name specified by szName and the values
// contained in vszData. Returns true if successful, false if an error is encountered.
bool VMFileParser::CreateVariable(const char* szName, const std::vector<std::string>& vszData)
{
try
{
m_vstrSourceFilenames.push_back("");
m_vstrVariableNames.push_back(szName);
m_v2dStrData.push_back(vszData);
return true;
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[0]; }
return false;
}
// Deletes the variable whose index is iVariable from a VMFileParser.
// Returns true if successful, false if an error is encountered.
bool VMFileParser::DeleteVariable(const int& iVariable)
{
try
{
// DeleteVariable() uses the erase/remove paradigm to delete
// the specified variable from the internal vectors. See my
// article on std::vector (http://www.codeproject.com/vcpp/stl/std_vector.asp)
// for more information on how this code works.
// remove the variable from m_vstrSourceFilenames
m_vstrSourceFilenames.erase
(
std::remove(m_vstrSourceFilenames.begin(),
m_vstrSourceFilenames.end(),
m_vstrSourceFilenames.at(iVariable)),
m_vstrSourceFilenames.end()
);
// remove the variable from m_vstrVariableNames
m_vstrVariableNames.erase
(
std::remove(m_vstrVariableNames.begin(),
m_vstrVariableNames.end(),
m_vstrVariableNames.at(iVariable)),
m_vstrVariableNames.end()
);
if(m_dwReadFlags & RF_READ_AS_DOUBLE)
{
// remove the variable from m_v2dData
m_v2dData.erase
(
std::remove(m_v2dData.begin(),
m_v2dData.end(),
m_v2dData.at(iVariable)),
m_v2dData.end()
);
}
else
{
// remove the variable from m_v2dStrData
m_v2dStrData.erase
(
std::remove(m_v2dStrData.begin(),
m_v2dStrData.end(),
m_v2dStrData.at(iVariable)),
m_v2dStrData.end()
);
}
return true;
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[0]; }
return false;
}
bool VMFileParser::WriteFile(const char* szFilename, const char* szDelim)
{
try
{
SetDelimiter(szDelim);
std::ofstream of(szFilename);
of << *this;
of.close();
return true;
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[0]; }
return false;
}
int VMFileParser::GetVariableName(const int& iVariable, char* lpStr)
{
try
{
strcpy(lpStr, m_vstrVariableNames.at(iVariable).c_str());
return static_cast<int>(m_vstrVariableNames.at(iVariable).length());
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[1]; }
return -1;
}
int VMFileParser::GetVariableName(const int& iVariable, std::string& rStr)
{
try
{
rStr = m_vstrVariableNames.at(iVariable).c_str();
return static_cast<int>(rStr.length());
}
catch(const exception& e) { m_szError = e.what(); }
catch(...) { m_szError = ERRORCODES[1]; }
return -1;
}
int VMFileParser::GetLargestVectorSize_() const
{
int retVal = 0;
int curVal = 0;
if(m_dwReadFlags & RF_READ_AS_DOUBLE)
{
vector<vector<double> >::const_iterator it = m_v2dData.begin();
while(it != m_v2dData.end())
{
curVal = static_cast<int>(it->size());
retVal = (curVal > retVal) ? curVal : retVal;
it++;
}
}
else if(m_dwReadFlags & RF_READ_AS_STRING)
{
vector<vector<string> >::const_iterator it = m_v2dStrData.begin();
while(it != m_v2dStrData.end())
{
curVal = static_cast<int>(it->size());
retVal = (curVal > retVal) ? curVal : retVal;
it++;
}
}
return retVal;
}
// Clears all data in the VMFileParser
void VMFileParser::ClearData()
{
m_szError.assign( "" );
std::vector<std::string>().swap(m_vstrVariableNames);
std::vector<std::string>().swap(m_vstrSourceFilenames);
std::vector<std::vector<std::string> >().swap(m_v2dStrData);
std::vector<std::vector<double> >().swap(m_v2dData);
}
// Returns the value at the specified location if successful.
// Returns ERRORVALUE if an error is encountered.
double VMFileParser::GetData(const int& iVariable, const int& iSample)
{
if(m_dwReadFlags & RF_READ_AS_DOUBLE)
{
try
{
return m_v2dData.at(iVariable).at(iSample);
}
catch(const exception& e)
{
m_szError = e.what();
}
catch(...)
{
m_szError = ERRORCODES[2];
}
}
else
m_szError = ERRORCODES[4];
return ERRORVALUE;
}
// Returns the length of the string if successful.
// Returns -1 if an error is encountered.
int VMFileParser::GetData(const int& iVariable, const int& iSample, std::string& rStr)
{
if(m_dwReadFlags & RF_READ_AS_STRING)
{
try
{
rStr = m_v2dStrData.at(iVariable).at(iSample).c_str();
return static_cast<int>(rStr.length());
}
catch(const exception& e)
{
m_szError = e.what();
return -1;
}
catch(...)
{
m_szError = ERRORCODES[1];
return -1;
}
}
else
{
m_szError = ERRORCODES[3];
return -1;
}
}
// Returns the length of the string if successful.
// Returns -1 if an error is encountered.
int VMFileParser::GetData(const char* szVariableName, const int& iSample, std::string& rStr)
{
int retVal = 0;
int index = LookupVariableIndex_(szVariableName);
if(index != -1)
retVal = GetData(index, iSample, rStr);
else
{
m_szError = ERRORCODES[5];
retVal = -1;
}
return retVal;
}
// Returns the length of the string if successful.
// Returns -1 if an error is encountered.
int VMFileParser::GetData(const int& iVariable, const int& iSample, char* lpStr)
{
int retVal = 0;
if(m_dwReadFlags & RF_READ_AS_STRING)
{
try
{
strcpy(lpStr,m_v2dStrData.at(iVariable).at(iSample).c_str());
retVal = static_cast<int>(m_v2dStrData.at(iVariable).at(iSample).length());
}
catch(const exception& e)
{
m_szError = e.what();
retVal = -1;
}
catch(...)
{
m_szError = ERRORCODES[1];
retVal = -1;
}
}
else
{
m_szError = ERRORCODES[3];
retVal = -1;
}
return retVal;
}
// Returns the new size of rVector if successful.
// Returns -1 if an error is encountered.
int VMFileParser::GetData(const int& iVariable, std::vector<double>& rVector)
{
int retVal = 0;
try
{
rVector = m_v2dData.at(iVariable);
retVal = static_cast<int>(rVector.size());
}
catch(const exception& e)
{
m_szError = e.what();
retVal = -1;
}
catch(...)
{
m_szError = ERRORCODES[2];
retVal = -1;
}
return retVal;
}
// Returns the new size of rVector if successful.
// Returns -1 if an error is encountered.
int VMFileParser::GetData(const int& iVariable, std::vector<std::string>& rVector)
{
int retVal = 0;
try
{
rVector = m_v2dStrData.at(iVariable);
retVal = static_cast<int>(rVector.size());
}
catch(const exception& e)
{
m_szError = e.what();
retVal = -1;
}
catch(...)
{
m_szError = ERRORCODES[2];
retVal = -1;
}
return retVal;
}
// Returns the value at the specified location if successful.
// Returns ERRORVALUE if an error is encountered.
double VMFileParser::GetData(const char* szVariableName, const int& iSample)
{
int index = LookupVariableIndex_(szVariableName);
if(index != -1)
return GetData(index, iSample);
else
m_szError = ERRORCODES[5];
return ERRORVALUE;
}
// Returns true if successful.
// Returns false if an error is encountered.
int VMFileParser::GetData(const char* szVariableName, const int& iSample, char* lpStr)
{
int retVal = 0;
int index = LookupVariableIndex_(szVariableName);
if(index != -1)
retVal = GetData(index, iSample, lpStr);
else
{
m_szError = ERRORCODES[5];
retVal = -1;
}
return retVal;
}
// Returns the new size of rVector if successful.
// Returns -1 if an error is encountered.
int VMFileParser::GetData(const char* szVariableName, std::vector<double>& rVector)
{
int index = LookupVariableIndex_(szVariableName);
if(index != -1)
return GetData(index, rVector);
else
m_szError = ERRORCODES[5];
return -1;
}
// Returns the new size of rVector if successful.
// Returns -1 if an error is encountered.
int VMFileParser::GetData(const char* szVariableName, std::vector<std::string>& rVector)
{
int index = LookupVariableIndex_(szVariableName);
if(index != -1)
return GetData(index, rVector);
else
m_szError = ERRORCODES[5];
return -1;
}
// Returns the index of the first instance of the specified variable
// found AFTER iStartingIndex.
// Returns -1 if the variable is not found.
int VMFileParser::GetVariableIndex(const char* szVariableName, const int& iStartingIndex /*=0*/)
{
return LookupVariableIndex_(szVariableName, iStartingIndex);
}
// Returns the index of the specified variable.
// Returns -1 if the variable is not found.
int VMFileParser::GetVariableIndex(const char* szVariableName, const char* szSourceFilename, const int& iStartingIndex /*=0*/)
{
int offset = 0;
vector<string>::iterator it =
find_if
(
m_vstrSourceFilenames.begin(),
m_vstrSourceFilenames.end(),
ByName(szSourceFilename)
);
if(it == m_vstrSourceFilenames.end())
{
m_szError = ERRORCODES[6];
return -1;
}
while(it != m_vstrSourceFilenames.begin())
{
offset++;
it--;
}
return LookupVariableIndex_(szVariableName, iStartingIndex+offset);
}
// Returns the index of the first variable name that matches szName.
// Returns -1 if szName is not found.
int VMFileParser::LookupVariableIndex_(const char* szName, const int& offset /*=0*/) const
{
int retVal = 0;
vector<string>::const_iterator it =
find_if
(
m_vstrVariableNames.begin() + offset,
m_vstrVariableNames.end(),
ByName(szName)
);
if(it == m_vstrVariableNames.end())
retVal = -1;
else
{
while(it != m_vstrVariableNames.begin())
{
retVal++;
it--;
}
}
return retVal;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -