]> granicus.if.org Git - taglib/commitdiff
Fix inconsistent negative seek behavior between Linux and Windows.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Wed, 27 May 2015 02:24:48 +0000 (11:24 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Thu, 18 Jun 2015 17:42:18 +0000 (02:42 +0900)
taglib/toolkit/tfilestream.cpp
tests/test_file.cpp

index 4480c274faa47a070c81eefb2c0bc1bd0e7f7a2d..65f37d52e888afa88b7f616fa20a7494cfc8082d 100644 (file)
@@ -365,13 +365,10 @@ void FileStream::seek(long offset, Position p)
 
   SetLastError(NO_ERROR);
   SetFilePointer(d->file, offset, NULL, whence);
-  if(GetLastError() == ERROR_NEGATIVE_SEEK) {
-    SetLastError(NO_ERROR);
-    SetFilePointer(d->file, 0, NULL, FILE_BEGIN);
-  }
-  if(GetLastError() != NO_ERROR) {
+
+  const int lastError = GetLastError();
+  if(lastError != NO_ERROR && lastError != ERROR_NEGATIVE_SEEK)
     debug("FileStream::seek() -- Failed to set the file pointer.");
-  }
 
 #else
 
@@ -442,7 +439,7 @@ long FileStream::length()
   SetLastError(NO_ERROR);
   const DWORD fileSize = GetFileSize(d->file, NULL);
   if(GetLastError() == NO_ERROR) {
-    return static_cast<ulong>(fileSize);
+    return static_cast<long>(fileSize);
   }
   else {
     debug("FileStream::length() -- Failed to get the file size.");
index b3751a26bbd9c1e8c65e1350b05edd94c28b25fa..d78d5faffa67f065d78f0bea89079fda2b867716 100644 (file)
@@ -40,6 +40,7 @@ class TestFile : public CppUnit::TestFixture
   CPPUNIT_TEST_SUITE(TestFile);
   CPPUNIT_TEST(testFindInSmallFile);
   CPPUNIT_TEST(testRFindInSmallFile);
+  CPPUNIT_TEST(testSeek);
   CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -100,6 +101,30 @@ public:
     }
   }
 
+  void testSeek()
+  {
+    ScopedFileCopy copy("empty", ".ogg");
+    std::string name = copy.fileName();
+
+    PlainFile f(name.c_str());
+    CPPUNIT_ASSERT_EQUAL((long)0, f.tell());
+    CPPUNIT_ASSERT_EQUAL((long)4328, f.length());
+
+    f.seek(100, File::Beginning);
+    CPPUNIT_ASSERT_EQUAL((long)100, f.tell());
+    f.seek(100, File::Current);
+    CPPUNIT_ASSERT_EQUAL((long)200, f.tell());
+    f.seek(-300, File::Current);
+    CPPUNIT_ASSERT_EQUAL((long)200, f.tell());
+
+    f.seek(-100, File::End);
+    CPPUNIT_ASSERT_EQUAL((long)4228, f.tell());
+    f.seek(-100, File::Current);
+    CPPUNIT_ASSERT_EQUAL((long)4128, f.tell());
+    f.seek(300, File::Current);
+    CPPUNIT_ASSERT_EQUAL((long)4428, f.tell());
+  }
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(TestFile);