Skip to content

Instantly share code, notes, and snippets.

@ifukazoo
Created December 13, 2015 02:57
Show Gist options
  • Save ifukazoo/e3466629a860033da546 to your computer and use it in GitHub Desktop.
Save ifukazoo/e3466629a860033da546 to your computer and use it in GitHub Desktop.
INIファイル読み込みからデータ作成
#include <windows.h>
#include <cstring>
#include <cstdlib>
#include <cstddef>
#include <string>
#include <map>
#include <vector>
#include <deque>
#include <sstream>
#include <iomanip>
#include <numeric>
typedef std::string String;
typedef std::map<std::string, std::string> KeyVal;
typedef std::map<std::string, std::string>::const_iterator KeyValIter;
typedef std::vector<char> ByteStoker;
typedef std::vector<std::string> Keys;
static char HEX[] = "0123456789ABCDEF";
struct GetPrivateProfileStringF {
GetPrivateProfileStringF(const String& inipath, const String& section) : inipath_(inipath), section_(section) {}
KeyVal& operator()(KeyVal& keyval, const String& key) {
char buf[127];
::GetPrivateProfileString(section_.c_str(), key.c_str(), "", buf, sizeof(buf), inipath_.c_str());
keyval[key] = String(buf);
return keyval;
}
const String& inipath_;
const String& section_;
};
KeyVal create_map(const String& inipath, const String& section, const Keys& keys)
{
KeyVal keyval;
return std::accumulate(keys.begin(), keys.end(), keyval, GetPrivateProfileStringF(inipath, section));
}
template <typename T>
static bool integer_field(const String& key, const KeyVal& map, ptrdiff_t offset, ByteStoker& result)
{
KeyValIter it = map.find(key);
if (it == map.end()) return false;
T v = static_cast<T>(strtoll(it->second.c_str(), NULL, 10));
std::ostringstream fmt;
fmt << std::setw(sizeof(v) * 2) << std::setfill('0') << std::hex << std::uppercase << +v;
strcpy(&result[offset], fmt.str().c_str());
return true;
}
static bool boolean_4field(const char* keys[4], const KeyVal& map, ptrdiff_t offset, ByteStoker& result)
{
String s;
KeyValIter it;
int boolean[4] = { 0 };
for (int i = 0; i < 4; i++) {
String key(keys[i]);
it = map.find(key);
if (it == map.end()) return false;
s = it->second;
boolean[i] = atoi(s.c_str());
}
unsigned merge = boolean[0] << 3 | boolean[1] << 2 | boolean[2] << 1 | boolean[3] << 0;
result[offset] = HEX[merge];
return true;
}
static bool integer8_field(const String& key, const KeyVal& map, ptrdiff_t offset, ByteStoker& result)
{
return integer_field<unsigned char>(key, map, offset, result);
}
static bool integer16_field(const String& key, const KeyVal& map, ptrdiff_t offset, ByteStoker& result)
{
return integer_field<unsigned short>(key, map, offset, result);
}
static bool integer32_field(const String& key, const KeyVal& map, ptrdiff_t offset, ByteStoker& result)
{
return integer_field<unsigned int>(key, map, offset, result);
}
static bool string_field(const String& key, const KeyVal& map, size_t length, ptrdiff_t offset, ByteStoker& result)
{
KeyValIter it = map.find(key);
if (it == map.end()) return false;
String sval = it->second;
strncpy(&result[offset], sval.c_str(), length);
return true;
}
static bool integer4_field(const String& key, const KeyVal& map, ptrdiff_t offset, ByteStoker& result)
{
KeyValIter it;
it = map.find(key);
if (it == map.end()) return false;
String val = it->second;
result[offset] = HEX[atoi(val.c_str())];
return true;
}
static bool integer4_fieldHL(const String& high, const String& low, const KeyVal& map, ptrdiff_t offset, ByteStoker& result)
{
KeyValIter it;
it = map.find(high);
if (it == map.end()) return false;
String hval = it->second;
it = map.find(low);
if (it == map.end()) return false;
String lval = it->second;
unsigned char highbit = atoi(hval.c_str());
unsigned char lowbit = atoi(lval.c_str());
unsigned char merge = ((highbit << 2) | lowbit) & 0x0F;
result[offset] = HEX[merge];
return true;
}
static bool build_data1(const KeyVal& map, ByteStoker& result)
{
const char* data1[] = { "a", "b", "c", "d" };
const char* data2[] = { "e", "f", "g", "h" };
if (!boolean_4field(data1, map, 0, result)) return false;
if (!boolean_4field(data2, map, 1, result)) return false;
if (!integer8_field("i", map, 2, result)) return false;
if (!integer8_field("j", map, 4, result)) return false;
if (!integer16_field("k", map, 6, result)) return false;
if (!integer16_field("l", map, 10, result)) return false;
if (!integer32_field("m", map, 14, result)) return false;
if (!integer32_field("n", map, 22, result)) return false;
if (!integer4_fieldHL("o", "p", map, 30, result)) return false;
if (!string_field("q", map, 2, 31, result)) return false;
if (!string_field("r", map, 6, 33, result)) return false;
if (!integer4_field("s", map, 39, result)) return false;
return true;
}
int main(int argc, char const* argv[])
{
// kyes を作る
// port を開く
// while (true) {
// iniからMapを作る
// データを待つ
// if timeout
// continue;
// データ到着完了
// case data1
// data1構築
// case data2
// data2構築
// ...
// STX + ETX + BCC
// Writefile
// }
// port を閉じる
KeyVal map;
Keys keys;
keys.push_back("a");
keys.push_back("b");
keys.push_back("c");
keys.push_back("d");
keys.push_back("e");
keys.push_back("f");
keys.push_back("g");
keys.push_back("h");
keys.push_back("i");
keys.push_back("j");
keys.push_back("k");
keys.push_back("l");
keys.push_back("m");
keys.push_back("n");
keys.push_back("o");
keys.push_back("p");
keys.push_back("q");
keys.push_back("r");
keys.push_back("s");
map = create_map("C:\\Users\\XXX\\Desktop\\sample.ini", "SECTION", keys);
ByteStoker v(40);
build_data1(map, v);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment