Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/filesystem/Makefile.am5
-rw-r--r--src/filesystem/filestream.cc60
-rw-r--r--src/filesystem/filestream.h70
-rw-r--r--src/filesystem/filesystem.cc22
-rw-r--r--src/filesystem/inifile.cc2
-rw-r--r--src/sys/sys.cc33
-rw-r--r--src/sys/sys.h5
7 files changed, 181 insertions, 16 deletions
diff --git a/src/filesystem/Makefile.am b/src/filesystem/Makefile.am
index 0793f61..46bb401 100644
--- a/src/filesystem/Makefile.am
+++ b/src/filesystem/Makefile.am
@@ -1,9 +1,10 @@
METASOURCES = AUTO
-libfilesystem_la_SOURCES = diskfile.cc file.cc filesystem.cc inifile.cc
+libfilesystem_la_SOURCES = diskfile.cc file.cc filestream.cc filesystem.cc \
+ inifile.cc
libfilesystem_la_LDFLAGS = -avoid-version -no-undefined
libfilesystem_la_LIBADD = $(top_builddir)/src/sys/libsys.la
noinst_LTLIBRARIES = libfilesystem.la
-noinst_HEADERS = diskfile.h file.h filesystem.h
+noinst_HEADERS = diskfile.h file.h filestream.h filesystem.h inifile.h
INCLUDES = -I$(top_srcdir)/src
diff --git a/src/filesystem/filestream.cc b/src/filesystem/filestream.cc
new file mode 100644
index 0000000..693d1c1
--- /dev/null
+++ b/src/filesystem/filestream.cc
@@ -0,0 +1,60 @@
+/*
+ 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 {
+
+IFileStream::IFileStream(const char *name) : std::ifstream()
+{
+ if (name)
+ fstream_name.assign(name);
+
+ open(name);
+}
+
+IFileStream::IFileStream(const std::string &name) : std::ifstream(), fstream_name(name)
+{
+ open(fstream_name);
+}
+
+void IFileStream::open(const char *name)
+{
+ if (!name) {
+ if (is_open()) {
+ close();
+ }
+ fstream_name.clear();
+ fstream_filename.clear();
+ return;
+ }
+
+ fstream_name.assign(name);
+
+ for (SearchPath::iterator path = searchpath().begin(); path != searchpath().end(); path++) {
+ fstream_filename.assign((*path));
+ fstream_filename.append(fstream_name);
+/*
+#ifdef _WIN32
+ for (size_t i = 0; i < fstream_filename.size(); i++)
+ if (fstream_filename[i] == '/') fstream_filename[i] = '\\';
+#endif
+*/
+ std::ifstream::open(fstream_filename.c_str());
+ if (good())
+ return;
+ }
+
+ fstream_filename.clear();
+}
+
+void IFileStream::open(const std::string &name)
+{
+ open(name.c_str());
+}
+
+}
diff --git a/src/filesystem/filestream.h b/src/filesystem/filestream.h
new file mode 100644
index 0000000..7dddda9
--- /dev/null
+++ b/src/filesystem/filestream.h
@@ -0,0 +1,70 @@
+/*
+ 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
+*/
+
+#ifndef __INCLUDED_FILESYSTEM_FILESTREAM_H__
+#define __INCLUDED_FILESYSTEM_FILESTREAM_H__
+
+#include <fstream>
+
+namespace filesystem
+{
+
+/**
+ * @brief input stream for files in the virtual filesystem
+ */
+class IFileStream : public std::ifstream
+{
+public:
+ IFileStream(const char *name);
+
+ IFileStream(const std::string &name);
+
+ void open(const char *name);
+
+ void open(const std::string &name);
+
+ /// name of the file in the virtual filesystem
+ inline const std::string &name() { return fstream_name; }
+
+ /// system filename
+ inline const std::string &filename() { return fstream_filename; }
+
+private:
+ std::string fstream_name;
+ std::string fstream_filename;
+};
+
+/**
+ * @brief output stream for files in the home directory
+ */
+/*
+class OFileStream : public std::ofstream
+{
+public:
+ IFileStream(const char *filename);
+
+ IFileStream(const std::string &filename);
+
+ void open(const char *filename);
+
+ void open(const std::string &filename);
+
+ /// name of the file in the virtual filesystem
+ inline const std::string &name() { return fstream_name; }
+
+ /// system filename
+ inline const std::string &filename() { return fstream_filename; }
+
+private:
+ std::string fstream_name;
+ std::string fstream_filename;
+};
+*/
+
+} // namespace filesystem
+
+#endif // __INCLUDED_FILESYSTEM_FILESTREAM_H__
+
diff --git a/src/filesystem/filesystem.cc b/src/filesystem/filesystem.cc
index d275667..11ece3c 100644
--- a/src/filesystem/filesystem.cc
+++ b/src/filesystem/filesystem.cc
@@ -64,7 +64,7 @@ void init(std::string const & basename, std::string const & modname)
filesystem_homedir.append("/.osirion");
// create homedir if necessary
- if (!sys::isdirectory(filesystem_homedir))
+ if (!sys::directory_exists(filesystem_homedir))
sys::mkdir(filesystem_homedir);
filesystem_homedir += '/';
@@ -78,11 +78,11 @@ void init(std::string const & basename, std::string const & modname)
if (filesystem_homedir.size()) {
filesystem_homedir.append("\\My Games");
- if (!sys::isdirectory(filesystem_homedir))
+ if (!sys::directory_exists(filesystem_homedir))
sys::mkdir(filesystem_homedir);
filesystem_homedir.append("\\Osirion");
- if (!sys::isdirectory(filesystem_homedir))
+ if (!sys::directory_exists(filesystem_homedir))
sys::mkdir(filesystem_homedir);
filesystem_homedir.append("\\");
@@ -90,7 +90,7 @@ void init(std::string const & basename, std::string const & modname)
con_warn << "using fallback home directory" << std::endl;
filesystem_homedir.assign("home");
- if (!sys::isdirectory(filesystem_homedir))
+ if (!sys::directory_exists(filesystem_homedir))
sys::mkdir(filesystem_homedir);
filesystem_homedir += '/';
}
@@ -111,7 +111,7 @@ void init(std::string const & basename, std::string const & modname)
}
// create writedir if necessary
- if (!sys::isdirectory(filesystem_writedir))
+ if (!sys::directory_exists(filesystem_writedir))
sys::mkdir(filesystem_writedir);
filesystem_writedir += '/';
@@ -119,21 +119,21 @@ void init(std::string const & basename, std::string const & modname)
if (filesystem_modname.size()) {
// HOME/modname
dir.assign(filesystem_homedir + filesystem_modname);
- if (sys::isdirectory(dir)) {
+ if (sys::directory_exists(dir)) {
dir += '/';
filesystem_searchpath.push_back(dir);
}
// CURRENT/data/modname
dir.assign(current_datadir + filesystem_modname);
- if (sys::isdirectory(dir)) {
+ if (sys::directory_exists(dir)) {
dir += '/';
filesystem_searchpath.push_back(dir);
}
// PACKAGE_DATADIR/modname
std::string dir(package_datadir + '/' + filesystem_modname);
- if (sys::isdirectory(dir)) {
+ if (sys::directory_exists(dir)) {
dir += '/';
filesystem_searchpath.push_back(dir);
}
@@ -142,21 +142,21 @@ void init(std::string const & basename, std::string const & modname)
// basename search path
// HOME/basename
dir.assign(filesystem_homedir + filesystem_basename);
- if (sys::isdirectory(dir)) {
+ if (sys::directory_exists(dir)) {
dir += '/';
filesystem_searchpath.push_back(dir);
}
// PACKAGE_DATADIR/basename
dir.assign(package_datadir + '/' + filesystem_basename);
- if (sys::isdirectory(dir)) {
+ if (sys::directory_exists(dir)) {
dir += '/';
filesystem_searchpath.push_back(dir);
filesystem_datadir.assign(dir);
} else {
// CURRENT/data/basename
dir.assign(current_datadir + filesystem_basename);
- if (sys::isdirectory(dir)) {
+ if (sys::directory_exists(dir)) {
dir += '/';
filesystem_searchpath.push_back(dir);
filesystem_datadir.assign(dir);
diff --git a/src/filesystem/inifile.cc b/src/filesystem/inifile.cc
index 96bfd55..e83c08f 100644
--- a/src/filesystem/inifile.cc
+++ b/src/filesystem/inifile.cc
@@ -73,7 +73,7 @@ bool IniFile::getline() {
if (!inifile_ifs.is_open())
return false;
- if (inifile_ifs.getline(line, 1024)) {
+ if (inifile_ifs.getline(line, 1023)) {
std::string s(line);
aux::trim(s);
line_number++;
diff --git a/src/sys/sys.cc b/src/sys/sys.cc
index 59ed11d..01c9e3b 100644
--- a/src/sys/sys.cc
+++ b/src/sys/sys.cc
@@ -32,7 +32,38 @@
namespace sys
{
-bool isdirectory(const std::string &path)
+bool file_exists(const std::string &filename)
+{
+#ifdef _WIN32
+ struct ::_stat path_stat;
+ memset(&path_stat, 0, sizeof(struct ::_stat));
+
+ if (::_stat(filename.c_str(), &path_stat) != 0) {
+ return false;
+ }
+
+ if (path_stat.st_mode & _S_IFDIR) {
+ return false;
+ }
+ return true;
+#else
+ struct stat path_stat;
+ memset(&path_stat, 0, sizeof(path_stat));
+
+ if (stat(filename.c_str(), &path_stat) != 0) {
+ return false;
+ }
+
+ if (path_stat.st_mode & S_IFDIR) {
+ return false;
+ }
+
+ return true;
+#endif
+}
+
+
+bool directory_exists(const std::string &path)
{
#ifdef _WIN32
struct ::_stat path_stat;
diff --git a/src/sys/sys.h b/src/sys/sys.h
index 81eba94..5e41daa 100644
--- a/src/sys/sys.h
+++ b/src/sys/sys.h
@@ -22,7 +22,10 @@ namespace sys
typedef void(* signalfunc)(int signum);
/// returns true if a path exists and it is a directory
-bool isdirectory(const std::string &path);
+bool directory_exists(const std::string &path);
+
+/// returns true if a file exists
+bool file_exists(const std::string &filename);
/// create a directory
void mkdir(const std::string &path);