From 38bfae13dda2a6c9466beb15328ac2b3bd3f26bc Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Wed, 28 Aug 2019 19:28:31 +0200 Subject: [PATCH] Add helper function for opening files. --- server/TracyUserData.cpp | 36 ++++++++++++++++++------------------ server/TracyUserData.hpp | 3 +++ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/server/TracyUserData.cpp b/server/TracyUserData.cpp index ae187c05..670b29c8 100644 --- a/server/TracyUserData.cpp +++ b/server/TracyUserData.cpp @@ -1,6 +1,5 @@ #include #include -#include #include "TracyStorage.hpp" #include "TracyUserData.hpp" @@ -18,20 +17,16 @@ UserData::UserData( const char* program, uint64_t time ) : m_program( program ) , m_time( time ) { - const auto descpath = GetSavePath( m_program.c_str(), m_time, FileDescription, false ); - if( descpath ) + FILE* f = OpenFile( FileDescription, false ); + if( f ) { - FILE* f = fopen( descpath, "rb" ); - if( f ) - { - fseek( f, 0, SEEK_END ); - const auto sz = ftell( f ); - fseek( f, 0, SEEK_SET ); - auto buf = std::make_unique( sz ); - fread( buf.get(), 1, sz, f ); - fclose( f ); - m_description.assign( buf.get(), buf.get() + sz ); - } + fseek( f, 0, SEEK_END ); + const auto sz = ftell( f ); + fseek( f, 0, SEEK_SET ); + auto buf = std::make_unique( sz ); + fread( buf.get(), 1, sz, f ); + fclose( f ); + m_description.assign( buf.get(), buf.get() + sz ); } } @@ -49,10 +44,7 @@ bool UserData::SetDescription( const char* description ) m_description = description; const auto sz = m_description.size(); - const auto path = GetSavePath( m_program.c_str(), m_time, FileDescription, true ); - if( !path ) return false; - - FILE* f = fopen( path, "wb" ); + FILE* f = OpenFile( FileDescription, true ); if( !f ) return false; fwrite( description, 1, sz, f ); @@ -60,4 +52,12 @@ bool UserData::SetDescription( const char* description ) return true; } +FILE* UserData::OpenFile( const char* filename, bool write ) +{ + const auto path = GetSavePath( m_program.c_str(), m_time, filename, write ); + if( !path ) return nullptr; + FILE* f = fopen( path, write ? "wb" : "rb" ); + return f; +} + } diff --git a/server/TracyUserData.hpp b/server/TracyUserData.hpp index 4c945a82..6db8bd4f 100644 --- a/server/TracyUserData.hpp +++ b/server/TracyUserData.hpp @@ -2,6 +2,7 @@ #define __TRACYUSERDATA_HPP__ #include +#include #include namespace tracy @@ -20,6 +21,8 @@ public: bool SetDescription( const char* description ); private: + FILE* OpenFile( const char* filename, bool write ); + std::string m_program; uint64_t m_time;