From f02501eb5e64536d78930fee10a657934dc0f7cd Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sat, 1 Oct 2011 01:58:10 +1000 Subject: [PATCH] Move io functions into the util namespace --- io.cpp | 3 ++- io.hpp | 78 +++++++++++++++++++++++++++-------------------------- json.cpp.rl | 1 + 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/io.cpp b/io.cpp index 14b02716..1618a90b 100644 --- a/io.cpp +++ b/io.cpp @@ -11,10 +11,11 @@ using namespace std; +using namespace util; //---------------------------------------------------------------------------- uint8_t * -slurp (const boost::filesystem::path& path) { +util::slurp (const boost::filesystem::path& path) { fd_ref fd(open (path.string ().c_str (), O_RDONLY)); // | O_CLOEXEC)); // Calculate the total file size diff --git a/io.hpp b/io.hpp index 65e5ed11..5e56a34f 100644 --- a/io.hpp +++ b/io.hpp @@ -25,57 +25,59 @@ #include #include #include -#include - -/// Specifies bitwise combinations of IO access rights. -enum access_t { - ACCESS_READ = 1 << 0, - ACCESS_WRITE = 1 << 1, - ACCESS_READWRITE = ACCESS_READ | ACCESS_WRITE -}; +#include -/// Reads an entire file into memory. Caller frees the result. Guarantees a -/// null trailing byte. -uint8_t * -slurp (const boost::filesystem::path&) mustuse; +namespace util { + /// Specifies bitwise combinations of IO access rights. + enum access_t { + ACCESS_READ = 1 << 0, + ACCESS_WRITE = 1 << 1, + ACCESS_READWRITE = ACCESS_READ | ACCESS_WRITE + }; -/// A simple RAII wrapper for file descriptors -struct fd_ref { - public: - int fd; - fd_ref (int _fd); - ~fd_ref (); + /// Reads an entire file into memory. Caller frees the result. Guarantees a + /// null trailing byte. + uint8_t * + slurp (const boost::filesystem::path&) mustuse; - operator int (void) const; -}; + /// A simple RAII wrapper for file descriptors + struct fd_ref { + public: + int fd; + + fd_ref (int _fd); + ~fd_ref (); + + operator int (void) const; + }; #if defined(HAVE_MMAP) -/// Wraps a mechanism to map a file into memory. Read only. -class mapped_file { - protected: - fd_ref m_fd; - uint8_t *m_data; - size_t m_size; + /// Wraps a mechanism to map a file into memory. Read only. + class mapped_file { + protected: + fd_ref m_fd; + uint8_t *m_data; + size_t m_size; - void load_fd (void); + void load_fd (void); - public: - mapped_file (const char *path); - mapped_file (const std::string &path); - mapped_file (const boost::filesystem::path &path); + public: + mapped_file (const char *path); + mapped_file (const std::string &path); + mapped_file (const boost::filesystem::path &path); - mapped_file (const mapped_file &rhs); - mapped_file& operator =(const mapped_file &rhs); + mapped_file (const mapped_file &rhs); + mapped_file& operator =(const mapped_file &rhs); - ~mapped_file (); + ~mapped_file (); - const uint8_t* data (void) const; - size_t size (void) const; -}; + const uint8_t* data (void) const; + size_t size (void) const; + }; #endif - +} #endif diff --git a/json.cpp.rl b/json.cpp.rl index 7b40336d..76cffea2 100644 --- a/json.cpp.rl +++ b/json.cpp.rl @@ -36,6 +36,7 @@ using namespace std; +using namespace util; /* * Parsing