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
|
/**
** Msgfile.cc - Read in text message file.
**
** Written: 6/25/03
**/
/*
* Copyright (C) 2002-2003 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.
*/
#include <iostream>
#include <vector>
#include "utils.h"
using std::istream;
using std::cerr;
using std::endl;
using std::vector;
/*
* Read in text, where each line is of the form "nnn:sssss", where nnn is
* to be the Flex entry #, and anything after the ':' is the string to
* store.
* NOTES: Entry #'s may be skipped, and may be given in hex (0xnnn)
* or decimal.
* Max. text length is 1024.
* A line beginning with a '#' is a comment.
* Output: # of first message (i.e., lowest-numbered msg), or -1 if
* error.
*/
int Read_text_msg_file
(
istream& in,
vector<char *>& strings // Strings returned here, each
// allocated on heap.
)
{
strings.resize(0); // Initialize.
strings.reserve(2000);
char buf[1024];
int linenum = 0;
#define NONEFOUND 0xffffffff
unsigned long first = NONEFOUND;// Index of first one found.
while (!in.eof())
{
++linenum;
in.get(buf, sizeof(buf));
char delim; // Check for end-of-line.
in.get(delim);
if (delim != '\n' && !in.eof())
{
cerr << "Line #" << linenum << " is too long" << endl;
return -1;
}
if (!buf[0])
continue; // Empty line.
char *ptr = &buf[0];
char *endptr; // Get line# in decimal, hex, or oct.
long index = strtol(ptr, &endptr, 0);
if (endptr == ptr) // No #?
{
if (*ptr == '#')
continue;
cerr << "Line " << linenum <<
" doesn't start with a number" << endl;
return -1;
}
if (*endptr != ':')
{
cerr << "Missing ':' in line " << linenum <<
". Ignoring line" << endl;
continue;
}
if (index >= strings.size())
strings.resize(index + 1);
strings[index] = newstrdup(endptr + 1);
if (index < first)
first = index;
}
return first == NONEFOUND ? -1 : (int) first;
}
|