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     const std::string ipx = ipx_path.file_string();
00050     ipx_ofs.reset(new std::ofstream(ipx.c_str()));
00051     ipx_writer.reset(new osl::record::KisenIpxWriter(*ipx_ofs));
00052   }
00053 
00054   for (size_t i = 0; i < files.size(); ++i)
00055   {
00056     try
00057     {
00058       osl::record::Record record;
00059       const std::string& filename = files[i];
00060       if (boost::algorithm::iends_with(filename, ".csa"))
00061       {
00062         const osl::record::csa::CsaFile csa(filename);
00063         record = csa.getRecord();
00064       }
00065       else if (boost::algorithm::iends_with(filename, ".ki2"))
00066       {
00067         const osl::Ki2File ki2(filename);
00068         record = ki2.getRecord();
00069         if (! accept_tournament(record.tounamentName()))
00070           continue;
00071       }
00072       else if (boost::algorithm::iends_with(filename, ".kif"))
00073       {
00074         const osl::KakinokiFile kif(filename);
00075         record = kif.getRecord();
00076       }
00077       else
00078       {
00079         std::cerr << "Unknown file type: " << filename << "\n";
00080         continue;
00081       }
00082 
00083       // 重複チェック 
00084       const osl::vector<osl::Move>& moves = record.getMoves();
00085       if (check_duplicates.regist(moves))
00086         continue;
00087 
00088       ks.save(&record);
00089       if (output_ipx)
00090       {
00091         ipx_writer->save(record, default_rating, default_rating, "", "");
00092       }
00093     }
00094     catch(std::exception& e)
00095     {
00096       std::cerr << "ERROR: reading " <<  files[i] << "; " << 
00097         e.what() << std::endl;
00098       continue;
00099     }
00100   }
00101 }
00102 
00103 int main(int argc, char **argv)
00104 {
00105   bool output_ipx;
00106   std::string kisen_filename, tournament_filename;
00107   int default_rating;
00108   boost::program_options::options_description command_line_options;
00109   command_line_options.add_options()
00110     ("output-ipx",
00111      boost::program_options::value<bool>(&output_ipx)->default_value(true),
00112      "Whether output IPX file in addition to KIF file")
00113     ("tournament-file", boost::program_options::value<std::string>(&tournament_filename)
00114      ->default_value(""),
00115      "ignore records unless the name of their tournament is listed in the file in EUC-JP")
00116     ("kisen-filename,o",
00117      boost::program_options::value<std::string>(&kisen_filename)->
00118      default_value("test.kif"),
00119      "Output filename of Kisen file")
00120     ("input-file", boost::program_options::value< std::vector<std::string> >(),
00121      "input files in kisen format")
00122     ("default-rating", boost::program_options::value<int>(&default_rating)->
00123      default_value(0),
00124      "default rating")
00125     ("help", "Show help message");
00126   boost::program_options::variables_map vm;
00127   boost::program_options::positional_options_description p;
00128   p.add("input-file", -1);
00129 
00130   try
00131   {
00132     boost::program_options::store(
00133       boost::program_options::command_line_parser(
00134         argc, argv).options(command_line_options).positional(p).run(), vm);
00135     boost::program_options::notify(vm);
00136     if (vm.count("help"))
00137     {
00138       std::cerr << "Usage: " << argv[0] << " [options] csa-files | ki2-files \n";
00139       std::cerr << "       " << argv[0] << " [options]\n";
00140       std::cout << command_line_options << std::endl;
00141       return 0;
00142     }
00143   }
00144   catch (std::exception &e)
00145   {
00146     std::cerr << "error in parsing options" << std::endl
00147               << e.what() << std::endl;
00148     std::cerr << "Usage: " << argv[0] << " [options] csa-files | ki2-files\n";
00149     std::cerr << "       " << argv[0] << " [options]\n";
00150     std::cerr << command_line_options << std::endl;
00151     return 1;
00152   }
00153 
00154   if (tournament_filename != "")
00155   {
00156     std::ifstream is(tournament_filename.c_str());
00157     std::string name;
00158     while(std::getline(is, name))
00159     {
00160       boost::algorithm::trim(name);
00161       good_tournaments.push_back(name);
00162     }
00163   }
00164 
00165   std::vector<std::string> files;
00166   if (vm.count("input-file"))
00167   {
00168     const std::vector<std::string> temp = vm["input-file"].as<std::vector<std::string> >();
00169     files.insert(files.end(), temp.begin(), temp.end());
00170   }
00171   else
00172   {
00173     std::string line;
00174     while(std::getline(std::cin , line))
00175     {
00176       boost::algorithm::trim(line);
00177       files.push_back(line);
00178     }
00179   }
00180 
00181   osl::record::CheckDuplicate check_duplicate;
00182   convert(kisen_filename, files, output_ipx, check_duplicate, default_rating);
00183 
00184   std::locale::global(std::locale(""));
00185   check_duplicate.print(std::cout);
00186 
00187   return 0;
00188 }
00189 // ;;; Local Variables:
00190 // ;;; mode:c++
00191 // ;;; c-basic-offset:2
00192 // ;;; coding:utf-8
00193 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines