diff options
author | Tetsuo Kiso <tetsuo-s@is.naist.jp> | 2012-03-10 14:04:43 +0400 |
---|---|---|
committer | Tetsuo Kiso <tetsuo-s@is.naist.jp> | 2012-03-10 14:04:43 +0400 |
commit | 5d600f2b50e918c9b16ef8fcc735c8aa8daaa5ce (patch) | |
tree | 2299060657217bf91eac18e905f15c7922505df1 /mert/ScoreArray.cpp | |
parent | 3ce46da4cd4c9c7779401548accf235f0c331059 (diff) |
Remove hard-coded "/dev/stdout".
This will improve the portability.
We also change the interface of I/O functions for ease of the
development unit tests.
Diffstat (limited to 'mert/ScoreArray.cpp')
-rw-r--r-- | mert/ScoreArray.cpp | 92 |
1 files changed, 49 insertions, 43 deletions
diff --git a/mert/ScoreArray.cpp b/mert/ScoreArray.cpp index 3afa9ce1a..972bca0e7 100644 --- a/mert/ScoreArray.cpp +++ b/mert/ScoreArray.cpp @@ -13,74 +13,82 @@ ScoreArray::ScoreArray() : m_num_scores(0), m_index("") {} -void ScoreArray::savetxt(std::ofstream& outFile, const std::string& sctype) +void ScoreArray::savetxt(ostream* os, const string& sctype) { - outFile << SCORES_TXT_BEGIN << " " << m_index << " " << m_array.size() - << " " << m_num_scores << " " << sctype << std::endl; + *os << SCORES_TXT_BEGIN << " " << m_index << " " << m_array.size() + << " " << m_num_scores << " " << sctype << endl; for (scorearray_t::iterator i = m_array.begin(); i !=m_array.end(); i++) { - i->savetxt(outFile); - outFile << std::endl; + i->savetxt(os); + *os << endl; } - outFile << SCORES_TXT_END << std::endl; + *os << SCORES_TXT_END << endl; } -void ScoreArray::savebin(std::ofstream& outFile, const std::string& sctype) +void ScoreArray::savebin(ostream* os, const string& score_type) { - outFile << SCORES_BIN_BEGIN << " " << m_index << " " << m_array.size() - << " " << m_num_scores << " " << sctype << std::endl; - for (scorearray_t::iterator i = m_array.begin(); i !=m_array.end(); i++) - i->savebin(outFile); - - outFile << SCORES_BIN_END << std::endl; + *os << SCORES_BIN_BEGIN << " " << m_index << " " << m_array.size() + << " " << m_num_scores << " " << score_type << endl; + for (scorearray_t::iterator i = m_array.begin(); + i != m_array.end(); i++) { + i->savebin(os); + } + *os << SCORES_BIN_END << endl; } -void ScoreArray::save(std::ofstream& inFile, const std::string& sctype, bool bin) +void ScoreArray::save(ostream* os, const string& score_type, bool bin) { if (size() <= 0) return; if (bin) { - savebin(inFile, sctype); + savebin(os, score_type); } else { - savetxt(inFile, sctype); + savetxt(os, score_type); } } -void ScoreArray::save(const std::string &file, const std::string& sctype, bool bin) +void ScoreArray::save(const string &file, const string& score_type, bool bin) { - std::ofstream outFile(file.c_str(), std::ios::out); // matches a stream with a file. Opens the file - save(outFile, sctype, bin); - outFile.close(); + ofstream ofs(file.c_str(), ios::out); + if (!ofs) { + cerr << "Failed to open " << file << endl; + exit(1); + } + ostream* os = &ofs; + save(os, score_type, bin); + ofs.close(); } -void ScoreArray::loadbin(ifstream& inFile, size_t n) +void ScoreArray::save(const string& score_type, bool bin) { + save(&cout, score_type, bin); +} + +void ScoreArray::loadbin(istream* is, size_t n) { ScoreStats entry(m_num_scores); - - for (size_t i=0 ; i < n; i++) { - entry.loadbin(inFile); + for (size_t i = 0; i < n; i++) { + entry.loadbin(is); add(entry); } } -void ScoreArray::loadtxt(ifstream& inFile, size_t n) +void ScoreArray::loadtxt(istream* is, size_t n) { ScoreStats entry(m_num_scores); - for (size_t i = 0; i < n; i++) { - entry.loadtxt(inFile); + entry.loadtxt(is); add(entry); } } -void ScoreArray::load(ifstream& inFile) +void ScoreArray::load(istream* is) { size_t number_of_entries = 0; bool binmode = false; - std::string substring, stringBuf; - std::string::size_type loc; + string substring, stringBuf; + string::size_type loc; - std::getline(inFile, stringBuf); - if (!inFile.good()) { + getline(*is, stringBuf); + if (!is->good()) { return; } @@ -105,12 +113,12 @@ void ScoreArray::load(ifstream& inFile) } if (binmode) { - loadbin(inFile, number_of_entries); + loadbin(is, number_of_entries); } else { - loadtxt(inFile, number_of_entries); + loadtxt(is, number_of_entries); } - std::getline(inFile, stringBuf); + getline(*is, stringBuf); if (!stringBuf.empty()) { if ((loc = stringBuf.find(SCORES_TXT_END)) != 0 && (loc = stringBuf.find(SCORES_BIN_END)) != 0) { @@ -120,15 +128,13 @@ void ScoreArray::load(ifstream& inFile) } } -void ScoreArray::load(const std::string &file) +void ScoreArray::load(const string &file) { - TRACE_ERR("loading data from " << file << std::endl); - - inputfilestream inFile(file); // matches a stream with a file. Opens the file - - load((ifstream&) inFile); - - inFile.close(); + TRACE_ERR("loading data from " << file << endl); + inputfilestream input_stream(file); // matches a stream with a file. Opens the file + istream* is = &input_stream; + load(is); + input_stream.close(); } |