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 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) {

View File

@ -11,7 +11,6 @@
#include <limits>
#include "FileStream.h"
#include "Util.h"
#include <cstdio>
static const float MIN_FLOAT = -1.0 * 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 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<string, size_t>::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("}");

View File

@ -3,17 +3,18 @@
#define BOOST_TEST_MODULE FeatureData
#include <boost/test/unit_test.hpp>
#include <cstdio>
#include <sstream>
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);
}
}

View File

@ -1,6 +1,5 @@
#include "Timer.h"
#include "Util.h"
#include <cstdio>
#if !defined(_WIN32) && !defined(_WIN64)
#include <sys/resource.h>
@ -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;
}