READLINE.CPP (2080B)
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 9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 10 // in the hope that it will be useful, but with permitted additional restrictions 11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 12 // distributed with this program. You should have received a copy of the 13 // GNU General Public License along with permitted additional restrictions 14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection 15 16 17 #include "function.h" 18 #include <ctype.h> 19 #include <string.h> 20 #include "wwfile.h" 21 #include "xstraw.h" 22 #include "readline.h" 23 24 25 // Disable the "temporary object used to initialize a non-constant reference" warning. 26 //#pragma warning 665 9 27 28 29 void strtrimcpp(char * buffer) 30 { 31 if (buffer) { 32 33 /* 34 ** Strip leading white space from the string. 35 */ 36 char * source = buffer; 37 while (isspace(*source)) { 38 source++; 39 } 40 if (source != buffer) { 41 strcpy(buffer, source); 42 } 43 44 /* 45 ** Clip trailing white space from the string. 46 */ 47 for (int index = strlen(buffer)-1; index >= 0; index--) { 48 if (isspace(buffer[index])) { 49 buffer[index] = '\0'; 50 } else { 51 break; 52 } 53 } 54 } 55 } 56 57 58 59 int Read_Line(FileClass & file, char * buffer, int len, bool & eof) 60 { 61 return(Read_Line(FileStraw(file), buffer, len, eof)); 62 } 63 64 65 int Read_Line(Straw & file, char * buffer, int len, bool & eof) 66 { 67 if (len == 0 || buffer == NULL) return(0); 68 69 int count = 0; 70 for (;;) { 71 char c; 72 if (file.Get(&c, sizeof(c)) != sizeof(c)) { 73 eof = true; 74 buffer[0] = '\0'; 75 break; 76 } 77 78 if (c == '\x0A') break; 79 if (c != '\x0D' && count+1 < len) { 80 buffer[count++] = c; 81 } 82 } 83 buffer[count] = '\0'; 84 85 strtrimcpp(buffer); 86 return(strlen(buffer)); 87 }