TEST(TestFile, Write) { XFILE::CFile *file; const char str[] = "TestFile.Write test string\n"; char buf[30]; memset(&buf, 0, sizeof(buf)); ASSERT_TRUE((file = XBMC_CREATETEMPFILE("")) != NULL); file->Close(); ASSERT_TRUE(file->OpenForWrite(XBMC_TEMPFILEPATH(file), true)); EXPECT_EQ((int)sizeof(str), file->Write(str, sizeof(str))); file->Flush(); EXPECT_EQ(0, file->GetPosition()); file->Close(); ASSERT_TRUE(file->Open(XBMC_TEMPFILEPATH(file))); EXPECT_EQ(0, file->GetPosition()); EXPECT_EQ((int64_t)sizeof(str), file->Seek(0, SEEK_END)); EXPECT_EQ(0, file->Seek(0, SEEK_SET)); EXPECT_EQ((int64_t)sizeof(str), file->GetLength()); EXPECT_EQ(sizeof(str), file->Read(buf, sizeof(buf))); file->Flush(); EXPECT_EQ((int64_t)sizeof(str), file->GetPosition()); EXPECT_TRUE(!memcmp(str, buf, sizeof(str))); file->Close(); EXPECT_TRUE(XBMC_DELETETEMPFILE(file)); }
/* The tests for XFILE::CFileFactory are tested indirectly through * XFILE::CFile. Since most parts of the VFS require some form of * network connection, the settings and VFS URLs must be given as * arguments in the main testsuite program. */ TEST_F(TestFileFactory, Read) { XFILE::CFile file; std::string str; unsigned int size, i; unsigned char buf[16]; int64_t count = 0; std::vector<std::string> urls = CXBMCTestUtils::Instance().getTestFileFactoryReadUrls(); std::vector<std::string>::iterator it; for (it = urls.begin(); it < urls.end(); ++it) { std::cout << "Testing URL: " << *it << std::endl; ASSERT_TRUE(file.Open(*it)); std::cout << "file.GetLength(): " << testing::PrintToString(file.GetLength()) << std::endl; std::cout << "file.Seek(file.GetLength() / 2, SEEK_CUR) return value: " << testing::PrintToString(file.Seek(file.GetLength() / 2, SEEK_CUR)) << std::endl; std::cout << "file.Seek(0, SEEK_END) return value: " << testing::PrintToString(file.Seek(0, SEEK_END)) << std::endl; std::cout << "file.Seek(0, SEEK_SET) return value: " << testing::PrintToString(file.Seek(0, SEEK_SET)) << std::endl; std::cout << "File contents:" << std::endl; while ((size = file.Read(buf, sizeof(buf))) > 0) { str = StringUtils::Format(" %08llX", count); std::cout << str << " "; count += size; for (i = 0; i < size; i++) { str = StringUtils::Format("%02X ", buf[i]); std::cout << str; } while (i++ < sizeof(buf)) std::cout << " "; std::cout << " ["; for (i = 0; i < size; i++) { if (buf[i] >= ' ' && buf[i] <= '~') std::cout << buf[i]; else std::cout << "."; } std::cout << "]" << std::endl; } file.Close(); } }
bool CKaraokeLyricsTextLRC::Load() { XFILE::CFile file; // Clear the lyrics array clearLyrics(); if ( !file.Open( m_lyricsFile ) ) return false; unsigned int lyricSize = (unsigned int) file.GetLength(); if ( !lyricSize ) { CLog::Log( LOGERROR, "LRC lyric loader: lyric file %s has zero length", m_lyricsFile.c_str() ); return false; } // Read the file into memory array std::vector<char> lyricData( lyricSize ); file.Seek( 0, SEEK_SET ); // Read the whole file if ( file.Read( &lyricData[0], lyricSize) != lyricSize ) return false; // disk error? file.Close(); // Parse the correction value int timing_correction = MathUtils::round_int( g_advancedSettings.m_karaokeSyncDelayLRC * 10 ); unsigned int offset = 0; CStdString ext, songfilename = getSongFile(); URIUtils::GetExtension( songfilename, ext ); // Skip windoze UTF8 file prefix, if any, and reject UTF16 files if ( lyricSize > 3 ) { if ( (unsigned char)lyricData[0] == 0xFF && (unsigned char)lyricData[1] == 0xFE ) { CLog::Log( LOGERROR, "LRC lyric loader: lyrics file is in UTF16 encoding, must be in UTF8" ); return false; } // UTF8 prefix added by some windoze apps if ( (unsigned char)lyricData[0] == 0xEF && (unsigned char)lyricData[1] == 0xBB && (unsigned char)lyricData[2] == 0xBF ) offset = 3; } if (checkMultiTime(&lyricData[offset], lyricSize - offset)) return ParserMultiTime(&lyricData[offset], lyricSize - offset, timing_correction); else return ParserNormal(&lyricData[offset], lyricSize - offset, timing_correction); }
static offset_t dvd_file_seek(void *h, offset_t pos, int whence) { if(interrupt_cb(NULL)) return -1; XFILE::CFile *pFile = (XFILE::CFile *)h; if(whence == AVSEEK_SIZE) return pFile->GetLength(); else return pFile->Seek(pos, whence & ~AVSEEK_FORCE); }
TEST_F(TestZipFile, Read) { XFILE::CFile file; char buf[20]; memset(&buf, 0, sizeof(buf)); std::string reffile, strpathinzip; CFileItemList itemlist; reffile = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.zip"); CURL zipUrl = URIUtils::CreateArchivePath("zip", CURL(reffile), ""); ASSERT_TRUE(XFILE::CDirectory::GetDirectory(zipUrl, itemlist, "", XFILE::DIR_FLAG_NO_FILE_DIRS)); EXPECT_GT(itemlist.Size(), 0); EXPECT_FALSE(itemlist[0]->GetPath().empty()); strpathinzip = itemlist[0]->GetPath(); ASSERT_TRUE(file.Open(strpathinzip)); EXPECT_EQ(0, file.GetPosition()); EXPECT_EQ(1616, file.GetLength()); EXPECT_EQ(sizeof(buf), static_cast<size_t>(file.Read(buf, sizeof(buf)))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_TRUE(file.ReadString(buf, sizeof(buf))); EXPECT_EQ(39, file.GetPosition()); EXPECT_STREQ("an award-winning fr", buf); EXPECT_EQ(100, file.Seek(100)); EXPECT_EQ(100, file.GetPosition()); EXPECT_EQ(sizeof(buf), static_cast<size_t>(file.Read(buf, sizeof(buf)))); file.Flush(); EXPECT_EQ(120, file.GetPosition()); EXPECT_TRUE(!memcmp("ent hub for digital ", buf, sizeof(buf) - 1)); EXPECT_EQ(220, file.Seek(100, SEEK_CUR)); EXPECT_EQ(220, file.GetPosition()); EXPECT_EQ(sizeof(buf), static_cast<size_t>(file.Read(buf, sizeof(buf)))); file.Flush(); EXPECT_EQ(240, file.GetPosition()); EXPECT_TRUE(!memcmp("rs, XBMC is a non-pr", buf, sizeof(buf) - 1)); EXPECT_EQ(1596, file.Seek(-(int64_t)sizeof(buf), SEEK_END)); EXPECT_EQ(1596, file.GetPosition()); EXPECT_EQ(sizeof(buf), static_cast<size_t>(file.Read(buf, sizeof(buf)))); file.Flush(); EXPECT_EQ(1616, file.GetPosition()); EXPECT_TRUE(!memcmp("multimedia jukebox.\n", buf, sizeof(buf) - 1)); EXPECT_EQ(-1, file.Seek(100, SEEK_CUR)); EXPECT_EQ(1616, file.GetPosition()); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(sizeof(buf), static_cast<size_t>(file.Read(buf, sizeof(buf)))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(-1, file.Seek(-100, SEEK_SET)); file.Close(); }
//======================================================================== int64_t CFileURLProtocol::SeekEx(URLContext *h, int64_t pos, int whence) { XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data; // seek to the end of file if (whence == AVSEEK_SIZE) pos = cfile->GetLength(); else pos = cfile->Seek(pos, whence & ~AVSEEK_FORCE); printf("CFileURLProtocol::SeekEx pos(%lld), whence(%d)\n", pos, whence); return pos; }
TEST(TestRarFile, Read) { XFILE::CFile file; char buf[20]; memset(&buf, 0, sizeof(buf)); CStdString reffile, strrarpath, strpathinrar; CFileItemList itemlist; reffile = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.rar"); URIUtils::CreateArchivePath(strrarpath, "rar", reffile, ""); ASSERT_TRUE(XFILE::CDirectory::GetDirectory(strrarpath, itemlist, "", XFILE::DIR_FLAG_NO_FILE_DIRS)); strpathinrar = itemlist[0]->GetPath(); ASSERT_TRUE(file.Open(strpathinrar)); EXPECT_EQ(0, file.GetPosition()); EXPECT_EQ(1616, file.GetLength()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_TRUE(file.ReadString(buf, sizeof(buf))); EXPECT_EQ(39, file.GetPosition()); EXPECT_STREQ("an award-winning fr", buf); EXPECT_EQ(100, file.Seek(100)); EXPECT_EQ(100, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(120, file.GetPosition()); EXPECT_TRUE(!memcmp("ent hub for digital ", buf, sizeof(buf) - 1)); EXPECT_EQ(220, file.Seek(100, SEEK_CUR)); EXPECT_EQ(220, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(240, file.GetPosition()); EXPECT_TRUE(!memcmp("rs, XBMC is a non-pr", buf, sizeof(buf) - 1)); EXPECT_EQ(1596, file.Seek(-(int64_t)sizeof(buf), SEEK_END)); EXPECT_EQ(1596, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(1616, file.GetPosition()); EXPECT_TRUE(!memcmp("multimedia jukebox.\n", buf, sizeof(buf) - 1)); EXPECT_EQ(1716, file.Seek(100, SEEK_CUR)); EXPECT_EQ(1716, file.GetPosition()); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(-1, file.Seek(-100, SEEK_SET)); file.Close(); }
//======================================================================== int64_t CFileURLProtocol::SeekEx(AML_URLContext *h, int64_t pos, int whence) { //CLog::Log(LOGDEBUG, "CFileURLProtocol::SeekEx1 pos(%lld), whence(%d)", pos, whence); XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data; whence &= ~AVSEEK_FORCE; // seek to the end of file if (pos == -1 || whence == AVSEEK_SIZE) pos = cfile->GetLength(); else pos = cfile->Seek(pos, whence); //CLog::Log(LOGDEBUG, "CFileURLProtocol::SeekEx2 pos(%lld), whence(%d)", pos, whence); return pos; }
TEST(TestFile, Read) { XFILE::CFile file; char buf[20]; memset(&buf, 0, sizeof(buf)); ASSERT_TRUE(file.Open( XBMC_REF_FILE_PATH("/xbmc/filesystem/test/reffile.txt"))); EXPECT_EQ(0, file.GetPosition()); EXPECT_EQ(1616, file.GetLength()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_TRUE(file.ReadString(buf, sizeof(buf))); EXPECT_EQ(39, file.GetPosition()); EXPECT_STREQ("an award-winning fr", buf); EXPECT_EQ(100, file.Seek(100)); EXPECT_EQ(100, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(120, file.GetPosition()); EXPECT_TRUE(!memcmp("ent hub for digital ", buf, sizeof(buf) - 1)); EXPECT_EQ(220, file.Seek(100, SEEK_CUR)); EXPECT_EQ(220, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(240, file.GetPosition()); EXPECT_TRUE(!memcmp("rs, XBMC is a non-pr", buf, sizeof(buf) - 1)); EXPECT_EQ(1596, file.Seek(-sizeof(buf), SEEK_END)); EXPECT_EQ(1596, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(1616, file.GetPosition()); EXPECT_TRUE(!memcmp("multimedia jukebox.\n", buf, sizeof(buf) - 1)); EXPECT_EQ(1716, file.Seek(100, SEEK_CUR)); EXPECT_EQ(1716, file.GetPosition()); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(-1, file.Seek(-100, SEEK_SET)); file.Close(); }
bool CKaraokeLyricsTextKAR::Load() { XFILE::CFile file; bool succeed = true; m_reportedInvalidVarField = false; // Clear the lyrics array clearLyrics(); if ( !file.Open( m_midiFile ) ) return false; m_midiSize = (unsigned int) file.GetLength(); if ( !m_midiSize ) return false; // shouldn't happen, but file.Seek( 0, SEEK_SET ); m_midiData = new unsigned char [ m_midiSize ]; // Read the whole file if ( !m_midiData || file.Read( m_midiData, m_midiSize) != m_midiSize ) return false; file.Close(); // Parse MIDI try { parseMIDI(); } catch ( const char * p ) { CLog::Log( LOGERROR, "KAR lyrics loader: cannot load file: %s", p ); succeed = false; } delete [] m_midiData; m_midiData = 0; return succeed; }
/* Test case to test for graceful handling of corrupted input. * NOTE: The test case is considered a "success" as long as the corrupted * file was successfully generated and the test case runs without a segfault. */ TEST(TestRarFile, CorruptedFile) { XFILE::CFile *file; char buf[16]; memset(&buf, 0, sizeof(buf)); CStdString reffilepath, strrarpath, strpathinrar, str; CFileItemList itemlist; unsigned int size, i; int64_t count = 0; reffilepath = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.rar"); ASSERT_TRUE((file = XBMC_CREATECORRUPTEDFILE(reffilepath, ".rar")) != NULL); std::cout << "Reference file generated at '" << XBMC_TEMPFILEPATH(file) << "'\n"; URIUtils::CreateArchivePath(strrarpath, "rar", XBMC_TEMPFILEPATH(file), ""); if (!XFILE::CDirectory::GetDirectory(strrarpath, itemlist, "", XFILE::DIR_FLAG_NO_FILE_DIRS)) { XBMC_DELETETEMPFILE(file); SUCCEED(); return; } if (itemlist.IsEmpty()) { XBMC_DELETETEMPFILE(file); SUCCEED(); return; } strpathinrar = itemlist[0]->GetPath(); if (!file->Open(strpathinrar)) { XBMC_DELETETEMPFILE(file); SUCCEED(); return; } std::cout << "file->GetLength(): " << testing::PrintToString(file->GetLength()) << "\n"; std::cout << "file->Seek(file->GetLength() / 2, SEEK_CUR) return value: " << testing::PrintToString(file->Seek(file->GetLength() / 2, SEEK_CUR)) << "\n"; std::cout << "file->Seek(0, SEEK_END) return value: " << testing::PrintToString(file->Seek(0, SEEK_END)) << "\n"; std::cout << "file->Seek(0, SEEK_SET) return value: " << testing::PrintToString(file->Seek(0, SEEK_SET)) << "\n"; std::cout << "File contents:\n"; while ((size = file->Read(buf, sizeof(buf))) > 0) { str.Format(" %08X", count); std::cout << str << " "; count += size; for (i = 0; i < size; i++) { str.Format("%02X ", buf[i]); std::cout << str; } while (i++ < sizeof(buf)) std::cout << " "; std::cout << " ["; for (i = 0; i < size; i++) { if (buf[i] >= ' ' && buf[i] <= '~') std::cout << buf[i]; else std::cout << "."; } std::cout << "]\n"; } file->Close(); XBMC_DELETETEMPFILE(file); }
bool CGUISound::LoadWav(const CStdString& strFile, WAVEFORMATEX* wfx, LPBYTE* ppWavData, int* pDataSize) { XFILE::CFile file; if (!file.Open(strFile)) return false; // read header WAVE_RIFFHEADER riffh; file.Read(&riffh, sizeof(WAVE_RIFFHEADER)); // file valid? if (strncmp(riffh.riff, "RIFF", 4)!=0 && strncmp(riffh.rifftype, "WAVE", 4)!=0) { file.Close(); return false; } long offset=0; offset += sizeof(WAVE_RIFFHEADER); offset -= sizeof(WAVE_CHUNK); // parse chunks do { WAVE_CHUNK chunk; // always seeking to the start of a chunk file.Seek(offset + sizeof(WAVE_CHUNK), SEEK_SET); file.Read(&chunk, sizeof(WAVE_CHUNK)); if (!strncmp(chunk.chunk_id, "fmt ", 4)) { // format chunk memset(wfx, 0, sizeof(WAVEFORMATEX)); file.Read(wfx, 16); // we only need 16 bytes of the fmt chunk if (chunk.chunksize-16>0) file.Seek(chunk.chunksize-16, SEEK_CUR); } else if (!strncmp(chunk.chunk_id, "data", 4)) { // data chunk *ppWavData=new BYTE[chunk.chunksize+1]; file.Read(*ppWavData, chunk.chunksize); *pDataSize=chunk.chunksize; if (chunk.chunksize & 1) offset++; } else { // other chunk - unused, just skip file.Seek(chunk.chunksize, SEEK_CUR); } offset+=(chunk.chunksize+sizeof(WAVE_CHUNK)); if (offset & 1) offset++; } while (offset+(int)sizeof(WAVE_CHUNK) < riffh.filesize); file.Close(); return (*ppWavData!=NULL); }
TEST(TestRarFile, NormalRAR) { XFILE::CFile file; char buf[20]; memset(&buf, 0, sizeof(buf)); CStdString reffile, strrarpath, strpathinrar; CFileItemList itemlist, itemlistemptydir; struct __stat64 stat_buffer; reffile = XBMC_REF_FILE_PATH("xbmc/filesystem/test/refRARnormal.rar"); URIUtils::CreateArchivePath(strrarpath, "rar", reffile, ""); ASSERT_TRUE(XFILE::CDirectory::GetDirectory(strrarpath, itemlist)); itemlist.Sort(SORT_METHOD_FULLPATH, SortOrderAscending); /* /reffile.txt */ strpathinrar = itemlist[1]->GetPath(); ASSERT_TRUE(StringUtils::EndsWith(strpathinrar, "/reffile.txt", true)); EXPECT_EQ(0, XFILE::CFile::Stat(strpathinrar, &stat_buffer)); EXPECT_TRUE((stat_buffer.st_mode & S_IFMT) | S_IFREG); ASSERT_TRUE(file.Open(strpathinrar)); EXPECT_EQ(0, file.GetPosition()); EXPECT_EQ(1616, file.GetLength()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_TRUE(file.ReadString(buf, sizeof(buf))); EXPECT_EQ(39, file.GetPosition()); EXPECT_STREQ("an award-winning fr", buf); EXPECT_EQ(100, file.Seek(100)); EXPECT_EQ(100, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(120, file.GetPosition()); EXPECT_TRUE(!memcmp("ent hub for digital ", buf, sizeof(buf) - 1)); EXPECT_EQ(220, file.Seek(100, SEEK_CUR)); EXPECT_EQ(220, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(240, file.GetPosition()); EXPECT_TRUE(!memcmp("rs, XBMC is a non-pr", buf, sizeof(buf) - 1)); EXPECT_EQ(1596, file.Seek(-(int64_t)sizeof(buf), SEEK_END)); EXPECT_EQ(1596, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(1616, file.GetPosition()); EXPECT_TRUE(!memcmp("multimedia jukebox.\n", buf, sizeof(buf) - 1)); EXPECT_EQ(1716, file.Seek(100, SEEK_CUR)); EXPECT_EQ(1716, file.GetPosition()); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(-1, file.Seek(-100, SEEK_SET)); file.Close(); /* /testsymlink -> testdir/reffile.txt */ strpathinrar = itemlist[2]->GetPath(); ASSERT_TRUE(StringUtils::EndsWith(strpathinrar, "/testsymlink", true)); EXPECT_EQ(0, XFILE::CFile::Stat(strpathinrar, &stat_buffer)); EXPECT_TRUE((stat_buffer.st_mode & S_IFMT) | S_IFLNK); /* * FIXME: Reading symlinks in RARs is currently broken. It takes a long time * to read them and they produce erroneous results. The expected result is * the target paths of the symlinks. */ ASSERT_TRUE(file.Open(strpathinrar)); EXPECT_EQ(19, file.GetLength()); file.Close(); /* /testsymlinksubdir -> testdir/testsubdir/reffile.txt */ strpathinrar = itemlist[3]->GetPath(); ASSERT_TRUE(StringUtils::EndsWith(strpathinrar, "/testsymlinksubdir", true)); EXPECT_EQ(0, XFILE::CFile::Stat(strpathinrar, &stat_buffer)); EXPECT_TRUE((stat_buffer.st_mode & S_IFMT) | S_IFLNK); ASSERT_TRUE(file.Open(strpathinrar)); EXPECT_EQ(30, file.GetLength()); file.Close(); /* /testdir/ */ strpathinrar = itemlist[0]->GetPath(); ASSERT_TRUE(StringUtils::EndsWith(strpathinrar, "/testdir/", true)); EXPECT_EQ(0, XFILE::CFile::Stat(strpathinrar, &stat_buffer)); EXPECT_TRUE((stat_buffer.st_mode & S_IFMT) | S_IFDIR); itemlist.Clear(); ASSERT_TRUE(XFILE::CDirectory::GetDirectory(strpathinrar, itemlist)); itemlist.Sort(SORT_METHOD_FULLPATH, SortOrderAscending); /* /testdir/reffile.txt */ strpathinrar = itemlist[1]->GetPath(); ASSERT_TRUE(StringUtils::EndsWith(strpathinrar, "/testdir/reffile.txt", true)); EXPECT_EQ(0, XFILE::CFile::Stat(strpathinrar, &stat_buffer)); EXPECT_TRUE((stat_buffer.st_mode & S_IFMT) | S_IFREG); ASSERT_TRUE(file.Open(strpathinrar)); EXPECT_EQ(0, file.GetPosition()); EXPECT_EQ(1616, file.GetLength()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_TRUE(file.ReadString(buf, sizeof(buf))); EXPECT_EQ(39, file.GetPosition()); EXPECT_STREQ("an award-winning fr", buf); EXPECT_EQ(100, file.Seek(100)); EXPECT_EQ(100, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(120, file.GetPosition()); EXPECT_TRUE(!memcmp("ent hub for digital ", buf, sizeof(buf) - 1)); EXPECT_EQ(220, file.Seek(100, SEEK_CUR)); EXPECT_EQ(220, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(240, file.GetPosition()); EXPECT_TRUE(!memcmp("rs, XBMC is a non-pr", buf, sizeof(buf) - 1)); EXPECT_EQ(1596, file.Seek(-(int64_t)sizeof(buf), SEEK_END)); EXPECT_EQ(1596, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(1616, file.GetPosition()); EXPECT_TRUE(!memcmp("multimedia jukebox.\n", buf, sizeof(buf) - 1)); EXPECT_EQ(1716, file.Seek(100, SEEK_CUR)); EXPECT_EQ(1716, file.GetPosition()); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(-1, file.Seek(-100, SEEK_SET)); file.Close(); /* /testdir/testemptysubdir */ strpathinrar = itemlist[2]->GetPath(); ASSERT_TRUE(StringUtils::EndsWith(strpathinrar, "/testdir/testemptysubdir", true)); /* TODO: Should this set the itemlist to an empty list instead? */ EXPECT_FALSE(XFILE::CDirectory::GetDirectory(strpathinrar, itemlistemptydir)); EXPECT_EQ(0, XFILE::CFile::Stat(strpathinrar, &stat_buffer)); EXPECT_TRUE((stat_buffer.st_mode & S_IFMT) | S_IFDIR); /* FIXME: This directory appears a second time as a file */ strpathinrar = itemlist[3]->GetPath(); ASSERT_TRUE(StringUtils::EndsWith(strpathinrar, "/testdir/testsubdir", true)); /* /testdir/testsymlink -> testsubdir/reffile.txt */ strpathinrar = itemlist[4]->GetPath(); ASSERT_TRUE(StringUtils::EndsWith(strpathinrar, "/testdir/testsymlink", true)); EXPECT_EQ(0, XFILE::CFile::Stat(strpathinrar, &stat_buffer)); EXPECT_TRUE((stat_buffer.st_mode & S_IFMT) | S_IFLNK); ASSERT_TRUE(file.Open(strpathinrar)); EXPECT_EQ(22, file.GetLength()); file.Close(); /* /testdir/testsubdir/ */ strpathinrar = itemlist[0]->GetPath(); ASSERT_TRUE(StringUtils::EndsWith(strpathinrar, "/testdir/testsubdir/", true)); EXPECT_EQ(0, XFILE::CFile::Stat(strpathinrar, &stat_buffer)); EXPECT_TRUE((stat_buffer.st_mode & S_IFMT) | S_IFDIR); itemlist.Clear(); ASSERT_TRUE(XFILE::CDirectory::GetDirectory(strpathinrar, itemlist)); itemlist.Sort(SORT_METHOD_FULLPATH, SortOrderAscending); /* /testdir/testsubdir/reffile.txt */ strpathinrar = itemlist[0]->GetPath(); ASSERT_TRUE(StringUtils::EndsWith(strpathinrar, "/testdir/testsubdir/reffile.txt", true)); EXPECT_EQ(0, XFILE::CFile::Stat(strpathinrar, &stat_buffer)); EXPECT_TRUE((stat_buffer.st_mode & S_IFMT) | S_IFREG); ASSERT_TRUE(file.Open(strpathinrar)); EXPECT_EQ(0, file.GetPosition()); EXPECT_EQ(1616, file.GetLength()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_TRUE(file.ReadString(buf, sizeof(buf))); EXPECT_EQ(39, file.GetPosition()); EXPECT_STREQ("an award-winning fr", buf); EXPECT_EQ(100, file.Seek(100)); EXPECT_EQ(100, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(120, file.GetPosition()); EXPECT_TRUE(!memcmp("ent hub for digital ", buf, sizeof(buf) - 1)); EXPECT_EQ(220, file.Seek(100, SEEK_CUR)); EXPECT_EQ(220, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(240, file.GetPosition()); EXPECT_TRUE(!memcmp("rs, XBMC is a non-pr", buf, sizeof(buf) - 1)); EXPECT_EQ(1596, file.Seek(-(int64_t)sizeof(buf), SEEK_END)); EXPECT_EQ(1596, file.GetPosition()); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(1616, file.GetPosition()); EXPECT_TRUE(!memcmp("multimedia jukebox.\n", buf, sizeof(buf) - 1)); EXPECT_EQ(1716, file.Seek(100, SEEK_CUR)); EXPECT_EQ(1716, file.GetPosition()); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(sizeof(buf), file.Read(buf, sizeof(buf))); file.Flush(); EXPECT_EQ(20, file.GetPosition()); EXPECT_TRUE(!memcmp("About\n-----\nXBMC is ", buf, sizeof(buf) - 1)); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(-1, file.Seek(-100, SEEK_SET)); file.Close(); }
std::vector< CStdString > CKaraokeLyricsTextUStar::readFile(const CStdString & lyricsFile, bool report_errors ) { std::vector< CStdString > lines; XFILE::CFile file; if ( !file.Open( lyricsFile ) ) return std::vector< CStdString >(); unsigned int lyricSize = (unsigned int) file.GetLength(); if ( !lyricSize ) { if ( report_errors ) CLog::Log( LOGERROR, "UStar lyric loader: lyric file %s has zero length", lyricsFile.c_str() ); return std::vector< CStdString >(); } // Read the file into memory array std::vector<char> lyricData( lyricSize ); file.Seek( 0, SEEK_SET ); // Read the whole file if ( file.Read( &lyricData[0], lyricSize) != lyricSize ) return std::vector< CStdString >(); // disk error? file.Close(); // Parse into the string array unsigned int offset = 0; unsigned int lineoffset = 0; while ( offset < lyricSize ) { // End of line? if ( lyricData[offset] == 0x0D || lyricData[offset] == 0x0A ) { // An empty line? if ( lineoffset != offset ) { lyricData[offset] = '\0'; lines.push_back( &lyricData[lineoffset] ); } // Point to the next symbol lineoffset = offset + 1; } offset++; } // Last line, if any if ( lineoffset < lyricSize ) { lyricData[lyricSize-1] = '\0'; lines.push_back( &lyricData[lineoffset] ); } return lines; }
TEST(TestFile, Read) { const std::string newLine = CXBMCTestUtils::Instance().getNewLineCharacters(); const int size = 1616; const int lines = 25; int addPerLine = newLine.length() - 1; int realSize = size + lines * addPerLine; const std::string firstBuf = "About" + newLine + "-----" + newLine + "XBMC is "; const std::string secondBuf = "an award-winning fre"; const std::string thirdBuf = "ent hub for digital "; const std::string fourthBuf = "rs, XBMC is a non-pr"; const std::string fifthBuf = "multimedia jukebox." + newLine; XFILE::CFile file; char buf[23]; memset(&buf, 0, sizeof(buf)); int currentPos; ASSERT_TRUE(file.Open( XBMC_REF_FILE_PATH("/xbmc/filesystem/test/reffile.txt"))); EXPECT_EQ(0, file.GetPosition()); EXPECT_EQ(realSize, file.GetLength()); EXPECT_EQ(firstBuf.length(), file.Read(buf, firstBuf.length())); file.Flush(); currentPos = firstBuf.length(); EXPECT_EQ(currentPos, file.GetPosition()); EXPECT_TRUE(memcmp(firstBuf.c_str(), buf, firstBuf.length()) == 0); EXPECT_TRUE(file.Read(buf, secondBuf.length())); currentPos += secondBuf.length(); EXPECT_EQ(currentPos, file.GetPosition()); EXPECT_TRUE(memcmp(secondBuf.c_str(), buf, secondBuf.length()) == 0); currentPos = 100 + addPerLine * 3; EXPECT_EQ(currentPos, file.Seek(currentPos)); EXPECT_EQ(currentPos, file.GetPosition()); EXPECT_EQ(thirdBuf.length(), file.Read(buf, thirdBuf.length())); file.Flush(); currentPos += thirdBuf.length(); EXPECT_EQ(currentPos, file.GetPosition()); EXPECT_TRUE(memcmp(thirdBuf.c_str(), buf, thirdBuf.length()) == 0); currentPos += 100 + addPerLine * 1; EXPECT_EQ(currentPos, file.Seek(100 + addPerLine * 1, SEEK_CUR)); EXPECT_EQ(currentPos, file.GetPosition()); EXPECT_EQ(fourthBuf.length(), file.Read(buf, fourthBuf.length())); file.Flush(); currentPos += fourthBuf.length(); EXPECT_EQ(currentPos, file.GetPosition()); EXPECT_TRUE(memcmp(fourthBuf.c_str(), buf, fourthBuf.length()) == 0); currentPos = realSize - fifthBuf.length(); EXPECT_EQ(currentPos, file.Seek(-(int64_t)fifthBuf.length(), SEEK_END)); EXPECT_EQ(currentPos, file.GetPosition()); EXPECT_EQ(fifthBuf.length(), file.Read(buf, fifthBuf.length())); file.Flush(); currentPos += fifthBuf.length(); EXPECT_EQ(currentPos, file.GetPosition()); EXPECT_TRUE(memcmp(fifthBuf.c_str(), buf, fifthBuf.length()) == 0); currentPos += 100; EXPECT_EQ(currentPos, file.Seek(100, SEEK_CUR)); EXPECT_EQ(currentPos, file.GetPosition()); currentPos = 0; EXPECT_EQ(currentPos, file.Seek(currentPos, SEEK_SET)); EXPECT_EQ(firstBuf.length(), file.Read(buf, firstBuf.length())); file.Flush(); currentPos += firstBuf.length(); EXPECT_EQ(currentPos, file.GetPosition()); EXPECT_TRUE(memcmp(firstBuf.c_str(), buf, firstBuf.length()) == 0); EXPECT_EQ(0, file.Seek(0, SEEK_SET)); EXPECT_EQ(-1, file.Seek(-100, SEEK_SET)); file.Close(); }