static bool GetSha1ForFile( CString &sFile, unsigned char *szHash ) { RageFile f; f.Open(sFile, RageFile::READ); bool bGot = GetSha1ForFile( f, szHash ); f.Close(); return bGot; }
static void RageFile_png_write( png_struct *pPng, png_byte *pData, png_size_t iSize ) { RageFile *pFile = (RageFile *) png_get_io_ptr(pPng); int iGot = pFile->Write( pData, iSize ); if( iGot == -1 ) SafePngError( pPng, pFile->GetError() ); }
static void RageFile_png_flush( png_struct *pPng ) { RageFile *pFile = (RageFile *) png_get_io_ptr(pPng); int iGot = pFile->Flush(); if( iGot == -1 ) SafePngError( pPng, pFile->GetError() ); }
void RageFileManager::CreateDir( const std::string &sDir ) { std::string sTempFile = sDir + "newdir.temp.newdir"; RageFile f; f.Open( sTempFile, RageFile::WRITE ); f.Close(); Remove( sTempFile ); }
static void WriteBytes( RageFile &f, RString &sError, const void *buf, int size ) { if( sError.size() != 0 ) return; int ret = f.Write( buf, size ); if( ret == -1 ) sError = f.GetError(); }
std::string MovieDecoder_FFMpeg::Open( std::string sFile ) { MovieTexture_FFMpeg::RegisterProtocols(); m_fctx = avcodec::avformat_alloc_context(); if( !m_fctx ) return "AVCodec: Couldn't allocate context"; RageFile *f = new RageFile; if( !f->Open(sFile, RageFile::READ) ) { std::string errorMessage = f->GetError(); std::string error = fmt::sprintf("MovieDecoder_FFMpeg: Error opening \"%s\": %s", sFile.c_str(), errorMessage.c_str() ); delete f; return error; } m_buffer = (unsigned char *)avcodec::av_malloc(STEPMANIA_FFMPEG_BUFFER_SIZE); m_avioContext = avcodec::avio_alloc_context(m_buffer, STEPMANIA_FFMPEG_BUFFER_SIZE, 0, f, AVIORageFile_ReadPacket, nullptr, AVIORageFile_Seek); m_fctx->pb = m_avioContext; int ret = avcodec::avformat_open_input( &m_fctx, sFile.c_str(), nullptr, nullptr ); if( ret < 0 ) return std::string( averr_format(ret, "AVCodec: Couldn't open \"%s\"", sFile.c_str()) ); ret = avcodec::avformat_find_stream_info( m_fctx, nullptr ); if( ret < 0 ) return std::string( averr_format(ret, "AVCodec (%s): Couldn't find codec parameters", sFile.c_str()) ); int stream_idx = avcodec::av_find_best_stream( m_fctx, avcodec::AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0 ); if ( stream_idx < 0 || static_cast<unsigned int>(stream_idx) >= m_fctx->nb_streams || m_fctx->streams[stream_idx] == nullptr ) return "Couldn't find any video streams"; m_pStream = m_fctx->streams[stream_idx]; if( m_pStream->codec->codec_id == avcodec::CODEC_ID_NONE ) return fmt::sprintf( "Unsupported codec %08x", m_pStream->codec->codec_tag ); std::string sError = OpenCodec(); if( !sError.empty() ) return fmt::sprintf( "AVCodec (%s): %s", sFile.c_str(), sError.c_str() ); LOG->Trace( "Bitrate: %i", m_pStream->codec->bit_rate ); char const* fmt_name= avcodec::av_get_pix_fmt_name(m_pStream->codec->pix_fmt); if(fmt_name == nullptr) { LOG->Trace("Codec pixel format: Unknown"); } else { LOG->Trace("Codec pixel format: %s", fmt_name); } return std::string(); }
bool IniFile::WriteFile( const CString &sPath ) { RageFile f; if( !f.Open( sPath, RageFile::WRITE ) ) { LOG->Trace( "Writing '%s' failed: %s", sPath.c_str(), f.GetError().c_str() ); m_sError = f.GetError(); return false; } for( keymap::const_iterator k = keys.begin(); k != keys.end(); ++k ) { if (k->second.empty()) continue; if( f.PutLine( ssprintf("[%s]", k->first.c_str()) ) < 0 ) { m_sError = f.GetError(); return false; } for( key::const_iterator i = k->second.begin(); i != k->second.end(); ++i ) f.PutLine( ssprintf("%s=%s", i->first.c_str(), i->second.c_str()) ); if( f.PutLine( "" ) < 0 ) { m_sError = f.GetError(); return false; } } return true; }
static bool WriteDWINotesTag( RageFile &f, const Steps &out ) { if( out.GetDifficulty() == Difficulty_Edit ) return false; // not supported by DWI LOG->Trace( "Steps::WriteDWINotesTag" ); switch( out.m_StepsType ) { case StepsType_dance_single: f.Write( "#SINGLE:" ); break; case StepsType_dance_couple: f.Write( "#COUPLE:" ); break; case StepsType_dance_double: f.Write( "#DOUBLE:" ); break; case StepsType_dance_solo: f.Write( "#SOLO:" ); break; default: return false; // not a type supported by DWI } switch( out.GetDifficulty() ) { case Difficulty_Beginner: f.Write( "BEGINNER:" ); break; case Difficulty_Easy: f.Write( "BASIC:" ); break; case Difficulty_Medium: f.Write( "ANOTHER:" ); break; case Difficulty_Hard: f.Write( "MANIAC:" ); break; case Difficulty_Challenge: f.Write( "SMANIAC:" ); break; default: ASSERT(0); return false; } f.PutLine( ssprintf("%d:", out.GetMeter()) ); return true; }
bool XNode::SaveToFile( const CString &sFile, DISP_OPT *opt ) const { RageFile f; if( !f.Open(sFile, RageFile::WRITE) ) { LOG->Warn("Couldn't open %s for writing: %s", sFile.c_str(), f.GetError().c_str() ); return false; } return SaveToFile( f, opt ); }
bool CourseWriterCRS::Write( const Course &course, const RString &sPath, bool bSavingCache ) { RageFile f; if( !f.Open( sPath, RageFile::WRITE ) ) { LOG->UserLog( "Course file", sPath, "couldn't be written: %s", f.GetError().c_str() ); return false; } return CourseWriterCRS::Write( course, f, bSavingCache ); }
bool XmlFileUtil::SaveToFile( const XNode *pNode, const RString &sFile, const RString &sStylesheet, bool bWriteTabs ) { RageFile f; if( !f.Open(sFile, RageFile::WRITE) ) { LuaHelpers::ReportScriptErrorFmt( "Couldn't open %s for writing: %s", sFile.c_str(), f.GetError().c_str() ); return false; } return SaveToFile( pNode, f, sStylesheet, bWriteTabs ); }
bool IniFile::WriteFile( const CString &sPath ) const { RageFile f; if( !f.Open( sPath, RageFile::WRITE ) ) { LOG->Trace( "Writing '%s' failed: %s", sPath.c_str(), f.GetError().c_str() ); m_sError = f.GetError(); return false; } return IniFile::WriteFile( f ); }
bool RageDisplay::SaveScreenshot( CString sPath, GraphicsFileFormat format ) { RageSurface* surface = this->CreateScreenshot(); /* Unless we're in lossless, resize the image to 640x480. If we're saving lossy, * there's no sense in saving 1280x960 screenshots, and we don't want to output * screenshots in a strange (non-1) sample aspect ratio. */ if( format != SAVE_LOSSLESS ) { /* Maintain the DAR. */ int iHeight = 480; int iWidth = lrintf( iHeight * GetVideoModeParams().fDisplayAspectRatio ); LOG->Trace( "%ix%i -> %ix%i (%.3f)", surface->w, surface->h, iWidth, iHeight, GetVideoModeParams().fDisplayAspectRatio ); RageSurfaceUtils::Zoom( surface, iWidth, iHeight ); } RageFile out; if( !out.Open( sPath, RageFile::WRITE ) ) { LOG->Trace("Couldn't write %s: %s", sPath.c_str(), out.GetError().c_str() ); return false; } bool bSuccess = false; switch( format ) { case SAVE_LOSSLESS: bSuccess = RageSurfaceUtils::SaveBMP( surface, out ); break; case SAVE_LOSSY_LOW_QUAL: bSuccess = RageSurfaceUtils::SaveJPEG( surface, out, false ); break; case SAVE_LOSSY_HIGH_QUAL: bSuccess = RageSurfaceUtils::SaveJPEG( surface, out, true ); break; default: ASSERT(0); return false; } delete surface; surface = NULL; if( !bSuccess ) { LOG->Trace("Couldn't write %s: %s", sPath.c_str(), out.GetError().c_str() ); return false; } return true; }
void RageFileManager::CreateDir( CString sDir ) { CString sTempFile = sDir + "temp"; RageFile f; f.Open( sTempFile, RageFile::WRITE ); f.Close(); // YUCK: The dir cache doesn't have this new file we just created, // so the delete will fail unless we flush. FILEMAN->FlushDirCache( sDir ); FILEMAN->Remove( sTempFile ); }
static int64_t AVIORageFile_Seek( void *opaque, int64_t offset, int whence ) { RageFile *f = (RageFile *)opaque; if( whence == AVSEEK_SIZE ) return f->GetFileSize(); if( whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END ) { LOG->Trace("Error: unsupported seek whence: %d", whence); return -1; } return f->Seek( (int) offset, whence ); }
SoundReader_FileReader::OpenResult RageSoundReader_Vorbisfile::Open(CString filename_) { filename=filename_; vf = new OggVorbis_File; memset( vf, 0, sizeof(*vf) ); RageFile *f = new RageFile; if( !f->Open( filename ) ) { SetError( ssprintf("ogg: opening \"%s\" failed: %s", filename.c_str(), f->GetError().c_str()) ); delete f; delete vf; vf = NULL; return OPEN_FATAL_ERROR; } ov_callbacks callbacks; callbacks.read_func = OggRageFile_read_func; callbacks.seek_func = OggRageFile_seek_func; callbacks.close_func = OggRageFile_close_func; callbacks.tell_func = OggRageFile_tell_func; int ret = ov_open_callbacks( f, vf, NULL, 0, callbacks ); if(ret < 0) { SetError( ov_ssprintf(ret, "ov_open failed") ); delete f; delete vf; vf = NULL; switch( ret ) { case OV_ENOTVORBIS: return OPEN_UNKNOWN_FILE_FORMAT; default: return OPEN_FATAL_ERROR; } } eof = false; read_offset = (int) ov_pcm_tell(vf); vorbis_info *vi = ov_info( vf, -1 ); ASSERT_M( vi->channels == 1 || vi->channels == 2, ssprintf("%i", vi->channels) ); channels = vi->channels; return OPEN_OK; }
bool IniFile::WriteFile( const RString &sPath ) const { RageFile f; if( !f.Open( sPath, RageFile::WRITE ) ) { LOG->Warn( "Writing '%s' failed: %s", sPath.c_str(), f.GetError().c_str() ); m_sError = f.GetError(); return false; } bool bSuccess = IniFile::WriteFile( f ); int iFlush = f.Flush(); bSuccess &= (iFlush != -1); return bSuccess; }
bool IniFile::ReadFile( const RString &sPath ) { m_sPath = sPath; CHECKPOINT_M( ssprintf("Reading '%s'",m_sPath.c_str()) ); RageFile f; if( !f.Open( m_sPath ) ) { LOG->Trace( "Reading '%s' failed: %s", m_sPath.c_str(), f.GetError().c_str() ); m_sError = f.GetError(); return 0; } return ReadFile( f ); }
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); }
SoundReader_FileReader::OpenResult RageSoundReader_Vorbisfile::Open(CString filename_) { filename=filename_; RageFile *f = new RageFile; if( !f->Open( filename ) ) { SetError( ssprintf("ogg: opening \"%s\" failed: %s", filename.c_str(), f->GetError().c_str()) ); delete f; return OPEN_FATAL_ERROR; } return Open( f ); }
RString CryptManager::GetMD5ForFile( RString fn ) { RageFile file; if( !file.Open( fn, RageFile::READ ) ) { LOG->Warn( "GetMD5: Failed to open file '%s'", fn.c_str() ); return RString(); } int iHash = register_hash( &md5_desc ); ASSERT( iHash >= 0 ); unsigned char digest[16]; HashFile( file, digest, iHash ); return RString( (const char *) digest, sizeof(digest) ); }
bool CryptManager::Sign( RString sPath, RString &sSignatureOut, RString sPrivKey ) { if( !IsAFile(sPath) ) { LOG->Trace( "SignFileToFile: \"%s\" doesn't exist", sPath.c_str() ); return false; } RageFile file; if( !file.Open(sPath) ) { LOG->Warn( "SignFileToFile: open(%s) failed: %s", sPath.c_str(), file.GetError().c_str() ); return false; } RSAKeyWrapper key; RString sError; if( !key.Load(sPrivKey, sError) ) { LOG->Warn( "Error loading RSA key: %s", sError.c_str() ); return false; } int iHash = register_hash( &sha1_desc ); ASSERT( iHash >= 0 ); unsigned char buf_hash[20]; if( !HashFile(file, buf_hash, iHash) ) return false; unsigned char signature[256]; unsigned long signature_len = sizeof(signature); int iRet = rsa_sign_hash_ex( buf_hash, sizeof(buf_hash), signature, &signature_len, LTC_PKCS_1_V1_5, &g_pPRNG->m_PRNG, g_pPRNG->m_iPRNG, iHash, 0, &key.m_Key); if( iRet != CRYPT_OK ) { LOG->Warn( "SignFileToFile error: %s", error_to_string(iRet) ); return false; } sSignatureOut.assign( (const char *) signature, signature_len ); return true; }
static void WriteLineList( RageFile &f, vector<CString> &lines, bool SkipLeadingBlankLines, bool OmitLastNewline ) { for( unsigned i = 0; i < lines.size(); ++i ) { TrimRight( lines[i] ); if( SkipLeadingBlankLines ) { if( lines.size() == 0 ) continue; SkipLeadingBlankLines = false; } f.Write( lines[i] ); if( !OmitLastNewline || i+1 < lines.size() ) f.PutLine( "" ); /* newline */ } }
bool XmlFileUtil::LoadFromFileShowErrors( XNode &xml, const RString &sFile ) { RageFile f; if( !f.Open(sFile, RageFile::READ) ) { LuaHelpers::ReportScriptErrorFmt("Couldn't open %s for reading: %s", sFile.c_str(), f.GetError().c_str() ); return false; } bool bSuccess = LoadFromFileShowErrors( xml, f ); if( !bSuccess ) { RString sWarning = ssprintf( "XML: LoadFromFile failed for file: %s", sFile.c_str() ); LuaHelpers::ReportScriptError(sWarning, "XML_PARSE_ERROR"); } return bSuccess; }
RageSurface *RageSurfaceUtils::LoadFile( const RString &sPath, RString &error, bool bHeaderOnly ) { { RageFile TestOpen; if( !TestOpen.Open( sPath ) ) { error = TestOpen.GetError(); return NULL; } } set<RString> FileTypes; vector<RString> const& exts= ActorUtil::GetTypeExtensionList(FT_Bitmap); for(vector<RString>::const_iterator curr= exts.begin(); curr != exts.end(); ++curr) { FileTypes.insert(*curr); } RString format = GetExtension(sPath); format.MakeLower(); bool bKeepTrying = true; /* If the extension matches a format, try that first. */ if( FileTypes.find(format) != FileTypes.end() ) { RageSurface *ret = TryOpenFile( sPath, bHeaderOnly, error, format, bKeepTrying ); if( ret ) return ret; FileTypes.erase( format ); } for( set<RString>::iterator it = FileTypes.begin(); bKeepTrying && it != FileTypes.end(); ++it ) { RageSurface *ret = TryOpenFile( sPath, bHeaderOnly, error, *it, bKeepTrying ); if( ret ) { LOG->UserLog( "Graphic file", sPath, "is really %s", it->c_str() ); return ret; } } return NULL; }
bool XNode::LoadFromFile( const CString &sFile ) { 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; } bool bSuccess = LoadFromFile( f ); if( !bSuccess ) { CString sWarning = ssprintf( "XML: LoadFromFile failed for file: %s", sFile.c_str() ); LOG->Warn( sWarning ); Dialog::OK( sWarning, "XML_PARSE_ERROR" ); } return bSuccess; }
SoundReader *SoundReader_FileReader::OpenFile( CString filename, CString &error ) { { RageFile TestOpen; if( !TestOpen.Open( filename ) ) { error = TestOpen.GetError(); return NULL; } } set<CString> FileTypes; FileTypes.insert("ogg"); FileTypes.insert("mp3"); FileTypes.insert("wav"); CString format = GetExtension(filename); format.MakeLower(); error = ""; bool bKeepTrying = true; /* If the extension matches a format, try that first. */ if( FileTypes.find(format) != FileTypes.end() ) { SoundReader_FileReader *NewSample = TryOpenFile( filename, error, format, bKeepTrying ); if( NewSample ) return NewSample; FileTypes.erase( format ); } for( set<CString>::iterator it = FileTypes.begin(); bKeepTrying && it != FileTypes.end(); ++it ) { SoundReader_FileReader *NewSample = TryOpenFile( filename, error, *it, bKeepTrying ); if( NewSample ) { LOG->Warn("File \"%s\" is really %s", filename.c_str(), it->c_str()); return NewSample; } } return NULL; }
RageSurfaceUtils::OpenResult RageSurface_Load_PNG( const RString &sPath, RageSurface *&ret, bool bHeaderOnly, RString &error ) { RageFile f; if( !f.Open( sPath ) ) { error = f.GetError(); return RageSurfaceUtils::OPEN_FATAL_ERROR; } char errorbuf[1024]; ret = RageSurface_Load_PNG( &f, sPath, errorbuf, bHeaderOnly ); if( ret == NULL ) { error = errorbuf; return RageSurfaceUtils::OPEN_UNKNOWN_FILE_FORMAT; // XXX } return RageSurfaceUtils::OPEN_OK; }
int URLRageFile_open( avcodec::URLContext *h, const char *filename, int flags ) { if( strncmp( filename, "rage://", 7 ) ) { LOG->Warn("URLRageFile_open: Unexpected path \"%s\"", filename ); return -EIO; } filename += 7; int mode = 0; switch( flags ) { case URL_RDONLY: mode = RageFile::READ; break; case URL_WRONLY: mode = RageFile::WRITE | RageFile::STREAMED; break; case URL_RDWR: FAIL_M( "O_RDWR unsupported" ); } RageFileBasic *pFile = new RageFile; { RageFile *f = new RageFile; if( !f->Open(filename, mode) ) { LOG->Trace("Error opening \"%s\": %s", filename, f->GetError().c_str() ); delete f; return -EIO; } pFile = f; } /* If possible, wrap this file in the read-ahead filter to avoid skips when we rewind. */ if( RageFileDriverReadAhead::FileSupported(pFile) ) { RageFileDriverReadAhead *pBufferedFile = new RageFileDriverReadAhead( pFile, 1024*128 ); pBufferedFile->DeleteFileWhenFinished(); pFile = pBufferedFile; } h->is_streamed = false; h->priv_data = pFile; return 0; }
bool RageFileDriverZip::Load( const CString &sPath ) { ASSERT( m_pZip == NULL ); /* don't load twice */ m_bFileOwned = true; m_sPath = sPath; m_Mutex.SetName( ssprintf("RageFileDriverZip(%s)", sPath.c_str()) ); RageFile *pFile = new RageFile; if( !pFile->Open(sPath) ) { LOG->Warn( "Couldn't open %s: %s", sPath.c_str(), pFile->GetError().c_str() ); delete pFile; return false; } m_pZip = pFile; return ParseZipfile(); }