The Battle for Wesnoth  1.17.17+dev
save_index.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2023
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"
20 
21 namespace savegame
22 {
23 class save_index_class;
24 
25 /** Filename and modification date for a file list */
26 class save_info
27 {
28 private:
29  friend class create_save_info;
30 
31  save_info(const std::string& name, const std::shared_ptr<save_index_class>& index, const std::time_t& modified)
32  : name_(name)
35  {
36  }
37 
38 public:
39  const std::string& name() const
40  {
41  return name_;
42  }
43 
44  const std::time_t& modified() const
45  {
46  return modified_;
47  }
48 
49  std::string format_time_summary() const;
50  std::string format_time_local() const;
51  const config& summary() const;
52 
53 private:
54  std::string name_;
55  std::shared_ptr<save_index_class> save_index_;
56  std::time_t modified_;
57 };
58 
59 /**
60  * A structure for comparing to save_info objects based on their modified time.
61  * If the times are equal, will order based on the name.
62  */
64 {
65  bool operator()(const save_info& a, const save_info& b) const;
66 };
67 
68 /** Read the complete config information out of a savefile. */
69 void read_save_file(const std::string& dir, const std::string& name, config& cfg, std::string* error_log);
70 
72 {
73 public:
74  explicit create_save_info(const std::shared_ptr<save_index_class>&);
75  save_info operator()(const std::string& filename) const;
76  std::shared_ptr<save_index_class> manager_;
77 };
78 
79 class save_index_class : public std::enable_shared_from_this<save_index_class>
80 {
81 public:
82  /**
83  * Constructor for a read-only instance. To get a writable instance, call default_saves_dir().
84  */
85  explicit save_index_class(const std::string& dir);
86  /** Syntatic sugar for choosing which constructor to use. */
89 
90  /** Returns an instance for managing saves in filesystem::get_saves_dir() */
91  static std::shared_ptr<save_index_class> default_saves_dir();
92 
93  std::vector<save_info> get_saves_list(const std::string* filter=nullptr);
94 
95  /** Delete a savegame, including deleting the underlying file. */
96  void delete_game(const std::string& name);
97 
98  void rebuild(const std::string& name);
99  void rebuild(const std::string& name, const std::time_t& modified);
100 
101  /** Delete a savegame from the index, without deleting the underlying file. */
102  void remove(const std::string& name);
103  void set_modified(const std::string& name, const std::time_t& modified);
104 
105  config& get(const std::string& name);
106  const std::string& dir() const;
107 
108  /** Delete autosaves that are no longer needed (according to the autosave policy in the preferences). */
109  void delete_old_auto_saves(const int autosavemax, const int infinite_auto_saves);
110 
111  /** Sync to disk, no-op if read_only_ is set */
112  void write_save_index();
113 
114  /**
115  * If true, all of delete_game, delete_old_auto_saves and write_save_index will be no-ops.
116  */
117  bool read_only()
118  {
119  return read_only_;
120  }
121 
122 private:
123  config& data(const std::string& name);
124  config& data();
125 
126  static void fix_leader_image_path(config& data);
127  /** Deletes non-existent save files from the index. */
128  void clean_up_index();
129 
130  bool loaded_;
132  std::map<std::string, std::time_t> modified_;
133  const std::string dir_;
134  /**
135  * The instance for default_saves_dir() writes a cache file. For other instances,
136  * write_save_index() and delete() are no-ops.
137  */
139  /** Flag to only run the clean_up_index method once. */
141 };
142 } // end of namespace savegame
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
std::shared_ptr< save_index_class > manager_
Definition: save_index.hpp:76
save_info operator()(const std::string &filename) const
Definition: save_index.cpp:396
create_save_info(const std::shared_ptr< save_index_class > &)
Definition: save_index.cpp:391
void clean_up_index()
Deletes non-existent save files from the index.
Definition: save_index.cpp:100
create_for_default_saves_dir
Syntatic sugar for choosing which constructor to use.
Definition: save_index.hpp:87
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:210
std::vector< save_info > get_saves_list(const std::string *filter=nullptr)
Get a list of available saves.
Definition: save_index.cpp:217
void remove(const std::string &name)
Delete a savegame from the index, without deleting the underlying file.
Definition: save_index.cpp:70
bool read_only_
The instance for default_saves_dir() writes a cache file.
Definition: save_index.hpp:138
void write_save_index()
Sync to disk, no-op if read_only_ is set.
Definition: save_index.cpp:116
const std::string & dir() const
Definition: save_index.cpp:95
void set_modified(const std::string &name, const std::time_t &modified)
Definition: save_index.cpp:77
config & get(const std::string &name)
Definition: save_index.cpp:82
const std::string dir_
Definition: save_index.hpp:133
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:355
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:117
std::map< std::string, std::time_t > modified_
Definition: save_index.hpp:132
void delete_game(const std::string &name)
Delete a savegame, including deleting the underlying file.
Definition: save_index.cpp:379
void rebuild(const std::string &name)
Definition: save_index.cpp:44
save_index_class(const std::string &dir)
Constructor for a read-only instance.
Definition: save_index.cpp:144
bool clean_up_index_
Flag to only run the clean_up_index method once.
Definition: save_index.hpp:140
static void fix_leader_image_path(config &data)
Definition: save_index.cpp:200
Filename and modification date for a file list.
Definition: save_index.hpp:27
save_info(const std::string &name, const std::shared_ptr< save_index_class > &index, const std::time_t &modified)
Definition: save_index.hpp:31
std::time_t modified_
Definition: save_index.hpp:56
const std::string & name() const
Definition: save_index.hpp:39
const config & summary() const
Definition: save_index.cpp:247
std::string format_time_local() const
Definition: save_index.cpp:252
std::string format_time_summary() const
Definition: save_index.cpp:268
std::string name_
Definition: save_index.hpp:54
std::shared_ptr< save_index_class > save_index_
Definition: save_index.hpp:55
const std::time_t & modified() const
Definition: save_index.hpp:44
int autosavemax()
Definition: game.cpp:791
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:314
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:72
A structure for comparing to save_info objects based on their modified time.
Definition: save_index.hpp:64
bool operator()(const save_info &a, const save_info &b) const
Definition: save_index.cpp:274
#define a
#define b