The Battle for Wesnoth  1.15.2+dev
save_index.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by Jörg Hinrichs, refactored from various
3  places formerly created by 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)
33  , save_index_(index)
34  , modified_(modified)
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. */
87  enum class create_for_default_saves_dir { yes };
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 
128  bool loaded_;
130  std::map<std::string, std::time_t> modified_;
131  const std::string dir_;
132  /**
133  * The instance for default_saves_dir() writes a cache file. For other instances,
134  * write_save_index() and delete() are no-ops.
135  */
137 };
138 } // end of namespace savegame
int autosavemax()
Definition: game.cpp:833
friend class create_save_info
Definition: save_index.hpp:29
#define a
const std::string dir_
Definition: save_index.hpp:131
Definitions for the interface to Wesnoth Markup Language (WML).
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:296
const config & summary() const
Definition: save_index.cpp:231
std::shared_ptr< save_index_class > manager_
Definition: save_index.hpp:76
#define b
std::string format_time_local() const
Definition: save_index.cpp:236
create_for_default_saves_dir
Syntatic sugar for choosing which constructor to use.
Definition: save_index.hpp:87
bool read_only_
The instance for default_saves_dir() writes a cache file.
Definition: save_index.hpp:136
std::string name_
Definition: save_index.hpp:54
const std::string & name() const
Definition: save_index.hpp:39
const std::time_t & modified() const
Definition: save_index.hpp:44
std::shared_ptr< save_index_class > save_index_
Definition: save_index.hpp:55
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::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:71
Filename and modification date for a file list.
Definition: save_index.hpp:26
std::map< std::string, std::time_t > modified_
Definition: save_index.hpp:130
std::string format_time_summary() const
Definition: save_index.cpp:250
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
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
A structure for comparing to save_info objects based on their modified time.
Definition: save_index.hpp:63
std::time_t modified_
Definition: save_index.hpp:56