Permalink
Browse files

Added Path

  • Loading branch information...
1 parent e475ff8 commit 270b398a6da8123ee0ebd3f0dda41804d1930da8 @Zguy committed Mar 1, 2012
@@ -23,13 +23,14 @@ THE SOFTWARE.
#define PZ_Archive_h__
#include <ProtoZed/DataChunk.h>
+#include <ProtoZed/Path.h>
#include <string>
#include <vector>
namespace PZ
{
- typedef std::vector<std::string> FileList;
+ typedef std::vector<Path> FileList;
class Archive
{
@@ -39,12 +40,12 @@ namespace PZ
virtual ~Archive()
{}
- virtual bool Open(const std::string &filename) = 0;
+ virtual bool Open(const Path &filename) = 0;
virtual bool Close() = 0;
virtual bool IsOpen() const = 0;
- virtual bool Has(const std::string &filename) const = 0;
- virtual DataChunk Get(const std::string &filename) const = 0;
+ virtual bool Has(const Path &filename) const = 0;
+ virtual DataChunk Get(const Path &filename) const = 0;
virtual void GetAllFiles(FileList &list) const = 0;
};
@@ -32,17 +32,17 @@ namespace PZ
FileSystemArchive();
~FileSystemArchive();
- virtual bool Open(const std::string &filename);
+ virtual bool Open(const Path &filename);
virtual bool Close();
virtual bool IsOpen() const;
- virtual bool Has(const std::string &filename) const;
- virtual DataChunk Get(const std::string &filename) const;
+ virtual bool Has(const Path &filename) const;
+ virtual DataChunk Get(const Path &filename) const;
virtual void GetAllFiles(FileList &list) const;
private:
- std::string basePath;
+ Path basePath;
};
}
@@ -24,6 +24,7 @@ THE SOFTWARE.
#include <ProtoZed/Component.h>
#include <ProtoZed/Vector2.h>
+#include <ProtoZed/Path.h>
namespace PZ
{
@@ -35,11 +36,11 @@ namespace PZ
Sprite(const EntityID &owner, EntityManager &manager);
~Sprite();
- inline const std::string &GetSprite() const
+ inline const Path &GetSprite() const
{
return sprite;
}
- inline void SetSprite(const std::string &newSprite)
+ inline void SetSprite(const Path &newSprite)
{
sprite = newSprite;
}
@@ -54,7 +55,7 @@ namespace PZ
}
private:
- std::string sprite;
+ Path sprite;
Vector2f center;
};
}
View
@@ -0,0 +1,81 @@
+/*
+Copyright (c) 2012 Johannes Häggqvist
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+#ifndef PZ_Path_h__
+#define PZ_Path_h__
+
+#include <string>
+
+namespace PZ
+{
+ class Path
+ {
+ public:
+ Path();
+ Path(const Path &other);
+ Path(const std::string &path);
+ Path(const char *path);
+ ~Path();
+
+ Path &operator=(const Path &other);
+ Path &operator=(const std::string &path);
+ Path &operator=(const char *path);
+
+ inline bool operator==(const Path &other) const
+ {
+ return (path == other.path);
+ }
+ inline bool operator!=(const Path &other) const
+ {
+ return (path != other.path);
+ }
+
+ inline bool operator<(const Path &other) const
+ {
+ return (path < other.path);
+ }
+ inline bool operator>(const Path &other) const
+ {
+ return (path > other.path);
+ }
+
+ inline bool IsEmpty() const
+ {
+ return path.empty();
+ }
+ inline void Clear()
+ {
+ path.clear();
+ }
+
+ const std::string &ToString() const
+ {
+ return path;
+ }
+
+ private:
+ void normalize();
+
+ std::string path;
+ };
+}
+
+#endif // PZ_Path_h__
@@ -32,6 +32,7 @@ THE SOFTWARE.
#include <ProtoZed/Math.h>
#include <ProtoZed/UniqueIDGenerator.h>
#include <ProtoZed/Messages.h>
+#include <ProtoZed/Path.h>
#include <ProtoZed/ServiceList.h>
#include <ProtoZed/AppStateManager.h>
@@ -23,6 +23,7 @@ THE SOFTWARE.
#define PZ_Resource_h__
#include <ProtoZed/DataChunk.h>
+#include <ProtoZed/Path.h>
namespace PZ
{
@@ -36,7 +37,7 @@ namespace PZ
virtual ~Resource()
{}
- const std::string &GetFilename() const
+ const Path &GetFilename() const
{
return filename;
}
@@ -45,7 +46,7 @@ namespace PZ
virtual bool load(const DataChunk &data) = 0;
virtual bool unload() = 0;
- std::string filename;
+ Path filename;
};
}
@@ -23,6 +23,7 @@ THE SOFTWARE.
#define PZ_ResourceManager_h__
#include <ProtoZed/ObjectFactory/ObjectFactory.h>
+#include <ProtoZed/Path.h>
#include <ProtoZed/Archive.h>
#include <ProtoZed/ArchiveType.h>
#include <ProtoZed/Resource/Resource.h>
@@ -61,23 +62,23 @@ namespace PZ
void SetType(const std::string &extension, const ResourceType &type);
- bool AddArchive(const std::string &filename, const ArchiveType &type, bool indexAll = true, bool onlyIndexRegisteredTypes = true);
- bool RemoveArchive(const std::string &filename);
+ bool AddArchive(const Path &filename, const ArchiveType &type, bool indexAll = true, bool onlyIndexRegisteredTypes = true);
+ bool RemoveArchive(const Path &filename);
void IndexAll(bool onlyIndexRegisteredTypes = true);
- bool IndexFile(const std::string &filename);
+ bool IndexFile(const Path &filename);
void LoadAll();
void UnloadAll();
- bool Load(const std::string &filename);
- bool Unload(const std::string &filename);
+ bool Load(const Path &filename);
+ bool Unload(const Path &filename);
template<class T>
- const T *Get(const std::string &filename, bool autoLoad = true)
+ const T *Get(const Path &filename, bool autoLoad = true)
{
return dynamic_cast<const T*>(Get(filename, autoLoad));
}
- const Resource *Get(const std::string &filename, bool autoLoad = true);
+ const Resource *Get(const Path &filename, bool autoLoad = true);
private:
class Impl;
@@ -24,39 +24,6 @@ THE SOFTWARE.
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
-namespace
-{
- boost::filesystem::path normalizePath(boost::filesystem::path &path)
- {
- std::vector<boost::filesystem::path> pathParts;
-
- for (boost::filesystem::path::iterator it = path.begin(); it != path.end(); ++it)
- {
- std::string part = (*it).generic_string();
-
- if (part == ".")
- {
- continue;
- }
- if (part == "..")
- {
- pathParts.pop_back();
- continue;
- }
-
- pathParts.push_back(*it);
- }
-
- boost::filesystem::path final;
- for (std::vector<boost::filesystem::path>::iterator it = pathParts.begin(); it != pathParts.end(); ++it)
- {
- final /= (*it);
- }
-
- return final;
- }
-}
-
namespace PZ
{
FileSystemArchive::FileSystemArchive() : basePath("")
@@ -66,9 +33,9 @@ namespace PZ
{
}
- bool FileSystemArchive::Open(const std::string &filename)
+ bool FileSystemArchive::Open(const Path &filename)
{
- boost::filesystem::path path(filename);
+ boost::filesystem::path path(filename.ToString());
if (boost::filesystem::is_directory(path))
{
basePath = path.generic_string();
@@ -80,21 +47,21 @@ namespace PZ
}
bool FileSystemArchive::Close()
{
- basePath = "";
+ basePath.Clear();
return true;
}
bool FileSystemArchive::IsOpen() const
{
- return !basePath.empty();
+ return !basePath.IsEmpty();
}
- bool FileSystemArchive::Has(const std::string &filename) const
+ bool FileSystemArchive::Has(const Path &filename) const
{
if (IsOpen())
{
- boost::filesystem::path base(basePath);
- boost::filesystem::path path(filename);
+ boost::filesystem::path base(basePath.ToString());
+ boost::filesystem::path path(filename.ToString());
boost::filesystem::path fullPath = base / path;
return boost::filesystem::is_regular_file(fullPath);
}
@@ -103,12 +70,12 @@ namespace PZ
return false;
}
}
- DataChunk FileSystemArchive::Get(const std::string &filename) const
+ DataChunk FileSystemArchive::Get(const Path &filename) const
{
if (IsOpen())
{
- boost::filesystem::path base(basePath);
- boost::filesystem::path path(filename);
+ boost::filesystem::path base(basePath.ToString());
+ boost::filesystem::path path(filename.ToString());
boost::filesystem::path fullPath = base / path;
if (boost::filesystem::is_regular_file(fullPath))
{
@@ -138,14 +105,14 @@ namespace PZ
{
list.clear();
- boost::filesystem::path base(basePath);
+ boost::filesystem::path base(basePath.ToString());
for (boost::filesystem::recursive_directory_iterator it = boost::filesystem::recursive_directory_iterator(base); it != boost::filesystem::recursive_directory_iterator(); ++it)
{
- boost::filesystem::path filePath = (*it).path();
+ const boost::filesystem::path &filePath = (*it).path();
if (boost::filesystem::is_regular_file(filePath))
{
- list.push_back(normalizePath(filePath).generic_string());
+ list.push_back(filePath.generic_string());
}
}
}
Oops, something went wrong.

0 comments on commit 270b398

Please sign in to comment.