msgfile.cc

Go to the documentation of this file.
00001 
00007 /*
00008  *  Copyright (C) 2002-2003  The Exult Team
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024 
00025 #include <iostream>
00026 #include <vector>
00027 #include "utils.h"
00028 
00029 using std::istream;
00030 using std::cerr;
00031 using std::endl;
00032 using std::vector;
00033 
00034 /*
00035  *  Read in text, where each line is of the form "nnn:sssss", where nnn is
00036  *  to be the Flex entry #, and anything after the ':' is the string to
00037  *  store.  
00038  *  NOTES:  Entry #'s may be skipped, and may be given in hex (0xnnn)
00039  *      or decimal.
00040  *    Max. text length is 1024.
00041  *    A line beginning with a '#' is a comment.
00042  *  Output: # of first message (i.e., lowest-numbered msg), or -1 if
00043  *    error.
00044  */
00045 
00046 int Read_text_msg_file
00047   (
00048   istream& in,
00049   vector<char *>& strings   // Strings returned here, each
00050           //   allocated on heap.
00051   )
00052   {
00053   strings.resize(0);    // Initialize.
00054   strings.reserve(2000);
00055   char buf[1024];
00056   int linenum = 0;
00057 #define NONEFOUND 0xffffffff
00058   unsigned long first = NONEFOUND;// Index of first one found.
00059   while (!in.eof())
00060     {
00061     ++linenum;
00062     in.get(buf, sizeof(buf));
00063     char delim;   // Check for end-of-line.
00064     in.get(delim);
00065     if (delim != '\n' && !in.eof())
00066       {
00067       cerr << "Line #" << linenum << " is too long" << endl;
00068       return -1;
00069       }
00070     if (!buf[0])
00071       continue; // Empty line.
00072     char *ptr = &buf[0];
00073     char *endptr;   // Get line# in decimal, hex, or oct.
00074     long index = strtol(ptr, &endptr, 0);
00075     if (endptr == ptr)  // No #?
00076       {
00077       if (*ptr == '#')
00078         continue;
00079       cerr << "Line " << linenum <<
00080           " doesn't start with a number" << endl;
00081       return -1;
00082       }
00083     if (*endptr != ':')
00084       {
00085       cerr << "Missing ':' in line " << linenum << 
00086         ".  Ignoring line" << endl;
00087       continue;
00088       }
00089     if (index >= strings.size())
00090       strings.resize(index + 1);
00091     strings[index] = newstrdup(endptr + 1);
00092     if (index < first)
00093       first = index;
00094     }
00095   return first == NONEFOUND ? -1 : (int) first;
00096   }
00097 

Generated on Mon Jul 9 14:42:45 2007 for ExultEngine by  doxygen 1.5.1