/* * This file part of makedict - convertor from any dictionary format to any * http://xdxf.sourceforge.net * Copyright (C) 2005-2006 Evgeniy * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #ifdef HAVE_LOCALE_H # include #endif #include #include #include #include #include #include #include #include #include #include using std::string; #include "utils.hpp" //#define DEBUG typedef std::map str_map_t; class makedict { public: int run(int argc, char *argv[]); private: str_map_t input_codecs; str_map_t output_codecs; string parser_options; string workdir; string input_format, output_format; int convert(const char *arg); const string& find_input_codec(const string& url); bool fill_codecs_table(const string& prgname, const string& dirname=""); }; static void unknown_input_format(const str_map_t& input_codecs, const string& format=""); static void unknown_output_format(const str_map_t& output_codecs, const string& format=""); #if 0 static void copy_file(const string& from, const string& to); #endif static long width_of_first(const str_map_t &sm); int makedict::run(int argc, char *argv[]) { #ifdef HAVE_LOCALE_H setlocale(LC_ALL, ""); #endif const gchar *dir=g_getenv("MAKEDICT_PLUGIN_DIR"); if (!fill_codecs_table(argv[0], dir ? dir : "")) return EXIT_FAILURE; #ifdef DEBUG for (str_map_t::iterator p=input_codecs.begin(); p!=input_codecs.end(); ++p) std::cout<first<<" "<second<first<<" "<second<first<<" "<<_("input codec: ")<second<first<<" "<<_("output codec: ")<second<0 && g_ascii_isspace(std_output[len-1])) --len; std_output[len]='\0'; return true; } return false; } bool makedict::fill_codecs_table(const string& prgname, const string& dirname_) { gchar *tmp_dirname=g_path_get_dirname(prgname.c_str()); string dirname(dirname_.empty() ? tmp_dirname : dirname_); g_free(tmp_dirname); GError *err; GDir *dir = g_dir_open(dirname.c_str(), 0, &err); if (dir==NULL) { std::cerr<message<first<first<first.length())) res=it->first.length(); return res; } const string& makedict::find_input_codec(const string &url) { if (!input_format.empty()) return input_codecs[input_format]; for (str_map_t::iterator it=input_codecs.begin(); it!=input_codecs.end(); ++it) { int status=system((it->second+" --is-your-format '"+url+"'").c_str()); if (status==-1) { std::cout<<_("system function failed: ") <second; } unknown_input_format(input_codecs, url); exit(EXIT_FAILURE); return input_format;//never reached } int makedict::convert(const char *arg) { if (!arg) return EXIT_FAILURE; string basename(arg); string::size_type pos=basename.rfind(G_DIR_SEPARATOR); string dirname; if (pos!=string::npos) { dirname.assign(basename, 0, pos); basename.erase(0, pos+1); } else dirname="."; string cur_workdir; if (workdir.empty()) cur_workdir=dirname; else cur_workdir=workdir; if ((pos=basename.rfind('.'))!=string::npos) basename.erase(pos, basename.length()-pos); string input_cmd("'"+find_input_codec(arg)+ "' '"+arg+"'"+parser_options); string output_cmd("'"+output_codecs[output_format]+"' --basename '"+ basename+"' --work-dir '"+cur_workdir+"'"); string cmd; #if 0 if (input_format=="xdxf") cmd=output_cmd+" < '"+arg+"'"; if (output_format=="xdxf") { string dirname(cur_workdir+G_DIR_SEPARATOR+basename); if (!make_directory(dirname)) return EXIT_FAILURE; cmd=input_cmd+" > '"+dirname+G_DIR_SEPARATOR+"dict.xdxf'"; } if (input_format=="xdxf" && output_format=="xdxf") { copy_file(arg, cur_workdir+G_DIR_SEPARATOR+basename+G_DIR_SEPARATOR+"dict.xdxf"); return EXIT_SUCCESS; } if (input_format!="xdxf") #endif cmd=input_cmd+" | "+output_cmd; std::cerr<