File: textpack.cc

package info (click to toggle)
exult 1.2-4
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 9,040 kB
  • ctags: 10,543
  • sloc: cpp: 99,373; sh: 8,333; ansic: 4,659; makefile: 988; yacc: 769; lex: 313; xml: 19
file content (218 lines) | stat: -rw-r--r-- 4,777 bytes parent folder | download | duplicates (8)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
 **	Textpack.cc - Convert text file to a Flex file.
 **
 **	Written: 2/14/2002
 **/

/*
 *  Copyright (C) 2002  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

#ifndef ALPHA_LINUX_CXX
#  include <unistd.h>
#  include <fstream>
#  include <cstdio>
#  include <cstdlib>
#endif
#include <iostream>
#include <vector>
#include "Flex.h"
#include "utils.h"
#include "exceptions.h"
#include "msgfile.h"

using std::atoi;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::exit;
using std::ifstream;
using std::ofstream;
using std::istream;
using std::ostream;
using std::size_t;
using std::strlen;
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<char *>& strings		// Strings are stored here.
	)
	{
	Flex in(filename);		// May throw exception.
	int cnt = in.number_of_objects();
	strings.resize(cnt);
	for (int i = 0; i < cnt; i++)
		{
		size_t len;
		strings[i] = in.retrieve(i, len);
		if (!len)		// Empty?
			{
			delete strings[i];
			strings[i] = 0;
			}
		}
	}	

/*
 *	Write out a Flex file where each entry is a 0-delimited text string.
 */

static void Write_flex
	(
	const char *filename,		// File to write.
	char *title,			// For the header.
	vector<char *>& strings		// Okay if some are null.
	)
	{
	ofstream out;
	U7open(out, filename);		// May throw exception.
	Flex_writer writer(out, title, strings.size());
	for (vector<char *>::const_iterator it = strings.begin();
					it != strings.end(); ++it)
		{
		const char *str = *it;
		if (str)
			out << str;
		out.put((char) 0);	// 0-delimit.
		writer.mark_section_done();
		}
	if (!writer.close())
		throw file_write_exception(filename);
	}

/*
 *	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<char *>& strings		// Strings to write.
	)
	{
	out << "# Written by Exult Textpack tool" << endl;
	int cnt = strings.size();
	for (int i = 0; i < cnt; i++)
		{
		char *text = strings[i];
		if (!text)
			continue;
		if (strlen(text) + 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<char *> strings;		// Text stored here.
	switch (argv[1][1])		// Which function?
		{
	case 'c':			// Create Flex.
		if (argc >= 4)		// Text filename given?
			{
			ifstream in;	// Open as text.
			try {
				U7open(in, argv[3], true);
			} catch (exult_exception& e) {
				cerr << e.what() << endl;
				exit(1);
			}
			if (Read_text_msg_file(in, strings) == -1)
				exit(1);
			}
		else			// Default to stdin.
			if (Read_text_msg_file(cin, strings) == -1)
				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?
			{
			ofstream out;
			try {
				U7open(out, argv[3],  true);
			} catch(exult_exception& e) {
				cerr << e.what() << endl;
				exit(1);
			}
			Write_text(out, strings);
			}
		else
			Write_text(cout, strings);
		break;
	default:
		Usage();
		}
					// Clean up allocated strings.
	for (vector<char *>::iterator it = strings.begin(); 
						it != strings.end(); ++it)
		delete *it;
	return 0;
	}