Debian Codegraph @

terps/bocfel/options.h

raw symbols SCIP
1 // vim: set ft=cpp:
2
3 #ifndef ZTERP_CONFIG_H
4 #define ZTERP_CONFIG_H
5
6 #include <bitset>
7 #include <cctype>
8 #include <functional>
9 #include <map>
10 #include <memory>
11 #include <string>
12 #include <unordered_map>
13 #include <utility>
14 #include <vector>
15
16 #include "stack.h"
17
18 struct Options {
19 unsigned long eval_stack_size = DEFAULT_STACK_SIZE;
20 unsigned long call_stack_size = DEFAULT_CALL_DEPTH;
21 bool disable_color = false;
22 bool disable_config = false;
23 bool disable_timed = false;
24 bool disable_sound = false;
25 bool enable_escape = false;
26 std::unique_ptr<std::string> escape_string = std::make_unique<std::string>("1m");
27 bool disable_fixed = false;
28 bool assume_fixed = false;
29 bool disable_graphics_font = false;
30 bool enable_alt_graphics = false;
31 bool show_help = false;
32 bool disable_history_playback = false;
33 bool show_id = false;
34 bool disable_term_keys = false;
35 std::unique_ptr<std::string> username = nullptr;
36 bool disable_meta_commands = false;
37 unsigned long int_number = 1; // DEC
38 unsigned char int_version = 'C';
39 bool disable_patches = false;
40 bool replay_on = false;
41 std::unique_ptr<std::string> replay_name = nullptr;
42 bool record_on = false;
43 std::unique_ptr<std::string> record_name = nullptr;
44 bool transcript_on = false;
45 std::unique_ptr<std::string> transcript_name = nullptr;
46 unsigned long undo_slots = 100;
47 bool show_version = false;
48 bool disable_abbreviations = false;
49 bool enable_censorship = false;
50 bool overwrite_transcript = false;
51 bool override_undo = false;
52 std::unique_ptr<unsigned long> random_seed = nullptr;
53 std::unique_ptr<std::string> random_device = nullptr;
54
55 bool autosave = false;
56 bool skip_autorestore = false;
57 std::unique_ptr<std::string> autosave_directory = nullptr;
58 bool autosave_librarystate = false;
59 bool persistent_transcript = false;
60 std::unique_ptr<std::string> editor = nullptr;
61 bool warn_on_v6 = true;
62 bool redirect_v6_windows = true;
63 bool disable_v6_hacks = false;
64 double v6_hack_max_scale = 4.0;
65 bool v6_borders = true;
66 bool aspect_correction = false;
67
68 void process_arguments(int argc, char **argv);
69
70 #ifndef ZTERP_NO_OPTIONS
71 Options();
72 void read_config();
73 void read_envvars();
74 void help();
75
76 const std::vector<std::string> &errors() {
77 return m_errors;
78 }
79
80 using Parser = std::function<void(const std::string &)>;
81
82 struct OptValue {
83 enum class Type { Flag, Number, Value } type;
84 std::string desc;
85 Parser parser;
86 };
87
88 private:
89 struct OptCompare {
90 bool operator()(const unsigned char &a, const unsigned char &b) const {
91 if (std::tolower(a) == std::tolower(b)) {
92 return std::islower(a) && std::isupper(b);
93 }
94
95 return std::tolower(a) < std::tolower(b);
96 }
97 };
98
99 std::unordered_map<std::string, Parser> m_from_config;
100 std::map<char, OptValue, OptCompare> m_opts;
101 std::vector<std::string> m_errors;
102
103 static bool m_initialized;
104
105 int getopt(int argc, char *const argv[]);
106 void add_parser(char opt, std::string desc, bool use_config, std::string name, const Parser &parser, OptValue::Type type);
107 const decltype(m_opts) &opts() const { return m_opts; }
108 #endif
109 };
110
111 // BadOption means an invalid option was provided (i.e. one that does
112 // not exist).
113 //
114 // BadValue means an invalid value was provided to an otherwise valid
115 // option (e.g. out of range).
116 //
117 // • BadValue by itself does not cause help to be printed: the user
118 // provided a valid option, but not a valid value. The list of options
119 // won’t be particularly useful.
120 // • BadOption and BadValue both cause the program to exit with a
121 // failure status.
122 namespace ArgStatus {
123 enum {
124 BadOption,
125 BadValue,
126 };
127 }
128
129 extern Options options;
130 extern std::bitset<3> arg_status;
131
132 #endif