csa-to-kisen.cc
Go to the documentation of this file.
00001 #include "osl/container/moveVector.h"
00002 #include "osl/hash/hashKey.h"
00003 #include "osl/state/numEffectState.h"
00004 #include "osl/record/ki2.h"
00005 #include "osl/record/kisen.h"
00006 #include "osl/record/kakinoki.h"
00007 #include "osl/record/csaRecord.h"
00008 #include "osl/record/checkDuplicate.h"
00009 
00010 #include <boost/algorithm/string/predicate.hpp>
00011 #include <boost/algorithm/string/trim.hpp>
00012 #include <boost/scoped_ptr.hpp>
00013 #include <boost/program_options.hpp>
00014 #include <boost/filesystem/convenience.hpp>
00015 #include <boost/foreach.hpp>
00016 #include <boost/format.hpp>
00017 #include <deque>
00018 #include <exception>
00019 #include <iostream>
00020 #include <fstream>
00021 #include <tr1/unordered_map>
00022 
00023 std::vector<std::string> good_tournaments;
00024 bool accept_tournament(const std::string& name) 
00025 {
00026   if (good_tournaments.empty())
00027     return true;
00028   for (size_t i=0; i<good_tournaments.size(); ++i)
00029     if (good_tournaments[i].find(name) == 0)
00030       return true;
00031   return false;
00032 }
00033 
00034 static void convert(const std::string &kisen_filename,
00035                     const std::vector<std::string> &files,
00036                     bool output_ipx,
00037                     osl::record::CheckDuplicate& check_duplicates,
00038                     int default_rating)
00039 {
00040   std::ofstream ofs(kisen_filename.c_str());
00041   osl::record::OKisenStream ks(ofs);
00042 
00043   boost::scoped_ptr<osl::record::KisenIpxWriter> ipx_writer;
00044   boost::scoped_ptr<std::ofstream> ipx_ofs;
00045   if (output_ipx)
00046   {
00047     const boost::filesystem::path ipx_path =
00048       boost::filesystem::change_extension(boost::filesystem::path(kisen_filename), ".ipx");
00049 #if BOOST_FILESYSTEM_VERSION >= 3
00050     const std::string ipx = ipx_path.string();
00051 #else
00052     const std::string ipx = ipx_path.file_string();
00053 #endif
00054     ipx_ofs.reset(new std::ofstream(ipx.c_str()));
00055     ipx_writer.reset(new osl::record::KisenIpxWriter(*ipx_ofs));
00056   }
00057 
00058   for (size_t i = 0; i < files.size(); ++i)
00059   {
00060     try
00061     {
00062       osl::record::Record record;
00063       const std::string& filename = files[i];
00064       if (boost::algorithm::iends_with(filename, ".csa"))
00065       {
00066         const osl::record::csa::CsaFile csa(filename);
00067         record = csa.getRecord();
00068       }
00069       else if (boost::algorithm::iends_with(filename, ".ki2"))
00070       {
00071         const osl::Ki2File ki2(filename);
00072         record = ki2.getRecord();
00073         if (! accept_tournament(record.tounamentName()))
00074           continue;
00075       }
00076       else if (boost::algorithm::iends_with(filename, ".kif"))
00077       {
00078         const osl::KakinokiFile kif(filename);
00079         record = kif.getRecord();
00080       }
00081       else
00082       {
00083         std::cerr << "Unknown file type: " << filename << "\n";
00084         continue;
00085       }
00086 
00087       // 重複チェック 
00088       const osl::vector<osl::Move>& moves = record.getMoves();
00089       if (check_duplicates.regist(moves))
00090         continue;
00091 
00092       ks.save(&record);
00093       if (output_ipx)
00094       {
00095         ipx_writer->save(record, default_rating, default_rating, "", "");
00096       }
00097     }
00098     catch(std::exception& e)
00099     {
00100       std::cerr << "ERROR: reading " <<  files[i] << "; " << 
00101         e.what() << std::endl;
00102       continue;
00103     }
00104   }
00105 }
00106 
00107 int main(int argc, char **argv)
00108 {
00109   bool output_ipx;
00110   std::string kisen_filename, tournament_filename;
00111   int default_rating;
00112   boost::program_options::options_description command_line_options;
00113   command_line_options.add_options()
00114     ("output-ipx",
00115      boost::program_options::value<bool>(&output_ipx)->default_value(true),
00116      "Whether output IPX file in addition to KIF file")
00117     ("tournament-file", boost::program_options::value<std::string>(&tournament_filename)
00118      ->default_value(""),
00119      "ignore records unless the name of their tournament is listed in the file in EUC-JP")
00120     ("kisen-filename,o",
00121      boost::program_options::value<std::string>(&kisen_filename)->
00122      default_value("test.kif"),
00123      "Output filename of Kisen file")
00124     ("input-file", boost::program_options::value< std::vector<std::string> >(),
00125      "input files in kisen format")
00126     ("default-rating", boost::program_options::value<int>(&default_rating)->
00127      default_value(0),
00128      "default rating")
00129     ("help", "Show help message");
00130   boost::program_options::variables_map vm;
00131   boost::program_options::positional_options_description p;
00132   p.add("input-file", -1);
00133 
00134   try
00135   {
00136     boost::program_options::store(
00137       boost::program_options::command_line_parser(
00138         argc, argv).options(command_line_options).positional(p).run(), vm);
00139     boost::program_options::notify(vm);
00140     if (vm.count("help"))
00141     {
00142       std::cerr << "Usage: " << argv[0] << " [options] csa-files | ki2-files \n";
00143       std::cerr << "       " << argv[0] << " [options]\n";
00144       std::cout << command_line_options << std::endl;
00145       return 0;
00146     }
00147   }
00148   catch (std::exception &e)
00149   {
00150     std::cerr << "error in parsing options" << std::endl
00151               << e.what() << std::endl;
00152     std::cerr << "Usage: " << argv[0] << " [options] csa-files | ki2-files\n";
00153     std::cerr << "       " << argv[0] << " [options]\n";
00154     std::cerr << command_line_options << std::endl;
00155     return 1;
00156   }
00157 
00158   if (tournament_filename != "")
00159   {
00160     std::ifstream is(tournament_filename.c_str());
00161     std::string name;
00162     while(std::getline(is, name))
00163     {
00164       boost::algorithm::trim(name);
00165       good_tournaments.push_back(name);
00166     }
00167   }
00168 
00169   std::vector<std::string> files;
00170   if (vm.count("input-file"))
00171   {
00172     const std::vector<std::string> temp = vm["input-file"].as<std::vector<std::string> >();
00173     files.insert(files.end(), temp.begin(), temp.end());
00174   }
00175   else
00176   {
00177     std::string line;
00178     while(std::getline(std::cin , line))
00179     {
00180       boost::algorithm::trim(line);
00181       files.push_back(line);
00182     }
00183   }
00184 
00185   osl::record::CheckDuplicate check_duplicate;
00186   convert(kisen_filename, files, output_ipx, check_duplicate, default_rating);
00187 
00188   std::locale::global(std::locale(""));
00189   check_duplicate.print(std::cout);
00190 
00191   return 0;
00192 }
00193 // ;;; Local Variables:
00194 // ;;; mode:c++
00195 // ;;; c-basic-offset:2
00196 // ;;; coding:utf-8
00197 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines