現実逃避気味(苦笑)にお手軽保存&読み込み用のクラスを作成してみました。
汚いソースなのは仕様なので気にしないでください(笑)
まずは、クラスのソースを…
#pragma once
#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class StatusFile
{
private:
std::map<std::string, std::string> m_data;
public:
StatusFile(void)
{
clear();
}
~StatusFile(void)
{
}
void clear(void)
{
m_data.clear();
}
void set(std::string name, std::string data)
{
m_data.insert(std::map<std::string, std::string>::value_type(name, data));
}
void set(const char* name, std::string data)
{
std::string buf = name;
set(buf, data);
}
std::string get(std::string name)
{
return m_data[name];
}
std::string get(const char* name)
{
std::string buf = name;
return get(buf);
}
bool save(const char* path)
{
std::fstream fs(path, std::ios::out);
if (fs.fail() == true)
{
return false;
}
for each (const std::pair<std::string, std::string> buf in m_data)
{
fs << buf.first << "=" << buf.second << std::endl;
}
fs.close();
return true;
}
bool load(const char* path)
{
std::fstream fs(path, std::ios::in);
if (fs.fail() == true)
{
return false;
}
m_data.clear();
while (fs.eof() == false)
{
std::string buf, pat("="), rep(" ");
std::getline(fs, buf);
if (buf.length() < 3) continue;
buf = replace(buf, pat, rep);
std::stringstream ss(buf);
std::string name, data;
ss >> name >> data;
set(name, data);
}
fs.close();
return true;
}
private:
std::string replace(const std::string& source, const std::string& pattern, const std::string& placement)
{
std::string result;
std::string::size_type pos_before = 0;
std::string::size_type pos = 0;
std::string::size_type len = pattern.size();
while ((pos = source.find(pattern, pos)) != std::string::npos)
{
result.append(source, pos_before, pos - pos_before);
result.append(placement);
pos += len;
pos_before = pos;
}
result.append(source, pos_before, source.size() - pos_before);
return result;
}
private:
};
#include <string>
#include <sstream>
template <class Type>
class Convert
{
public:
static std::string toString(const Type& val)
{
std::stringstream ss;
ss << val;
return ss.str();
}
static Type toValue(std::string str)
{
std::stringstream ss;
ss << str;
Type type;
ss >> type;
return type;
}
};
次に使用例を…
#include <iostream>
#include "StatusFile.h"
void main(void)
{
// StatusFileクラスの実体を作成する
StatusFile sf;
// 保存したいデータをセットしていく
sf.set("HP", Convert<int>::toString(10));
sf.set("MP", Convert<int>::toString(20));
sf.set("AP", Convert<int>::toString(30));
sf.set("DP", Convert<int>::toString(40));
sf.set("SP", Convert<int>::toString(50));
sf.set("PositionX", Convert<double>::toString(123.45));
sf.set("PositionY", Convert<double>::toString(678.90));
sf.set("EventFlag", Convert<bool>::toString(true));
sf.set("String", Convert<char*>::toString("TestString"));
// パスを指定して保存する
sf.save("test.txt");
// パスを指定してStatusFileクラス内へデータを展開する
sf.load("test.txt");
// 値を取り出して、文字列→数値へ
int hp = Convert<int>::toValue(sf.get("HP"));
int mp = Convert<int>::toValue(sf.get("MP"));
int ap = Convert<int>::toValue(sf.get("AP"));
int dp = Convert<int>::toValue(sf.get("DP"));
int sp = Convert<int>::toValue(sf.get("SP"));
double posx = Convert<double>::toValue(sf.get("PositionX"));
double posy = Convert<double>::toValue(sf.get("PositionY"));
bool flag = Convert<bool>::toValue(sf.get("EventFlag"));
}