Skip to content

Commit

Permalink
some makefile improvements
Browse files Browse the repository at this point in the history
added sequences


git-svn-id: https://projectname.googlecode.com/svn/trunk@3 c416075f-80b4-e980-4839-00ea3ed24e77
  • Loading branch information
[email protected] committed Mar 14, 2011
1 parent 22a3c1b commit 4b6a2bd
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 40 deletions.
37 changes: 18 additions & 19 deletions trunk/Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
SRC_DIR = src
PROJECT_NAME = pn
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
LIBS = boost_program_options boost_filesystem boost_system
CFLAGS = -O2 -Wall -pedantic
PROJECT_NAME = pn
SRC_DIR = src
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
OBJS = $(patsubst $(SRC_DIR)/%.cpp,%.o,$(SRCS))

builds = debug release
debug_flags = -ggdb
release_flags = -DNDEBUG

.PHONY: first debug release makedirs clean
first: debug
debug:
@make makedirs DIR=debug
@make debug/$(PROJECT_NAME) DIR=debug FLAGS=-ggdb
release:
@make makedirs DIR=release
@make release/$(PROJECT_NAME) DIR=release FLAGS=-DNDEBUG
makedirs:
-@mkdir -p $(dir $(subst $(SRC_DIR),$(DIR),$(SRCS))) $(dir $(subst $(SRC_DIR),.depend,$(SRCS)))
.PHONY: $(builds) clean
$(builds): %: %-makedirs %/$(PROJECT_NAME)
$(addsuffix -makedirs,$(builds)): %-makedirs:
-@mkdir -p $(dir $(subst $(SRC_DIR),$*,$(SRCS))) $(dir $(subst $(SRC_DIR),.depend,$(SRCS)))
clean:
-@rm -rf debug release $(DIR) .depend
-@rm -rf $(builds) .depend

-include $(patsubst $(SRC_DIR)/%.cpp, .depend/%.dep, $(SRCS))
-include $(patsubst $(SRC_DIR)/%.cpp,.depend/%.dep,$(SRCS))

$(DIR)/$(PROJECT_NAME): $(patsubst $(SRC_DIR)/%.cpp, $(DIR)/%.o, $(SRCS))
g++ $^ -o $@ $(patsubst %,-l%,$(LIBS))
$(DIR)/%.o: $(SRC_DIR)/%.cpp
g++ $< -c $(CFLAGS) -o $@ $(FLAGS) -MMD -MT $@ -MF .depend/$*.dep
$(addsuffix /%.o,$(builds)): $(SRC_DIR)/%.cpp
g++ $< -c -o $@ $(CFLAGS) $($(D@)_flags) -MMD -MT $@ -MF .depend/$*.dep
$(addsuffix /$(PROJECT_NAME),$(builds)): %$(PROJECT_NAME): $(addprefix %,$(OBJS))
g++ $^ -o $@ $(addprefix -l,$(LIBS))
15 changes: 6 additions & 9 deletions trunk/src/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,26 @@

#include "logger.h"
#include "functor_iterator.h"
#include "sequence.h"

