summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorerihel <erihel@gmail.com>2013-03-17 19:01:32 +0100
committererihel <erihel@gmail.com>2013-03-17 19:01:32 +0100
commitd6bbc99c90ef586b041651d373524b30fe6c8676 (patch)
treee01eee683dc4e693450769a441a37c9d070b97d1 /src/common
parent9f5bef030d97e9ee20d8c635335fdafd854fed44 (diff)
downloadcolobot-d6bbc99c90ef586b041651d373524b30fe6c8676.tar.gz
colobot-d6bbc99c90ef586b041651d373524b30fe6c8676.tar.bz2
colobot-d6bbc99c90ef586b041651d373524b30fe6c8676.zip
* Changed file loading to fix issue #73
* Moved few functions from misc.cpp to profile.cpp (used to set/get user dir) * Removed some warnings * More work to change const char* to std::string * Some work on file path to fix issue #60 with bad slashes on POSIX platform
Diffstat (limited to 'src/common')
-rw-r--r--src/common/misc.cpp124
-rw-r--r--src/common/misc.h3
-rw-r--r--src/common/profile.cpp45
-rw-r--r--src/common/profile.h28
4 files changed, 75 insertions, 125 deletions
diff --git a/src/common/misc.cpp b/src/common/misc.cpp
index 2bce3b8..b96abca 100644
--- a/src/common/misc.cpp
+++ b/src/common/misc.cpp
@@ -25,10 +25,6 @@
#include <time.h>
-static bool g_bUserDir = false;
-static char g_userDir[100] = "";
-
-
// Returns a non-accented letter.
char GetNoAccent(char letter)
@@ -234,72 +230,11 @@ void TimeToAscii(time_t time, char *buffer)
#endif*/
}
-
-// Makes a copy of a file.
-
-bool Xfer(char* src, char* dst)
-{
- FILE *fs, *fd;
- char *buffer;
- int len;
-
- fs = fopen(src, "rb");
- if ( fs == 0 )
- {
- return false;
- }
-
- fd = fopen(dst, "wb");
- if ( fd == 0 )
- {
- fclose(fs);
- return false;
- }
-
- buffer = static_cast<char*>(malloc(10000));
-
- while ( true )
- {
- len = fread(buffer, 1, 10000, fs);
- if ( len == 0 ) break;
- fwrite(buffer, 1, len, fd);
- }
-
- free(buffer);
- fclose(fs);
- fclose(fd);
- return true;
-}
-
-// Copy a file into the temporary folder.
-
-bool CopyFileToTemp(char* filename)
-{
- char src[100];
- char dst[100];
- char save[100];
-
- UserDir(src, filename, "textures");
-
- strcpy(save, g_userDir);
- strcpy(g_userDir, "temp");
- UserDir(dst, filename, "textures");
- strcpy(g_userDir, save);
-
- //_mkdir("temp");
- system("mkdir temp");
-
- if ( !Xfer(src, dst) ) return false;
-
- strcpy(filename, dst);
- return true;
-}
-
// Copy a list of numbered files into the temporary folder.
bool CopyFileListToTemp(char* filename, int* list, int total)
{
- char name[100];
+ /*char name[100];
char ext[10];
char file[100];
char save[100];
@@ -329,8 +264,8 @@ bool CopyFileListToTemp(char* filename, int* list, int total)
UserDir(file, filename, "textures");
strcpy(filename, file);
strcpy(g_userDir, save);
-
- return true;
+*/
+ return false;
}
@@ -342,56 +277,3 @@ void AddExt(char* filename, const char* ext)
strcat(filename, ext);
}
-
-// Specifies the user folder.
-
-void UserDir(bool bUser, const char* dir)
-{
- g_bUserDir = bUser;
- strcpy(g_userDir, dir);
-}
-
-// Replaces the string %user% by the user folder.
-// in: dir = "%user%toto.txt"
-// def = "abc\"
-// out: buffer = "abc\toto.txt"
-
-void UserDir(char* buffer, const char* dir, const char* def)
-{
- char ddir[100];
- const char* add;
-
- if ( strstr(dir, "\\") == 0 && def[0] != 0 )
- {
- sprintf(ddir, "%s\\%s", def, dir);
- }
- else
- {
- strcpy(ddir, dir);
- }
- dir = ddir;
-
- while ( *dir != 0 )
- {
- if ( dir[0] == '%' &&
- dir[1] == 'u' &&
- dir[2] == 's' &&
- dir[3] == 'e' &&
- dir[4] == 'r' &&
- dir[5] == '%' ) // %user% ?
- {
- if ( g_bUserDir ) add = g_userDir;
- else add = def;
-
- while ( *add != 0 )
- {
- *buffer++ = *add++;
- }
- dir += 6; // jumps to %user%
- continue;
- }
-
- *buffer++ = *dir++;
- }
- *buffer = 0;
-}
diff --git a/src/common/misc.h b/src/common/misc.h
index f210706..e2ddc44 100644
--- a/src/common/misc.h
+++ b/src/common/misc.h
@@ -29,8 +29,5 @@ extern char GetToLower(char letter);
extern void TimeToAscii(time_t time, char *buffer);
-extern bool CopyFileToTemp(char* filename);
extern bool CopyFileListToTemp(char* filename, int* list, int total);
extern void AddExt(char* filename, const char* ext);
-extern void UserDir(bool bUser, const char* dir);
-extern void UserDir(char* buffer, const char* dir, const char* def);
diff --git a/src/common/profile.cpp b/src/common/profile.cpp
index 18cc187..2d11217 100644
--- a/src/common/profile.cpp
+++ b/src/common/profile.cpp
@@ -182,3 +182,48 @@ std::vector< std::string > CProfile::GetLocalProfileSection(std::string section,
return ret_list;
}
+
+
+void CProfile::SetUserDir(std::string dir)
+{
+ m_userDirectory = dir;
+}
+
+
+std::string CProfile::GetUserBasedPath(std::string dir, std::string default_dir)
+{
+ std::string path = dir;
+ boost::replace_all(path, "\\", "/");
+ if (dir.find("/") == std::string::npos) {
+ path = default_dir + "/" + dir;
+ }
+
+ if (m_userDirectory.length() > 0) {
+ boost::replace_all(path, "%user%", m_userDirectory);
+ } else {
+ boost::replace_all(path, "%user%", default_dir);
+ }
+
+ return fs::path(path).make_preferred().string();
+}
+
+
+bool CProfile::CopyFileToTemp(std::string filename)
+{
+ std::string src, dst;
+ std::string tmp_user_dir = m_userDirectory;
+
+ src = GetUserBasedPath(filename, "textures");
+ SetUserDir("temp");
+ dst = GetUserBasedPath(filename, "textures");
+ SetUserDir(tmp_user_dir);
+
+ fs::create_directory(fs::path(dst).parent_path().make_preferred().string());
+ fs::copy_file(src, dst, fs::copy_option::overwrite_if_exists);
+ if (fs::exists(dst)) {
+ return true;
+ }
+
+ return false;
+}
+
diff --git a/src/common/profile.h b/src/common/profile.h
index 9bc6c37..bcd76c3 100644
--- a/src/common/profile.h
+++ b/src/common/profile.h
@@ -21,14 +21,20 @@
#pragma once
-
#include "common/singleton.h"
+// this is just to fix problem with undefined reference when compiling with c++11 support
+#define BOOST_NO_SCOPED_ENUMS
+
#include <boost/property_tree/ptree.hpp>
+#include <boost/filesystem.hpp>
+#include <boost/algorithm/string/replace.hpp>
#include <string>
#include <vector>
+namespace fs = boost::filesystem;
+
/**
* \class CProfile
@@ -101,10 +107,30 @@ class CProfile : public CSingleton<CProfile>
* \return vector of values
*/
std::vector< std::string > GetLocalProfileSection(std::string section, std::string key);
+
+ /** Sets current user directory
+ * \param dir
+ */
+ void SetUserDir(std::string dir);
+
+ /** Returns path based on current user. Replaces %user% in path with current user dir or
+ * uses default_dir param if no user dir is specified
+ * \param dir
+ * \param default_dir
+ * \return path
+ */
+ std::string GetUserBasedPath(std::string dir, std::string default_dir);
+
+ /** opy a file into the temporary folder.
+ * \param filename
+ * \return true on success
+ */
+ bool CopyFileToTemp(std::string filename);
private:
boost::property_tree::ptree m_propertyTree;
bool m_profileNeedSave;
+ std::string m_userDirectory;
};
//! Global function to get profile instance