The Battle for Wesnoth  1.19.0-dev
save_index.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by Jörg Hinrichs, David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "config.hpp"
19 
20 namespace savegame
21 {
22 class save_index_class;
23 
24 /** Filename and modification date for a file list */
25 class save_info
26 {
27 private:
28  friend class create_save_info;
29 
30  save_info(const std::string& name, const std::shared_ptr<save_index_class>& index, const std::time_t& modified)
31  : name_(name)
34  {
35  }
36 
37 public:
38  const std::string& name() const
39  {
40  return name_;
41  }
42 
43  const std::time_t& modified() const
44  {
45  return modified_;
46  }
47 
48  std::string format_time_summary() const;
49  std::string format_time_local() const;
50  const config& summary() const;
51 
52 private:
53  std::string name_;
54  std::shared_ptr<save_index_class> save_index_;
55  std::time_t modified_;
56 };
57 
58 /**
59  * A structure for comparing to save_info objects based on their modified time.
60  * If the times are equal, will order based on the name.
61  */
63 {
64  bool operator()(const save_info& a, const save_info& b) const;
65 };
66 
67 /** Read the complete config information out of a savefile. */
68 void read_save_file(const std::string& dir, const std::string& name, config& cfg, std::string* error_log);
69 
71 {
72 public:
73  explicit create_save_info(const std::shared_ptr<save_index_class>&);
74  save_info operator()(const std::string& filename) const;
75  std::shared_ptr<save_index_class> manager_;
76 };
77 
78 class save_index_class : public std::enable_shared_from_this<save_index_class>
79 {
80 public:
81  /**
82  * Constructor for a read-only instance. To get a writable instance, call default_saves_dir().
83  */
84  explicit save_index_class(const std::string& dir);
85  /** Syntatic sugar for choosing which constructor to use. */
88 
89  /** Returns an instance for managing saves in filesystem::get_saves_dir() */
90  static std::shared_ptr<save_index_class> default_saves_dir();
91 
92  std::vector<save_info> get_saves_list(const std::string* filter=nullptr);
93 
94  /** Delete a savegame, including deleting the underlying file. */
95  void delete_game(const std::string& name);
96 
97  void rebuild(const std::string& name);
98  void rebuild(const std::string& name, const std::time_t& modified);
99 
100  /** Delete a savegame from the index, without deleting the underlying file. */
101  void remove(const std::string& name);
102  void set_modified(const std::string& name, const std::time_t& modified);
103 
104  config& get(const std::string& name);
105  const std::string& dir() const;
106 
107  /** Delete autosaves that are no longer needed (according to the autosave policy in the preferences). */
108  void delete_old_auto_saves(const int autosavemax, const int infinite_auto_saves);
109 
110  /** Sync to disk, no-op if read_only_ is set */
111  void write_save_index();
112 
113  /**
114  * If true, all of delete_game, delete_old_auto_saves and write_save_index will be no-ops.
115  */
116  bool read_only()
117  {
118  return read_only_;
119  }
120 
121 private:
122  config& data(const std::string& name);
123  config& data();
124 
125  static void fix_leader_image_path(config& data);
126  /** Deletes non-existent save files from the index. */
127  void clean_up_index();
128 
129  bool loaded_;
131  std::map<std::string, std::time_t> modified_;
132  const std::string dir_;
133  /**
134  * The instance for default_saves_dir() writes a cache file. For other instances,
135  * write_save_index() and delete() are no-ops.
136  */
138  /** Flag to only run the clean_up_index method once. */
140 };
141 } // end of namespace savegame
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
std::shared_ptr< save_index_class > manager_
Definition: save_index.hpp:75
save_info operator()(const std::string &filename) const
Definition: save_index.cpp:394
create_save_info(const std::shared_ptr< save_index_class > &)
Definition: save_index.cpp:389
void clean_up_index()
Deletes non-existent save files from the index.
Definition: save_index.cpp:98
create_for_default_saves_dir
Syntatic sugar for choosing which constructor to use.
Definition: save_index.hpp:86
static std::shared_ptr< save_index_class > default_saves_dir()
Returns an instance for managing saves in filesystem::get_saves_dir()
Definition: save_index.cpp:208
std::vector< save_info > get_saves_list(const std::string *filter=nullptr)
Get a list of available saves.
Definition: save_index.cpp:215
void remove(const std::string &name)
Delete a savegame from the index, without deleting the underlying file.
Definition: save_index.cpp:68
bool read_only_
The instance for default_saves_dir() writes a cache file.
Definition: save_index.hpp:137
void write_save_index()
Sync to disk, no-op if read_only_ is set.
Definition: save_index.cpp:114
const std::string & dir() const
Definition: save_index.cpp:93
void set_modified(const std::string &name, const std::time_t &modified)
Definition: save_index.cpp:75
config & get(const std::string &name)
Definition: save_index.cpp:80
const std::string dir_
Definition: save_index.hpp:132
void delete_old_auto_saves(const int autosavemax, const int infinite_auto_saves)
Delete autosaves that are no longer needed (according to the autosave policy in the preferences).
Definition: save_index.cpp:353
bool read_only()
If true, all of delete_game, delete_old_auto_saves and write_save_index will be no-ops.
Definition: save_index.hpp:116
std::map< std::string, std::time_t > modified_
Definition: save_index.hpp:131
void delete_game(const std::string &name)
Delete a savegame, including deleting the underlying file.
Definition: save_index.cpp:377
void rebuild(const std::string &name)
Definition: save_index.cpp:42
save_index_class(const std::string &dir)
Constructor for a read-only instance.
Definition: save_index.cpp:142
bool clean_up_index_
Flag to only run the clean_up_index method once.
Definition: save_index.hpp:139
static void fix_leader_image_path(config &data)
Definition: save_index.cpp:198
Filename and modification date for a file list.
Definition: save_index.hpp:26
save_info(const std::string &name, const std::shared_ptr< save_index_class > &index, const std::time_t &modified)
Definition: save_index.hpp:30
std::time_t modified_
Definition: save_index.hpp:55
const std::string & name() const
Definition: save_index.hpp:38
const config & summary() const
Definition: save_index.cpp:245
std::string format_time_local() const
Definition: save_index.cpp:250
std::string format_time_summary() const
Definition: save_index.cpp:266
std::string name_
Definition: save_index.hpp:53
std::shared_ptr< save_index_class > save_index_
Definition: save_index.hpp:54
const std::time_t & modified() const
Definition: save_index.hpp:43
int autosavemax()
Definition: game.cpp:788
void read_save_file(const std::string &dir, const std::string &name, config &cfg, std::string *error_log)
Read the complete config information out of a savefile.
Definition: save_index.cpp:312
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
A structure for comparing to save_info objects based on their modified time.
Definition: save_index.hpp:63
bool operator()(const save_info &a, const save_info &b) const
Definition: save_index.cpp:272
#define a
#define b