| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /*
- ** Command & Conquer Renegade(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** 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 3 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, see <http://www.gnu.org/licenses/>.
- */
- /***********************************************************************************************
- *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
- ***********************************************************************************************
- * *
- * Project Name : Command & Conquer *
- * *
- * $Archive:: /Commando/Code/wwlib/textfile.cpp $*
- * *
- * $Author:: Patrick $*
- * *
- * $Modtime:: 7/26/01 9:37p $*
- * *
- * $Revision:: 4 $*
- * *
- * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- #include "textfile.h"
- #include "wwstring.h"
- ///////////////////////////////////////////////////////////////////////////////
- //
- // TextFileClass
- //
- ///////////////////////////////////////////////////////////////////////////////
- TextFileClass::TextFileClass (void)
- {
- return ;
- }
- ///////////////////////////////////////////////////////////////////////////////
- //
- // TextFileClass
- //
- ///////////////////////////////////////////////////////////////////////////////
- TextFileClass::TextFileClass (char const *filename)
- : RawFileClass (filename)
- {
- return ;
- }
- ///////////////////////////////////////////////////////////////////////////////
- //
- // ~TextFileClass
- //
- ///////////////////////////////////////////////////////////////////////////////
- TextFileClass::~TextFileClass (void)
- {
- return ;
- }
- ///////////////////////////////////////////////////////////////////////////////
- //
- // Read_Line
- //
- ///////////////////////////////////////////////////////////////////////////////
- bool
- TextFileClass::Read_Line (StringClass &string)
- {
- //
- // Start with a fresh string
- //
- // string.Empty ();
- string="";
- const int BUFFER_SIZE = 64;
- char buffer[BUFFER_SIZE] = { 0 };
- bool keep_going = true;
- while (keep_going) {
- //
- // Read a chunk of characters from the file
- //
- int size = Read (buffer, BUFFER_SIZE - 1);
- //
- // Keep going if we still have more data to
- // read from the file
- //
- keep_going = (size == BUFFER_SIZE - 1);
- if (size > 0) {
-
- //
- // Try to find the linefeed character
- //
- for (int index = 0; index < size; index ++) {
- if (buffer[index] == '\n') {
-
- //
- // Terminate the buffer after the linefeed
- //
- buffer[index + 1] = 0;
- //
- // Seek backwards in the file to the position
- // directly after the linefeed
- //
- Seek (-(size - (index + 1)), SEEK_CUR);
- keep_going = false;
- break;
- }
- }
-
- //
- // Concat this buffer to the end of the string
- //
- string += buffer;
- }
- }
- bool retval = (string.Get_Length () > 0);
- if (retval) {
-
- int len = string.Get_Length ();
- char *raw_string = string.Peek_Buffer ();
-
- //
- // Strip the CR\LF or LF from the string
- //
- if (len > 1 && raw_string[len - 2] == '\r') {
- string.Erase (len - 2, 2);
- //raw_string[len - 2] = 0;
- } else if (raw_string[len - 1] == '\n') {
- string.Erase (len - 1, 1);
- //raw_string[len - 1] = 0;
- }
- }
-
- return retval;
- }
- ///////////////////////////////////////////////////////////////////////////////
- //
- // Write_Line
- //
- ///////////////////////////////////////////////////////////////////////////////
- bool
- TextFileClass::Write_Line (const StringClass &string)
- {
- bool retval = false;
- //
- // Write the line of text out to the file
- //
- int len = string.Get_Length ();
- int size = Write ((const char *)string, len);
- //
- // Now append a CR\LF pair to the end of the line
- //
- if (size == len) {
- retval = (Write ("\r\n", 2) == 2);
- }
- return retval;
- }
|