Skip to content

Commit

Permalink
Merge pull request #580 from TsudaKageyu/negative-seek
Browse files Browse the repository at this point in the history
Fix inconsistent negative seek behavior between Linux and Windows.
  • Loading branch information
TsudaKageyu committed Aug 6, 2015
2 parents ec3d050 642baca commit 173c58c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
11 changes: 4 additions & 7 deletions taglib/toolkit/tfilestream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.");
Expand Down
25 changes: 25 additions & 0 deletions tests/test_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -100,6 101,30 @@ class TestFile : public CppUnit::TestFixture
}
}

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);
Expand Down

0 comments on commit 173c58c

Please sign in to comment.