/* * This file part of makedict - convertor from any dictionary format to any * http://sdcv.sourceforge.net * Copyright (C) 2005 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 #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #include "utils.hpp" #include "generator.hpp" class stardict_generator : public makedict_generator { public: stardict_generator() { set_format("stardict"); set_version(_("stardict_generator, version 0.2")); cur_off=0; keys_list.reserve(20000); } ~stardict_generator() { for (vector::iterator p=keys_list.begin(); p!=keys_list.end(); ++p) delete []p->value; } protected: int generate(const string& basename, const string& workdir); void on_have_data(); private: ofstream idx_file; fstream tmp_dict_file; guint32 cur_off; string full_name; struct Key { char *value; guint32 data_off; guint32 data_size; Key(char *val=NULL, guint32 off=0, guint32 size=0) : value(val), data_off(off), data_size(size) { } Key(const string& val, guint32 off=0, guint32 size=0) : value(NULL), data_off(off), data_size(size) { value = new char [val.length()+1]; strcpy(value, val.c_str()); } }; vector keys_list; static gint stardict_strcmp(const gchar *s1, const gchar *s2) { gint a = g_ascii_strcasecmp(s1, s2); if (a == 0) return strcmp(s1, s2); return a; } class stardict_less { public: bool operator()(const Key & lh, const Key & rh) { return stardict_strcmp(lh.value, rh.value)<0; } }; bool create_ifo_file(const string& basename, guint32 wordcount, guint32 idx_file_size); string get_current_date(void); }; int stardict_generator::generate(const string& bname, const string& workdir) { int res=EXIT_FAILURE; string basename="stardict-"+bname+"-2.4.2"; string dirname=workdir+G_DIR_SEPARATOR+basename; if (!make_directory(dirname)) return res; string realbasename = dirname+G_DIR_SEPARATOR+bname; string file_name=realbasename+".dict.tmp"; tmp_dict_file.open(file_name.c_str(), ios::binary|ios::out|ios::trunc|ios::in); if (!tmp_dict_file) { cerr<<_("Can not open/create: ")< buf; guint32 tmpguint32=0; data_size=0; for (;;) { tmp_dict_file.seekg(pitem->data_off); buf.resize(pitem->data_size+1); tmp_dict_file.read(&buf[0], pitem->data_size); buf[pitem->data_size]='\0'; if (!g_utf8_validate(&buf[0], gssize(-1), NULL)) { cerr<<_("Not valid UTF-8 string: ")<data_size)) { cerr<<_("Can not write in data file\n"); return res; } data_size+=pitem->data_size; prev_item_value=pitem->value; ++i; pitem = &keys_list[i]; if (i::iterator p=keys.begin(); p!=keys.end(); ++p) keys_list.push_back(Key(*p, cur_off, data.length())); tmp_dict_file.write(&data[0], data.length()); cur_off+=data.length(); } bool stardict_generator::create_ifo_file(const string& basename, guint32 wordcount, guint32 idx_file_size) { string filename=basename+".ifo"; std::ofstream f(filename.c_str(), std::ios::binary|std::ios::out); if (!f) { cerr<<_("Can not create/open: ")<