Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
textfile.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** 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 ***
21 ***********************************************************************************************
22 * *
23 * Project Name : Command & Conquer *
24 * *
25 * $Archive:: /Commando/Code/wwlib/textfile.cpp $*
26 * *
27 * $Author:: Patrick $*
28 * *
29 * $Modtime:: 7/26/01 9:37p $*
30 * *
31 * $Revision:: 4 $*
32 * *
33 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
34
35#include "textfile.h"
36#include "wwstring.h"
37
38
40//
41// TextFileClass
42//
48
49
51//
52// TextFileClass
53//
55TextFileClass::TextFileClass (char const *filename)
56 : RawFileClass (filename)
57{
58 return ;
59}
60
61
63//
64// ~TextFileClass
65//
71
72
74//
75// Read_Line
76//
78bool
80{
81 //
82 // Start with a fresh string
83 //
84// string.Empty ();
85 string="";
86
87 const int BUFFER_SIZE = 64;
88 char buffer[BUFFER_SIZE] = { 0 };
89 bool keep_going = true;
90
91 while (keep_going) {
92
93 //
94 // Read a chunk of characters from the file
95 //
96 int size = Read (buffer, BUFFER_SIZE - 1);
97
98 //
99 // Keep going if we still have more data to
100 // read from the file
101 //
102 keep_going = (size == BUFFER_SIZE - 1);
103 if (size > 0) {
104
105 //
106 // Try to find the linefeed character
107 //
108 for (int index = 0; index < size; index ++) {
109 if (buffer[index] == '\n') {
110
111 //
112 // Terminate the buffer after the linefeed
113 //
114 buffer[index + 1] = 0;
115
116 //
117 // Seek backwards in the file to the position
118 // directly after the linefeed
119 //
120 Seek (-(size - (index + 1)), SEEK_CUR);
121 keep_going = false;
122 break;
123 }
124 }
125
126 //
127 // Concat this buffer to the end of the string
128 //
129 string += buffer;
130 }
131 }
132
133 bool retval = (string.Get_Length () > 0);
134 if (retval) {
135
136 int len = string.Get_Length ();
137 char *raw_string = string.Peek_Buffer ();
138
139 //
140 // Strip the CR\LF or LF from the string
141 //
142 if (len > 1 && raw_string[len - 2] == '\r') {
143 string.Erase (len - 2, 2);
144 //raw_string[len - 2] = 0;
145 } else if (raw_string[len - 1] == '\n') {
146 string.Erase (len - 1, 1);
147 //raw_string[len - 1] = 0;
148 }
149 }
150
151 return retval;
152}
153
154
156//
157// Write_Line
158//
160bool
162{
163 bool retval = false;
164
165 //
166 // Write the line of text out to the file
167 //
168 int len = string.Get_Length ();
169 int size = Write ((const char *)string, len);
170
171 //
172 // Now append a CR\LF pair to the end of the line
173 //
174 if (size == len) {
175 retval = (Write ("\r\n", 2) == 2);
176 }
177
178 return retval;
179}
#define SEEK_CUR
Definition WWFILE.H:56
#define BUFFER_SIZE
Definition Save.cpp:63
virtual int Seek(int pos, int dir=SEEK_CUR)
Definition rawfile.cpp:773
RawFileClass(char const *filename)
Definition rawfile.cpp:246
virtual int Write(void const *buffer, int size)
Definition rawfile.cpp:698
virtual int Read(void *buffer, int size)
Definition rawfile.cpp:614
bool Write_Line(const StringClass &string)
Definition textfile.cpp:161
bool Read_Line(StringClass &string)
Definition textfile.cpp:79
TextFileClass(void)
Definition textfile.cpp:44
virtual ~TextFileClass(void)
Definition textfile.cpp:67
else return(RetVal)