/* * Copyright (C) 2004-2005 Vadim Berezniker * http://www.kryptolus.com * * 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, 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 GNU Make; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * http://www.gnu.org/copyleft/gpl.html * */ #include "stdafx.h" #include "common.h" #include "sabbu.h" #include "gui_status_bar.h" #include "stringutils.h" #include "kryTextFileReader.h" #include "krySubSSA.h" #include "krySubReader.h" #include "krySubReaderTXT.h" #include "krySubReaderSSA.h" extern struct sabbu app; krySubReaderTXT::krySubReaderTXT(char *filename) : krySubReader(filename) { } /* * Reads script data from the given file. * filename: the file to read * subscript: pointer to the structure where the data will be stored (structure must be created via script_new functin) * * Returns * SCRIPT_OK if the file is successfully read. * SCRIPT_ERROR if a fatal error occured while reading */ enum krySubReader::retval krySubReaderTXT::ReadScript (kryScript *script, kryList *error_list) { kryEventDetailed *line_last = NULL; double prev_progress = 0; int i; char *buffer = NULL; this->m_errors = error_list; this->m_script = script; if(!this->Open()) return krySubReader::SCRIPT_ERROR; while(TRUE) { kryEventDetailed *line; char *text_str; char *name_str; char *name; char *ptr; int len; if(buffer) { kry_free(buffer); buffer = NULL; } buffer = this->GetLine(); if(!buffer) break; len = strlen(buffer); // don't update too often if(this->GetProgress() - prev_progress > 0.05) { gui_status_bar_set_progress(app.ui.status_bar, this->GetProgress()); prev_progress = this->GetProgress(); } if(strlen(buffer) > 0 && buffer[0] == ';') continue; ptr = string_ignore_whitespce(buffer); if(ptr[0] == 0) continue; line = new kryEventDetailed(); if(line_last && strlen(buffer) >= 2 && (buffer[0] == ' ' && buffer[1] == ' ' || buffer[0] == '\t')) { name_str = kry_strdup(line_last->GetName()); text_str = kry_strdup(ptr); } else { name = g_utf8_strchr(ptr, -1, 0xFF1A); if(!name) name = g_utf8_strchr(ptr, -1, ':'); if(name) { text_str = kry_strdup(string_ignore_whitespce(g_utf8_next_char(name))); name_str = (char *) kry_malloc(name - ptr + 1); memcpy(name_str, ptr, name - ptr); name_str[name - ptr] = 0; } else { name_str = kry_strdup(""); text_str = kry_strdup(ptr); } // get rid of extra whitespace at the end of name for(i = name - buffer - 1; i >= 0 && (name_str[i] == ' ' || name_str[i] == '\t'); i--) name_str[i] = 0; } // get rid of extra whitespace at the end of text for(i = strlen(text_str) - 1; i >= 0 && (text_str[i] == ' ' || text_str[i] == '\t'); i--) text_str[i] = 0; if(line_last && name_str[0] == 0) name_str = kry_strdup(line_last->GetName()); line->SetName(name_str); kry_free(name_str); line->SetText(text_str); kry_free(text_str); line->SetEffect(""); this->AddEvent(line); line_last = line; } if(buffer) kry_free(buffer); if(!script->GetStyle("Default")) script->AddDefaultStyle(); this->Close(); return krySubReader::SCRIPT_OK; }