// returns true if successful, false otherwise bool MsdFile::ReadFile( CString sNewPath ) { error = ""; RageFile f; /* Open a file. */ if( !f.Open( sNewPath ) ) { error = f.GetError(); return false; } // allocate a string to hold the file CString FileString; FileString.reserve( f.GetFileSize() ); int iBytesRead = f.Read( FileString ); if( iBytesRead == -1 ) { error = f.GetError(); return false; } ReadBuf( (char*) FileString.c_str(), iBytesRead ); return true; }
bool RageMovieTexture::GetFourCC( RString fn, RString &handler, RString &type ) { RString ignore, ext; splitpath( fn, ignore, ignore, ext); if( !ext.CompareNoCase(".mpg") || !ext.CompareNoCase(".mpeg") || !ext.CompareNoCase(".mpv") || !ext.CompareNoCase(".mpe") ) { handler = type = "MPEG"; return true; } if( !ext.CompareNoCase(".ogv") ) { handler = type = "Ogg"; return true; } //Not very pretty but should do all the same error checking without iostream #define HANDLE_ERROR(x) { \ LOG->Warn( "Error reading %s: %s", fn.c_str(), x ); \ handler = type = ""; \ return false; \ } RageFile file; if( !file.Open(fn) ) HANDLE_ERROR("Could not open file."); if( !file.Seek(0x70) ) HANDLE_ERROR("Could not seek."); type = " "; if( file.Read((char *)type.c_str(), 4) != 4 ) HANDLE_ERROR("Could not read."); ForceToAscii( type ); if( file.Seek(0xBC) != 0xBC ) HANDLE_ERROR("Could not seek."); handler = " "; if( file.Read((char *)handler.c_str(), 4) != 4 ) HANDLE_ERROR("Could not read."); ForceToAscii( handler ); return true; #undef HANDLE_ERROR }
MidiFile* ReadMidiFile(std::string fileName) { RageFile f; /* Open a file. */ if( !f.Open( fileName ) )return nullptr; // allocate a string to hold the file std::string FileString; FileString.reserve( f.GetFileSize() ); int iBytesRead = f.Read( FileString ); if( iBytesRead == -1 )return nullptr; return ParseMidi(FileString.c_str(), iBytesRead); }
bool XNode::LoadFromFile( CString sFile, PARSEINFO *pi ) { RageFile f; if( !f.Open(sFile, RageFile::READ) ) { LOG->Warn("Couldn't open %s for reading: %s", sFile.c_str(), f.GetError().c_str() ); return false; } CString s; if( f.Read( s ) == -1 ) { if( pi ) { pi->erorr_occur = true; pi->error_pointer = NULL; pi->error_code = PIE_READ_ERROR; pi->error_string = f.GetError(); } return false; } this->Load( s, pi ); return true; }
CString CryptManager::GetMD5( CString fn ) { struct MD5Context md5c; unsigned char digest[16]; int iBytesRead; unsigned char buffer[1024]; RageFile file; if( !file.Open( fn, RageFile::READ ) ) { LOG->Warn( "GetMD5: Failed to open file '%s'", fn.c_str() ); return ""; } MD5Init(&md5c); while( !file.AtEOF() && file.GetError().empty() ) { iBytesRead = file.Read( buffer, sizeof(buffer) ); MD5Update(&md5c, buffer, iBytesRead); } MD5Final(digest, &md5c); return BinaryToHex( digest, sizeof(digest) ); }
void FileTransfer::StartTransfer( TransferType type, const RString &sURL, const RString &sSrcFile, const RString &sDestFile ) { RString Proto; RString Server; int Port=80; RString sAddress; if( !ParseHTTPAddress( sURL, Proto, Server, Port, sAddress ) ) { m_sStatus = "Invalid URL."; m_bFinished = true; UpdateProgress(); return; } m_bSavingFile = sDestFile != ""; m_sBaseAddress = "http://" + Server; if( Port != 80 ) m_sBaseAddress += ssprintf( ":%d", Port ); m_sBaseAddress += "/"; if( sAddress.Right(1) != "/" ) { m_sEndName = Basename( sAddress ); m_sBaseAddress += Dirname( sAddress ); } else { m_sEndName = ""; } // Open the file... // First find out if a file by this name already exists if so, then we gotta // ditch out. // XXX: This should be fixed by a prompt or something? // if we are not talking about a file, let's not worry if( m_sEndName != "" && m_bSavingFile ) { if( !m_fOutputFile.Open( sDestFile, RageFile::WRITE | RageFile::STREAMED ) ) { m_sStatus = m_fOutputFile.GetError(); UpdateProgress(); return; } } // Continue... sAddress = URLEncode( sAddress ); if ( sAddress != "/" ) sAddress = "/" + sAddress; m_wSocket.close(); m_wSocket.create(); m_wSocket.blocking = true; if( !m_wSocket.connect( Server, (short) Port ) ) { m_sStatus = "Failed to connect."; UpdateProgress(); return; } // Produce HTTP header RString sAction; switch( type ) { case upload: sAction = "POST"; break; case download: sAction = "GET"; break; } vector<RString> vsHeaders; vsHeaders.push_back( sAction+" "+sAddress+" HTTP/1.0" ); vsHeaders.push_back( "Host: " + Server ); vsHeaders.push_back( "Cookie: " + g_sCookie.Get() ); vsHeaders.push_back( "Connection: closed" ); string sBoundary = "--ZzAaB03x"; vsHeaders.push_back( "Content-Type: multipart/form-data; boundary=" + sBoundary ); RString sRequestPayload; if( type == upload ) { RageFile f; if( !f.Open( sSrcFile ) ) FAIL_M( f.GetError() ); sRequestPayload.reserve( f.GetFileSize() ); int iBytesRead = f.Read( sRequestPayload ); if( iBytesRead == -1 ) FAIL_M( f.GetError() ); sRequestPayload = "--" + sBoundary + "\r\n" + "Content-Disposition: form-data; name=\"name\"\r\n" + "\r\n" + "Chris\r\n" + "--" + sBoundary + "\r\n" + "Content-Disposition: form-data; name=\"userfile\"; filename=\"" + Basename(sSrcFile) + "\"\r\n" + "Content-Type: application/zip\r\n" + "\r\n" + sRequestPayload + "\r\n" + "--" + sBoundary + "--"; } /* if( sRequestPayload.size() > 0 ) { sHeader += "Content-Type: application/octet-stream\r\n"; sHeader += "Content-Length: multipart/form-data; boundary=" + sBoundary + "\r\n"; //sHeader += "Content-Length: " + ssprintf("%d",sRequestPayload.size()) + "\r\n"; } */ vsHeaders.push_back( "Content-Length: " + ssprintf("%zd",sRequestPayload.size()) ); RString sHeader; FOREACH_CONST( RString, vsHeaders, h ) sHeader += *h + "\r\n"; sHeader += "\r\n"; m_wSocket.SendData( sHeader.c_str(), sHeader.length() ); m_wSocket.SendData( "\r\n" ); m_wSocket.SendData( sRequestPayload.GetBuffer(), sRequestPayload.size() ); m_sStatus = "Header Sent."; m_wSocket.blocking = false; m_bIsDownloading = true; m_sBUFFER = ""; m_bGotHeader = false; UpdateProgress(); }
static size_t OggRageFile_read_func( void *ptr, size_t size, size_t nmemb, void *datasource ) { RageFile *f = (RageFile *) datasource; return f->Read( ptr, size, nmemb ); }
static int AVIORageFile_ReadPacket( void *opaque, uint8_t *buf, int buf_size ) { RageFile *f = (RageFile *)opaque; return f->Read( buf, buf_size ); }
void SanityCheck() { /* Read sanity check. */ do { g_TestFile = "hello"; g_TestFilename = "file"; RageFile test; if( !test.Open("/test/file", RageFile::READ ) ) Fail( "Sanity check Open() failed: %s", test.GetError().c_str() ); RString str; int got = test.GetLine( str ); if( got <= 0 ) Fail( "Sanity check GetLine(): got %i", got ); if( str != "hello" ) Fail( "Sanity check Read(): expected \"hello\", got \"%s\"", str.c_str() ); } while(false); /* Read error sanity check. */ do { g_TestFile = "hello world"; g_TestFilename = "file"; g_BytesUntilError = 5; RageFile test; if( !test.Open("/test/file", RageFile::READ ) ) Fail( "Sanity check 2 Open() failed: %s", test.GetError().c_str() ); RString str; int got = test.Read( str, 5 ); if( got != 5 ) Fail( "Sanity check 2 Read(): got %i", got ); if( str != "hello" ) Fail( "Sanity check 2 Read(): expected \"hello\", got \"%s\"", str.c_str() ); got = test.Read( str, 5 ); if( got != -1 ) Fail( "Sanity check 2 GetLine(): expected -1, got %i", got ); if( test.GetError() != "Fake error" ) Fail( "Sanity check 2 GetError(): expected \"Fake error\", got \"%s\"", test.GetError().c_str() ); } while(false); /* Write error sanity check. */ do { g_TestFilename = "file"; g_BytesUntilError = 5; RageFile test; if( !test.Open("/test/file", RageFile::WRITE ) ) Fail( "Write error check Open() failed: %s", test.GetError().c_str() ); int wrote = test.Write( "test", 4 ); if( wrote != 4 ) Fail( "Write error check Write(): wrote %i", wrote ); wrote = test.Write( "ing", 3 ); if( wrote != -1 ) Fail( "Write error check Write(): expected -1, got %i", wrote ); if( test.GetError() != "Fake error" ) Fail( "Write error check GetError(): expected \"Fake error\", got \"%s\"", test.GetError().c_str() ); } while(false); }
void BackgroundLoader::LoadThread() { while( !m_bShutdownThread ) { /* Wait for a request. It's normal for this to wait for a long time; don't * fail on timeout. */ m_StartSem.Wait(); CString sFile = GetRequest(); if( sFile.empty() ) continue; { /* If the file already exists, short circuit. */ m_Mutex.Lock(); map<CString,int>::iterator it; it = m_FinishedRequests.find( sFile ); if( it != m_FinishedRequests.end() ) { ++it->second; LOG->Trace("XXX: request %s done loading (already done), cnt now %i", sFile.c_str(), m_FinishedRequests[sFile] ); m_Mutex.Unlock(); continue; } m_Mutex.Unlock(); } m_sThreadIsActive = true; LOG->Trace("XXX: reading %s", sFile.c_str()); CString sCachePath = GetCachePath( sFile ); /* Open the file and read it. */ RageFile src; if( src.Open(sFile) ) { /* If we're writing to a file cache ... */ RageFile dst; bool bWriteToCache = g_bWriteToCache; if( bWriteToCache ) bWriteToCache = dst.Open( sCachePath, RageFile::WRITE ); LOG->Trace("XXX: go on '%s' to '%s'", sFile.c_str(), sCachePath.c_str()); char buf[1024*4]; while( !m_sThreadShouldAbort && !src.AtEOF() ) { int got = src.Read( buf, sizeof(buf) ); if( got > 0 && bWriteToCache ) dst.Write( buf, got ); } if( bWriteToCache ) dst.Close(); LOG->Trace("XXX: done"); } src.Close(); m_Mutex.Lock(); if( !m_sThreadShouldAbort ) { ++m_FinishedRequests[sFile]; LOG->Trace("XXX: request %s done loading, cnt now %i", sFile.c_str(), m_FinishedRequests[sFile] ); } else { FILEMAN->Remove( sCachePath ); LOG->Trace("XXX: request %s aborted", sFile.c_str() ); } m_sThreadShouldAbort = false; m_sThreadIsActive = false; m_Mutex.Unlock(); } }
int URLRageFile_read( avcodec::URLContext *h, unsigned char *buf, int size ) { RageFile *f = (RageFile *) h->priv_data; return f->Read( buf, size ); }