Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 8218a484d332e56042234fb4643cb4a46293a24d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
   filesystem/ifilestream.h
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "filesystem/filesystem.h"
#include "filesystem/filestream.h"

namespace filesystem
{

/* ---- class IFileStream ------------------------------------------ */

IFileStream::IFileStream(const char *name) : std::ifstream()
{
	if (name && name[0]) {
		open(name);
	}
}

IFileStream::IFileStream(const std::string &name) : std::ifstream(), ifstream_name(name)
{
	if (name.size()) {
		open(ifstream_name);
	}
}

void IFileStream::open(const char *name)
{
	if (!name || !name[0]) {
		if (is_open()) {
			close();
		}
		ifstream_name.clear();
		ifstream_filename.clear();
		return;
	}

	ifstream_name.assign(name);
	
	for (SearchPath::iterator path = searchpath().begin(); path != searchpath().end(); path++) {
		ifstream_filename.assign((*path));
		ifstream_filename.append(ifstream_name);
		/*
		#ifdef _WIN32
				for (size_t i = 0; i < fstream_filename.size(); i++)
					if (fstream_filename[i] == '/') fstream_filename[i] = '\\';
		#endif
		*/
		if (sys::file_exists(ifstream_filename)) {
			std::ifstream::open(ifstream_filename.c_str());
			if (good()) {
				return;
			} else {
				con_warn << "Could not read file " << filename() << std::endl;
			}
				
		}
	}

	ifstream_filename.clear();
}

void IFileStream::open(const std::string &name)
{
	open(name.c_str());
}

/* ---- class OFileStream ------------------------------------------ */

OFileStream::OFileStream(const char *name) : std::ofstream()
{
	if (name && name[0]) {
		open(name);
	}
}

OFileStream::OFileStream(const std::string &name) : std::ofstream(), ofstream_name(name)
{
	if (name.size()) {
		open(ofstream_name);
	}
}

void OFileStream::open(const char *name)
{
	if (!name || !name[0]) {
		if (is_open()) {
			close();
		}
		ofstream_name.clear();
		ofstream_filename.clear();
		return;
	}

	ofstream_name.assign(name);
	
	ofstream_filename.assign(writedir());
	ofstream_filename.append(ofstream_name);
	std::ofstream::open(ofstream_filename.c_str());

	if (!good()) {
		ofstream_filename.clear();
	}
}

void OFileStream::open(const std::string &name)
{
	open(name.c_str());
}

}