textfile.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ** Command & Conquer Renegade(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. *** 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 ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/textfile.cpp $*
  25. * *
  26. * $Author:: Patrick $*
  27. * *
  28. * $Modtime:: 7/26/01 9:37p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  33. #include "textfile.h"
  34. #include "wwstring.h"
  35. ///////////////////////////////////////////////////////////////////////////////
  36. //
  37. // TextFileClass
  38. //
  39. ///////////////////////////////////////////////////////////////////////////////
  40. TextFileClass::TextFileClass (void)
  41. {
  42. return ;
  43. }
  44. ///////////////////////////////////////////////////////////////////////////////
  45. //
  46. // TextFileClass
  47. //
  48. ///////////////////////////////////////////////////////////////////////////////
  49. TextFileClass::TextFileClass (char const *filename)
  50. : RawFileClass (filename)
  51. {
  52. return ;
  53. }
  54. ///////////////////////////////////////////////////////////////////////////////
  55. //
  56. // ~TextFileClass
  57. //
  58. ///////////////////////////////////////////////////////////////////////////////
  59. TextFileClass::~TextFileClass (void)
  60. {
  61. return ;
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////
  64. //
  65. // Read_Line
  66. //
  67. ///////////////////////////////////////////////////////////////////////////////
  68. bool
  69. TextFileClass::Read_Line (StringClass &string)
  70. {
  71. //
  72. // Start with a fresh string
  73. //
  74. // string.Empty ();
  75. string="";
  76. const int BUFFER_SIZE = 64;
  77. char buffer[BUFFER_SIZE] = { 0 };
  78. bool keep_going = true;
  79. while (keep_going) {
  80. //
  81. // Read a chunk of characters from the file
  82. //
  83. int size = Read (buffer, BUFFER_SIZE - 1);
  84. //
  85. // Keep going if we still have more data to
  86. // read from the file
  87. //
  88. keep_going = (size == BUFFER_SIZE - 1);
  89. if (size > 0) {
  90. //
  91. // Try to find the linefeed character
  92. //
  93. for (int index = 0; index < size; index ++) {
  94. if (buffer[index] == '\n') {
  95. //
  96. // Terminate the buffer after the linefeed
  97. //
  98. buffer[index + 1] = 0;
  99. //
  100. // Seek backwards in the file to the position
  101. // directly after the linefeed
  102. //
  103. Seek (-(size - (index + 1)), SEEK_CUR);
  104. keep_going = false;
  105. break;
  106. }
  107. }
  108. //
  109. // Concat this buffer to the end of the string
  110. //
  111. string += buffer;
  112. }
  113. }
  114. bool retval = (string.Get_Length () > 0);
  115. if (retval) {
  116. int len = string.Get_Length ();
  117. char *raw_string = string.Peek_Buffer ();
  118. //
  119. // Strip the CR\LF or LF from the string
  120. //
  121. if (len > 1 && raw_string[len - 2] == '\r') {
  122. string.Erase (len - 2, 2);
  123. //raw_string[len - 2] = 0;
  124. } else if (raw_string[len - 1] == '\n') {
  125. string.Erase (len - 1, 1);
  126. //raw_string[len - 1] = 0;
  127. }
  128. }
  129. return retval;
  130. }
  131. ///////////////////////////////////////////////////////////////////////////////
  132. //
  133. // Write_Line
  134. //
  135. ///////////////////////////////////////////////////////////////////////////////
  136. bool
  137. TextFileClass::Write_Line (const StringClass &string)
  138. {
  139. bool retval = false;
  140. //
  141. // Write the line of text out to the file
  142. //
  143. int len = string.Get_Length ();
  144. int size = Write ((const char *)string, len);
  145. //
  146. // Now append a CR\LF pair to the end of the line
  147. //
  148. if (size == len) {
  149. retval = (Write ("\r\n", 2) == 2);
  150. }
  151. return retval;
  152. }