/* * subtitle editor * * http://kitone.free.fr/subtitleeditor/ * * Copyright @ 2005-2006, kitone * * Contact: kitone at free dot fr * * 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 * 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 * * See gpl.txt for more information regarding the GNU General Public License. * * * \file * \brief * \author kitone (kitone at free dot fr) */ #include "SubtitleMPsub.h" #include #include #include #include "utility.h" #include "RegEx.h" #include /* * */ Glib::ustring SubtitleMPsub::get_name() { return "MPsub"; } /* * */ Glib::ustring SubtitleMPsub::get_extension() { return "sub"; } /* * */ bool SubtitleMPsub::check(const std::string &line) { static RegEx ex("^FORMAT=(TIME|[0-9])"); return ex.exec(line); } /* * */ SubtitleMPsub::SubtitleMPsub(Document* doc) :SubtitleFormat(doc) { se_debug(SE_DEBUG_LOADER | SE_DEBUG_SAVER); } /* * */ SubtitleMPsub::~SubtitleMPsub() { se_debug(SE_DEBUG_LOADER | SE_DEBUG_SAVER); } /* * */ bool SubtitleMPsub::open(const Glib::ustring &filename, const Glib::ustring &encoding) { se_debug(SE_DEBUG_LOADER); SubtitleFormat::open(filename, encoding); std::ifstream file(filename.c_str()); //file.imbue(std::locale("C")); if(!file) { throw SubtitleException("SubtitleMPsub", _("I can't open this file.")); } bool use_time; int d = 0; //FORMAT=%d float multiplier = 1.; SubtitleTime old_end; std::string start, end; pcrecpp::RE ex("^(-?\\d+(?:\\.\\d+)?) (-?\\d+(?:\\.\\d+)?)\\s*$", pcrecpp::RE_Options(PCRE_UTF8|PCRE_MULTILINE)); std::string line; while(!file.eof() && std::getline(file, line)) { if(ex.FullMatch(line, &start, &end)) { // recupere le temps float fs = 0, fe = 0; //start, end from_string(start, fs); from_string(end, fe); // calcul le temps par rapport au sous-titre precedent SubtitleTime start_time = old_end + SubtitleTime((long int)(fs * multiplier)); SubtitleTime end_time = start_time + SubtitleTime((long int)(fe * multiplier)); // recupere le texte bool count = false; Glib::ustring text; while(std::getline(file, line)) { line = check_end_char(charset_to_utf8(line)); if(line.empty()) break; else { if(count) text += get_newline(); text += line; count = true; } } // ajoute le sous-titre SubtitleModifier subtitle(m_subtitleModel->append()); subtitle.set_start_and_end( start_time, end_time); subtitle.set_text(text); // pour le prochain sous-titre old_end = end_time; } else if(std::sscanf(line.c_str(), "FORMAT=%d", &d) == 1) { multiplier = d; } else if(line.find("FORMAT=TIME") != std::string::npos) { use_time = true; multiplier = 1000; } } file.close(); return true; } /* * */ bool SubtitleMPsub::save(const Glib::ustring &filename, const Glib::ustring &encoding) { se_debug(SE_DEBUG_SAVER); SubtitleFormat::save(filename, encoding); std::ofstream file(filename.c_str()); if(!file) { throw SubtitleException("SubtitleMPsub", _("I can't open this file.")); } file << "FORMAT=TIME" << std::endl; file << utf8_to_charset("# This script was created by subtitleeditor ") << utf8_to_charset(VERSION) << std::endl; file << utf8_to_charset("# http://kitone.free.fr/subtitleeditor/") << std::endl << std::endl; Glib::ustring text; SubtitleTime old_end; Gtk::TreeNodeChildren rows = m_subtitleModel->children(); for(Gtk::TreeIter it = rows.begin(); it; ++it) { SubtitleModifier subtitle(it); SubtitleTime start = subtitle.get_start(); SubtitleTime end = subtitle.get_end(); text = subtitle.get_text(); // pas besion on utilise déjà \n //newline_to_characters(text, "\n"); double s = (double)((start - old_end).totalmsecs) * 0.001; double e = (double)((end - start).totalmsecs) * .001; file << s << " " << e << std::endl; file << utf8_to_charset(text) << std::endl; file << std::endl; old_end = end; } file.close(); return true; }