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