READLINE.CPP 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. ** Command & Conquer Red Alert(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. #include <ctype.h>
  19. #include <string.h>
  20. #include "wwfile.h"
  21. #include "xstraw.h"
  22. #include "readline.h"
  23. // Disable the "temporary object used to initialize a non-constant reference" warning.
  24. #pragma warning 665 9
  25. void strtrim(char * buffer)
  26. {
  27. if (buffer) {
  28. /*
  29. ** Strip leading white space from the string.
  30. */
  31. char * source = buffer;
  32. while (isspace(*source)) {
  33. source++;
  34. }
  35. if (source != buffer) {
  36. strcpy(buffer, source);
  37. }
  38. /*
  39. ** Clip trailing white space from the string.
  40. */
  41. for (int index = strlen(buffer)-1; index >= 0; index--) {
  42. if (isspace(buffer[index])) {
  43. buffer[index] = '\0';
  44. } else {
  45. break;
  46. }
  47. }
  48. }
  49. }
  50. int Read_Line(FileClass & file, char * buffer, int len, bool & eof)
  51. {
  52. return(Read_Line(FileStraw(file), buffer, len, eof));
  53. }
  54. int Read_Line(Straw & file, char * buffer, int len, bool & eof)
  55. {
  56. if (len == 0 || buffer == NULL) return(0);
  57. int count = 0;
  58. for (;;) {
  59. char c;
  60. if (file.Get(&c, sizeof(c)) != sizeof(c)) {
  61. eof = true;
  62. buffer[0] = '\0';
  63. break;
  64. }
  65. if (c == '\x0A') break;
  66. if (c != '\x0D' && count+1 < len) {
  67. buffer[count++] = c;
  68. }
  69. }
  70. buffer[count] = '\0';
  71. strtrim(buffer);
  72. return(strlen(buffer));
  73. }