Skip to content

Commit

Permalink
magic added
Browse files Browse the repository at this point in the history
git-svn-id: https://projectname.googlecode.com/svn/trunk@17 c416075f-80b4-e980-4839-00ea3ed24e77
  • Loading branch information
[email protected] committed Mar 28, 2011
1 parent 9a616dc commit 7db308b
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 19 deletions.
4 changes: 2 additions & 2 deletions trunk/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
LIBS = boost_program_options boost_filesystem boost_system
CFLAGS = -O2 -Wall -pedantic `Magick++-config --cppflags`
LIBS = boost_program_options boost_filesystem boost_system magic
CFLAGS = -O2 -Wall `Magick++-config --cppflags`
LINKER_FLAGS = `Magick++-config --libs`
PROJECT_NAME = pn
SRC_DIR = src
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/default_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class accumulator: public arr<boost::shared_ptr<T>, boost::shared_ptr<T> > {

void default_main(const program_options& po) {
struct : end< boost::shared_ptr<file_type::base> > {
void next(const boost::shared_ptr<file_type::base>& t) { std::cout << t->path() << "\n"; }
void next(const boost::shared_ptr<file_type::base>& t) { std::cout << t->path().string() << "\n"; }
void stop() { std::cout << "\n"; }
} output;

Expand Down
6 changes: 5 additions & 1 deletion trunk/src/file_types/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
#include "base.h"
#include "../kleisli.h"
#include "../filesystem.h"
#include "../magic_file.h"

namespace file_type {

base::base(const fs::path& p): _path(p), _size(fs::file_size(p)) { }
base::base(const fs::path& p): _path(p), _size(fs::file_size(p)) {
const char* s = magic::mime_type(p.c_str());
_mime = s ? s : "";
}

boost::shared_ptr<base> base::try_file(const fs::path& file) {
return boost::make_shared<base>(file);
Expand Down
16 changes: 16 additions & 0 deletions trunk/src/file_types/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define _FILE_TYPE_BASE_H_

#include <boost/shared_ptr.hpp>
#include <boost/algorithm/string.hpp>
#include <vector>
#include <string>

#include "../filesystem.h"
#include "../type_list.h"
Expand All @@ -19,12 +21,26 @@ enum comparison_result
class base {
fs::path _path;
unsigned int _size;
std::string _mime;
public:
base(const fs::path& p);
const fs::path& path() const { return _path; }
const std::string& mime() const { return _mime; }
static boost::shared_ptr<base> try_file(const fs::path& file);
virtual boost::shared_ptr<base> compare(const boost::shared_ptr<base>& a) const;
virtual comparison_result precompare(const boost::shared_ptr<base>& a) const;

template<typename It1, typename It2>
bool check_type(It1 mimes, It1 mimes_end, It2 exts, It2 exts_end) const {
bool res;
if (!_mime.empty()) {
res = std::find(mimes, mimes_end, _mime) != mimes_end;
} else {
std::string ext = boost::to_lower_copy(_path.extension().string());
res = std::find(exts, exts_end, ext) != exts_end;
}
return res;
}
};

}
Expand Down
17 changes: 10 additions & 7 deletions trunk/src/file_types/img.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ img::img(const fs::path& p, const Magick::Image& image): base(p), _image(image)
}
}
for (unsigned int j = 0; j < BUCKET_COUNT; ++j) {
bucket[0][j] /= size;
bucket[1][j] /= size;
bucket[2][j] /= size;
for (unsigned int k = 0; k < HISTOGRAM_COUNT; ++k) {
bucket[k][j] /= size;
}
}
}

