Make video dumping to go through generic A/V snooping interface
[lsnes.git] / misc.hpp
blobf2aa5f7c2fe66524d8f923c833de01602670ee84
1 #ifndef _misc__hpp__included__
2 #define _misc__hpp__included__
4 #include <string>
5 #include <vector>
6 #include "window.hpp"
7 #include <boost/lexical_cast.hpp>
9 /**
10 * \brief Recognize "foo", "foo (anything)" and "foo\t(anything)".
12 * \param haystack The string to search.
13 * \param needle The string to find.
14 * \return True if found, false if not.
16 bool is_cmd_prefix(const std::string& haystack, const std::string& needle) throw();
18 /**
19 * \brief Get random hexes
21 * Get string of random hex characters of specified length.
23 * \param length The number of hex characters to return.
24 * \return The random hexadecimal string.
25 * \throws std::bad_alloc Not enough memory.
27 std::string get_random_hexstring(size_t length) throw(std::bad_alloc);
29 /**
30 * \brief Set random seed
32 * This function sets the random seed to use.
34 * \param seed The value to use as seed.
35 * \throw std::bad_alloc Not enough memory.
37 void set_random_seed(const std::string& seed) throw(std::bad_alloc);
39 /**
40 * \brief Set random seed to (hopefully) unique value
42 * This function sets the random seed to value that should only be used once. Note, the value is not necressarily
43 * crypto-secure, even if it is unique.
45 * \throw std::bad_alloc Not enough memory.
47 void set_random_seed() throw(std::bad_alloc);
49 /**
50 * \brief Load a ROM.
52 * Given commandline arguments, load a ROM.
54 * \param cmdline The command line.
55 * \param win Handle to send the messages to.
56 * \return The loaded ROM set.
57 * \throws std::bad_alloc Not enough memory.
58 * \throws std::runtime_error Can't load the ROMset.
60 struct loaded_rom load_rom_from_commandline(std::vector<std::string> cmdline, window* win) throw(std::bad_alloc,
61 std::runtime_error);
63 /**
64 * \brief Dump listing of regions to graphics system messages.
66 * \param win Handle to send the messages to.
67 * \throws std::bad_alloc Not enough memory.
69 void dump_region_map(window* win) throw(std::bad_alloc);
71 /**
72 * \brief Return printing stream.
74 * \param win Handle to graphics system.
75 * \return Stream. If win is NULL, this is std::cout. Otherwise it is win->out().
76 * \throws std::bad_alloc Not enough memory.
78 std::ostream& out(window* win) throw(std::bad_alloc);
80 /**
81 * \brief Fatal error.
83 * Fatal error. If win is non-NULL, it calls win->fatal_error(). Otherwise just immediately quits with error.
85 void fatal_error(window* win) throw();
87 /**
88 * \brief Get path to config directory.
90 * \param win Graphics system handle.
91 * \return The config directory path.
92 * \throw std::bad_alloc Not enough memory.
94 std::string get_config_path(window* win) throw(std::bad_alloc);
96 /**
97 * \brief Panic on OOM.
99 void OOM_panic(window* win);
102 * \brief Typeconvert string.
104 template<typename T> inline T parse_value(const std::string& value) throw(std::bad_alloc, std::runtime_error)
106 try {
107 //Hack, since lexical_cast lets negative values slip through.
108 if(!std::numeric_limits<T>::is_signed && value.length() && value[0] == '-') {
109 throw std::runtime_error("Unsigned values can't be negative");
111 return boost::lexical_cast<T>(value);
112 } catch(std::exception& e) {
113 throw std::runtime_error("Can't parse value '" + value + "': " + e.what());
117 template<> inline std::string parse_value(const std::string& value) throw(std::bad_alloc, std::runtime_error)
119 return value;
122 void create_lsnesrc(window* win);
125 * \brief Opaque internal state of SHA256
127 struct sha256_opaque;
130 * \brief SHA-256 function
132 * This class implements interface to SHA-256.
134 class sha256
136 public:
138 * \brief Create new SHA-256 context
140 * Creates new SHA-256 context, initially containing empty data.
142 sha256() throw(std::bad_alloc);
145 * \brief Destructor
147 ~sha256() throw();
150 * \brief Append data to be hashed
152 * This function appends specified data to be hashed. Don't call after calling read().
154 * \param data The data to write.
155 * \param datalen The length of data written.
157 void write(const uint8_t* data, size_t datalen) throw();
160 * \brief Read the hash value
162 * Reads the hash of data written. Can be called multiple times, but after the first call, data can't be appended
163 * anymore.
165 * \param hashout 32-byte buffer to store the hash to.
167 void read(uint8_t* hashout) throw();
170 * \brief Read the hash value
172 * Similar to read(uint8_t*) but instead returns the hash as hexadecimal string.
174 * \return The hash in hex form.
175 * \throw std::bad_alloc Not enough memory.
177 std::string read() throw(std::bad_alloc);
180 * \brief Hash block of data.
182 * Hashes block of data.
184 * \param hashout 32-byte buffer to write the hash to.
185 * \param data The data to hash.
186 * \param datalen The length of data hashed.
188 static void hash(uint8_t* hashout, const uint8_t* data, size_t datalen) throw();
191 * \brief Hash block of data.
193 * Hashes block of data.
195 * \param hashout 32-byte buffer to write the hash to.
196 * \param data The data to hash.
198 static void hash(uint8_t* hashout, const std::vector<uint8_t>& data) throw()
200 hash(hashout, &data[0], data.size());
204 * \brief Hash block of data.
206 * Hashes block of data.
208 * \param hashout 32-byte buffer to write the hash to.
209 * \param data The data to hash.
211 static void hash(uint8_t* hashout, const std::vector<char>& data) throw()
213 hash(hashout, reinterpret_cast<const uint8_t*>(&data[0]), data.size());
217 * \brief Hash block of data.
219 * Hashes block of data.
221 * \param data The data to hash.
222 * \param datalen The length of data hashed.
223 * \return Hexadecimal hash of the data.
225 static std::string hash(const uint8_t* data, size_t datalen) throw(std::bad_alloc)
227 uint8_t hashout[32];
228 hash(hashout, data, datalen);
229 return tostring(hashout);
233 * \brief Hash block of data.
235 * Hashes block of data.
237 * \param data The data to hash.
238 * \return Hexadecimal hash of the data.
240 static std::string hash(const std::vector<uint8_t>& data) throw(std::bad_alloc)
242 uint8_t hashout[32];
243 hash(hashout, &data[0], data.size());
244 return tostring(hashout);
248 * \brief Hash block of data.
250 * Hashes block of data.
252 * \param data The data to hash.
253 * \return Hexadecimal hash of the data.
255 static std::string hash(const std::vector<char>& data) throw(std::bad_alloc)
257 uint8_t hashout[32];
258 hash(hashout, reinterpret_cast<const uint8_t*>(&data[0]), data.size());
259 return tostring(hashout);
263 * \brief Translate binary hash to hexadecimal hash
265 * Reads 32-byte binary hash from hashout and returns 64-hex hexadecimal hash.
267 * \param hashout The binary hash
268 * \return Hexadecimal hash
269 * \throws std::bad_alloc Not enough memory.
271 static std::string tostring(const uint8_t* hashout) throw(std::bad_alloc);
272 private:
273 sha256(const sha256& x) throw();
274 sha256& operator=(const sha256& x) throw();
275 sha256_opaque* opaque;
276 bool finished;
279 #endif