All Classes Namespaces Files Functions Variables Enumerations Enumerator
libs/monitor/stripTimingData.cpp
Go to the documentation of this file.
00001 
00018 #include <boost/filesystem.hpp>
00019 #include <boost/algorithm/string.hpp>
00020 #include <fstream>
00021 #include <iostream>
00022 #include <string>
00023 #include <cstdlib>
00024 
00025 using namespace boost;
00026 using namespace boost::algorithm;
00027 using namespace std;
00028 
00029 
00030 namespace
00031 {
00032   void printUsage(const char* program_name)
00033   {
00034     cout << "usage:" << endl
00035          << "\t" << program_name << " <log file>" << endl;
00036   }
00037 
00038 
00039   void checkFileCreated(ofstream& fout, const string& file)
00040   {
00041     if (! fout.is_open()) {
00042       perror(file.c_str());
00043       exit(1);
00044     }
00045   }
00046 }
00047 
00048 
00049 int main(int argc, char *argv[])
00050 {
00051   if (argc < 2) {
00052     printUsage(argv[0]);
00053     exit(1);
00054   }
00055   const char* logfile = argv[1];
00056 
00057 
00058   // 出力ファイル名の作成
00059   filesystem::path file_path(logfile);
00060   string basename = file_path.leaf();
00061   string basename_body = basename.substr(0, basename.rfind("."));
00062 
00063   string send_file = basename_body + "_send.txt";
00064   string recv_file = basename_body + "_recv.txt";
00065 
00066   ofstream send_fout(send_file.c_str());
00067   checkFileCreated(send_fout, send_file);
00068 
00069   ofstream recv_fout(recv_file.c_str());
00070   checkFileCreated(recv_fout, recv_file);
00071 
00072   // 送受信データの生成
00073   ifstream fin(logfile);
00074   if (! fin.is_open()) {
00075     perror(logfile);
00076     exit(1);
00077   }
00078 
00079   string line;
00080   vector<char> buffer;
00081   while (getline(fin, line)) {
00082 
00083     // コメントのある行のみ処理する
00084     size_t comment_first = line.rfind("#");
00085     if (comment_first == string::npos) {
00086       continue;
00087     }
00088     string comment = line.substr(comment_first);
00089 
00090     // send, recv のとき以外は、処理を行わない
00091     if (comment.compare("# recv()") && comment.compare("# send()")) {
00092       continue;
00093     }
00094 
00095     vector<string> tokens;
00096     split(tokens, line, boost::is_any_of(", "));
00097     if (tokens.size() != 4) {
00098       continue;
00099     }
00100     size_t data_size = atoi(tokens[1].c_str());
00101     if (buffer.size() < data_size) {
00102       buffer.resize(data_size);
00103     }
00104     fin.read(&buffer[0], data_size);
00105 
00106     // データの格納
00107     if (comment[2] == 'r') {
00108       recv_fout.write(&buffer[0], data_size);
00109     } else {
00110       send_fout.write(&buffer[0], data_size);
00111     }
00112 
00113     // 改行の読み飛ばし
00114     char ch;
00115     fin.read(&ch, 1);
00116   }
00117   return 0;
00118 }