1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/**
** Textpack.cc - Convert text file to a Flex file.
**
** Written: 2/14/2002
**/
/*
* Copyright (C) 2002-2022 The Exult Team
*
* 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.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "Flex.h"
#include "databuf.h"
#include "exceptions.h"
#include "msgfile.h"
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::exit;
using std::ifstream;
using std::istream;
using std::ofstream;
using std::ostream;
using std::size_t;
using std::string;
using std::vector;
/*
* Read in a Flex file where each entry is a 0-delimited text string.
*/
static void Read_flex(
const char* filename, // File to read.
vector<string>& strings // Strings are stored here.
) {
FlexFile in(filename); // May throw exception.
const int cnt = in.number_of_objects();
strings.resize(cnt);
for (int i = 0; i < cnt; i++) {
size_t len;
auto ptr = in.retrieve(i, len);
if (len) { // Not empty?
strings[i] = reinterpret_cast<char*>(ptr.get());
}
}
}
/*
* Write out a Flex file where each entry is a 0-delimited text string.
*/
static void Write_flex(
const char* filename, // File to write.
const char* title, // For the header.
vector<string>& strings // Okay if some are null.
) {
OFileDataSource out(filename); // May throw exception.
Flex_writer writer(out, title, strings.size());
for (auto& str : strings) {
if (!str.empty()) {
writer.write_object(str.c_str(), str.size() + 1);
} else {
writer.empty_object();
}
}
}
/*
* Write out text, with each line in the form "nnn:sssss", where nnn is
* the Flex entry #, and anything after the ':' is the string.
* NOTES: Null entry #'s will be skipped in the output.
* Max. text length is 1024.
*/
static void Write_text(
ostream& out,
vector<string>& strings // Strings to write.
) {
out << "# Written by Exult Textpack tool" << endl;
const int cnt = strings.size();
for (int i = 0; i < cnt; i++) {
const string& text = strings[i];
if (text.empty()) {
continue;
}
if (text.size() + 1 > 1024) {
cerr << "Text in entry " << i << " is too long" << endl;
exit(1);
}
out << i << ':' << text << endl;
}
out.flush();
}
/*
* Print usage and exit.
*/
static void Usage() {
cerr << "Usage: textpack -[x|c] flexfile [textfile]" << endl
<< " Missing [textfile] => stdin/stdout" << endl;
exit(1);
}
/*
* Create or extract from Flex files consisting of text entries.
*/
int main(int argc, char** argv) {
if (argc < 3 || argv[1][0] != '-') {
Usage(); // (Exits.)
}
char* flexname = argv[2];
vector<string> strings; // Text stored here.
switch (argv[1][1]) { // Which function?
case 'c': // Create Flex.
if (argc >= 4) { // Text filename given?
// Open as text.
IFileDataSource fin(argv[3], true);
if (!fin.good()) {
cerr << "Failed to open " << argv[3] << endl;
exit(1);
}
Text_msg_file_reader reader(fin);
reader.get_global_section_strings(strings);
if (strings.empty()) {
exit(1);
}
} else {
// Default to stdin.
IStreamDataSource fin(&cin);
Text_msg_file_reader reader(fin);
reader.get_global_section_strings(strings);
if (strings.empty()) {
exit(1);
}
}
try {
Write_flex(flexname, "Flex created by Exult", strings);
} catch (exult_exception& e) {
cerr << e.what() << endl;
exit(1);
}
break;
case 'x': // Extract to text.
try {
Read_flex(flexname, strings);
} catch (exult_exception& e) {
cerr << e.what() << endl;
exit(1);
}
if (argc >= 4) { // Text file given?
std::unique_ptr<std::ostream> pOut;
try {
pOut = U7open_out(argv[3], true);
} catch (exult_exception& e) {
cerr << e.what() << endl;
exit(1);
}
if (!pOut) {
cerr << "Failed to open " << argv[3] << endl;
exit(1);
}
auto& out = *pOut;
Write_text(out, strings);
} else {
Write_text(cout, strings);
}
break;
default:
Usage();
}
return 0;
}
|