git repos / blockattack-game

blame: source/code/sago/SagoMisc.cpp

normal view · raw

82da8a1c sago007 2015-12-19 18:00 1
/*
82da8a1c sago007 2015-12-19 18:00 2
  Copyright (c) 2015 Poul Sander
82da8a1c sago007 2015-12-19 18:00 3
82da8a1c sago007 2015-12-19 18:00 4
  Permission is hereby granted, free of charge, to any person
82da8a1c sago007 2015-12-19 18:00 5
  obtaining a copy of this software and associated documentation files
82da8a1c sago007 2015-12-19 18:00 6
  (the "Software"), to deal in the Software without restriction,
82da8a1c sago007 2015-12-19 18:00 7
  including without limitation the rights to use, copy, modify, merge,
82da8a1c sago007 2015-12-19 18:00 8
  publish, distribute, sublicense, and/or sell copies of the Software,
82da8a1c sago007 2015-12-19 18:00 9
  and to permit persons to whom the Software is furnished to do so,
82da8a1c sago007 2015-12-19 18:00 10
  subject to the following conditions:
82da8a1c sago007 2015-12-19 18:00 11
82da8a1c sago007 2015-12-19 18:00 12
  The above copyright notice and this permission notice shall be
82da8a1c sago007 2015-12-19 18:00 13
  included in all copies or substantial portions of the Software.
82da8a1c sago007 2015-12-19 18:00 14
82da8a1c sago007 2015-12-19 18:00 15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
82da8a1c sago007 2015-12-19 18:00 16
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
82da8a1c sago007 2015-12-19 18:00 17
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
82da8a1c sago007 2015-12-19 18:00 18
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
82da8a1c sago007 2015-12-19 18:00 19
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
82da8a1c sago007 2015-12-19 18:00 20
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
82da8a1c sago007 2015-12-19 18:00 21
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
82da8a1c sago007 2015-12-19 18:00 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::endl;
96eb7a16 sago007 2016-04-29 17:01 35
using std::vector;
82da8a1c sago007 2015-12-19 18:00 36
82da8a1c sago007 2015-12-19 18:00 37
namespace sago {
82da8a1c sago007 2015-12-19 18:00 38
82da8a1c sago007 2015-12-19 18:00 39
82da8a1c sago007 2015-12-19 18:00 40
std::vector<std::string> GetFileList(const char* dir) {
82da8a1c sago007 2015-12-19 18:00 41
	std::vector<std::string> ret;
82da8a1c sago007 2015-12-19 18:00 42
	char** rc = PHYSFS_enumerateFiles(dir);
82da8a1c sago007 2015-12-19 18:00 43
	for (char** i = rc; *i != NULL; i++) {
82da8a1c sago007 2015-12-19 18:00 44
		ret.push_back(*i);
82da8a1c sago007 2015-12-19 18:00 45
	}
82da8a1c sago007 2015-12-19 18:00 46
	PHYSFS_freeList(rc);
82da8a1c sago007 2015-12-19 18:00 47
	return ret;
82da8a1c sago007 2015-12-19 18:00 48
}
82da8a1c sago007 2015-12-19 18:00 49
82da8a1c sago007 2015-12-19 18:00 50
bool FileExists(const char* filename) {
82da8a1c sago007 2015-12-19 18:00 51
	return PHYSFS_exists(filename);
82da8a1c sago007 2015-12-19 18:00 52
}
82da8a1c sago007 2015-12-19 18:00 53
82da8a1c sago007 2015-12-19 18:00 54
std::string GetFileContent(const char* filename) {
82da8a1c sago007 2015-12-19 18:00 55
	std::string ret;
82da8a1c sago007 2015-12-19 18:00 56
	if (!PHYSFS_exists(filename)) {
82da8a1c sago007 2015-12-19 18:00 57
		std::cerr << "GetFileContent - File does not exists: " << filename << std::endl;
82da8a1c sago007 2015-12-19 18:00 58
		return ret;
82da8a1c sago007 2015-12-19 18:00 59
	}
82da8a1c sago007 2015-12-19 18:00 60
	PHYSFS_file* myfile = PHYSFS_openRead(filename);
82da8a1c sago007 2015-12-19 18:00 61
	unsigned int m_size = PHYSFS_fileLength(myfile);
82da8a1c sago007 2015-12-19 18:00 62
	char* m_data = new char[m_size+1];
82da8a1c sago007 2015-12-19 18:00 63
	int length_read = PHYSFS_read (myfile, m_data, 1, m_size);
82da8a1c sago007 2015-12-19 18:00 64
	if (length_read != (int)m_size) {
82da8a1c sago007 2015-12-19 18:00 65
		delete [] m_data;
82da8a1c sago007 2015-12-19 18:00 66
		m_data = nullptr;
82da8a1c sago007 2015-12-19 18:00 67
		PHYSFS_close(myfile);
82da8a1c sago007 2015-12-19 18:00 68
		std::cerr << "Error: Curropt data file: " << filename << std::endl;
82da8a1c sago007 2015-12-19 18:00 69
		return ret;
82da8a1c sago007 2015-12-19 18:00 70
	}
82da8a1c sago007 2015-12-19 18:00 71
	PHYSFS_close(myfile);
82da8a1c sago007 2015-12-19 18:00 72
	m_data[m_size] = 0; //ensure that we are 0 terminated
82da8a1c sago007 2015-12-19 18:00 73
	ret = m_data;
82da8a1c sago007 2015-12-19 18:00 74
	delete [] m_data;
82da8a1c sago007 2015-12-19 18:00 75
	return ret;
82da8a1c sago007 2015-12-19 18:00 76
}
82da8a1c sago007 2015-12-19 18:00 77
82da8a1c sago007 2015-12-19 18:00 78
void WriteFileContent(const char* filename, const std::string& content) {
82da8a1c sago007 2015-12-19 18:00 79
	PHYSFS_file* myfile = PHYSFS_openWrite(filename);
82da8a1c sago007 2015-12-19 18:00 80
	if (!myfile) {
82da8a1c sago007 2015-12-19 18:00 81
		cerr << "Failed to open file for writing, " << PHYSFS_getLastError() << endl;
82da8a1c sago007 2015-12-19 18:00 82
		return;
82da8a1c sago007 2015-12-19 18:00 83
	}
c9d7d4aa sago007 2016-04-30 09:20 84
	PHYSFS_write(myfile, content.c_str(), sizeof(char), content.length());
82da8a1c sago007 2015-12-19 18:00 85
	PHYSFS_close(myfile);
82da8a1c sago007 2015-12-19 18:00 86
}
82da8a1c sago007 2015-12-19 18:00 87
82da8a1c sago007 2015-12-19 18:00 88
}
1970-01-01 00:00 89