00001 #include "osl/apply_move/applyMove.h"
00002 #include "osl/container/moveVector.h"
00003 #include "osl/hash/hashKey.h"
00004 #include "osl/state/numEffectState.h"
00005 #include "osl/record/ki2.h"
00006 #include "osl/record/kisen.h"
00007 #include "osl/record/kakinoki.h"
00008 #include "osl/record/csaRecord.h"
00009 #include "osl/record/checkDuplicate.h"
00010
00011 #include <boost/algorithm/string/predicate.hpp>
00012 #include <boost/algorithm/string/trim.hpp>
00013 #include <boost/scoped_ptr.hpp>
00014 #include <boost/program_options.hpp>
00015 #include <boost/filesystem/convenience.hpp>
00016 #include <boost/foreach.hpp>
00017 #include <boost/format.hpp>
00018 #include <deque>
00019 #include <exception>
00020 #include <iostream>
00021 #include <fstream>
00022 #include <tr1/unordered_map>
00023
00024 std::vector<std::string> good_tournaments;
00025 bool accept_tournament(const std::string& name)
00026 {
00027 if (good_tournaments.empty())
00028 return true;
00029 for (size_t i=0; i<good_tournaments.size(); ++i)
00030 if (good_tournaments[i].find(name) == 0)
00031 return true;
00032 return false;
00033 }
00034
00035 static void convert(const std::string &kisen_filename,
00036 const std::vector<std::string> &files,
00037 bool output_ipx,
00038 osl::record::CheckDuplicate& check_duplicates)
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, 0, 0, "", "");
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 boost::program_options::options_description command_line_options;
00108 command_line_options.add_options()
00109 ("output-ipx",
00110 boost::program_options::value<bool>(&output_ipx)->default_value(true),
00111 "Whether output IPX file in addition to KIF file")
00112 ("tournament-file", boost::program_options::value<std::string>(&tournament_filename)
00113 ->default_value(""),
00114 "ignore records unless the name of their tournament is listed in the file in EUC-JP")
00115 ("kisen-filename,o",
00116 boost::program_options::value<std::string>(&kisen_filename)->
00117 default_value("test.kif"),
00118 "Output filename of Kisen file")
00119 ("input-file", boost::program_options::value< std::vector<std::string> >(),
00120 "input files in kisen format")
00121 ("help", "Show help message");
00122 boost::program_options::variables_map vm;
00123 boost::program_options::positional_options_description p;
00124 p.add("input-file", -1);
00125
00126 try
00127 {
00128 boost::program_options::store(
00129 boost::program_options::command_line_parser(
00130 argc, argv).options(command_line_options).positional(p).run(), vm);
00131 boost::program_options::notify(vm);
00132 if (vm.count("help"))
00133 {
00134 std::cerr << "Usage: " << argv[0] << " [options] csa-files | ki2-files \n";
00135 std::cerr << " " << argv[0] << " [options]\n";
00136 std::cout << command_line_options << std::endl;
00137 return 0;
00138 }
00139 }
00140 catch (std::exception &e)
00141 {
00142 std::cerr << "error in parsing options" << std::endl
00143 << e.what() << std::endl;
00144 std::cerr << "Usage: " << argv[0] << " [options] csa-files | ki2-files\n";
00145 std::cerr << " " << argv[0] << " [options]\n";
00146 std::cerr << command_line_options << std::endl;
00147 return 1;
00148 }
00149
00150 if (tournament_filename != "")
00151 {
00152 std::ifstream is(tournament_filename.c_str());
00153 std::string name;
00154 while(std::getline(is, name))
00155 {
00156 boost::algorithm::trim(name);
00157 good_tournaments.push_back(name);
00158 }
00159 }
00160
00161 std::vector<std::string> files;
00162 if (vm.count("input-file"))
00163 {
00164 const std::vector<std::string> temp = vm["input-file"].as<std::vector<std::string> >();
00165 files.insert(files.end(), temp.begin(), temp.end());
00166 }
00167 else
00168 {
00169 std::string line;
00170 while(std::getline(std::cin , line))
00171 {
00172 boost::algorithm::trim(line);
00173 files.push_back(line);
00174 }
00175 }
00176
00177 osl::record::CheckDuplicate check_duplicate;
00178 convert(kisen_filename, files, output_ipx, check_duplicate);
00179
00180 std::locale::global(std::locale(""));
00181 check_duplicate.print(std::cout);
00182
00183 return 0;
00184 }
00185
00186
00187
00188