From fe79b96328c3aadf034529427b21d98be03aab77 Mon Sep 17 00:00:00 2001 From: Tetsuo Kiso Date: Wed, 18 Apr 2012 23:47:48 +0900 Subject: [PATCH] Use std::stringstream instead of using snprintf() for Windows. This commit fixes compilation problems related to snprintf() for Windows users. Thanks to Raka Prasetya for reporting the errors. Thanks also to Kenneth Heafield and Barry Haddow for suggestions. --- mert/Data.cpp | 6 +++--- mert/FeatureData.cpp | 28 +++++++++++++++------------- mert/FeatureDataTest.cpp | 11 ++++++----- mert/Timer.cpp | 10 +++++----- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/mert/Data.cpp b/mert/Data.cpp index 5405b0cb9..be4c65fb2 100644 --- a/mert/Data.cpp +++ b/mert/Data.cpp @@ -173,15 +173,15 @@ void Data::InitFeatureMap(const string& str) { string features = ""; string tmp_name = ""; size_t tmp_index = 0; - char tmp[64]; // for snprintf(); while (!buf.empty()) { getNextPound(buf, substr); // string ending with ":" are skipped, because they are the names of the features if (!EndsWith(substr, ":")) { - snprintf(tmp, sizeof(tmp), "%s_%lu ", tmp_name.c_str(), tmp_index); - features.append(tmp); + stringstream ss; + ss << tmp_name << "_" << tmp_index << " "; + features.append(ss.str()); tmp_index++; } else if (substr.find("_") != string::npos) { diff --git a/mert/FeatureData.cpp b/mert/FeatureData.cpp index 7e46f1803..957d36ad4 100644 --- a/mert/FeatureData.cpp +++ b/mert/FeatureData.cpp @@ -11,7 +11,6 @@ #include #include "FileStream.h" #include "Util.h" -#include static const float MIN_FLOAT = -1.0 * numeric_limits::max(); static const float MAX_FLOAT = numeric_limits::max(); @@ -148,23 +147,26 @@ void FeatureData::setFeatureMap(const string& feat) string FeatureData::ToString() const { string res; - char buf[100]; - snprintf(buf, sizeof(buf), "number of features: %lu, ", m_num_features); - res.append(buf); - - res.append("features: "); - res.append(m_features); - - snprintf(buf, sizeof(buf), ", sparse flag: %s, ", (m_sparse_flag) ? "yes" : "no"); - res.append(buf); + { + stringstream ss; + ss << "number of features: " << m_num_features + << ", features: " << m_features + << ", sparse flag: "; + if (m_sparse_flag) { + ss << "yes, "; + } else { + ss << "no, "; + } + res.append(ss.str()); + } res.append("feature_id_map = { "); for (map::const_iterator it = m_feature_name_to_index.begin(); it != m_feature_name_to_index.end(); ++it) { - snprintf(buf, sizeof(buf), "%s => %lu, ", - it->first.c_str(), it->second); - res.append(buf); + stringstream ss; + ss << it->first << " => " << it->second << ", "; + res.append(ss.str()); } res.append("}"); diff --git a/mert/FeatureDataTest.cpp b/mert/FeatureDataTest.cpp index aa885169f..42ac5996c 100644 --- a/mert/FeatureDataTest.cpp +++ b/mert/FeatureDataTest.cpp @@ -3,17 +3,18 @@ #define BOOST_TEST_MODULE FeatureData #include -#include +#include namespace { void CheckFeatureMap(const FeatureData* feature_data, const char* str, int num_feature, int* cnt) { - char tmp[32]; for (int i = 0; i < num_feature; ++i) { - std::snprintf(tmp, sizeof(tmp), "%s_%d", str, i); - BOOST_CHECK_EQUAL(feature_data->getFeatureIndex(tmp), *cnt); - BOOST_CHECK_EQUAL(feature_data->getFeatureName(*cnt).c_str(), tmp); + std::stringstream ss; + ss << str << "_" << i; + const string& s = ss.str(); + BOOST_CHECK_EQUAL(feature_data->getFeatureIndex(s), *cnt); + BOOST_CHECK_EQUAL(feature_data->getFeatureName(*cnt).c_str(), s); ++(*cnt); } } diff --git a/mert/Timer.cpp b/mert/Timer.cpp index 70bc91de5..5235edb04 100644 --- a/mert/Timer.cpp +++ b/mert/Timer.cpp @@ -1,6 +1,5 @@ #include "Timer.h" #include "Util.h" -#include #if !defined(_WIN32) && !defined(_WIN64) #include @@ -91,14 +90,15 @@ void Timer::check(const char* msg) std::string Timer::ToString() const { std::string res; - char tmp[64]; const double wall = get_elapsed_wall_time(); CPUTime e; GetCPUTimeMicroSeconds(&e); const double utime = (e.user_time - m_start_time.user_time) * 1e-6; const double stime = (e.sys_time - m_start_time.sys_time) * 1e-6; - std::snprintf(tmp, sizeof(tmp), "wall %f user %f sec. sys %f sec. total %f sec.", - wall, utime, stime, utime + stime); - res.append(tmp); + std::stringstream ss; + ss << "wall " << wall << " sec. user " << utime << " sec. sys " << stime + << " sec. total " << utime + stime << " sec."; + res.append(ss.str()); + return res; }