git repos / blockattack-game

blame: source/code/sago/SagoMisc.cpp

normal view · raw

82da8a1c sago007 2015-12-19 18:00 1
/*
47fef8e8 sago007 2016-11-24 21:37 2
Copyright (c) 2015 Poul Sander
47fef8e8 sago007 2016-11-24 21:37 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:
47fef8e8 sago007 2016-11-24 21:37 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.
47fef8e8 sago007 2016-11-24 21:37 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
#include "SagoMisc.hpp"
82da8a1c sago007 2015-12-19 18:00 26
#include <physfs.h>
82da8a1c sago007 2015-12-19 18:00 27
#include <iostream>
82da8a1c sago007 2015-12-19 18:00 28
#include <iconv.h>
82da8a1c sago007 2015-12-19 18:00 29
#include <string.h>
82da8a1c sago007 2015-12-19 18:00 30
3ebc2c88 Poul Sander 2019-05-22 17:03 31
#if PHYSFS_VER_MAJOR < 3
3ebc2c88 Poul Sander 2019-05-22 17:03 32
#define PHYSFS_readBytes(X,Y,Z) PHYSFS_read(X,Y,1,Z)
3ebc2c88 Poul Sander 2019-05-22 17:03 33
#define PHYSFS_writeBytes(X,Y,Z) PHYSFS_write(X,Y,1,Z)
3ebc2c88 Poul Sander 2019-05-22 17:03 34
#endif
3ebc2c88 Poul Sander 2019-05-22 17:03 35
82da8a1c sago007 2015-12-19 18:00 36
namespace sago {
82da8a1c sago007 2015-12-19 18:00 37
82da8a1c sago007 2015-12-19 18:00 38
82da8a1c sago007 2015-12-19 18:00 39
std::vector<std::string> GetFileList(const char* dir) {
c3cd4f49 Poul Sander 2020-08-08 17:25 40
	std::vector<std::string> ret;
82da8a1c sago007 2015-12-19 18:00 41
	char** rc = PHYSFS_enumerateFiles(dir);
82da8a1c sago007 2015-12-19 18:00 42
	for (char** i = rc; *i != NULL; i++) {
82da8a1c sago007 2015-12-19 18:00 43
		ret.push_back(*i);
82da8a1c sago007 2015-12-19 18:00 44
	}
82da8a1c sago007 2015-12-19 18:00 45
	PHYSFS_freeList(rc);
82da8a1c sago007 2015-12-19 18:00 46
	return ret;
82da8a1c sago007 2015-12-19 18:00 47
}
82da8a1c sago007 2015-12-19 18:00 48
82da8a1c sago007 2015-12-19 18:00 49
bool FileExists(const char* filename) {
82da8a1c sago007 2015-12-19 18:00 50
	return PHYSFS_exists(filename);
82da8a1c sago007 2015-12-19 18:00 51
}
82da8a1c sago007 2015-12-19 18:00 52
3dd66f5b sago007 2020-07-04 08:31 53
void ReadBytesFromFile(const char* filename, std::unique_ptr<char[]>& dest, unsigned int& bytes) {
3dd66f5b sago007 2020-07-04 08:31 54
	bytes = 0;
82da8a1c sago007 2015-12-19 18:00 55
	if (!PHYSFS_exists(filename)) {
c3cd4f49 Poul Sander 2020-08-08 17:25 56
		std::cerr << "ReadBytesFromFile - File does not exists: " << filename << "\n";
3dd66f5b sago007 2020-07-04 08:31 57
		return;
82da8a1c sago007 2015-12-19 18:00 58
	}
82da8a1c sago007 2015-12-19 18:00 59
	PHYSFS_file* myfile = PHYSFS_openRead(filename);
82da8a1c sago007 2015-12-19 18:00 60
	unsigned int m_size = PHYSFS_fileLength(myfile);
b830df03 sago007 2016-06-11 16:17 61
	std::unique_ptr<char[]> m_data(new char[m_size]);
e49cda71 Poul Sander 2019-05-22 15:48 62
	int length_read = PHYSFS_readBytes (myfile, m_data.get(), m_size);
82da8a1c sago007 2015-12-19 18:00 63
	if (length_read != (int)m_size) {
82da8a1c sago007 2015-12-19 18:00 64
		PHYSFS_close(myfile);
c3cd4f49 Poul Sander 2020-08-08 17:25 65
		std::cerr << "Error: Curropt data file: " << filename << "\n";
3dd66f5b sago007 2020-07-04 08:31 66
		return;
82da8a1c sago007 2015-12-19 18:00 67
	}
82da8a1c sago007 2015-12-19 18:00 68
	PHYSFS_close(myfile);
3dd66f5b sago007 2020-07-04 08:31 69
	std::swap(m_data, dest);
3dd66f5b sago007 2020-07-04 08:31 70
	bytes = m_size;
3dd66f5b sago007 2020-07-04 08:31 71
}
3dd66f5b sago007 2020-07-04 08:31 72
3dd66f5b sago007 2020-07-04 08:31 73
std::string GetFileContent(const char* filename) {
c3cd4f49 Poul Sander 2020-08-08 17:25 74
	std::string ret;
3dd66f5b sago007 2020-07-04 08:31 75
	if (!PHYSFS_exists(filename)) {
c3cd4f49 Poul Sander 2020-08-08 17:25 76
		std::cerr << "GetFileContent - File does not exists: " << filename << "\n";
3dd66f5b sago007 2020-07-04 08:31 77
		return ret;
3dd66f5b sago007 2020-07-04 08:31 78
	}
3dd66f5b sago007 2020-07-04 08:31 79
	unsigned int m_size = 0;
3dd66f5b sago007 2020-07-04 08:31 80
	std::unique_ptr<char[]> m_data;
3dd66f5b sago007 2020-07-04 08:31 81
	ReadBytesFromFile(filename, m_data, m_size);
3dd66f5b sago007 2020-07-04 08:31 82
	//Now create a std::string
c3cd4f49 Poul Sander 2020-08-08 17:25 83
	ret = std::string(m_data.get(), m_data.get()+m_size);
82da8a1c sago007 2015-12-19 18:00 84
	return ret;
82da8a1c sago007 2015-12-19 18:00 85
}
82da8a1c sago007 2015-12-19 18:00 86
55ea55dd sago007 2018-03-24 16:07 87
static void CreatePathToFile(const std::string& path) {
55ea55dd sago007 2018-03-24 16:07 88
	size_t end_of_path = path.find_last_of("/");
55ea55dd sago007 2018-03-24 16:07 89
	if (end_of_path == std::string::npos) {
55ea55dd sago007 2018-03-24 16:07 90
		//No path
55ea55dd sago007 2018-03-24 16:07 91
		return;
55ea55dd sago007 2018-03-24 16:07 92
	}
55ea55dd sago007 2018-03-24 16:07 93
	std::string path2dir = path.substr(0, end_of_path);
55ea55dd sago007 2018-03-24 16:07 94
	PHYSFS_mkdir(path2dir.c_str());
55ea55dd sago007 2018-03-24 16:07 95
}
55ea55dd sago007 2018-03-24 16:07 96
82da8a1c sago007 2015-12-19 18:00 97
void WriteFileContent(const char* filename, const std::string& content) {
55ea55dd sago007 2018-03-24 16:07 98
	CreatePathToFile(filename);
82da8a1c sago007 2015-12-19 18:00 99
	PHYSFS_file* myfile = PHYSFS_openWrite(filename);
82da8a1c sago007 2015-12-19 18:00 100
	if (!myfile) {
3ebc2c88 Poul Sander 2019-05-22 17:03 101
#if PHYSFS_VER_MAJOR > 2
e49cda71 Poul Sander 2019-05-22 15:48 102
		PHYSFS_ErrorCode code = PHYSFS_getLastErrorCode();
3ebc2c88 Poul Sander 2019-05-22 17:03 103
		std::cerr << "Failed to open file for writing, " << PHYSFS_getErrorByCode(code) << " (" << code << ")\n";
3ebc2c88 Poul Sander 2019-05-22 17:03 104
#else
3ebc2c88 Poul Sander 2019-05-22 17:03 105
		std::cerr << "Failed to open file for writing, " << PHYSFS_getLastError() << "\n";
3ebc2c88 Poul Sander 2019-05-22 17:03 106
#endif
82da8a1c sago007 2015-12-19 18:00 107
		return;
82da8a1c sago007 2015-12-19 18:00 108
	}
e49cda71 Poul Sander 2019-05-22 15:48 109
	PHYSFS_writeBytes(myfile, content.c_str(), sizeof(char)*content.length());
82da8a1c sago007 2015-12-19 18:00 110
	PHYSFS_close(myfile);
82da8a1c sago007 2015-12-19 18:00 111
}
82da8a1c sago007 2015-12-19 18:00 112
b84f3eee Poul Sander 2016-06-10 16:55 113
long int StrToLong(const char* c_string) {
b84f3eee Poul Sander 2016-06-10 16:55 114
	auto ret = strtol(c_string, nullptr, 10);
b84f3eee Poul Sander 2016-06-10 16:55 115
	return ret;
b84f3eee Poul Sander 2016-06-10 16:55 116
}
b84f3eee Poul Sander 2016-06-10 16:55 117
82da8a1c sago007 2015-12-19 18:00 118
}
1970-01-01 00:00 119