std::map<int, std::string> parseMessages(const char * inputBuffer)
{
std::map<int, std::string> rtn;
std::string buf(inputBuffer);
unsigned pos = 0;
while ((pos = buf.find(':', pos)) != std::string::npos) {
++pos;// ':'の次をポイント
if (buf.compare(pos, 3, "msg") == 0) {// メッセージタグ
int id = atoi(buf.c_str() + pos + 3);// 必要ならstrtol()やsscanf()も
unsigned nextPos;
unsigned bodyPos = buf.find('\n', pos + 3);
if (bodyPos != std::string::npos) {
++bodyPos;
nextPos = buf.find("\n:", bodyPos);
if (nextPos == std::string::npos) {
// 次のタグがない場合は最終行まで(末尾の改行は除く)
nextPos = buf.size() - 1;
}
rtn[id] = buf.substr(bodyPos, nextPos - bodyPos);
std::cout << '[' << rtn[id] << "]\n";
}
pos = nextPos;
} else {// その他のタグでは改行を見つけておく
pos = buf.find('\n', pos);
if (pos == std::string::npos) {// 改行が見つからないなら最後をポイント
pos = buf.size();
}
}
}
return rtn;
}