Evdev joystick plugin
[lsnes.git] / generic / misc.hpp
blob4a93e21ddfcb6321018ea26a41f3faabef850aaf
1 #ifndef _misc__hpp__included__
2 #define _misc__hpp__included__
4 #include <string>
5 #include <vector>
6 #include <boost/lexical_cast.hpp>
8 extern "C"
10 time_t __real_time(time_t* t);
13 /**
14 * \brief Get random hexes
16 * Get string of random hex characters of specified length.
18 * \param length The number of hex characters to return.
19 * \return The random hexadecimal string.
20 * \throws std::bad_alloc Not enough memory.
22 std::string get_random_hexstring(size_t length) throw(std::bad_alloc);
24 /**
25 * \brief Set random seed
27 * This function sets the random seed to use.
29 * \param seed The value to use as seed.
30 * \throw std::bad_alloc Not enough memory.
32 void set_random_seed(const std::string& seed) throw(std::bad_alloc);
34 /**
35 * \brief Set random seed to (hopefully) unique value
37 * This function sets the random seed to value that should only be used once. Note, the value is not necressarily
38 * crypto-secure, even if it is unique.
40 * \throw std::bad_alloc Not enough memory.
42 void set_random_seed() throw(std::bad_alloc);
44 /**
45 * \brief Load a ROM.
47 * Given commandline arguments, load a ROM.
49 * \param cmdline The command line.
50 * \return The loaded ROM set.
51 * \throws std::bad_alloc Not enough memory.
52 * \throws std::runtime_error Can't load the ROMset.
54 struct loaded_rom load_rom_from_commandline(std::vector<std::string> cmdline) throw(std::bad_alloc,
55 std::runtime_error);
57 /**
58 * \brief Dump listing of regions to graphics system messages.
60 * \throws std::bad_alloc Not enough memory.
62 void dump_region_map() throw(std::bad_alloc);
64 /**
65 * \brief Fatal error.
67 * Fatal error.
69 void fatal_error() throw();
71 /**
72 * \brief Get path to config directory.
74 * \return The config directory path.
75 * \throw std::bad_alloc Not enough memory.
77 std::string get_config_path() throw(std::bad_alloc);
79 /**
80 * \brief Panic on OOM.
82 void OOM_panic();
84 /**
85 * messages -> window::out().
87 std::ostream& _messages();
88 #define messages _messages()
90 /**
91 * \brief Typeconvert string.
93 template<typename T> inline T parse_value(const std::string& value) throw(std::bad_alloc, std::runtime_error)
95 try {
96 //Hack, since lexical_cast lets negative values slip through.
97 if(!std::numeric_limits<T>::is_signed && value.length() && value[0] == '-') {
98 throw std::runtime_error("Unsigned values can't be negative");
100 return boost::lexical_cast<T>(value);
101 } catch(std::exception& e) {
102 throw std::runtime_error("Can't parse value '" + value + "': " + e.what());
106 template<> inline std::string parse_value(const std::string& value) throw(std::bad_alloc, std::runtime_error)
108 return value;
111 uint32_t gcd(uint32_t a, uint32_t b) throw();
113 void create_lsnesrc();
116 * \brief Opaque internal state of SHA256
118 struct sha256_opaque;
121 * \brief SHA-256 function
123 * This class implements interface to SHA-256.
125 class sha256
127 public:
129 * \brief Create new SHA-256 context
131 * Creates new SHA-256 context, initially containing empty data.
133 sha256() throw(std::bad_alloc);
136 * \brief Destructor
138 ~sha256() throw();
141 * \brief Append data to be hashed
143 * This function appends specified data to be hashed. Don't call after calling read().
145 * \param data The data to write.
146 * \param datalen The length of data written.
148 void write(const uint8_t* data, size_t datalen) throw();
151 * \brief Read the hash value
153 * Reads the hash of data written. Can be called multiple times, but after the first call, data can't be appended
154 * anymore.
156 * \param hashout 32-byte buffer to store the hash to.
158 void read(uint8_t* hashout) throw();
161 * \brief Read the hash value
163 * Similar to read(uint8_t*) but instead returns the hash as hexadecimal string.
165 * \return The hash in hex form.
166 * \throw std::bad_alloc Not enough memory.
168 std::string read() throw(std::bad_alloc);
171 * \brief Hash block of data.
173 * Hashes block of data.
175 * \param hashout 32-byte buffer to write the hash to.
176 * \param data The data to hash.
177 * \param datalen The length of data hashed.
179 static void hash(uint8_t* hashout, const uint8_t* data, size_t datalen) throw();
182 * \brief Hash block of data.
184 * Hashes block of data.
186 * \param hashout 32-byte buffer to write the hash to.
187 * \param data The data to hash.
189 static void hash(uint8_t* hashout, const std::vector<uint8_t>& data) throw()
191 hash(hashout, &data[0], data.size());
195 * \brief Hash block of data.
197 * Hashes block of data.
199 * \param hashout 32-byte buffer to write the hash to.
200 * \param data The data to hash.
202 static void hash(uint8_t* hashout, const std::vector<char>& data) throw()
204 hash(hashout, reinterpret_cast<const uint8_t*>(&data[0]), data.size());
208 * \brief Hash block of data.
210 * Hashes block of data.
212 * \param data The data to hash.
213 * \param datalen The length of data hashed.
214 * \return Hexadecimal hash of the data.
216 static std::string hash(const uint8_t* data, size_t datalen) throw(std::bad_alloc)
218 uint8_t hashout[32];
219 hash(hashout, data, datalen);
220 return tostring(hashout);
224 * \brief Hash block of data.
226 * Hashes block of data.
228 * \param data The data to hash.
229 * \return Hexadecimal hash of the data.
231 static std::string hash(const std::vector<uint8_t>& data) throw(std::bad_alloc)
233 uint8_t hashout[32];
234 hash(hashout, &data[0], data.size());
235 return tostring(hashout);
239 * \brief Hash block of data.
241 * Hashes block of data.
243 * \param data The data to hash.
244 * \return Hexadecimal hash of the data.
246 static std::string hash(const std::vector<char>& data) throw(std::bad_alloc)
248 uint8_t hashout[32];
249 hash(hashout, reinterpret_cast<const uint8_t*>(&data[0]), data.size());
250 return tostring(hashout);
254 * \brief Translate binary hash to hexadecimal hash
256 * Reads 32-byte binary hash from hashout and returns 64-hex hexadecimal hash.
258 * \param hashout The binary hash
259 * \return Hexadecimal hash
260 * \throws std::bad_alloc Not enough memory.
262 static std::string tostring(const uint8_t* hashout) throw(std::bad_alloc);
263 private:
264 sha256(const sha256& x) throw();
265 sha256& operator=(const sha256& x) throw();
266 sha256_opaque* opaque;
267 bool finished;
270 #endif