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
96eb7a16 sago007 2016-04-29 17:01 32
using std::string;
96eb7a16 sago007 2016-04-29 17:01 33
using std::cerr;
96eb7a16 sago007 2016-04-29 17:01 34
using std::vector;
82da8a1c sago007 2015-12-19 18:00 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) {
1a3524b7 sago007 2016-04-30 10:16 40
	vector<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
82da8a1c sago007 2015-12-19 18:00 53
std::string GetFileContent(const char* filename) {
1a3524b7 sago007 2016-04-30 10:16 54
	string ret;
82da8a1c sago007 2015-12-19 18:00 55
	if (!PHYSFS_exists(filename)) {
161a010c sago007 2016-05-06 14:00 56
		cerr << "GetFileContent - File does not exists: " << filename << "\n";
82da8a1c sago007 2015-12-19 18:00 57
		return ret;
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]);
b84f3eee Poul Sander 2016-06-10 16:55 62
	int length_read = PHYSFS_read (myfile, m_data.get(), 1, 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);
161a010c sago007 2016-05-06 14:00 65
		cerr << "Error: Curropt data file: " << filename << "\n";
82da8a1c sago007 2015-12-19 18:00 66
		return ret;
82da8a1c sago007 2015-12-19 18:00 67
	}
82da8a1c sago007 2015-12-19 18:00 68
	PHYSFS_close(myfile);
b830df03 sago007 2016-06-11 16:17 69
	//Now create a std::string 
b830df03 sago007 2016-06-11 16:17 70
	ret = string(m_data.get(), m_data.get()+m_size);
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
55ea55dd sago007 2018-03-24 16:07 74
static void CreatePathToFile(const std::string& path) {
55ea55dd sago007 2018-03-24 16:07 75
	size_t end_of_path = path.find_last_of("/");
55ea55dd sago007 2018-03-24 16:07 76
	if (end_of_path == std::string::npos) {
55ea55dd sago007 2018-03-24 16:07 77
		//No path
55ea55dd sago007 2018-03-24 16:07 78
		return;
55ea55dd sago007 2018-03-24 16:07 79
	}
55ea55dd sago007 2018-03-24 16:07 80
	std::string path2dir = path.substr(0, end_of_path);
55ea55dd sago007 2018-03-24 16:07 81
	PHYSFS_mkdir(path2dir.c_str());
55ea55dd sago007 2018-03-24 16:07 82
}
55ea55dd sago007 2018-03-24 16:07 83
82da8a1c sago007 2015-12-19 18:00 84
void WriteFileContent(const char* filename, const std::string& content) {
55ea55dd sago007 2018-03-24 16:07 85
	CreatePathToFile(filename);
82da8a1c sago007 2015-12-19 18:00 86
	PHYSFS_file* myfile = PHYSFS_openWrite(filename);
82da8a1c sago007 2015-12-19 18:00 87
	if (!myfile) {
161a010c sago007 2016-05-06 14:00 88
		cerr << "Failed to open file for writing, " << PHYSFS_getLastError() << "\n";
82da8a1c sago007 2015-12-19 18:00 89
		return;
82da8a1c sago007 2015-12-19 18:00 90
	}
c9d7d4aa sago007 2016-04-30 09:20 91
	PHYSFS_write(myfile, content.c_str(), sizeof(char), content.length());
82da8a1c sago007 2015-12-19 18:00 92
	PHYSFS_close(myfile);
82da8a1c sago007 2015-12-19 18:00 93
}
82da8a1c sago007 2015-12-19 18:00 94
b84f3eee Poul Sander 2016-06-10 16:55 95
long int StrToLong(const char* c_string) {
b84f3eee Poul Sander 2016-06-10 16:55 96
	auto ret = strtol(c_string, nullptr, 10);
b84f3eee Poul Sander 2016-06-10 16:55 97
	return ret;
b84f3eee Poul Sander 2016-06-10 16:55 98
}
b84f3eee Poul Sander 2016-06-10 16:55 99
82da8a1c sago007 2015-12-19 18:00 100
}
1970-01-01 00:00 101