namespace fs {

using namespace boost::filesystem;

template<typename Funct>
class recursive {
Funct _funct;
class recursive: public seq<std::string, fs::path> {
public:
typedef typename Funct::value_type value_type;
recursive(const Funct& funct): _funct(funct) {}
template<typename T>
void operator()(const T& value) {
void operator()(const fs::path& p) { put(p); }
void operator()(const std::string& value) {
if (!exists(value)) {
logger::std_stream() << value << " does not exists" << std::endl;
} else
if (is_directory(value)) {
remove_copy_if(recursive_directory_iterator(value), recursive_directory_iterator(),
functor_iterator<Funct>(_funct),
functor_iterator<recursive, fs::path>(*this),
std::not1(std::ptr_fun((bool(*)(const path&))(&is_regular_file))));
} else
if (is_regular_file(value)) {
_funct(value);
put(value);
} else {
logger::std_stream() << value << " neither directory nor regular file" << std::endl;
}
Expand Down
11 changes: 4 additions & 7 deletions trunk/src/functor_iterator.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#ifndef _FUNCTOR_ITERATOR_H_
#define _FUNCTOR_ITERATOR_H_

#include <vector>

template<typename Funct>
template<typename F, typename V>
class functor_iterator {
Funct _funct;
F _funct;
public:
typedef typename Funct::value_type value_type;
functor_iterator(const Funct& funct): _funct(funct) {}
functor_iterator& operator=(const value_type& elem) { _funct(elem); return *this; }
functor_iterator(const F& funct): _funct(funct) {}
functor_iterator& operator=(const V& elem) { _funct(elem); return *this; }
functor_iterator& operator*() { return *this; }
functor_iterator& operator++() { return *this; }
functor_iterator operator++(int) { return *this; }
Expand Down
24 changes: 20 additions & 4 deletions trunk/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,31 @@
#include "filesystem.h"
#include "binary_functor.h"
#include "comparer.h"
#include "sequence.h"

class HasExtension {
const std::vector<std::string>& _extensions;
public:
HasExtension(const std::vector<std::string>& extensions): _extensions(extensions) { }
bool operator()(const fs::path& file) const {
return find(_extensions.begin(), _extensions.end(), file.extension()) != _extensions.end();
}
};

int main(int argc, char* argv[]) {
stderr_logger std_logger(argv[0]);
logger::set_std(&std_logger);

program_options po(argc, argv);

typedef std::ostream_iterator<comparer::result_type> out_iter;
typedef binary_functor<fs::path, out_iter, comparer> bin_funct;
for_each(po.input_files().begin(), po.input_files().end(),
fs::recursive<bin_funct>(bin_funct(out_iter(std::cout), comparer())));
make_pair(po.input_files().begin(), po.input_files().end())
>>= fs::recursive()
>>= po.extensions().empty()
? (seq<fs::path, fs::path>&) The<id, fs::path>()
: The<filter, fs::path>(HasExtension(po.extensions()))
// >>= clusterize()
// >>= comparator()
>>= The<iterator_end, fs::path>(std::ostream_iterator<fs::path>(std::cout, "\n"));

return 0;
}
15 changes: 14 additions & 1 deletion trunk/src/program_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ namespace po = boost::program_options;
program_options::program_options(int argc, char* argv[]) {
const std::string usage = std::string("Usage: ") + argv[0] + " [OPTIONS] FILES\n";
try {
std::string extensions;
po::options_description visible_options("Available options");
visible_options.add_options()
("extensions", po::value(&extensions), "Search through files with <extensions>")
("help", "Print this message and exit")
;

po::options_description command_line_options;
command_line_options.add(visible_options).add_options()
("files", po::value< std::vector<std::string> >(&_input_files))
("files", po::value(&_input_files))
;

po::positional_options_description positional_options;
Expand All @@ -35,6 +37,17 @@ program_options::program_options(int argc, char* argv[]) {
std::cout << visible_options << "\n";
exit(0);
}

if (!extensions.empty()) {
for (size_t p = 0, p2 = 0; p2 != std::string::npos; p = p2 + 1) {
p2 = extensions.find(',', p);
std::string ext = extensions.substr(p, p2 != std::string::npos ? p2 - p : p2);
if (ext.length() > 0 && ext[0] != '.') {
ext = "." + ext;
}
_extensions.push_back(ext);
}
}
} catch(std::exception const& e) {
logger::std(e.what());
exit(1);
Expand Down
2 changes: 2 additions & 0 deletions trunk/src/program_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

class program_options {
std::vector<std::string> _input_files;
std::vector<std::string> _extensions;
public:
program_options(int argc, char* argv[]);
const std::vector<std::string>& input_files() const { return _input_files; }
const std::vector<std::string>& extensions() const { return _extensions; }
};

#endif
77 changes: 77 additions & 0 deletions trunk/src/sequence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef _SEQUENCE_H_
#define _SEQUENCE_H_

template<typename S>
struct end {
virtual void operator()(const S& v) = 0;
};

template<typename V>
void operator>>=(const V& from, end<V>& to) {
to(from);
}

template<typename Iter, typename V>
void operator>>=(const std::pair<Iter, Iter>& from, end<V>& to) {
for (Iter it = from.first; it != from.second; to(*it++)) ;
}

template<typename Funct, typename V>
void operator>>=(const std::pair<Funct, int>& from, end<V>& to) {
for (int i = 0; i < from.second; ++i) {
to(from.first());
}
}

template<typename S, typename T>
class seq: public end<S> {
end<T>* e;
protected:
void put(const T& t) {
(*e)(t);
}
public:
end<S>& operator>>=(end<T>& to) {
e = &to;
return *this;
}
};

template<typename V, typename Iter>
class iterator_end: public end<V> {
Iter it;
public:
iterator_end(const Iter& i): it(i) {}
void operator()(const V& value) { *it++ = value; }
};

template<typename T, typename P>
class filter: public seq<T, T> {
P _p;
public:
filter(const P& p): _p(p) {}
void operator()(const T& value) {
if (_p(value)) {
put(value);
}
}
};

template<typename T>
struct id: seq<T, T> {
void operator()(const T& t) { put(t); }
};

template<typename T> T& The() { static T r; return r; }
template<typename T> T& The(const T& t) { static T r(t); return r; }
template<template<typename S> class R, typename S> R<S>& The() { static R<S> r; return r; }
template<template<typename S> class R, typename S> R<S>& The(const S& s) { static R<S> r(s); return r; }
template<template<typename S> class R, typename S> R<S>& The(const R<S>& t) { static R<S> r(t); return r; }
template<template<typename S, typename T> class R, typename S, typename T> R<S, T>& The() { static R<S, T> r; return r; }
template<template<typename S, typename T> class R, typename S, typename T> R<S, T>& The(const T& t) { static R<S, T> r(t); return r; }
template<template<typename S, typename T> class R, typename S, typename T> R<S, T>& The(const R<S, T>& t) { static R<S, T> r(t); return r; }
template<template<typename S, typename T, typename U> class R, typename S, typename T, typename U> R<S, T, U>& The() { static R<S, T, U> r; return r; }
template<template<typename S, typename T, typename U> class R, typename S, typename T, typename U> R<S, T, U>& The(const U& u) { static R<S, T, U> r(u); return r; }
template<template<typename S, typename T, typename U> class R, typename S, typename T, typename U> R<S, T, U>& The(const R<S, T, U>& t) { static R<S, T, U> r(t); return r; }

#endif

0 comments on commit 4b6a2bd

Please sign in to comment.