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.
This commit is contained in:
Tetsuo Kiso 2012-04-18 23:47:48 +09:00
parent fd5defdb4f
commit fe79b96328
4 changed files with 29 additions and 26 deletions

View File

@ -173,15 +173,15 @@ void Data::InitFeatureMap(const string& str) {
string features = ""; string features = "";
string tmp_name = ""; string tmp_name = "";
size_t tmp_index = 0; size_t tmp_index = 0;
char tmp[64]; // for snprintf();
while (!buf.empty()) { while (!buf.empty()) {
getNextPound(buf, substr); getNextPound(buf, substr);
// string ending with ":" are skipped, because they are the names of the features // string ending with ":" are skipped, because they are the names of the features
if (!EndsWith(substr, ":")) { if (!EndsWith(substr, ":")) {
snprintf(tmp, sizeof(tmp), "%s_%lu ", tmp_name.c_str(), tmp_index); stringstream ss;
features.append(tmp); ss << tmp_name << "_" << tmp_index << " ";
features.append(ss.str());
tmp_index++; tmp_index++;
} else if (substr.find("_") != string::npos) { } else if (substr.find("_") != string::npos) {

View File

@ -11,7 +11,6 @@
#include <limits> #include <limits>
#include "FileStream.h" #include "FileStream.h"
#include "Util.h" #include "Util.h"
#include <cstdio>
static const float MIN_FLOAT = -1.0 * numeric_limits<float>::max(); static const float MIN_FLOAT = -1.0 * numeric_limits<float>::max();
static const float MAX_FLOAT = numeric_limits<float>::max(); static const float MAX_FLOAT = numeric_limits<float>::max();
@ -148,23 +147,26 @@ void FeatureData::setFeatureMap(const string& feat)
string FeatureData::ToString() const { string FeatureData::ToString() const {
string res; string res;
char buf[100];
snprintf(buf, sizeof(buf), "number of features: %lu, ", m_num_features); {
res.append(buf); stringstream ss;
ss << "number of features: " << m_num_features
res.append("features: "); << ", features: " << m_features
res.append(m_features); << ", sparse flag: ";
if (m_sparse_flag) {
snprintf(buf, sizeof(buf), ", sparse flag: %s, ", (m_sparse_flag) ? "yes" : "no"); ss << "yes, ";
res.append(buf); } else {
ss << "no, ";
}
res.append(ss.str());
}
res.append("feature_id_map = { "); res.append("feature_id_map = { ");
for (map<string, size_t>::const_iterator it = m_feature_name_to_index.begin(); for (map<string, size_t>::const_iterator it = m_feature_name_to_index.begin();
it != m_feature_name_to_index.end(); ++it) { it != m_feature_name_to_index.end(); ++it) {
snprintf(buf, sizeof(buf), "%s => %lu, ", stringstream ss;
it->first.c_str(), it->second); ss << it->first << " => " << it->second << ", ";
res.append(buf); res.append(ss.str());
} }
res.append("}"); res.append("}");

View File

@ -3,17 +3,18 @@
#define BOOST_TEST_MODULE FeatureData #define BOOST_TEST_MODULE FeatureData
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <cstdio> #include <sstream>
namespace { namespace {
void CheckFeatureMap(const FeatureData* feature_data, void CheckFeatureMap(const FeatureData* feature_data,
const char* str, int num_feature, int* cnt) { const char* str, int num_feature, int* cnt) {
char tmp[32];
for (int i = 0; i < num_feature; ++i) { for (int i = 0; i < num_feature; ++i) {
std::snprintf(tmp, sizeof(tmp), "%s_%d", str, i); std::stringstream ss;
BOOST_CHECK_EQUAL(feature_data->getFeatureIndex(tmp), *cnt); ss << str << "_" << i;
BOOST_CHECK_EQUAL(feature_data->getFeatureName(*cnt).c_str(), tmp); const string& s = ss.str();
BOOST_CHECK_EQUAL(feature_data->getFeatureIndex(s), *cnt);
BOOST_CHECK_EQUAL(feature_data->getFeatureName(*cnt).c_str(), s);
++(*cnt); ++(*cnt);
} }
} }

View File

@ -1,6 +1,5 @@
#include "Timer.h" #include "Timer.h"
#include "Util.h" #include "Util.h"
#include <cstdio>
#if !defined(_WIN32) && !defined(_WIN64) #if !defined(_WIN32) && !defined(_WIN64)
#include <sys/resource.h> #include <sys/resource.h>
@ -91,14 +90,15 @@ void Timer::check(const char* msg)
std::string Timer::ToString() const { std::string Timer::ToString() const {
std::string res; std::string res;
char tmp[64];
const double wall = get_elapsed_wall_time(); const double wall = get_elapsed_wall_time();
CPUTime e; CPUTime e;
GetCPUTimeMicroSeconds(&e); GetCPUTimeMicroSeconds(&e);
const double utime = (e.user_time - m_start_time.user_time) * 1e-6; 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; 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.", std::stringstream ss;
wall, utime, stime, utime + stime); ss << "wall " << wall << " sec. user " << utime << " sec. sys " << stime
res.append(tmp); << " sec. total " << utime + stime << " sec.";
res.append(ss.str());
return res; return res;
} }