Skip to content

Commit

Permalink
move value root directory for relative path to filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Frantisek Boranek authored and Frantisek Boranek committed Sep 19, 2018
1 parent 014078f commit 5e3f50e
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 133 deletions.
12 changes: 6 additions & 6 deletions python/teng.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "tengfilesystem.h"

#include <iostream>
#include <memory>
#include <stdexcept>

#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
Expand Down Expand Up @@ -349,17 +350,16 @@ PyObject* Teng_Teng(TengObject *self, PyObject *args, PyObject *keywds) {
dictionaryCacheSize);

try {
FilesystemInterface_t* fsInterface = 0;
// create teng object
if (fileSystem) {
fsInterface = new FilesystemWrapper_t(fileSystem);
std::auto_ptr<FilesystemInterface_t> fsInterface(new FilesystemWrapper_t(fileSystem));
new (&s->teng) Teng_t(fsInterface.get(), settings);
fsInterface.release();
}
else {
fsInterface = new Filesystem_t();
new (&s->teng) Teng_t((root) ? root : std::string(), settings);
}

// create teng object
new (&s->teng) Teng_t((root) ? root : std::string(), settings, fsInterface);

// set default encoding
new (&s->defaultEncoding) std::string(encoding ? encoding
: DEFAULT_DEFAULT_ENCODING);
Expand Down
21 changes: 4 additions & 17 deletions src/teng.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,34 +86,21 @@ static int logErrors(const ContentType_t *contentType,
}

Teng_t::Teng_t(const std::string &root, const Teng_t::Settings_t &settings)
: root(root), filesystem(new Filesystem_t()), templateCache(0), err()
: filesystem(new Filesystem_t(root)), templateCache(0), err()
{
init(settings);
}

Teng_t::Teng_t(const std::string &root,
const Settings_t &settings,
FilesystemInterface_t *filesystem)
: root(root), filesystem(filesystem), templateCache(0), err()
Teng_t::Teng_t(FilesystemInterface_t *filesystem, const Settings_t &settings)
: filesystem(filesystem), templateCache(0), err()
{
init(settings);
}

void Teng_t::init(const Settings_t &settings)
{
// if not absolute path, prepend current working directory
if (root.empty() || !ISROOT(root)) {
char cwd[2048];
if (!getcwd(cwd, sizeof(cwd))) {
Error_t::Position_t pos;
err.logSyscallError(Error_t::LL_FATAL, pos, "Cannot get cwd");
throw std::runtime_error("Cannot get cwd.");
}
root = std::string(cwd) + '/' + root;
}

// create template cache
templateCache = new TemplateCache_t(root, filesystem,
templateCache = new TemplateCache_t(filesystem,
settings.programCacheSize,
settings.dictCacheSize);
}
Expand Down
9 changes: 1 addition & 8 deletions src/teng.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,10 @@ class Teng_t {
Teng_t(const std::string &root, const Settings_t &settings);

/** @short Create new engine.
* @param root root of relative paths
* @param settings teng options
* @param filesystem Filesystem to use; ownership is transferred
*/
Teng_t(const std::string &root,
const Settings_t &settings,
FilesystemInterface_t *filesystem);
Teng_t(FilesystemInterface_t *filesystem, const Settings_t &settings);

/** @short Destroy engine.
*/
Expand Down Expand Up @@ -181,10 +178,6 @@ class Teng_t {

void init(const Settings_t &settings);

/** @short Root of relative paths.
*/
std::string root;

/** @short Holds filesystem implementation.
*/
FilesystemInterface_t *filesystem;
Expand Down
10 changes: 1 addition & 9 deletions src/tengcache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,9 @@

namespace Teng {

int tengCreateKey(const std::string &root,
const std::string &_filename,
int tengCreateKey(const std::string &filename,
std::vector<std::string> &key)
{
std::string filename = _filename;
// if filename is relative prepend root
if (!filename.empty() && !ISROOT(filename))
filename = root + '/' + filename;

// normalize filename
tengNormalizeFilename(filename);
// add it to the key
key.push_back(filename);
return 0;
Expand Down
3 changes: 1 addition & 2 deletions src/tengcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ typedef std::vector<std::string> Key_t;
* @param key key vector (result)
* @return 0 OK !0 error
*/
int tengCreateKey(const std::string &root,
const std::string &filename, std::vector<std::string> &key);
int tengCreateKey(const std::string &filename, std::vector<std::string> &key);

/**
* @short
Expand Down
9 changes: 4 additions & 5 deletions src/tengconfiguration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@

namespace Teng {

Configuration_t::Configuration_t(const std::string &root)
: Dictionary_t(root), debug(false), errorFragment(false),
Configuration_t::Configuration_t(const FilesystemInterface_t *filesystem)
: Dictionary_t(filesystem), debug(false), errorFragment(false),
logToOutput(false), bytecode(false), watchFiles(true),
alwaysEscape(true), shortTag(false), maxIncludeDepth(10),
format(true), maxDebugValLength(40)
Expand All @@ -66,8 +66,7 @@ std::string strip(const std::string &str) {

} // namespace

int Configuration_t::processDirective(const FilesystemInterface_t *filesystem,
const std::string &directive,
int Configuration_t::processDirective(const std::string &directive,
const std::string &param,
Error_t::Position_t &pos)
{
Expand Down Expand Up @@ -126,7 +125,7 @@ int Configuration_t::processDirective(const FilesystemInterface_t *filesystem,
else if (directive == "disable") value = false;
else {
// other directive
return Dictionary_t::processDirective(filesystem, directive, param, pos);
return Dictionary_t::processDirective(directive, param, pos);
}

if (argument == "debug") debug = value;
Expand Down
7 changes: 4 additions & 3 deletions src/tengconfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include "tengdictionary.h"

namespace Teng {

class FilesystemInterface_t;

/**
* @short Configuration -- language independed dictionary and
Expand All @@ -55,7 +57,7 @@ class Configuration_t : public Dictionary_t {
*
* @param root path of root for locating files
*/
Configuration_t(const std::string &root = std::string());
Configuration_t(const FilesystemInterface_t *filesystem);

/**
* @short Destroy dictionary object.
Expand All @@ -70,8 +72,7 @@ class Configuration_t : public Dictionary_t {
* @param pos position in current file
* @return 0 OK !0 error
*/
virtual int processDirective(const FilesystemInterface_t *filesystem,
const std::string &directive,
virtual int processDirective(const std::string &directive,
const std::string &param,
Error_t::Position_t &pos);

Expand Down
28 changes: 9 additions & 19 deletions src/tengdictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@

namespace Teng {

int Dictionary_t::parse(const FilesystemInterface_t *filesystem,
const std::string &filename)
int Dictionary_t::parse(const std::string &filename)
{
level = MAX_RECURSION_LEVEL;
Error_t::Position_t pos(filename);
return parse(filesystem, filename, pos);
return parse(filename, pos);
}

Dictionary_t::~Dictionary_t() {
Expand Down Expand Up @@ -316,8 +315,7 @@ const std::string *Dictionary_t::lookup(const std::string &key) const {
return &f->second;
}

int Dictionary_t::parseString(const FilesystemInterface_t *filesystem,
const std::string &data,
int Dictionary_t::parseString(const std::string &data,
Error_t::Position_t &pos)
{
// position of newline
Expand Down Expand Up @@ -351,8 +349,7 @@ int Dictionary_t::parseString(const FilesystemInterface_t *filesystem,
}
// split directive to name and value
std::string::size_type sep = line.find_first_of(" \t\v", 1);
if (processDirective(filesystem,
line.substr(1, ((sep == std::string::npos)
if (processDirective(line.substr(1, ((sep == std::string::npos)
? sep : (sep - 1))),
((sep == std::string::npos)
? std::string()
Expand Down Expand Up @@ -415,30 +412,23 @@ int Dictionary_t::parseString(const FilesystemInterface_t *filesystem,
return ret;
}

int Dictionary_t::parse(const FilesystemInterface_t *filesystem,
const std::string &infilename,
int Dictionary_t::parse(const std::string &filename,
Error_t::Position_t &pos)
{
// if relative path => prepend root
std::string filename = infilename;
if (!filename.empty() && !ISROOT(filename) && (!root.empty()))
filename = root + '/' + filename;

// insert source into source list
sources.addSource(filesystem, filename, pos, err);

try {
Error_t::Position_t newPos(filename, 1);
return parseString(filesystem, filesystem->read(filename), newPos);
return parseString(filesystem->read(filename), newPos);
}
catch (const std::exception &e) {
err.logSyscallError(Error_t::LL_ERROR, pos, e.what());
return -1;
}
return -1;
}

int Dictionary_t::processDirective(const FilesystemInterface_t *filesystem,
const std::string &directive,
int Dictionary_t::processDirective(const std::string &directive,
const std::string &param,
Error_t::Position_t &pos)
{
Expand Down Expand Up @@ -472,7 +462,7 @@ int Dictionary_t::processDirective(const FilesystemInterface_t *filesystem,
// decrement recursion level
--level;
// parse given file
int ret = parse(filesystem, filename, pos);
int ret = parse(filename, pos);
// indecrement recursion level
++level;
return ret;
Expand Down
22 changes: 9 additions & 13 deletions src/tengdictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class Dictionary_t {
*
* @param root path of root for locating files
*/
Dictionary_t(const std::string &root)
: root(root), level(0), sources(), err(), expandValue(false),
Dictionary_t(const FilesystemInterface_t *filesystem)
: filesystem(filesystem), level(0), sources(), err(), expandValue(false),
replaceValue(false)
{}

Expand All @@ -77,8 +77,7 @@ class Dictionary_t {
* @param filename name of file to parse
* @return 0 OK !0 error
*/
int parse(const FilesystemInterface_t *filesystem,
const std::string &filename);
int parse(const std::string &filename);

/**
* @short Adds new entry into dictionary. Doesn't replace
Expand All @@ -103,7 +102,7 @@ class Dictionary_t {
*
* @return 0 OK !0 changed
*/
inline int check(const FilesystemInterface_t* filesystem) const {
inline int check() const {
return sources.isChanged(filesystem);
}

Expand Down Expand Up @@ -141,8 +140,7 @@ class Dictionary_t {
* @param pos position in current file
* @return 0 OK !0 error
*/
virtual int parseString(const FilesystemInterface_t *filesystem,
const std::string &data,
virtual int parseString(const std::string &data,
Error_t::Position_t &pos);

/**
Expand Down Expand Up @@ -178,8 +176,7 @@ class Dictionary_t {
* @param pos position in current file
* @return 0 OK !0 error
*/
virtual int processDirective(const FilesystemInterface_t *filesystem,
const std::string &directive,
virtual int processDirective(const std::string &directive,
const std::string &param,
Error_t::Position_t &pos);

Expand All @@ -202,8 +199,7 @@ class Dictionary_t {
* @param pos position in current file
* @return 0 OK !0 error
*/
int parse(const FilesystemInterface_t *filesystem,
const std::string &filename,
int parse(const std::string &filename,
Error_t::Position_t &pos);

/**
Expand All @@ -212,9 +208,9 @@ class Dictionary_t {
static const unsigned int MAX_RECURSION_LEVEL = 10;

/**
* @short Root directory for file lookup.
* @short Filesystem for file lookup.
*/
std::string root;
const FilesystemInterface_t *filesystem;

/**
* @short Current level of recursion. Valid only when parsing.
Expand Down
34 changes: 30 additions & 4 deletions src/tengfilesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,41 @@

#include "tengfilesystem.h"
#include "tengutil.h"
#include "tengplatform.h"

#include <boost/functional/hash.hpp>

namespace Teng {

static std::string makeFilename(const std::string& root, const std::string& filename_)
{
std::string filename =
(!root.empty() && !filename_.empty() && filename_[0] != '/')
? root + "/" + filename_
: filename_;

tengNormalizeFilename(filename);
return filename;
}

Filesystem_t::Filesystem_t(const std::string& root_)
: root(root_)
{
// if not absolute path, prepend current working directory
if (root.empty() || !ISROOT(root)) {
char cwd[2048];
if (!getcwd(cwd, sizeof(cwd))) {
throw std::runtime_error("Cannot get cwd.");
}
root = std::string(cwd) + '/' + root;
}
}

std::string Filesystem_t::read(const std::string& filename_) const
{
std::string result;

std::string filename(filename_);
tengNormalizeFilename(filename);

std::string filename = makeFilename(root, filename_);

FILE* fp = fopen(filename.c_str(), "rb");
if (fp == 0) throw std::runtime_error("Cannot open input file '" + filename + "'");
Expand All @@ -55,9 +79,11 @@ std::string Filesystem_t::read(const std::string& filename_) const
return result;
}

size_t Filesystem_t::hash(const std::string& filename) const
size_t Filesystem_t::hash(const std::string& filename_) const
{
std::size_t seed = 0;

std::string filename = makeFilename(root, filename_);

// stat given file
struct stat buf;
Expand Down
Loading

0 comments on commit 5e3f50e

Please sign in to comment.