READLINE.CPP 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. #include "function.h"
  15. #include <ctype.h>
  16. #include <string.h>
  17. #include "wwfile.h"
  18. #include "xstraw.h"
  19. #include "readline.h"
  20. // Disable the "temporary object used to initialize a non-constant reference" warning.
  21. //#pragma warning 665 9
  22. void strtrimcpp(char * buffer)
  23. {
  24. if (buffer) {
  25. /*
  26. ** Strip leading white space from the string.
  27. */
  28. char * source = buffer;
  29. while (isspace(*source)) {
  30. source++;
  31. }
  32. if (source != buffer) {
  33. strcpy(buffer, source);
  34. }
  35. /*
  36. ** Clip trailing white space from the string.
  37. */
  38. for (int index = strlen(buffer)-1; index >= 0; index--) {
  39. if (isspace(buffer[index])) {
  40. buffer[index] = '\0';
  41. } else {
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. int Read_Line(FileClass & file, char * buffer, int len, bool & eof)
  48. {
  49. return(Read_Line(FileStraw(file), buffer, len, eof));
  50. }
  51. int Read_Line(Straw & file, char * buffer, int len, bool & eof)
  52. {
  53. if (len == 0 || buffer == NULL) return(0);
  54. int count = 0;
  55. for (;;) {
  56. char c;
  57. if (file.Get(&c, sizeof(c)) != sizeof(c)) {
  58. eof = true;
  59. buffer[0] = '\0';
  60. break;
  61. }
  62. if (c == '\x0A') break;
  63. if (c != '\x0D' && count+1 < len) {
  64. buffer[count++] = c;
  65. }
  66. }
  67. buffer[count] = '\0';
  68. strtrimcpp(buffer);
  69. return(strlen(buffer));
  70. }