Skip to content

Commit

Permalink
Added PathUtil class. Move FileUtil to util subdir. Added path normal…
Browse files Browse the repository at this point in the history
…ization to prefix determination. Improved Tests.
  • Loading branch information
dedeibel committed Oct 25, 2010
1 parent 8439810 commit 81a9a21
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 128 deletions.
3 changes: 2 additions & 1 deletion cmake/build_annoyme.cmake
Expand Up @@ -25,7 +25,8 @@ SET(ANNOYME_SRC
SoundOutputFactory.cpp

ResourceLoader.cpp
FileUtil.cpp
util/FileUtil.cpp
util/PathUtil.cpp
config/AnnoymeConfiguration.cpp
config/BasicConfiguration.cpp
config/ConfigurationMap.cpp
Expand Down
3 changes: 2 additions & 1 deletion cmake/test.cmake
Expand Up @@ -16,7 +16,8 @@ FILE(GLOB ConfigSRC
${CMAKE_CURRENT_SOURCE_DIR}/config/AggregateConfiguration.cpp
${CMAKE_CURRENT_SOURCE_DIR}/config/YAMLConfig.cpp
${CMAKE_CURRENT_SOURCE_DIR}/config/SystemConfigurationLinux.cpp
${CMAKE_CURRENT_SOURCE_DIR}/FileUtil.cpp
${CMAKE_CURRENT_SOURCE_DIR}/util/FileUtil.cpp
${CMAKE_CURRENT_SOURCE_DIR}/util/PathUtil.cpp
)
SET(UnitTestDep_SRCS ${ConfigSRC})

Expand Down
8 changes: 2 additions & 6 deletions src/ResourceLoader.cpp
Expand Up @@ -34,13 +34,12 @@ using namespace std;
#include "exceptions.h"

#include "ResourceLoader.h"
#include "FileUtil.h"
#include "util/FileUtil.h"

#include "config/Configuration.h"
#include "config/BasicConfiguration.h"
#include "config/AnnoymeConfiguration.h"

#include <iostream> // TODO DEBUG
ResourceLoader::ResourceLoader(FileUtil* fileUtil) :
m_fileUtil(fileUtil)
{
Expand Down Expand Up @@ -78,8 +77,6 @@ void ResourceLoader::init()
throw AnnoymeException(
"Could not determine resource path. Please check your compile parameters and installation paths.");
}

std::cout << "Resource path is: " << m_path << std::endl; // TODO DEBUG
}

// TODO document methods and about "resource paths" relative to resource dir
Expand All @@ -100,8 +97,7 @@ void ResourceLoader::listResources(const string &dir, vector<string> &resources)

string ResourceLoader::getPath(const string &resource)
{
return m_path + "/"
+ resource;
return m_path + "/" + resource;
}

