@@ -9,7 +9,6 @@ using namespace ChasmReverse;
99
1010#include " save_load.hpp"
1111
12- #define SAVES_DIR " saves"
1312
1413namespace PanzerChasm
1514{
@@ -54,14 +53,14 @@ SaveHeader::HashType SaveHeader::CalculateHash( const unsigned char* data, unsig
5453}
5554
5655bool SaveData (
57- const char * file_name,
56+ const std::filesystem::path& file_name,
5857 const SaveComment& save_comment,
5958 const SaveLoadBuffer& data )
6059{
61- FILE * f= std::fopen ( file_name, " wb" );
60+ FILE * f= std::fopen ( file_name. native (). c_str () , " wb" );
6261 if ( f == nullptr )
6362 {
64- Log::Warning ( " Can not write save \" " , file_name, " \" " );
63+ Log::Warning ( " Can not write save \" " , file_name. native () , " \" " );
6564 return false ;
6665 }
6766
@@ -81,13 +80,13 @@ bool SaveData(
8180
8281// Returns true, if all ok
8382bool LoadData (
84- const char * file_name,
83+ const std::filesystem::path& file_name,
8584 SaveLoadBuffer& out_data )
8685{
87- FILE * const f= std::fopen ( file_name, " rb" );
86+ FILE * const f= std::fopen ( file_name. native (). c_str () , " rb" );
8887 if ( f == nullptr )
8988 {
90- Log::Warning ( " Can not read save \" " , file_name, " \" ." );
89+ Log::Warning ( " Can not read save \" " , file_name. native () , " \" ." );
9190 return false ;
9291 }
9392
@@ -146,10 +145,10 @@ bool LoadData(
146145}
147146
148147bool LoadSaveComment (
149- const char * file_name,
148+ const std::filesystem::path& file_name,
150149 SaveComment& out_save_comment )
151150{
152- FILE * const f= std::fopen ( file_name, " rb" );
151+ FILE * const f= std::fopen ( file_name. native (). c_str () , " rb" );
153152 if ( f == nullptr )
154153 {
155154 return false ;
@@ -172,57 +171,16 @@ bool LoadSaveComment(
172171 return true ;
173172}
174173
175- void GetSaveFileNameForSlot (
176- const unsigned int slot_number,
177- char * const out_file_name,
178- const unsigned int out_file_name_max_length )
174+ std::filesystem::path GetSaveFileNameForSlot (const uint8_t slot_number)
179175{
180- std::snprintf ( out_file_name, out_file_name_max_length, SAVES_DIR " /save_%02d.pcs" , slot_number );
181- }
182-
183- std::string GetScreenshotFileNameForDir (const std::string& dir)
184- {
185- static uint8_t slot_number = 0 ;
186- char * str = nullptr ;
187-
188- // TODO: use list directory to determine slot_number
189- ssize_t len = asprintf ( &str, " %s/cshot_%02d.tga" , dir.empty () ? SAVES_DIR : dir.c_str (), slot_number );
190- if (len < 14 )
191- {
192- free (str);
193- Log::Warning ( " Couldn't allocate memory for screenshot name: " , errno, " - " , strerror (errno));
194- return std::string ();
195- }
196-
197- slot_number = slot_number < 99 ? slot_number + 1 : 0 ;
198- std::string dst (str, len);
199-
200- free (str);
201- return dst;
202- }
203-
204- std::string CreateScreenshotsDir (const std::string& file)
205- {
206- std::string dst_dir = dir_name<std::string>(file);
207- std::string dst_file = base_name<std::string>(file);
208- std::string cwd = get_current_dir_name ();
209-
210- if (dst_dir.empty ())
211- dst_dir = file[0 ] == ' /' ? " /" : std::string (get_current_dir_name ()) + " /" + SAVES_DIR ;
212-
213- if ((!exists ( dst_dir ) && !create_directories<std::string>( dst_dir )) || !real_path ( dst_dir ))
214- {
215- Log::Warning (" Couldn't create screenshot directory: " , dst_dir, " - " , strerror (errno));
216- dst_dir.clear ();
217- }
218-
219- return dst_dir;
176+ static char tmp[12 ] = " save_" ;
177+ std::snprintf ( tmp, 12 , " save_%02hhu.pcs" , slot_number );
178+ return std::filesystem::absolute (SAVES_DIR / tmp);
220179}
221180
222181void CreateSlotSavesDir ()
223182{
224- std::string dir (SAVES_DIR );
225- if (!create_directories<std::string>( dir ))
183+ if (!std::filesystem::exists (SAVES_DIR ) && !std::filesystem::create_directories ( SAVES_DIR ))
226184 Log::Warning (" Couldn't create saves directory: " , strerror (errno));
227185}
228186
0 commit comments