boost::shared_ptr<img> img::try_file(const boost::shared_ptr<base>& file) {
static const fs::path exts[] = { ".JPG", ".png" };
static const fs::path* exts_end = exts + sizeof(exts)/sizeof(fs::path);
if (find(exts, exts_end, file->path().extension()) != exts_end) {
static const std::string mimes[] = { "image/jpeg", "image/png" };
static const std::string exts[] = { ".jpg", ".png" };
if (file->check_type(mimes, mimes + sizeof(mimes)/sizeof(std::string),
exts, exts + sizeof(exts)/sizeof(std::string))) {
try {
Magick::Image image;
image.read(file->path().string());
Expand All @@ -54,7 +55,9 @@ boost::shared_ptr<base> img::compare(const boost::shared_ptr<base>& _a) const {
const img* a = static_cast<const img*>(_a.get());
double res = 0;
for (unsigned int i = 0; i < BUCKET_COUNT; ++i) {
res += abs(bucket[0][i] - a->bucket[0][i]) + abs(bucket[1][i] - a->bucket[1][i]) + abs(bucket[2][i] - a->bucket[2][i]);
for (unsigned int k = 0; k < HISTOGRAM_COUNT; ++k) {
res += abs(bucket[k][i] - a->bucket[k][i]);
}
}
return res > THRESHOLD ? boost::shared_ptr<img>() : _a;
}
Expand Down
3 changes: 2 additions & 1 deletion trunk/src/file_types/img.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ struct img: base {
comparison_result precompare(const boost::shared_ptr<base>& a) const;
private:
static const unsigned int BUCKET_COUNT = 4;
double bucket[3][BUCKET_COUNT];
static const unsigned int HISTOGRAM_COUNT = 3;
double bucket[HISTOGRAM_COUNT][BUCKET_COUNT];
Magick::Image _image;
};

Expand Down
11 changes: 5 additions & 6 deletions trunk/src/file_types/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
namespace file_type {

boost::shared_ptr<text> text::try_file(const boost::shared_ptr<base>& file) {
static const fs::path exts[] = { ".txt" };
static const fs::path* exts_end = exts + sizeof(exts)/sizeof(fs::path);
if (find(exts, exts_end, file->path().extension()) != exts_end) {
return boost::make_shared<text>(file->path());
}
return boost::shared_ptr<text>();
static const std::string mimes[] = { "text/plain" };
static const std::string exts[] = { ".txt" };
return file->check_type(mimes, mimes + sizeof(mimes)/sizeof(std::string),
exts, exts + sizeof(exts)/sizeof(std::string)) ?
boost::make_shared<text>(file->path()) : boost::shared_ptr<text>();
}

static int clean_str(char* s, int size) {
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void recursive::next(const std::string& value) {
void next(const fs::path& value) { if (is_regular_file(value)) pass(value); }
} filter;
boost::system::error_code e;
make_pair(recursive_directory_iterator(value, e), recursive_directory_iterator())
std::make_pair(recursive_directory_iterator(value, e), recursive_directory_iterator())
>>= filter >>= continuation();
if (e) {
logger::std_stream() << value << ": " << e.message() << "\n";
Expand Down
25 changes: 25 additions & 0 deletions trunk/src/magic_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <magic.h>

#include "magic_file.h"

static magic_t magic_cookie = 0;

namespace magic {

void initialize() {
if ((magic_cookie = magic_open(MAGIC_MIME_TYPE))) {
magic_load(magic_cookie, 0);
}
}

void destroy() {
if (magic_cookie) {
magic_close(magic_cookie);
}
}

const char* mime_type(const char* s) {
return magic_cookie ? magic_file(magic_cookie, s) : 0;
}

}
10 changes: 10 additions & 0 deletions trunk/src/magic_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _MAGIC_FILE_H_
#define _MAGIC_FILE_H_

namespace magic {
void initialize();
void destroy();
const char* mime_type(const char* s);
}

#endif
5 changes: 5 additions & 0 deletions trunk/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "logger.h"
#include "program_options.h"
#include "magic_file.h"

void default_main(const program_options& po);

Expand All @@ -9,7 +10,11 @@ int main(int argc, char* argv[]) {

program_options po(argc, argv);

magic::initialize();

default_main(po);

magic::destroy();

return 0;
}

0 comments on commit 7db308b

Please sign in to comment.