Skip to content

Commit

Permalink
💾 λcommon v1.6.6
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Aug 20, 2018
1 parent 7967bc9 commit 0ae2eb0
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 14 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ endif ()
set(HEADERS_CONNECTION include/lambdacommon/connection/address.h)
set(HEADERS_DOCUMENT include/lambdacommon/documents/document.h)
set(HEADERS_GRAPHICS include/lambdacommon/graphics/color.h)
set(HEADERS_FILESYSTEM include/lambdacommon/system/filesystem/filesystem.h)
set(HEADERS_FILESYSTEM include/lambdacommon/system/fs/filesystem.h)
set(HEADERS_EXCEPTIONS include/lambdacommon/exceptions/exceptions.h)
set(HEADERS_SYSTEM ${HEADERS_FILESYSTEM} include/lambdacommon/system/os.h include/lambdacommon/system/devices.h include/lambdacommon/system/terminal.h include/lambdacommon/system/system.h include/lambdacommon/system/uri.h)
set(HEADERS_BASE include/lambdacommon/lambdacommon.h include/lambdacommon/serializable.h include/lambdacommon/lstring.h include/lambdacommon/path.h include/lambdacommon/resources.h)
Expand All @@ -31,7 +31,7 @@ set(HEADERS_FILES ${HEADERS_CONNECTION} ${HEADERS_DOCUMENT} ${HEADERS_GRAPHICS}
set(SOURCES_CONNECTION src/connection/address.cpp)
set(SOURCES_DOCUMENT)
set(SOURCES_GRAPHICS src/graphics/color.cpp)
set(SOURCES_FILESYSTEM src/system/filesystem/filesystem.cpp)
set(SOURCES_FILESYSTEM src/system/fs/filesystem.cpp)
set(SOURCES_SERIALIZERS)
set(SOURCES_SYSTEM ${SOURCES_FILESYSTEM} src/system/os.cpp src/system/terminal.cpp src/system/system.cpp src/system/uri.cpp)
set(SOURCES_BASE src/lambdacommon.cpp src/serializable.cpp src/lstring.cpp src/path.cpp src/resources.cpp)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ Build the sources with CMake and make and install with `make install`, and keep

## Use in CMake

Use `Findlambdacommon.cmake` in [LambdaCMakeModules](https://github.com/AperLambda/LambdaCMakeModules.git) to find λcommon on your computer.
Use `FindLAMBDACOMMON.cmake` in [LambdaCMakeModules](https://github.com/AperLambda/LambdaCMakeModules.git) to find λcommon on your computer.
2 changes: 1 addition & 1 deletion include/lambdacommon/lambdacommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

#define LAMBDACOMMON_VERSION_MAJOR 1
#define LAMBDACOMMON_VERSION_MINOR 6
#define LAMBDACOMMON_VERSION_PATCH 5
#define LAMBDACOMMON_VERSION_PATCH 6

namespace lambdacommon
{
Expand Down
46 changes: 46 additions & 0 deletions include/lambdacommon/resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#ifndef LAMBDACOMMON_RESOURCES_H
#define LAMBDACOMMON_RESOURCES_H

#include "system/fs/filesystem.h"
#include "lstring.h"
#include <utility>

#ifdef LAMBDA_WINDOWS
# pragma warning(push)
# pragma warning(disable:4251)
Expand Down Expand Up @@ -73,6 +75,50 @@ namespace lambdacommon

bool operator<(const ResourceName &other) const;
};

class LAMBDACOMMON_API ResourcesManager
{
private:
lambdacommon::fs::FilePath _workingDirectory;

public:
ResourcesManager(const lambdacommon::fs::FilePath &workingDirectory = fs::getCurrentWorkingDirectory());

ResourcesManager(const ResourcesManager &resourcesManager);

ResourcesManager(ResourcesManager &&resourcesManager) noexcept;

/*! @brief Gets the working directory of the resource manager.
*
* @return The working directory.
*/
const lambdacommon::fs::FilePath &getWorkingDirectory() const;

/*! @brief Checks whether the resource exists or not.
*
* @param resourceName The resource to check.
* @param extension The extension of the resource file.
* @return True if the resource exists else false.
*/
bool doesResourceExist(const ResourceName &resourceName, const std::string &extension) const;

lambdacommon::fs::FilePath
getResourcePath(const ResourceName &resourceName, const std::string &extension) const;

/*! @brief Loads the resource content into a string value.
*
* @param resourceName The resource to load.
* @param extension The extension of the resource file.
* @return The resource content if successfully loaded else an empty string.
*/
std::string loadResource(const ResourceName &resourceName, const std::string &extension) const;

ResourcesManager &operator=(const ResourcesManager &resourcesManager);

ResourcesManager &operator=(ResourcesManager &&resourcesManager) noexcept;

bool operator==(const ResourcesManager &other) const;
};
}

#ifdef LAMBDA_WINDOWS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ namespace lambdacommon
* Operators
*/

FilePath operator/(const FilePath &other);
FilePath operator/(const FilePath &other) const;

FilePath &operator=(const FilePath &path);

FilePath &operator=(FilePath &&path) noexcept;

bool operator==(const FilePath &_path);
bool operator==(const FilePath &_path) const;

bool operator!=(const FilePath &_path);
bool operator!=(const FilePath &_path) const;
};

#ifdef LAMBDA_WINDOWS
Expand Down
2 changes: 1 addition & 1 deletion include/lambdacommon/system/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef LAMBDACOMMON_SYSTEM_H
#define LAMBDACOMMON_SYSTEM_H

#include "filesystem/filesystem.h"
#include "fs/filesystem.h"
#include "devices.h"
#include "os.h"
#include "terminal.h"
Expand Down
2 changes: 1 addition & 1 deletion include/lambdacommon/system/uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef LAMBDACOMMON_URL_H
#define LAMBDACOMMON_URL_H

#include "filesystem/filesystem.h"
#include "fs/filesystem.h"
#include "../connection/address.h"

namespace lambdacommon
Expand Down
71 changes: 71 additions & 0 deletions src/resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "../include/lambdacommon/resources.h"
#include <stdexcept>
#include <tuple>
#include <fstream>
#include <sstream>

namespace lambdacommon
{
Expand Down Expand Up @@ -78,4 +80,73 @@ namespace lambdacommon
{
return std::tie(_domain, _path) < std::tie(other._domain, other._path);
}


ResourcesManager::ResourcesManager(const fs::FilePath &workingDirectory) : _workingDirectory(
workingDirectory / "resources")
{}

ResourcesManager::ResourcesManager(const ResourcesManager &resourcesManager) = default;

ResourcesManager::ResourcesManager(ResourcesManager &&resourcesManager) noexcept : _workingDirectory(
std::move(resourcesManager._workingDirectory))
{}

const lambdacommon::fs::FilePath &ResourcesManager::getWorkingDirectory() const
{
return _workingDirectory;
}

bool ResourcesManager::doesResourceExist(const ResourceName &resourceName, const std::string &extension) const
{
return getResourcePath(resourceName, extension).exists();
}

lambdacommon::fs::FilePath
ResourcesManager::getResourcePath(const ResourceName &resourceName, const std::string &extension) const
{
return (_workingDirectory / resourceName.getDomain()) / (resourceName.getName() + "." + extension);
}

std::string
ResourcesManager::loadResource(const ResourceName &resourceName, const std::string &extension) const
{
auto resourcePath = getResourcePath(resourceName, extension);
if (!resourcePath.exists())
return "";
try
{
std::ifstream fileStream{resourcePath.toString()};
std::stringstream outputStream;
outputStream << fileStream.rdbuf();
fileStream.close();
return outputStream.str();
}
catch (std::exception &e)
{
return "";
}
}

ResourcesManager &ResourcesManager::operator=(const ResourcesManager &other)
{
if (this != &other)
{
if (other._workingDirectory != _workingDirectory)
_workingDirectory = other._workingDirectory;
}
return *this;
}

ResourcesManager &ResourcesManager::operator=(ResourcesManager &&other) noexcept
{
if (this != &other)
_workingDirectory = std::move(other._workingDirectory);
return *this;
}

bool ResourcesManager::operator==(const ResourcesManager &other) const
{
return other._workingDirectory == _workingDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* see the LICENSE file.
*/

#include "../../../include/lambdacommon/system/filesystem/filesystem.h"
#include "../../../include/lambdacommon/system/fs/filesystem.h"
#include "../../../include/lambdacommon/lstring.h"
#include <stdexcept>
#include <sstream>
Expand Down Expand Up @@ -243,7 +243,7 @@ namespace lambdacommon
FilePath FilePath::sub(const FilePath &other) const
{
if (other._absolute)
throw std::runtime_error("lambdacommon->system/filesystem/filesystem.cpp@FilePath.sub(const FilePath&)(Line " + std::to_string(__LINE__ - 1) + "): Expected a relative path as argument!");
throw std::runtime_error("lambdacommon->system/fs/filesystem.cpp@FilePath.sub(const FilePath&)(Line " + std::to_string(__LINE__ - 1) + "): Expected a relative path as argument!");

FilePath result(*this);

Expand All @@ -257,7 +257,7 @@ namespace lambdacommon
* OPERATORS
*/

FilePath FilePath::operator/(const FilePath &other)
FilePath FilePath::operator/(const FilePath &other) const
{
return sub(other);
}
Expand All @@ -280,12 +280,12 @@ namespace lambdacommon
return *this;
}

bool FilePath::operator==(const FilePath &_p)
bool FilePath::operator==(const FilePath &_p) const
{
return _p._path == _path;
}

bool FilePath::operator!=(const FilePath &_p)
bool FilePath::operator!=(const FilePath &_p) const
{
return _p._path != _path;
}
Expand Down

0 comments on commit 0ae2eb0

Please sign in to comment.