]> git.mxchange.org Git - simgear.git/commitdiff
sgstream: detect two directly following CR/LF EOL correctly in skipeol,
authorChristian Schmitt <chris@ilovelinux.de>
Mon, 25 Feb 2013 22:03:32 +0000 (23:03 +0100)
committerChristian Schmitt <chris@ilovelinux.de>
Tue, 26 Feb 2013 10:56:14 +0000 (11:56 +0100)
supply a testcase for this.

simgear/misc/CMakeLists.txt
simgear/misc/sgstream.cxx
simgear/misc/sgstream_test.cxx [new file with mode: 0644]

index 8d7852f105b3e328a3a1a1f4c4c26b879b1b98b4..9c8071374e2a9cca37a69ae6a6b87234a4974265 100644 (file)
@@ -47,6 +47,10 @@ add_executable(test_strings strutils_test.cxx )
 add_test(test_strings ${EXECUTABLE_OUTPUT_PATH}/test_strings)
 target_link_libraries(test_strings ${TEST_LIBS})
 
+add_executable(test_streams sgstream_test.cxx )
+add_test(test_streams ${EXECUTABLE_OUTPUT_PATH}/test_streams)
+target_link_libraries(test_streams ${TEST_LIBS})
+
 add_executable(test_path path_test.cxx )
 add_test(test_path ${EXECUTABLE_OUTPUT_PATH}/test_path)
 target_link_libraries(test_path ${TEST_LIBS})
index 70da33afd1316052ebc9925a699a9fb4946392f6..c2ed93b5869e8bfe43708d9f0f07409220062f11 100644 (file)
@@ -102,14 +102,17 @@ istream&
 skipeol( istream& in )
 {
     char c = '\0';
-    // skip to end of line.
 
+    // make sure we detect LF, CR and CR/LF
     while ( in.get(c) ) {
-       if ( (c == '\n') || (c == '\r') ) {
-           break;
-       }       
+        if (c == '\n')
+            break;
+        else if (c == '\r') {
+            if (in.peek() == '\n')
+                in.get(c);
+            break;
+        }
     }
-
     return in;
 }
 
diff --git a/simgear/misc/sgstream_test.cxx b/simgear/misc/sgstream_test.cxx
new file mode 100644 (file)
index 0000000..0188f99
--- /dev/null
@@ -0,0 +1,51 @@
+#include <iostream>
+#include <fstream>
+
+#include <cstdlib> // for EXIT_FAILURE
+
+using std::ofstream;
+using std::cout;
+using std::endl;
+
+#include <simgear/misc/sgstream.hxx>
+
+int main()
+{
+    const char* fileName = "testfile";
+    {
+       ofstream f;
+       f.open(fileName, std::ios::binary | std::ios::trunc | std::ios::out);
+       f.write("first line ends with line-feed\n"
+           "second line ends with just a cr\r"
+           "third line ends with both\r\n"
+           "fourth line as well\r\n"
+           "fifth line is another CR/LF line\r\n"
+           "end of test\r\n", 1024);
+       f.close();
+   }
+
+   sg_gzifstream sg(fileName);
+   std::string stuff;
+   sg >> skipeol;
+   sg >> stuff;
+   if (stuff != "second") return EXIT_FAILURE;
+   cout << "Detection of LF works." << endl;
+
+   sg >> skipeol;
+   sg >> stuff;
+   if (stuff != "third") return EXIT_FAILURE;
+   cout << "Detection of CR works." << endl;
+
+   sg >> skipeol;
+   sg >> stuff;
+   if (stuff != "fourth") return EXIT_FAILURE;
+   cout << "Detection of CR/LF works." << endl;
+
+   sg >> skipeol;
+   sg >> skipeol;
+   sg >> stuff;
+   if (stuff != "end") return EXIT_FAILURE;
+   cout << "Detection of 2 following CR/LF lines works." << endl;
+
+   return EXIT_SUCCESS;
+}