git repos / blockattack-game

blame: source/code/sago/SagoMisc.hpp

normal view · raw

82da8a1c sago007 2015-12-19 18:00 1
/*
47fef8e8 sago007 2016-11-24 21:37 2
Copyright (c) 2015 Poul Sander
82da8a1c sago007 2015-12-19 18:00 3
47fef8e8 sago007 2016-11-24 21:37 4
Permission is hereby granted, free of charge, to any person
47fef8e8 sago007 2016-11-24 21:37 5
obtaining a copy of this software and associated documentation files
47fef8e8 sago007 2016-11-24 21:37 6
(the "Software"), to deal in the Software without restriction,
47fef8e8 sago007 2016-11-24 21:37 7
including without limitation the rights to use, copy, modify, merge,
47fef8e8 sago007 2016-11-24 21:37 8
publish, distribute, sublicense, and/or sell copies of the Software,
47fef8e8 sago007 2016-11-24 21:37 9
and to permit persons to whom the Software is furnished to do so,
47fef8e8 sago007 2016-11-24 21:37 10
subject to the following conditions:
82da8a1c sago007 2015-12-19 18:00 11
47fef8e8 sago007 2016-11-24 21:37 12
The above copyright notice and this permission notice shall be
47fef8e8 sago007 2016-11-24 21:37 13
included in all copies or substantial portions of the Software.
82da8a1c sago007 2015-12-19 18:00 14
47fef8e8 sago007 2016-11-24 21:37 15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47fef8e8 sago007 2016-11-24 21:37 16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
47fef8e8 sago007 2016-11-24 21:37 17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
47fef8e8 sago007 2016-11-24 21:37 18
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
47fef8e8 sago007 2016-11-24 21:37 19
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
47fef8e8 sago007 2016-11-24 21:37 20
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
47fef8e8 sago007 2016-11-24 21:37 21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
47fef8e8 sago007 2016-11-24 21:37 22
SOFTWARE.
82da8a1c sago007 2015-12-19 18:00 23
*/
82da8a1c sago007 2015-12-19 18:00 24
82da8a1c sago007 2015-12-19 18:00 25
#ifndef SAGOMISC_HPP
e94af580 sago007 2020-12-30 17:45 26
#define SAGOMISC_HPP
82da8a1c sago007 2015-12-19 18:00 27
82da8a1c sago007 2015-12-19 18:00 28
#include <vector>
82da8a1c sago007 2015-12-19 18:00 29
#include <string>
3dd66f5b sago007 2020-07-04 08:31 30
#include <memory>
82da8a1c sago007 2015-12-19 18:00 31
82da8a1c sago007 2015-12-19 18:00 32
namespace sago {
e373cd75 Poul Sander 2018-11-07 19:21 33
e94af580 sago007 2020-12-30 17:45 34
/**
e94af580 sago007 2020-12-30 17:45 35
 * Returns a vector with all filenames in a given directory.
e94af580 sago007 2020-12-30 17:45 36
 * PHYSFS must be setup before hand. The directory is relative to the PHYSFS base
e94af580 sago007 2020-12-30 17:45 37
 * @param dir The directory to list
e94af580 sago007 2020-12-30 17:45 38
 * @return A vector with the filenames in the given directory. If empty the directory was empty or did not exist
e94af580 sago007 2020-12-30 17:45 39
 */
e94af580 sago007 2020-12-30 17:45 40
std::vector<std::string> GetFileList(const char* dir);
e94af580 sago007 2020-12-30 17:45 41
e94af580 sago007 2020-12-30 17:45 42
/**
e94af580 sago007 2020-12-30 17:45 43
 * Reads an entire file into memory.
e94af580 sago007 2020-12-30 17:45 44
 * PHYSFS must be setup before hand
e94af580 sago007 2020-12-30 17:45 45
 * @param filename The file to read
e94af580 sago007 2020-12-30 17:45 46
 * @param dest The unique pointer in which the bytes will be written
e94af580 sago007 2020-12-30 17:45 47
 * @param bytes Number of bytes written
e94af580 sago007 2020-12-30 17:45 48
 * @return The content of the file. If empty either the file was empty, did not exist or could not be opened
e94af580 sago007 2020-12-30 17:45 49
 */
e94af580 sago007 2020-12-30 17:45 50
void ReadBytesFromFile(const char* filename, std::unique_ptr<char[]>& dest, unsigned int& bytes);
e94af580 sago007 2020-12-30 17:45 51
e94af580 sago007 2020-12-30 17:45 52
/**
e94af580 sago007 2020-12-30 17:45 53
 * Reads an entire file into memory.
e94af580 sago007 2020-12-30 17:45 54
 * PHYSFS must be setup before hand
e94af580 sago007 2020-12-30 17:45 55
 * @param filename The file to read
e94af580 sago007 2020-12-30 17:45 56
 * @return The content of the file. If empty either the file was empty, did not exist or could not be opened
e94af580 sago007 2020-12-30 17:45 57
 */
e94af580 sago007 2020-12-30 17:45 58
std::string GetFileContent(const char* filename);
e94af580 sago007 2020-12-30 17:45 59
e94af580 sago007 2020-12-30 17:45 60
/**
e94af580 sago007 2020-12-30 17:45 61
 * Reads an entire file into memory.
e94af580 sago007 2020-12-30 17:45 62
 * PHYSFS must be setup before hand
e94af580 sago007 2020-12-30 17:45 63
 * @param filename The file to read
e94af580 sago007 2020-12-30 17:45 64
 * @return The content of the file. If empty either the file was empty, did not exist or could not be opened
e94af580 sago007 2020-12-30 17:45 65
 */
e94af580 sago007 2020-12-30 17:45 66
inline std::string GetFileContent(const std::string& filename) {
e94af580 sago007 2020-12-30 17:45 67
	return GetFileContent(filename.c_str());
e94af580 sago007 2020-12-30 17:45 68
};
e94af580 sago007 2020-12-30 17:45 69
e94af580 sago007 2020-12-30 17:45 70
bool FileExists(const char* filename);
e94af580 sago007 2020-12-30 17:45 71
e94af580 sago007 2020-12-30 17:45 72
void WriteFileContent(const char* filename, const std::string& content);
e94af580 sago007 2020-12-30 17:45 73
e94af580 sago007 2020-12-30 17:45 74
/**
e94af580 sago007 2020-12-30 17:45 75
 * This functions converts a string on a best effort basis
e94af580 sago007 2020-12-30 17:45 76
 * Unlike atol this does NOT cause undefined behavior if out of range
e94af580 sago007 2020-12-30 17:45 77
 * @param c_string A string that may contain a number
e94af580 sago007 2020-12-30 17:45 78
 * @return A number between LONG_MIN and LONG_MAX (both inclusive)
e94af580 sago007 2020-12-30 17:45 79
 */
e94af580 sago007 2020-12-30 17:45 80
long int StrToLong(const char* c_string);
e373cd75 Poul Sander 2018-11-07 19:21 81
82da8a1c sago007 2015-12-19 18:00 82
}  //namespace sago
82da8a1c sago007 2015-12-19 18:00 83
e94af580 sago007 2020-12-30 17:45 84
#endif  /* SAGOMISC_HPP */
82da8a1c sago007 2015-12-19 18:00 85
1970-01-01 00:00 86