void ResourceLoader::getContent(const string &resource, char **data,
Expand Down
42 changes: 4 additions & 38 deletions src/config/AnnoymeConfiguration.cpp
Expand Up @@ -43,6 +43,8 @@ using namespace std;
#include "YAMLConfig.h"
#include "AnnoymeConfiguration.h"

#include "util/PathUtil.h"

/* Created by cmake */
#include "config.h"

Expand Down Expand Up @@ -70,7 +72,7 @@ void AnnoymeConfiguration::initWithBinaryPath(const std::string &binary_path)

AnnoymeConfiguration::AnnoymeConfiguration() :
m_buildConfig(new ConfigurationMap()), m_yamlConfig(new YAMLConfig()),
m_configs(new AggregateConfiguration())
m_configs(new AggregateConfiguration()), m_pathUtil(new PathUtil())
{

}
Expand Down Expand Up @@ -102,7 +104,7 @@ void AnnoymeConfiguration::init(const string& binary_path)
string yamlPath = sys->getNormalized("system.home") + "/"
+ ANNOYME_CONFIG_NAME;

m_buildConfig->setNormalized("dynamic_prefix", getDynamicPrefix(
m_buildConfig->setNormalized("dynamic_prefix", m_pathUtil->getDynamicPrefix(
sys->getNormalized("system.pwd"), binary_path));

m_yamlConfig->setConfigFilePath(yamlPath);
Expand Down Expand Up @@ -140,39 +142,3 @@ std::string AnnoymeConfiguration::getNormalized(const std::string &path)
{
return m_configs->getNormalized(path);
}

std::string AnnoymeConfiguration::getDynamicPrefix(const std::string &pwd,
const std::string &binary_path) const
{
// TODO write properly and extract method

// If binary path is empty or not containing a directory, return pwd
if (binary_path.empty() || binary_path.find("/") == binary_path.npos) {
return pwd;
}
else {
std::string path;
if (binary_path.substr(0, 1).compare("/") == 0) {
path = binary_path;
}
else {
/*
* Build the complete path pwd + binary path, then go one up
*/
path = pwd + "/" + binary_path;
}

const std::string searchString = "/./";
const std::string replaceString = "/";
std::string::size_type pos = 0;
while ((pos = path.find(searchString, pos)) != string::npos) {
path.replace(pos, searchString.size(), replaceString);
pos++;
}
/* Path without the binary, basename so to speak */
path = path.substr(0, path.find_last_of("/"));
/* One up */
path = path.substr(0, path.find_last_of("/"));
return path;
}
}
13 changes: 6 additions & 7 deletions src/config/AnnoymeConfiguration.h
Expand Up @@ -32,6 +32,7 @@ class YAMLConfig;
class ConfigurationMap;
class AggregateConfiguration;
class UnknownOptionException;
class PathUtil;

class AnnoymeConfiguration : public BasicConfiguration
{
Expand All @@ -49,14 +50,12 @@ class AnnoymeConfiguration : public BasicConfiguration
private:
AnnoymeConfiguration();

protected:
std::string getDynamicPrefix(const std::string &pwd, const std::string &binary_path) const;

private:
static AnnoymeConfiguration *m_annoymeConfiguration;
ConfigurationMap *m_buildConfig;
YAMLConfig *m_yamlConfig;
AggregateConfiguration *m_configs;
static AnnoymeConfiguration* m_annoymeConfiguration;
ConfigurationMap* m_buildConfig;
YAMLConfig* m_yamlConfig;
AggregateConfiguration* m_configs;
PathUtil* m_pathUtil;

friend class AnnoymeConfigurationTest;
};
Expand Down
44 changes: 0 additions & 44 deletions src/config/AnnoymeConfigurationTest.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/config/YAMLConfig.cpp
Expand Up @@ -37,7 +37,7 @@
using namespace std;

#include "exceptions.h"
#include "FileUtil.h"
#include "util/FileUtil.h"

#include "config/Configuration.h"
#include "config/BasicConfiguration.h"
Expand Down
40 changes: 22 additions & 18 deletions src/FileUtil.cpp → src/util/FileUtil.cpp
Expand Up @@ -30,24 +30,25 @@
#include <vector>

#include <cstring>
using namespace std;
#include "exceptions.h"

using namespace std;

#include <cerrno>
#include "exceptions.h"
#include "FileUtil.h"
#include "util/FileUtil.h"

extern "C"
{
#include <sys/types.h>
#include <sys/stat.h>
#include <glob.h>
#include <unistd.h>
#include <limits.h>
}

bool FileUtil::isAccessableDirectory(const string &path) throw (AnnoyErrnoException)
#include <cstdlib>

bool FileUtil::isAccessableDirectory(const std::string &path) const
throw (AnnoyErrnoException)
{
struct stat buf;
int retval = stat(path.c_str(), &buf);
Expand All @@ -58,17 +59,19 @@ bool FileUtil::isAccessableDirectory(const string &path) throw (AnnoyErrnoExcept
return S_ISDIR(buf.st_mode) && access(path.c_str(), R_OK);
}

bool FileUtil::isAccessableFile(const string &path) throw (AnnoyErrnoException)
bool FileUtil::isAccessableFile(const std::string &path) const
throw (AnnoyErrnoException)
{
struct stat buf;
int retval = stat(path.c_str(), &buf);
if (retval == -1) {
throw AnnoyErrnoException("Could not stat path", path, errno);
}
return (S_ISREG(buf.st_mode) || S_ISLNK(buf.st_mode) || S_ISCHR(buf.st_mode)) && access(path.c_str(), R_OK);
return (S_ISREG(buf.st_mode) || S_ISLNK(buf.st_mode) || S_ISCHR(buf.st_mode))
&& access(path.c_str(), R_OK);
}

bool FileUtil::isDirectory(const string &path)
bool FileUtil::isDirectory(const std::string &path) const
{
struct stat buf;
int retval = stat(path.c_str(), &buf);
Expand All @@ -79,7 +82,7 @@ bool FileUtil::isDirectory(const string &path)
return S_ISDIR(buf.st_mode);
}

bool FileUtil::isFile(const string &path)
bool FileUtil::isFile(const std::string &path) const
{
struct stat buf;
int retval = stat(path.c_str(), &buf);
Expand All @@ -89,12 +92,12 @@ bool FileUtil::isFile(const string &path)
return S_ISREG(buf.st_mode) || S_ISLNK(buf.st_mode) || S_ISCHR(buf.st_mode);
}

bool FileUtil::isReadable(const string &path)
bool FileUtil::isReadable(const std::string &path) const
{
return access(path.c_str(), R_OK) == 0;
}

bool FileUtil::copy(const string &src, const string &dst)
bool FileUtil::copy(const std::string &src, const std::string &dst) const
{
std::ifstream ifs(src.c_str()); // I'll never get why they excpect a cstring
if (!ifs) {
Expand Down Expand Up @@ -124,22 +127,22 @@ bool FileUtil::copy(const string &src, const string &dst)
return true;
}

string findFile(const string &filename, const vector<string> &paths)
std::string FileUtil::findFile(const std::string &filename, const std::vector<std::string> &paths) const
throw (FileNotFoundException)
{
vector<string>::const_iterator it = paths.begin();
std::vector<std::string>::const_iterator it = paths.begin();

while (it != paths.end()) {
ifstream file(it->c_str(), ios::in | ios::binary);
std::ifstream file(it->c_str(), std::ios::in | std::ios::binary);
if (file.is_open()) {
return *it;
}
}
throw FileNotFoundException(filename, "reading");
}

void loadFile(const string &filename, const vector<string> paths, char **data,
unsigned int *size) throw (FileNotFoundException)
void FileUtil::loadFile(const std::string &filename, const vector<string> paths, char **data,
unsigned int *size) const throw (FileNotFoundException)
{
vector<string>::const_iterator it = paths.begin();

Expand All @@ -157,7 +160,7 @@ void loadFile(const string &filename, const vector<string> paths, char **data,
throw FileNotFoundException(filename, "reading");
}

void FileUtil::loadFile(const string &path, char** data, unsigned int* size)
void FileUtil::loadFile(const std::string &path, char** data, unsigned int* size) const
throw (FileNotFoundException)
{
ifstream file(path.c_str(), ios::in | ios::binary | ios::ate);
Expand All @@ -175,7 +178,7 @@ void FileUtil::loadFile(const string &path, char** data, unsigned int* size)

// TODO document methods, specify return values
void FileUtil::listFiles(const std::string &path,
std::vector<std::string> &files) throw (IllegalArgumentException)
std::vector<std::string> &files) const throw (IllegalArgumentException)
{
if (!isDirectory(path)) {
throw IllegalArgumentException("Given path is not a directory: " + path);
Expand All @@ -190,3 +193,4 @@ void FileUtil::listFiles(const std::string &path,
}
globfree(&globbuf);
}

28 changes: 16 additions & 12 deletions src/FileUtil.h → src/util/FileUtil.h
Expand Up @@ -34,38 +34,42 @@ class IllegalArgumentException;
class FileUtil
{
public:
bool copy(const std::string &src, const std::string &dst);
bool copy(const std::string &src, const std::string &dst) const;
std::string findFile(const std::string &filename, const std::vector<
std::string> paths) throw (FileNotFoundException);
void loadFile(const string &path, char** data, unsigned int* size)
throw (FileNotFoundException);
void loadFile(const std::string &filename,
const std::vector<std::string> paths, char** data, unsigned int* size)
std::string> paths) const throw (FileNotFoundException);
std::string findFile(const std::string &filename, const std::vector<
std::string> &paths) const throw (FileNotFoundException);
void loadFile(const std::string &filename, const vector<string> paths,
char **data, unsigned int *size) const throw (FileNotFoundException);
void loadFile(const std::string &path, char** data, unsigned int* size) const
throw (FileNotFoundException);
void listFiles(const std::string &dirname, std::vector<std::string> &files)
void
listFiles(const std::string &dirname, std::vector<std::string> &files) const
throw (IllegalArgumentException);

/**
* Returns true if the directory exists and is a readable directory. If there is a problem accessing the directory, invalid path, not existing, too long, ... an exception is thrown.
*/
bool isAccessableDirectory(const string &path) throw (AnnoyErrnoException);
bool isAccessableDirectory(const std::string &path) const
throw (AnnoyErrnoException);

/**
* Returns true if the file exists and is readable. If there is a problem accessing the directory, invalid path, not existing, too long, ... an exception is thrown.
*/
bool isAccessableFile(const string &path) throw (AnnoyErrnoException);
bool isAccessableFile(const std::string &path) const
throw (AnnoyErrnoException);

/**
* Returns true if the directory exists and is a directory.
*/
bool isDirectory(const string &path);
bool isDirectory(const std::string &path) const;

/**
* Returns true if the file exists and is a file.
*/
bool isFile(const string &path);
bool isFile(const std::string &path) const;

bool isReadable(const string &path);
bool isReadable(const std::string &path) const;
};

#endif // FILEUTIL_H

0 comments on commit 81a9a21

Please sign in to comment.