From 7280f8106e38fbb50b7cfb1eee8e653ffe4b286b Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Thu, 4 Aug 2016 20:28:49 -0400 Subject: [PATCH] tweaks. --- Makefile | 6 +++--- applefile.h | 1 - defer.h | 26 ++++++++++++++++++++++++++ dot_clean.cpp | 38 ++++++++++++-------------------------- mapped_file.cpp | 18 ------------------ unique_resource.h | 19 +++++++++++++------ 6 files changed, 54 insertions(+), 54 deletions(-) create mode 100644 defer.h diff --git a/Makefile b/Makefile index 8325764..00cc978 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ CXX = clang++ LINK.o = $(LINK.cc) -CXXFLAGS += -std=c++11 -g +CXXFLAGS += -std=c++11 -g -Wall -dot_clean: dot_clean.o mapped_file.o +dot_clean: dot_clean.o mapped_file.o applefile.h defer.h -mapped_file.o : mapped_file.cpp mapped_file.h +mapped_file.o : mapped_file.cpp mapped_file.h unique_resource.h dot_clean.o : dot_clean.cpp mapped_file.h applefile.h diff --git a/applefile.h b/applefile.h index e5ffc3e..ead3582 100644 --- a/applefile.h +++ b/applefile.h @@ -155,7 +155,6 @@ typedef struct ASEntry ASEntry; #define AS_MSDOSINFO 12 /* MS-DOS file info, attributes, etc */ #define AS_AFPNAME 13 /* Short name on AFP server */ #define AS_AFPINFO 14 /* AFP file info, attrib., etc */ - #define AS_AFPDIRID 15 /* AFP directory ID */ /* matrix of entry types and their usage: diff --git a/defer.h b/defer.h new file mode 100644 index 0000000..9a3ba1b --- /dev/null +++ b/defer.h @@ -0,0 +1,26 @@ +#ifndef __defer_h__ +#define __defer_h__ + +#include +#include + + +class defer { +public: + typedef std::function FX; + defer() = default; + + defer(FX &&fx) : _fx(std::forward(fx)) {} + defer(const defer &) = delete; + defer(defer &&) = default; + defer & operator=(const defer &) = delete; + defer & operator=(defer &&) = default; + + void cancel() { _fx = nullptr; } + ~defer() { if (_fx) _fx(); } +private: + FX _fx; +}; + + +#endif diff --git a/dot_clean.cpp b/dot_clean.cpp index a9f1764..c16fb4a 100644 --- a/dot_clean.cpp +++ b/dot_clean.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -23,6 +24,7 @@ #include "applefile.h" #include "mapped_file.h" +#include "defer.h" std::vector unlink_list; @@ -63,25 +65,6 @@ void throw_errno(const std::string &what) { } -class defer { -public: - typedef std::function FX; - defer() = default; - - defer(FX fx) : _fx(fx) {} - defer(const defer &) = delete; - defer(defer &&) = default; - defer & operator=(const defer &) = delete; - defer & operator=(defer &&) = default; - - void cancel() { _fx = nullptr; } - ~defer() { if (_fx) _fx(); } -private: - FX _fx; -}; - - - void one_file(const std::string &data, const std::string &rsrc) noexcept try { struct stat rsrc_st; @@ -119,9 +102,15 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try { - if (header->magicNum != APPLEDOUBLE_MAGIC || header->versionNum != 0x00020000) + if (header->magicNum != APPLEDOUBLE_MAGIC) throw_not_apple_double(); + // v 2 is a super set of v1. v1 had type 7 for os-specific info, since split into + // separate entries. + if (header->versionNum != 0x00010000 && header->versionNum != 0x00020000) + throw_not_apple_double(); + + if (header->numEntries * sizeof(ASEntry) + sizeof(ASHeader) > mf.size()) throw_eof(); ASEntry *begin = (ASEntry *)(mf.data() + sizeof(ASHeader)); @@ -139,11 +128,9 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try { }); + std::for_each(begin, end, [&mf,fd](ASEntry &e){ - for (auto iter = begin; iter != end; ++iter) { - const auto &e = *iter; - - if (e.entryLength == 0) continue; + if (e.entryLength == 0) return; switch(e.entryID) { #if 0 @@ -185,8 +172,7 @@ void one_file(const std::string &data, const std::string &rsrc) noexcept try { break; } } - - } + }); if (!_p) unlink_list.push_back(rsrc); diff --git a/mapped_file.cpp b/mapped_file.cpp index f9a4862..a67aaa4 100644 --- a/mapped_file.cpp +++ b/mapped_file.cpp @@ -6,22 +6,6 @@ #include "unique_resource.h" namespace { - class defer { - public: - typedef std::function FX; - defer() = default; - - defer(FX fx) : _fx(fx) {} - defer(const defer &) = delete; - defer(defer &&) = default; - defer & operator=(const defer &) = delete; - defer & operator=(defer &&) = default; - - void cancel() { _fx = nullptr; } - ~defer() { if (_fx) _fx(); } - private: - FX _fx; - }; void throw_error(int error) { throw std::system_error(error, std::system_category()); @@ -32,8 +16,6 @@ namespace { throw std::system_error(error, std::system_category(), what); } - - } #ifdef _WIN32 diff --git a/unique_resource.h b/unique_resource.h index 9ec9de0..1326b46 100644 --- a/unique_resource.h +++ b/unique_resource.h @@ -18,6 +18,7 @@ public: unique_resource(T t, D d): _pair(t,d), _active(true) {} + ~unique_resource() { reset(); } @@ -40,14 +41,14 @@ public: void reset(T t) { reset(); - _active = true; _pair.first = t; + _active = true; } void reset(T t, D d) { reset(); - _active = true; _pair = std::make_pair(t, d); + _active = true; } void reset() { @@ -59,10 +60,6 @@ public: T release() { _active = false; - return _pair.first;; - } - - T get() { return _pair.first; } @@ -70,6 +67,16 @@ public: return _active; } + + T& get() { + return _pair.first; + } + + const T& get() const { + return _pair.first; + } + + D& get_deleter() { return _pair.second; }