readline.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/readline.cpp $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 6/14/01 7:25p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * strtrim -- Trim leading and trailing white space off of string. *
  35. * Read_Line -- Read a text line from the file. *
  36. * Read_Line -- Reads a text line from the straw object specified. *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "always.h"
  39. #include "readline.h"
  40. #include "trim.h"
  41. #include "wwfile.h"
  42. #include "xstraw.h"
  43. //#include <ctype.h>
  44. #include <string.h>
  45. #if !defined(__BORLANDC__) && !defined(_MSC_VER)
  46. // Disable the "temporary object used to initialize a non-constant reference" warning.
  47. #pragma warning 665 9
  48. #endif
  49. /***********************************************************************************************
  50. * Read_Line -- Read a text line from the file. *
  51. * *
  52. * This will read a line of text from the file specified. *
  53. * *
  54. * INPUT: file -- Reference to the file to read the line from. *
  55. * *
  56. * buffer -- Pointer to the location to store the line of text. *
  57. * *
  58. * len -- The length of the buffer to store the line. *
  59. * *
  60. * eof -- Reference to the EOF flag that will be set to true if the source *
  61. * file has been exhausted. *
  62. * *
  63. * OUTPUT: Returns with the number of characters stored into the buffer. *
  64. * *
  65. * WARNINGS: none *
  66. * *
  67. * HISTORY: *
  68. * 02/06/1997 JLB : Created. *
  69. *=============================================================================================*/
  70. int Read_Line(FileClass & file, char * buffer, int len, bool & eof)
  71. {
  72. FileStraw fs(file);
  73. return(Read_Line(fs, buffer, len, eof));
  74. }
  75. /***********************************************************************************************
  76. * Read_Line -- Reads a text line from the straw object specified. *
  77. * *
  78. * This reads a line of text from the straw object. *
  79. * *
  80. * INPUT: file -- Reference to the straw object to fetch the line of text from. *
  81. * *
  82. * buffer -- Pointer to the buffer that will hold the line of text. *
  83. * *
  84. * len -- The size of the buffer. *
  85. * *
  86. * eof -- Reference to the EOF flag that will be set to true if the source *
  87. * straw data has been exhausted. *
  88. * *
  89. * OUTPUT: Returns with the number of characters stored into the buffer. *
  90. * *
  91. * WARNINGS: none *
  92. * *
  93. * HISTORY: *
  94. * 02/06/1997 JLB : Created. *
  95. *=============================================================================================*/
  96. int Read_Line(Straw & file, char * buffer, int len, bool & eof)
  97. {
  98. if (len == 0 || buffer == NULL) return(0);
  99. int count = 0;
  100. for (;;) {
  101. char c;
  102. if (file.Get(&c, sizeof(c)) != sizeof(c)) {
  103. eof = true;
  104. buffer[count] = '\0';
  105. break;
  106. }
  107. if (c == '\x0A') break;
  108. if (c != '\x0D' && count+1 < len) {
  109. buffer[count++] = c;
  110. }
  111. }
  112. buffer[count] = '\0';
  113. strtrim(buffer);
  114. return(strlen(buffer));
  115. }
  116. int Read_Line(Straw & file, wchar_t * buffer, int len, bool & eof)
  117. {
  118. if (len == 0 || buffer == NULL) return(0);
  119. int count = 0;
  120. for (;;) {
  121. wchar_t c;
  122. if (file.Get(&c, sizeof(c)) != sizeof(c)) {
  123. eof = true;
  124. buffer[count] = L'\0';
  125. break;
  126. }
  127. if (c == L'\x0A') break;
  128. if (c != L'\x0D' && count+1 < len) {
  129. buffer[count++] = c;
  130. }
  131. }
  132. buffer[count] = '\0';
  133. wcstrim(buffer);
  134. return(wcslen(buffer));
  135. }