Incremental Engine  1.0.6
A 2D Game Engine to create Idle Games
IncrementalEngine::SaveSystem Class Reference

#include <SaveSystem.h>

Public Member Functions

 SaveSystem ()
 
void SaveInt (int value, std::string key)
 
void SaveFloat (float value, std::string key)
 
void SaveString (std::string value, std::string key)
 
int GetInt (std::string key)
 
float GetFloat (std::string key)
 
std::string GetString (std::string key)
 
bool HasKey (std::string key)
 
bool DeleteKey (std::string key)
 
void DeleteAll ()
 

Detailed Description

Definition at line 13 of file SaveSystem.h.

Constructor & Destructor Documentation

◆ SaveSystem()

IncrementalEngine::SaveSystem::SaveSystem ( )

Definition at line 7 of file SaveSystem.cpp.

8  {
9  struct stat info;
10  if (stat(SAVES_PATH.c_str(), &info) != 0)
11  {
12  if (!std::filesystem::create_directory(SAVES_PATH.c_str()))
13  {
14  MessageBox(NULL, L"Failed to create Saves Path", L"SaveSystem Error", MB_OK);
15  }
16  }
17  }

Member Function Documentation

◆ DeleteAll()

void IncrementalEngine::SaveSystem::DeleteAll ( )

Definition at line 104 of file SaveSystem.cpp.

105  {
106  std::filesystem::remove_all("myDirectory");
107  }

◆ DeleteKey()

bool IncrementalEngine::SaveSystem::DeleteKey ( std::string  key)

Definition at line 99 of file SaveSystem.cpp.

100  {
101  return remove((SAVES_PATH + key + SAVE_EXTENSION).c_str()) == 0;
102  }

◆ GetFloat()

float IncrementalEngine::SaveSystem::GetFloat ( std::string  key)

Definition at line 60 of file SaveSystem.cpp.

61  {
62  std::string content = GetString(key);
63 
64  try
65  {
66  return std::stof(content);
67  }
68  catch (std::invalid_argument const& e)
69  {
70  MessageBox(NULL, L"Bad input : std::invalid_argument thrown", L"SaveSystem", MB_OK);
71  }
72  catch (std::out_of_range const& e)
73  {
74  MessageBox(NULL, L"Integer overflow: std::out_of_range thrown", L"SaveSystem", MB_OK);
75  }
76 
77  return NULL;
78  }

◆ GetInt()

int IncrementalEngine::SaveSystem::GetInt ( std::string  key)

Definition at line 40 of file SaveSystem.cpp.

41  {
42  std::string content = GetString(key);
43 
44  try
45  {
46  return std::stoi(content);
47  }
48  catch (std::invalid_argument const& e)
49  {
50  MessageBox(NULL, L"Bad input : std::invalid_argument thrown", L"SaveSystem", MB_OK);
51  }
52  catch (std::out_of_range const& e)
53  {
54  MessageBox(NULL, L"Integer overflow: std::out_of_range thrown", L"SaveSystem", MB_OK);
55  }
56 
57  return NULL;
58  }

◆ GetString()

std::string IncrementalEngine::SaveSystem::GetString ( std::string  key)

Definition at line 80 of file SaveSystem.cpp.

81  {
82  std::ifstream file(SAVES_PATH + key + SAVE_EXTENSION);
83  if (!file.good()) return "0";
84 
85  std::string content(
86  (std::istreambuf_iterator<char>(file)),
87  (std::istreambuf_iterator<char>())
88  );
89 
90  return content;
91  }

◆ HasKey()

bool IncrementalEngine::SaveSystem::HasKey ( std::string  key)

Definition at line 93 of file SaveSystem.cpp.

94  {
95  std::ifstream file((SAVES_PATH + key + SAVE_EXTENSION).c_str());
96  return file.good();
97  }

◆ SaveFloat()

void IncrementalEngine::SaveSystem::SaveFloat ( float  value,
std::string  key 
)

Definition at line 26 of file SaveSystem.cpp.

27  {
28  std::ofstream out(SAVES_PATH + key + SAVE_EXTENSION, std::ofstream::trunc);
29  out << value;
30  out.close();
31  }

◆ SaveInt()

void IncrementalEngine::SaveSystem::SaveInt ( int  value,
std::string  key 
)

Definition at line 19 of file SaveSystem.cpp.

20  {
21  std::ofstream out(SAVES_PATH + key + SAVE_EXTENSION, std::ofstream::trunc);
22  out << value;
23  out.close();
24  }

◆ SaveString()

void IncrementalEngine::SaveSystem::SaveString ( std::string  value,
std::string  key 
)

Definition at line 33 of file SaveSystem.cpp.

34  {
35  std::ofstream out(SAVES_PATH + key + SAVE_EXTENSION, std::ofstream::trunc);
36  out << value;
37  out.close();
38  }
IncrementalEngine::SaveSystem::GetString
std::string GetString(std::string key)
Definition: SaveSystem.cpp:80