// This test will fail until ActionDeleteFolder has a proper implementation TEST(TestFileOperationJob, ActionDeleteFolder) { XFILE::CFile *tmpfile; CStdString tmpfilepath, destpath; CFileItemList items; CFileOperationJob job; ASSERT_TRUE((tmpfile = XBMC_CREATETEMPFILE(""))); tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); tmpfile->Close(); destpath = tmpfilepath; destpath += ".deletefolder"; ASSERT_FALSE(XFILE::CFile::Exists(destpath)); CFileItemPtr item(new CFileItem(destpath)); item->SetPath(destpath); item->m_bIsFolder = true; item->Select(true); items.Add(item); job.SetFileOperation(CFileOperationJob::ActionCreateFolder, items, destpath); EXPECT_EQ(CFileOperationJob::ActionCreateFolder, job.GetAction()); EXPECT_TRUE(job.DoWork()); EXPECT_TRUE(XFILE::CDirectory::Exists(destpath)); job.SetFileOperation(CFileOperationJob::ActionDeleteFolder, items, destpath); EXPECT_EQ(CFileOperationJob::ActionDeleteFolder, job.GetAction()); EXPECT_TRUE(job.DoWork()); EXPECT_FALSE(XFILE::CDirectory::Exists(destpath)); EXPECT_TRUE(XBMC_DELETETEMPFILE(tmpfile)); }
void CKaraokeLyricsText::saveLyrics() { XFILE::CFile file; CStdString out; for ( unsigned int i = 0; i < m_lyrics.size(); i++ ) { CStdString timing; timing.Format( "%02d:%02d.%d", m_lyrics[i].timing / 600, (m_lyrics[i].timing % 600) / 10, (m_lyrics[i].timing % 10) ); if ( (m_lyrics[i].flags & LYRICS_NEW_PARAGRAPH) != 0 ) out += "\n\n"; if ( (m_lyrics[i].flags & LYRICS_NEW_LINE) != 0 ) out += "\n"; out += "[" + timing + "]" + m_lyrics[i].text; } out += "\n"; if ( !file.OpenForWrite( "special://temp/tmp.lrc", true ) ) return; file.Write( out, out.size() ); }
void CAirTunesServer::SetCoverArtFromBuffer(const char *buffer, unsigned int size) { XFILE::CFile tmpFile; const char *tmpFileName = "special://temp/airtunes_album_thumb.jpg"; if(!size) return; if (tmpFile.OpenForWrite(tmpFileName, true)) { int writtenBytes=0; writtenBytes = tmpFile.Write(buffer, size); tmpFile.Close(); if(writtenBytes) { //reset to empty before setting the new one //else it won't get refreshed because the name didn't change g_infoManager.SetCurrentAlbumThumb(""); g_infoManager.SetCurrentAlbumThumb(tmpFileName); //update the ui CGUIMessage msg(GUI_MSG_NOTIFY_ALL,0,0,GUI_MSG_REFRESH_THUMBS); g_windowManager.SendThreadMessage(msg); } } }
HRESULT CD3DEffect::Open(D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID* ppData, UINT* pBytes) { XFILE::CFile includeFile; std::string fileName("special://xbmc/system/shaders/"); fileName.append(pFileName); if (!includeFile.Open(fileName)) { CLog::Log(LOGERROR, "%s: Could not open 3DLUT file: %s", __FUNCTION__, fileName.c_str()); return E_FAIL; } int64_t length = includeFile.GetLength(); void *pData = malloc(length); if (includeFile.Read(pData, length) != length) { free(pData); return E_FAIL; } *ppData = pData; *pBytes = length; return S_OK; }
bool CXBMCTinyXML::LoadFile(const std::string& _filename, TiXmlEncoding encoding) { value = _filename.c_str(); XFILE::CFile file; XFILE::auto_buffer buffer; if (file.LoadFile(value, buffer) <= 0) { SetError(TIXML_ERROR_OPENING_FILE, NULL, NULL, TIXML_ENCODING_UNKNOWN); return false; } // Delete the existing data: Clear(); location.Clear(); std::string data(buffer.get(), buffer.length()); buffer.clear(); // free memory early if (encoding == TIXML_ENCODING_UNKNOWN) Parse(data, file.GetContentCharset()); else Parse(data, encoding); if (Error()) return false; return true; }
bool CBaseTexture::LoadFromFileInternal(const std::string& texturePath, unsigned int maxWidth, unsigned int maxHeight, bool autoRotate, bool requirePixels, const std::string& strMimeType) { if (URIUtils::HasExtension(texturePath, ".dds")) { // special case for DDS images CDDSImage image; if (image.ReadFile(texturePath)) { Update(image.GetWidth(), image.GetHeight(), 0, image.GetFormat(), image.GetData(), false); return true; } return false; } unsigned int width = maxWidth ? std::min(maxWidth, g_Windowing.GetMaxTextureSize()) : g_Windowing.GetMaxTextureSize(); unsigned int height = maxHeight ? std::min(maxHeight, g_Windowing.GetMaxTextureSize()) : g_Windowing.GetMaxTextureSize(); // Read image into memory to use our vfs XFILE::CFile file; XFILE::auto_buffer buf; if (file.LoadFile(texturePath, buf) <= 0) return false; CURL url(texturePath); // make sure resource:// paths are properly resolved if (url.IsProtocol("resource")) { std::string translatedPath; if (XFILE::CResourceFile::TranslatePath(url, translatedPath)) url.Parse(translatedPath); } // handle xbt:// paths differently because it allows loading the texture directly from memory if (url.IsProtocol("xbt")) { XFILE::CXbtFile xbtFile; if (!xbtFile.Open(url)) return false; return LoadFromMemory(xbtFile.GetImageWidth(), xbtFile.GetImageHeight(), 0, xbtFile.GetImageFormat(), xbtFile.HasImageAlpha(), reinterpret_cast<unsigned char*>(buf.get())); } IImage* pImage; if(strMimeType.empty()) pImage = ImageFactory::CreateLoader(texturePath); else pImage = ImageFactory::CreateLoaderFromMimeType(strMimeType); if (!LoadIImage(pImage, (unsigned char *)buf.get(), buf.size(), width, height, autoRotate)) { CLog::Log(LOGDEBUG, "%s - Load of %s failed.", __FUNCTION__, texturePath.c_str()); delete pImage; return false; } delete pImage; return true; }
bool Gif::IsAnimated(const char* file) { if (!m_dll.IsLoaded()) return false; if (m_isAnimated < 0) { m_filename = file; m_isAnimated = 0; GifFileType* gif = nullptr; XFILE::CFile gifFile; if (!gifFile.Open(file) || !Open(gif, &gifFile, ReadFromVfs)) return false; if (gif) { if (Slurp(gif) && gif->ImageCount > 1) m_isAnimated = 1; Close(gif); gifFile.Close(); } } return m_isAnimated > 0; }
bool COMXImage::CreateThumbnailFromSurface(unsigned char* buffer, unsigned int width, unsigned int height, unsigned int format, unsigned int pitch, const CStdString& destFile) { if(format != XB_FMT_A8R8G8B8 || !buffer) { CLog::Log(LOGDEBUG, "%s::%s : %s failed format=0x%x\n", CLASSNAME, __func__, destFile.c_str(), format); return false; } if(!Encode(buffer, height * pitch, width, height, pitch)) { CLog::Log(LOGDEBUG, "%s::%s : %s encode failed\n", CLASSNAME, __func__, destFile.c_str()); return false; } XFILE::CFile file; if (file.OpenForWrite(destFile, true)) { CLog::Log(LOGDEBUG, "%s::%s : %s width %d height %d\n", CLASSNAME, __func__, destFile.c_str(), width, height); file.Write(GetEncodedData(), GetEncodedSize()); file.Close(); return true; } return false; }
bool CJpegIO::Open(const CStdString &texturePath, unsigned int minx, unsigned int miny, bool read) { m_texturePath = texturePath; unsigned int imgsize = 0; XFILE::CFile file; if (file.Open(m_texturePath.c_str(), 0)) { imgsize = (unsigned int)file.GetLength(); m_inputBuff = new unsigned char[imgsize]; m_inputBuffSize = file.Read(m_inputBuff, imgsize); file.Close(); if ((imgsize != m_inputBuffSize) || (m_inputBuffSize == 0)) return false; } else return false; if (!read) return true; if (Read(m_inputBuff, m_inputBuffSize, minx, miny)) return true; return false; }
//======================================================================== int CFileURLProtocol::Open(URLContext *h, const char *filename, int flags) { if (flags != URL_RDONLY) { CLog::Log(LOGDEBUG, "CFileURLProtocol::Open: Only read-only is supported"); return -EINVAL; } CStdString url = filename; if (url.Left(strlen("xb-http://")).Equals("xb-http://")) { url = url.Right(url.size() - strlen("xb-")); } CLog::Log(LOGDEBUG, "CFileURLProtocol::Open filename2(%s)", url.c_str()); // open the file, always in read mode, calc bitrate unsigned int cflags = READ_BITRATE; XFILE::CFile *cfile = new XFILE::CFile(); if (CFileItem(url, false).IsInternetStream()) cflags |= READ_CACHED; // open file in binary mode if (!cfile->Open(url, cflags)) { delete cfile; return -EIO; } h->priv_data = (void *)cfile; return 0; }
bool CPODocument::LoadFile(const std::string &pofilename) { CURL poFileUrl(pofilename); if (!XFILE::CFile::Exists(poFileUrl)) return false; XFILE::CFile file; XFILE::auto_buffer buf; if (file.LoadFile(poFileUrl, buf) < 18) // at least a size of a minimalistic header { CLog::Log(LOGERROR, "%s: can't load file \"%s\" or file is too small", __FUNCTION__, pofilename.c_str()); return false; } m_strBuffer = '\n'; m_strBuffer.append(buf.get(), buf.size()); buf.clear(); ConvertLineEnds(pofilename); // we make sure, to have an LF at the end of buffer if (*m_strBuffer.rbegin() != '\n') { m_strBuffer += "\n"; } m_POfilelength = m_strBuffer.size(); if (GetNextEntry() && m_Entry.Type == MSGID_FOUND) return true; CLog::Log(LOGERROR, "POParser: unable to read PO file header from file: %s", pofilename.c_str()); return false; }
bool CPicture::CreateThumbnailFromSurface(const unsigned char *buffer, int width, int height, int stride, const std::string &thumbFile) { CLog::Log(LOGDEBUG, "cached image '%s' size %dx%d", CURL::GetRedacted(thumbFile).c_str(), width, height); if (URIUtils::HasExtension(thumbFile, ".jpg")) { #if defined(HAS_OMXPLAYER) if (COMXImage::CreateThumbnailFromSurface((BYTE *)buffer, width, height, XB_FMT_A8R8G8B8, stride, thumbFile.c_str())) return true; #endif } unsigned char *thumb = NULL; unsigned int thumbsize=0; IImage* pImage = ImageFactory::CreateLoader(thumbFile); if(pImage == NULL || !pImage->CreateThumbnailFromSurface((BYTE *)buffer, width, height, XB_FMT_A8R8G8B8, stride, thumbFile.c_str(), thumb, thumbsize)) { CLog::Log(LOGERROR, "Failed to CreateThumbnailFromSurface for %s", CURL::GetRedacted(thumbFile).c_str()); delete pImage; return false; } XFILE::CFile file; const bool ret = file.OpenForWrite(thumbFile, true) && file.Write(thumb, thumbsize) == thumbsize; pImage->ReleaseThumbnailBuffer(); delete pImage; return ret; }
bool CCurlFile::Download(const CStdString& strURL, const CStdString& strFileName, LPDWORD pdwSize) { CLog::Log(LOGINFO, "Download: %s->%s", strURL.c_str(), strFileName.c_str()); CStdString strData; if (!Get(strURL, strData)) return false; XFILE::CFile file; if (!file.OpenForWrite(strFileName, true)) { CLog::Log(LOGERROR, "Unable to open file %s: %u", strFileName.c_str(), GetLastError()); return false; } if (strData.size()) file.Write(strData.c_str(), strData.size()); file.Close(); if (pdwSize != NULL) { *pdwSize = strData.size(); } return true; }
bool CKaraokeLyricsTextKAR::Load() { XFILE::CFile file; bool succeed = true; m_reportedInvalidVarField = false; // Clear the lyrics array clearLyrics(); if (file.LoadFile(m_midiFile, m_midiData) <= 0) 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; } m_midiData.clear(); return succeed; }
TEST_F(TestRegExpLog, DumpOvector) { CRegExp regex; CStdString logfile, logstring; char buf[100]; unsigned int bytesread; XFILE::CFile file; logfile = CSpecialProtocol::TranslatePath("special://temp/") + "xbmc.log"; EXPECT_TRUE(CLog::Init(CSpecialProtocol::TranslatePath("special://temp/"))); EXPECT_TRUE(XFILE::CFile::Exists(logfile)); EXPECT_TRUE(regex.RegComp("^(?<first>Test)\\s*(?<second>.*)\\.")); EXPECT_EQ(0, regex.RegFind("Test string.")); regex.DumpOvector(LOGDEBUG); CLog::Close(); EXPECT_TRUE(file.Open(logfile)); while ((bytesread = file.Read(buf, sizeof(buf) - 1)) > 0) { buf[bytesread] = '\0'; logstring.append(buf); } file.Close(); EXPECT_FALSE(logstring.empty()); EXPECT_STREQ("\xEF\xBB\xBF", logstring.substr(0, 3).c_str()); EXPECT_TRUE(regex.RegComp(".*DEBUG: regexp ovector=\\{\\[0,12\\],\\[0,4\\]," "\\[5,11\\]\\}.*")); EXPECT_GE(regex.RegFind(logstring), 0); EXPECT_TRUE(XFILE::CFile::Delete(logfile)); }
TEST(TestFileOperationJob, ActionMove) { XFILE::CFile *tmpfile; CStdString tmpfilepath, destpath, destfile; CFileItemList items; CFileOperationJob job; ASSERT_TRUE((tmpfile = XBMC_CREATETEMPFILE(""))); tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); tmpfile->Close(); CFileItemPtr item(new CFileItem(tmpfilepath)); item->SetPath(tmpfilepath); item->m_bIsFolder = false; item->Select(true); items.Add(item); URIUtils::GetDirectory(tmpfilepath, destpath); destpath = URIUtils::AddFileToFolder(destpath, "move"); destfile = URIUtils::AddFileToFolder(destpath, URIUtils::GetFileName(tmpfilepath)); ASSERT_FALSE(XFILE::CFile::Exists(destfile)); ASSERT_TRUE(XFILE::CDirectory::Create(destpath)); job.SetFileOperation(CFileOperationJob::ActionMove, items, destpath); EXPECT_EQ(CFileOperationJob::ActionMove, job.GetAction()); EXPECT_TRUE(job.DoWork()); EXPECT_FALSE(XFILE::CFile::Exists(tmpfilepath)); EXPECT_TRUE(XFILE::CFile::Exists(destfile)); EXPECT_TRUE(XFILE::CFile::Delete(destfile)); EXPECT_TRUE(XFILE::CDirectory::Remove(destpath)); }
TEST(TestFileUtils, DeleteItemString) { XFILE::CFile *tmpfile; ASSERT_NE(nullptr, (tmpfile = XBMC_CREATETEMPFILE(""))); tmpfile->Close(); //Close tmpfile before we try to delete it EXPECT_TRUE(CFileUtils::DeleteItem(XBMC_TEMPFILEPATH(tmpfile))); }
static int dvd_file_read(void *h, uint8_t* buf, int size) { if(interrupt_cb(NULL)) return -1; XFILE::CFile *pFile = (XFILE::CFile *)h; return pFile->Read(buf, size); }
//======================================================================== int CFileURLProtocol::Read(AML_URLContext *h, unsigned char *buf, int size) { XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data; int readsize = cfile->Read(buf, size); //CLog::Log(LOGDEBUG, "CFileURLProtocol::Read size(%d), readsize(%d)", size, readsize); return readsize; }
//======================================================================== int CFileURLProtocol::Close(AML_URLContext *h) { CLog::Log(LOGDEBUG, "CFileURLProtocol::Close"); XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data; cfile->Close(); delete cfile; return 0; }
TEST(TestFile, Exists) { XFILE::CFile *file; ASSERT_TRUE((file = XBMC_CREATETEMPFILE("")) != NULL); file->Close(); EXPECT_TRUE(XFILE::CFile::Exists(XBMC_TEMPFILEPATH(file))); EXPECT_FALSE(XFILE::CFile::Exists("")); EXPECT_TRUE(XBMC_DELETETEMPFILE(file)); }
unsigned int CFileUtils::LoadFile(const std::string &filename, void* &outputBuffer) { XFILE::auto_buffer buffer; XFILE::CFile file; const unsigned int total_read = file.LoadFile(filename, buffer); outputBuffer = buffer.detach(); return total_read; }
bool CXBMCTinyXML::SaveFile(const std::string& filename) const { XFILE::CFile file; if (file.OpenForWrite(filename, true)) { TiXmlPrinter printer; Accept(&printer); return file.Write(printer.CStr(), printer.Size()) == static_cast<ssize_t>(printer.Size()); } return false; }
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(Testlog, Log) { std::string logfile, logstring; char buf[100]; unsigned int bytesread; XFILE::CFile file; CRegExp regex; std::string appName = CCompileInfo::GetAppName(); StringUtils::ToLower(appName); logfile = CSpecialProtocol::TranslatePath("special://temp/") + appName + ".log"; EXPECT_TRUE(CLog::Init(CSpecialProtocol::TranslatePath("special://temp/").c_str())); EXPECT_TRUE(XFILE::CFile::Exists(logfile)); CLog::Log(LOGDEBUG, "debug log message"); CLog::Log(LOGINFO, "info log message"); CLog::Log(LOGNOTICE, "notice log message"); CLog::Log(LOGWARNING, "warning log message"); CLog::Log(LOGERROR, "error log message"); CLog::Log(LOGSEVERE, "severe log message"); CLog::Log(LOGFATAL, "fatal log message"); CLog::Log(LOGNONE, "none type log message"); CLog::Close(); EXPECT_TRUE(file.Open(logfile)); while ((bytesread = file.Read(buf, sizeof(buf) - 1)) > 0) { buf[bytesread] = '\0'; logstring.append(buf); } file.Close(); EXPECT_FALSE(logstring.empty()); EXPECT_STREQ("\xEF\xBB\xBF", logstring.substr(0, 3).c_str()); EXPECT_TRUE(regex.RegComp(".*DEBUG: debug log message.*")); EXPECT_GE(regex.RegFind(logstring), 0); EXPECT_TRUE(regex.RegComp(".*INFO: info log message.*")); EXPECT_GE(regex.RegFind(logstring), 0); EXPECT_TRUE(regex.RegComp(".*NOTICE: notice log message.*")); EXPECT_GE(regex.RegFind(logstring), 0); EXPECT_TRUE(regex.RegComp(".*WARNING: warning log message.*")); EXPECT_GE(regex.RegFind(logstring), 0); EXPECT_TRUE(regex.RegComp(".*ERROR: error log message.*")); EXPECT_GE(regex.RegFind(logstring), 0); EXPECT_TRUE(regex.RegComp(".*SEVERE: severe log message.*")); EXPECT_GE(regex.RegFind(logstring), 0); EXPECT_TRUE(regex.RegComp(".*FATAL: fatal log message.*")); EXPECT_GE(regex.RegFind(logstring), 0); EXPECT_TRUE(regex.RegComp(".*NONE: none type log message.*")); EXPECT_GE(regex.RegFind(logstring), 0); EXPECT_TRUE(XFILE::CFile::Delete(logfile)); }
bool COMXImage::CreateThumbnailFromSurface(unsigned char* buffer, unsigned int width, unsigned int height, unsigned int format, unsigned int pitch, const CStdString& destFile) { if(format != XB_FMT_A8R8G8B8 || !buffer) return false; // the omx encoder needs alligned sizes if(width%16 || height%16) { unsigned int new_width = (width + 15)&~15; unsigned int new_height = (height + 15)&~15; unsigned int new_pitch = new_width * 4; unsigned int size = new_height * new_pitch; unsigned char *dstBuffer = (unsigned char *)malloc(size); unsigned char *dst = dstBuffer; unsigned char *src = buffer; if(!dstBuffer) return false; memset(dst, 0x0, size); for(unsigned int y = 0; y < height; y++) { memcpy(dst, src, pitch); src += pitch; dst += new_pitch; } if(!Encode(dstBuffer, size, new_width, new_height)) { free(dstBuffer); return false; } free(dstBuffer); } else { if(!Encode(buffer, height * pitch, width, height)) return false; } XFILE::CFile file; if (file.OpenForWrite(destFile, true)) { CLog::Log(LOGDEBUG, "%s::%s : %s width %d height %d\n", CLASSNAME, __func__, destFile.c_str(), width, height); file.Write(GetEncodedData(), GetEncodedSize()); file.Close(); return true; } return false; }
TEST(TestFile, Delete) { XFILE::CFile *file; CStdString path; ASSERT_TRUE((file = XBMC_CREATETEMPFILE("")) != NULL); file->Close(); path = XBMC_TEMPFILEPATH(file); EXPECT_TRUE(XFILE::CFile::Exists(path)); EXPECT_TRUE(XFILE::CFile::Delete(path)); EXPECT_FALSE(XFILE::CFile::Exists(path)); }
bool CXBMCTinyXML::SaveFile(const CStdString &filename) const { XFILE::CFile file; if (file.OpenForWrite(filename, true)) { TiXmlPrinter printer; Accept(&printer); file.Write(printer.CStr(), printer.Size()); return true; } return false; }
TEST(TestFile, Delete) { XFILE::CFile *file; std::string path; ASSERT_NE(nullptr, file = XBMC_CREATETEMPFILE("")); file->Close(); path = XBMC_TEMPFILEPATH(file); EXPECT_TRUE(XFILE::CFile::Exists(path)); EXPECT_TRUE(XFILE::CFile::Delete(path)); EXPECT_FALSE(XFILE::CFile::Exists(path)); }
FT_Face GetFont(const std::string &filename, float size, float aspect, XUTILS::auto_buffer& memoryBuf) { // don't have it yet - create it if (!m_library) FT_Init_FreeType(&m_library); if (!m_library) { CLog::Log(LOGERROR, "Unable to initialize freetype library"); return NULL; } FT_Face face; // ok, now load the font face CURL realFile(CSpecialProtocol::TranslatePath(filename)); if (realFile.GetFileName().empty()) return NULL; memoryBuf.clear(); #ifndef TARGET_WINDOWS if (!realFile.GetProtocol().empty()) #endif // ! TARGET_WINDOWS { // load file into memory if it is not on local drive // in case of win32: always load file into memory as filename is in UTF-8, // but freetype expect filename in ANSI encoding XFILE::CFile f; if (f.LoadFile(realFile, memoryBuf) <= 0) return NULL; if (FT_New_Memory_Face(m_library, (const FT_Byte*)memoryBuf.get(), memoryBuf.size(), 0, &face) != 0) return NULL; } #ifndef TARGET_WINDOWS else if (FT_New_Face( m_library, realFile.GetFileName().c_str(), 0, &face )) return NULL; #endif // ! TARGET_WINDOWS unsigned int ydpi = 72; // 72 points to the inch is the freetype default unsigned int xdpi = (unsigned int)MathUtils::round_int(ydpi * aspect); // we set our screen res currently to 96dpi in both directions (windows default) // we cache our characters (for rendering speed) so it's probably // not a good idea to allow free scaling of fonts - rather, just // scaling to pixel ratio on screen perhaps? if (FT_Set_Char_Size( face, 0, (int)(size*64 + 0.5f), xdpi, ydpi )) { FT_Done_Face(face); return NULL; } return face; };