diff --git a/src/Arguments.cc b/src/Arguments.cc index a6e9f4b..15ddec2 100644 --- a/src/Arguments.cc +++ b/src/Arguments.cc @@ -2,6 +2,8 @@ using namespace std; +namespace phosg { + Arguments::ArgText::ArgText(std::string&& text) : text(std::move(text)), used(false) {} @@ -71,3 +73,5 @@ void Arguments::parse(vector&& args) { } const string Arguments::empty_string; + +} // namespace phosg diff --git a/src/Arguments.hh b/src/Arguments.hh index 8eca9e7..8d27d7f 100644 --- a/src/Arguments.hh +++ b/src/Arguments.hh @@ -7,7 +7,7 @@ #include "Strings.hh" -using namespace std; +namespace phosg { class Arguments { public: @@ -24,7 +24,7 @@ public: template requires(std::is_same_v) - std::vector get_multi(const string& name) { + std::vector get_multi(const std::string& name) { std::vector ret; for (auto& value : this->get_values_multi(name)) { ret.emplace_back(value.text); @@ -34,7 +34,7 @@ public: } template requires(std::is_same_v) - const RetT& get(const string& name, bool throw_if_missing = false) { + const RetT& get(const std::string& name, bool throw_if_missing = false) { try { auto& values = this->named.at(name); if (values.empty()) { @@ -47,7 +47,7 @@ public: } values[0].used = true; return values[0].text; - } catch (const out_of_range&) { + } catch (const std::out_of_range&) { if (throw_if_missing) { throw std::out_of_range(Arguments::exc_prefix(name) + "argument is missing"); } else { @@ -62,7 +62,7 @@ public: auto& arg = this->positional.at(position); arg.used = true; return arg.text; - } catch (const out_of_range&) { + } catch (const std::out_of_range&) { if (throw_if_missing) { throw std::out_of_range(Arguments::exc_prefix(position) + "argument is missing"); } else { @@ -155,17 +155,17 @@ private: void parse(std::vector&& args); - inline std::vector& get_values_multi(const string& name) { + inline std::vector& get_values_multi(const std::string& name) { try { return this->named.at(name); - } catch (const out_of_range&) { + } catch (const std::out_of_range&) { static std::vector empty_vec; return empty_vec; } } static inline std::string exc_prefix(const std::string& name) { - string ret = "("; + std::string ret = "("; ret += name; ret += ") "; return ret; @@ -241,3 +241,5 @@ private: }; std::vector split_args(const std::string& s); + +} // namespace phosg diff --git a/src/ArgumentsTest.cc b/src/ArgumentsTest.cc index 51a593a..6207fc1 100644 --- a/src/ArgumentsTest.cc +++ b/src/ArgumentsTest.cc @@ -5,6 +5,9 @@ #include "Encoding.hh" #include "UnitTest.hh" +using namespace std; +using namespace phosg; + int main(int, char**) { Arguments a("pos0 --named1 300 --named2=value2 4.0 --int3=40000 --float4=2.0"); diff --git a/src/BinDiff.cc b/src/BinDiff.cc index c3e7c31..efdf6cd 100644 --- a/src/BinDiff.cc +++ b/src/BinDiff.cc @@ -8,6 +8,7 @@ #include "JSON.hh" using namespace std; +using namespace phosg; void print_usage() { fprintf(stderr, "\ diff --git a/src/Containers.hh b/src/Containers.hh index d39a0b5..76b4f7c 100644 --- a/src/Containers.hh +++ b/src/Containers.hh @@ -6,6 +6,8 @@ #include #include +namespace phosg { + template std::vector set_to_vec(const std::set& s) { std::vector ret; @@ -83,3 +85,5 @@ std::vector map_values_to_vec(const std::unordered_map& s) { } return ret; } + +} // namespace phosg diff --git a/src/Encoding.cc b/src/Encoding.cc index dfca723..59b054c 100644 --- a/src/Encoding.cc +++ b/src/Encoding.cc @@ -4,6 +4,8 @@ using namespace std; +namespace phosg { + const char* DEFAULT_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const char* URLSAFE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; @@ -131,3 +133,5 @@ string rot13(const void* vdata, size_t size) { } return ret; } + +} // namespace phosg diff --git a/src/Encoding.hh b/src/Encoding.hh index 799cf1e..a21a31e 100644 --- a/src/Encoding.hh +++ b/src/Encoding.hh @@ -8,6 +8,8 @@ #include "Platform.hh" #include "Types.hh" +namespace phosg { + template constexpr uint8_t bits_for_type = sizeof(T) << 3; @@ -465,3 +467,5 @@ std::string base64_decode(const void* data, size_t size, const char* alphabet = std::string base64_decode(const std::string& data, const char* alphabet = nullptr); std::string rot13(const void* data, size_t size); + +} // namespace phosg diff --git a/src/EncodingTest.cc b/src/EncodingTest.cc index ca6264a..eaf3646 100644 --- a/src/EncodingTest.cc +++ b/src/EncodingTest.cc @@ -4,6 +4,8 @@ #include "Encoding.hh" #include "UnitTest.hh" +using namespace phosg; + int main(int, char**) { expect_eq(0x0000, (sign_extend(0x00))); expect_eq(0x0001, (sign_extend(0x01))); diff --git a/src/Filesystem.cc b/src/Filesystem.cc index 82094bc..2a34366 100644 --- a/src/Filesystem.cc +++ b/src/Filesystem.cc @@ -27,6 +27,8 @@ using namespace std; +namespace phosg { + std::string basename(const std::string& filename) { size_t slash_pos = filename.rfind('/'); return (slash_pos == string::npos) ? filename : filename.substr(slash_pos + 1); @@ -82,7 +84,7 @@ vector list_directory_sorted(const string& dirname) { string getcwd() { string ret(MAXPATHLEN, '\0'); - if (!getcwd(ret.data(), ret.size())) { + if (!::getcwd(ret.data(), ret.size())) { throw runtime_error("cannot get working directory"); } ret.resize(strlen(ret.c_str())); @@ -140,7 +142,7 @@ io_error::io_error(int fd, const string& what) struct stat stat(const string& filename) { struct stat st; - if (stat(filename.c_str(), &st)) { + if (::stat(filename.c_str(), &st)) { throw cannot_stat_file(filename); } return st; @@ -148,7 +150,7 @@ struct stat stat(const string& filename) { struct stat lstat(const string& filename) { struct stat st; - if (lstat(filename.c_str(), &st)) { + if (::lstat(filename.c_str(), &st)) { throw cannot_stat_file(filename); } return st; @@ -156,7 +158,7 @@ struct stat lstat(const string& filename) { struct stat fstat(int fd) { struct stat st; - if (fstat(fd, &st)) { + if (::fstat(fd, &st)) { throw cannot_stat_file(fd); } return st; @@ -220,7 +222,7 @@ bool islink(const string& filename) { string readlink(const string& filename) { string data(1024, 0); - ssize_t length = readlink(filename.c_str(), data.data(), data.size()); + ssize_t length = ::readlink(filename.c_str(), data.data(), data.size()); if (length < 0) { throw cannot_stat_file(filename); } @@ -231,7 +233,7 @@ string readlink(const string& filename) { string realpath(const string& path) { string data(PATH_MAX, 0); - if (!realpath(path.c_str(), data.data())) { + if (!::realpath(path.c_str(), data.data())) { throw cannot_stat_file(path); } data.resize(strlen(data.c_str())); @@ -307,7 +309,7 @@ string read_all(int fd) { vector buffers; for (;;) { buffers.emplace_back(read_size, 0); - ssize_t bytes_read = read(fd, buffers.back().data(), read_size); + ssize_t bytes_read = ::read(fd, buffers.back().data(), read_size); if (bytes_read < 0) { throw io_error(fd); } @@ -339,7 +341,7 @@ string read_all(FILE* f) { vector buffers; for (;;) { buffers.emplace_back(read_size, 0); - ssize_t bytes_read = fread(buffers.back().data(), 1, read_size, f); + ssize_t bytes_read = ::fread(buffers.back().data(), 1, read_size, f); if (bytes_read < 0) { throw io_error(fileno(f)); } @@ -366,7 +368,7 @@ string read_all(FILE* f) { string read(int fd, size_t size) { string data(size, '\0'); - ssize_t ret_size = read(fd, data.data(), size); + ssize_t ret_size = ::read(fd, data.data(), size); if (ret_size < 0) { throw io_error(fd); } else if (ret_size != static_cast(size)) { @@ -377,7 +379,7 @@ string read(int fd, size_t size) { string fread(FILE* f, size_t size) { string data(size, '\0'); - ssize_t ret_size = fread(data.data(), 1, size, f); + ssize_t ret_size = ::fread(data.data(), 1, size, f); if (ret_size < 0) { throw io_error(fileno(f)); } else if (ret_size != static_cast(size)) { @@ -387,7 +389,7 @@ string fread(FILE* f, size_t size) { } void readx(int fd, void* data, size_t size) { - ssize_t ret_size = read(fd, data, size); + ssize_t ret_size = ::read(fd, data, size); if (ret_size < 0) { throw io_error(fd); } else if (ret_size != static_cast(size)) { @@ -415,7 +417,7 @@ void writex(int fd, const string& data) { } void preadx(int fd, void* data, size_t size, off_t offset) { - ssize_t ret_size = pread(fd, data, size, offset); + ssize_t ret_size = ::pread(fd, data, size, offset); if (ret_size < 0) { throw io_error(fd); } else if (ret_size != static_cast(size)) { @@ -424,7 +426,7 @@ void preadx(int fd, void* data, size_t size, off_t offset) { } void pwritex(int fd, const void* data, size_t size, off_t offset) { - ssize_t ret_size = pwrite(fd, data, size, offset); + ssize_t ret_size = ::pwrite(fd, data, size, offset); if (ret_size < 0) { throw io_error(fd); } else if (ret_size != static_cast(size)) { @@ -443,7 +445,7 @@ void pwritex(int fd, const string& data, off_t offset) { } void freadx(FILE* f, void* data, size_t size) { - ssize_t ret_size = fread(data, 1, size, f); + ssize_t ret_size = ::fread(data, 1, size, f); if (ret_size < 0) { throw io_error(fileno(f)); } else if (ret_size != static_cast(size)) { @@ -452,7 +454,7 @@ void freadx(FILE* f, void* data, size_t size) { } void fwritex(FILE* f, const void* data, size_t size) { - ssize_t ret_size = fwrite(data, 1, size, f); + ssize_t ret_size = ::fwrite(data, 1, size, f); if (ret_size < 0) { throw io_error(fileno(f)); } else if (ret_size != static_cast(size)) { @@ -471,7 +473,7 @@ void fwritex(FILE* f, const string& data) { } uint8_t fgetcx(FILE* f) { - int ret = fgetc(f); + int ret = ::fgetc(f); if (ret == EOF) { if (feof(f)) { throw io_error(fileno(f), "end of stream"); @@ -486,9 +488,9 @@ std::string fgets(FILE* f) { deque blocks; for (;;) { std::string& block = blocks.emplace_back(0x100, '\0'); - if (!fgets(block.data(), block.size(), f)) { + if (!::fgets(block.data(), block.size(), f)) { blocks.pop_back(); - if (feof(f)) { + if (::feof(f)) { break; } else { throw io_error(fileno(f), "cannot read from stream"); @@ -513,7 +515,7 @@ string load_file(const string& filename) { ssize_t file_size = fstat(fd).st_size; string data(file_size, 0); - ssize_t bytes_read = read(fd, data.data(), data.size()); + ssize_t bytes_read = ::read(fd, data.data(), data.size()); if (bytes_read != file_size) { if (errno == 0) { throw runtime_error(string_printf("can\'t read from %s: %zd/%zd bytes read", @@ -571,11 +573,9 @@ static void fclose_raw(FILE* f) { fclose(f); } -unique_ptr fopen_unique(const string& filename, - const string& mode, FILE* dash_file) { +unique_ptr fopen_unique(const string& filename, const string& mode, FILE* dash_file) { if (dash_file && (filename == "-")) { - return unique_ptr( - dash_file, +[](FILE*) {}); + return unique_ptr(dash_file, +[](FILE*) {}); } return unique_ptr(fopen_binary_raw(filename, mode), fclose_raw); } @@ -585,13 +585,10 @@ unique_ptr fdopen_unique(int fd, const string& mode) { } unique_ptr fmemopen_unique(const void* buf, size_t size) { - return unique_ptr(fmemopen( - const_cast(buf), size, "rb"), - fclose_raw); + return unique_ptr(fmemopen(const_cast(buf), size, "rb"), fclose_raw); } -shared_ptr fopen_shared(const string& filename, const string& mode, - FILE* dash_file) { +shared_ptr fopen_shared(const string& filename, const string& mode, FILE* dash_file) { if (dash_file && (filename == "-")) { return shared_ptr(dash_file, [](FILE*) {}); } @@ -603,7 +600,7 @@ shared_ptr fdopen_shared(int fd, const string& mode) { } void rename(const std::string& old_filename, const std::string& new_filename) { - if (rename(old_filename.c_str(), new_filename.c_str()) != 0) { + if (::rename(old_filename.c_str(), new_filename.c_str()) != 0) { throw runtime_error("can\'t rename file " + old_filename + " to " + new_filename + ": " + string_for_error(errno)); } } @@ -614,7 +611,7 @@ void unlink(const string& filename, bool recursive) { for (const string& item : list_directory(filename)) { unlink(filename + "/" + item, true); } - if ((rmdir(filename.c_str()) != 0) && (errno != ENOENT)) { + if ((::rmdir(filename.c_str()) != 0) && (errno != ENOENT)) { throw runtime_error("can\'t delete directory " + filename + ": " + string_for_error(errno)); } } else { @@ -622,7 +619,7 @@ void unlink(const string& filename, bool recursive) { } } else { - if ((unlink(filename.c_str()) != 0) && (errno != ENOENT)) { + if ((::unlink(filename.c_str()) != 0) && (errno != ENOENT)) { throw runtime_error("can\'t delete file " + filename + ": " + string_for_error(errno)); } } @@ -640,7 +637,7 @@ void make_fd_nonblocking(int fd) { pair pipe() { int fds[2]; - if (pipe(fds)) { + if (::pipe(fds)) { throw runtime_error("pipe failed: " + string_for_error(errno)); } return make_pair(fds[0], fds[1]); @@ -700,3 +697,5 @@ unordered_map Poll::poll(int timeout_ms) { } return ret; } + +} // namespace phosg diff --git a/src/Filesystem.hh b/src/Filesystem.hh index 1e907d8..41fbc9b 100644 --- a/src/Filesystem.hh +++ b/src/Filesystem.hh @@ -20,6 +20,8 @@ #include #include +namespace phosg { + std::string basename(const std::string& filename); std::string dirname(const std::string& filename); @@ -98,15 +100,6 @@ private: int fd; }; -namespace std { -template <> -struct hash { - size_t operator()(const scoped_fd& fd) const { - return std::hash()(static_cast(fd)); - } -}; -} // namespace std - std::string read_all(int fd); std::string read_all(FILE* f); @@ -167,6 +160,10 @@ void fwritex(FILE* f, const T& t) { fwritex(f, &t, sizeof(T)); } +std::string load_file(const std::string& filename); +void save_file(const std::string& filename, const void* data, size_t size); +void save_file(const std::string& filename, const std::string& data); + template T load_object_file(const std::string& filename, bool allow_oversize = false) { scoped_fd fd(filename, O_RDONLY); @@ -203,10 +200,6 @@ void save_vector_file(const std::string& filename, const std::vector& v) { save_file(filename, v.data(), v.size() * sizeof(T)); } -std::string load_file(const std::string& filename); -void save_file(const std::string& filename, const void* data, size_t size); -void save_file(const std::string& filename, const std::string& data); - std::unique_ptr fopen_unique(const std::string& filename, const std::string& mode = "rb", FILE* dash_file = nullptr); std::unique_ptr fdopen_unique(int fd, @@ -238,3 +231,5 @@ public: private: std::vector poll_fds; }; + +} // namespace phosg diff --git a/src/FilesystemTest.cc b/src/FilesystemTest.cc index 43b4b0f..f22bc51 100644 --- a/src/FilesystemTest.cc +++ b/src/FilesystemTest.cc @@ -6,6 +6,7 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; int main(int, char**) { { diff --git a/src/Hash.cc b/src/Hash.cc index 1c634cf..b2deff5 100644 --- a/src/Hash.cc +++ b/src/Hash.cc @@ -11,6 +11,8 @@ using namespace std; +namespace phosg { + uint32_t fnv1a32(const void* data, size_t size, uint32_t hash) { const uint8_t* data_ptr = reinterpret_cast(data); const uint8_t* end_ptr = data_ptr + size; @@ -366,3 +368,5 @@ uint32_t crc32(const void* vdata, size_t size, uint32_t cs) { } return ~cs; } + +} // namespace phosg diff --git a/src/Hash.hh b/src/Hash.hh index bb7d857..0a9be52 100644 --- a/src/Hash.hh +++ b/src/Hash.hh @@ -4,15 +4,17 @@ #include -#define fnv1a32_start 0x811C9DC5 +namespace phosg { -uint32_t fnv1a32(const void* data, size_t size, uint32_t hash = fnv1a32_start); -uint32_t fnv1a32(const std::string& data, uint32_t hash = fnv1a32_start); +constexpr uint32_t FNV1A32_START = 0x811C9DC5; -#define fnv1a64_start 0xCBF29CE484222325 +uint32_t fnv1a32(const void* data, size_t size, uint32_t hash = FNV1A32_START); +uint32_t fnv1a32(const std::string& data, uint32_t hash = FNV1A32_START); -uint64_t fnv1a64(const void* data, size_t size, uint64_t hash = fnv1a64_start); -uint64_t fnv1a64(const std::string& data, uint64_t hash = fnv1a64_start); +constexpr uint64_t FNV1A64_START = 0xCBF29CE484222325; + +uint64_t fnv1a64(const void* data, size_t size, uint64_t hash = FNV1A64_START); +uint64_t fnv1a64(const std::string& data, uint64_t hash = FNV1A64_START); std::string sha1(const void* data, size_t size); std::string sha1(const std::string& data); @@ -24,3 +26,5 @@ std::string md5(const void* data, size_t size); std::string md5(const std::string& data); uint32_t crc32(const void* vdata, size_t size, uint32_t cs = 0); + +} // namespace phosg diff --git a/src/HashTest.cc b/src/HashTest.cc index 053eb7b..6aa9778 100644 --- a/src/HashTest.cc +++ b/src/HashTest.cc @@ -6,6 +6,7 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; int main(int, char**) { { diff --git a/src/Image.cc b/src/Image.cc index ceea66b..13b8cf5 100644 --- a/src/Image.cc +++ b/src/Image.cc @@ -23,6 +23,8 @@ using namespace std; +namespace phosg { + Image::unknown_format::unknown_format(const std::string& what) : runtime_error(what) {} struct ExpandedColor { @@ -1715,3 +1717,5 @@ void Image::resize_blit(const Image& source, ssize_t x, ssize_t y, ssize_t w, } } } + +} // namespace phosg diff --git a/src/Image.hh b/src/Image.hh index 8089755..c56f17b 100644 --- a/src/Image.hh +++ b/src/Image.hh @@ -9,6 +9,8 @@ #include "Platform.hh" +namespace phosg { + // an Image represents a drawing canvas. this class is fairly simple; it // supports reading/writing individual pixels, drawing lines, and saving the // image as a PPM or Windows BMP file. @@ -193,3 +195,5 @@ private: template void save_helper(Format format, Writer&& writer) const; }; + +} // namespace phosg diff --git a/src/ImageTest.cc b/src/ImageTest.cc index 5b8988f..225aa9e 100644 --- a/src/ImageTest.cc +++ b/src/ImageTest.cc @@ -11,6 +11,7 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; struct Color { uint8_t r; diff --git a/src/ImageTextFont.hh b/src/ImageTextFont.hh index 487bf1f..43663a1 100644 --- a/src/ImageTextFont.hh +++ b/src/ImageTextFont.hh @@ -1,5 +1,7 @@ #pragma once +namespace phosg { + // clang-format off static uint8_t font[96][35] = { { 0,0,0,0,0, @@ -676,3 +678,5 @@ static uint8_t font[96][35] = { 1,1,1,1,1}, //0x7F }; // clang-format on + +} // namespace phosg diff --git a/src/JSON.cc b/src/JSON.cc index a37d4ce..41769b2 100644 --- a/src/JSON.cc +++ b/src/JSON.cc @@ -11,6 +11,8 @@ using namespace std; +namespace phosg { + JSON::parse_error::parse_error(const string& what) : runtime_error(what) {} JSON::type_error::type_error(const string& what) : runtime_error(what) {} @@ -744,3 +746,5 @@ void JSON::clear() { throw type_error("cannot clear primitive JSON value"); } } + +} // namespace phosg diff --git a/src/JSON.hh b/src/JSON.hh index 8ecaf27..2fdaf3d 100644 --- a/src/JSON.hh +++ b/src/JSON.hh @@ -12,6 +12,8 @@ #include "Strings.hh" #include "Types.hh" +namespace phosg { + class JSON { public: enum class StringEscapeMode { @@ -477,3 +479,5 @@ private: dict_type> value; }; + +} // namespace phosg diff --git a/src/JSONFormat.cc b/src/JSONFormat.cc index 24378ed..cf470e6 100644 --- a/src/JSONFormat.cc +++ b/src/JSONFormat.cc @@ -8,6 +8,7 @@ #include "JSON.hh" using namespace std; +using namespace phosg; void print_usage() { fprintf(stderr, "\ diff --git a/src/JSONTest.cc b/src/JSONTest.cc index d859441..aef21e3 100644 --- a/src/JSONTest.cc +++ b/src/JSONTest.cc @@ -9,6 +9,7 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; enum JSONTestEnum { ONE = 1, @@ -17,7 +18,7 @@ enum JSONTestEnum { }; template <> -JSONTestEnum enum_for_name(const char* name) { +JSONTestEnum phosg::enum_for_name(const char* name) { if (!strcmp(name, "ONE")) { return JSONTestEnum::ONE; } else if (!strcmp(name, "TWO")) { @@ -30,7 +31,7 @@ JSONTestEnum enum_for_name(const char* name) { } template <> -const char* name_for_enum(JSONTestEnum v) { +const char* phosg::name_for_enum(JSONTestEnum v) { switch (v) { case JSONTestEnum::ONE: return "ONE"; diff --git a/src/KDTree-inl.hh b/src/KDTree-inl.hh index d2b5661..608b4f8 100644 --- a/src/KDTree-inl.hh +++ b/src/KDTree-inl.hh @@ -9,6 +9,8 @@ #include #include +namespace phosg { + template KDTree::KDTree() : root(nullptr), @@ -530,3 +532,5 @@ typename KDTree::Iterator KDTree::end() const { return Iterator(nullptr); } + +} // namespace phosg diff --git a/src/KDTree.hh b/src/KDTree.hh index 013c878..8a7ea58 100644 --- a/src/KDTree.hh +++ b/src/KDTree.hh @@ -7,6 +7,8 @@ #include #include +namespace phosg { + template class KDTree { private: @@ -94,4 +96,6 @@ public: Iterator end() const; }; +} // namespace phosg + #include "KDTree-inl.hh" diff --git a/src/KDTreeTest.cc b/src/KDTreeTest.cc index e52eec1..e167384 100644 --- a/src/KDTreeTest.cc +++ b/src/KDTreeTest.cc @@ -11,6 +11,7 @@ #include "Vector.hh" using namespace std; +using namespace phosg; void run_randomized_test() { printf("-- randomized\n"); diff --git a/src/LRUMap.hh b/src/LRUMap.hh index 01c051e..ed6ed4d 100644 --- a/src/LRUMap.hh +++ b/src/LRUMap.hh @@ -6,6 +6,8 @@ #include #include +namespace phosg { + template class LRUMap { protected: @@ -264,3 +266,5 @@ public: other.total_size = this_total_size; } }; + +} // namespace phosg diff --git a/src/LRUMapTest.cc b/src/LRUMapTest.cc index d1ca413..f7cfafb 100644 --- a/src/LRUMapTest.cc +++ b/src/LRUMapTest.cc @@ -6,6 +6,7 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; int main(int, char**) { LRUMap c; diff --git a/src/LRUSet-inl.hh b/src/LRUSet-inl.hh index a9cfeb4..4a0dca0 100644 --- a/src/LRUSet-inl.hh +++ b/src/LRUSet-inl.hh @@ -4,6 +4,8 @@ #include #include +namespace phosg { + template LRUSet::Item::Item(size_t size) : prev(nullptr), @@ -195,3 +197,5 @@ void LRUSet::swap(LRUSet& other) { other.tail = this_tail; other.total_size = this_total_size; } + +} // namespace phosg diff --git a/src/LRUSet.hh b/src/LRUSet.hh index b7e6a29..ad6ad6e 100644 --- a/src/LRUSet.hh +++ b/src/LRUSet.hh @@ -5,6 +5,8 @@ #include +namespace phosg { + template class LRUSet { protected: @@ -50,4 +52,6 @@ public: void swap(LRUSet& other); }; +} // namespace phosg + #include "LRUSet-inl.hh" diff --git a/src/LRUSetTest.cc b/src/LRUSetTest.cc index 8e952b3..a0bf22c 100644 --- a/src/LRUSetTest.cc +++ b/src/LRUSetTest.cc @@ -6,6 +6,7 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; int main(int, char**) { LRUSet c; diff --git a/src/Math.hh b/src/Math.hh index 416e113..b7e4426 100644 --- a/src/Math.hh +++ b/src/Math.hh @@ -1,5 +1,7 @@ #include +namespace phosg { + template constexpr IntT gcd(IntT a, IntT b) { if (b == 0) { @@ -18,3 +20,5 @@ template constexpr IntT log2i(IntT v) { return (sizeof(IntT) << 3) - 1 - __builtin_clz(v); } + +} // namespace phosg diff --git a/src/Network.cc b/src/Network.cc index e5df876..c4f399b 100644 --- a/src/Network.cc +++ b/src/Network.cc @@ -26,6 +26,8 @@ using namespace std; +namespace phosg { + uint32_t resolve_ipv4(const string& addr) { struct addrinfo* res0; if (getaddrinfo(addr.c_str(), nullptr, nullptr, &res0)) { @@ -184,7 +186,7 @@ int listen(const string& addr, int port, int backlog, bool nonblocking) { } // Only listen() on stream sockets - if (backlog && (listen(fd, backlog) != 0)) { + if (backlog && (::listen(fd, backlog) != 0)) { close(fd); throw runtime_error("can\'t listen on socket: " + string_for_error(errno)); } @@ -257,7 +259,7 @@ pair parse_netloc(const string& netloc, int default_port) { string gethostname() { string buf(sysconf(_SC_HOST_NAME_MAX) + 1, '\0'); - if (gethostname(buf.data(), buf.size())) { + if (::gethostname(buf.data(), buf.size())) { throw runtime_error("can\'t get hostname"); } buf.resize(strlen(buf.c_str())); @@ -266,7 +268,7 @@ string gethostname() { pair socketpair(int domain, int type, int protocol) { int fds[2]; - if (socketpair(domain, type, protocol, fds)) { + if (::socketpair(domain, type, protocol, fds)) { throw runtime_error("socketpair failed: " + string_for_error(errno)); } return make_pair(fds[0], fds[1]); @@ -289,3 +291,5 @@ unordered_map get_network_interfaces() { return ret; } + +} // namespace phosg diff --git a/src/Network.hh b/src/Network.hh index f125dcb..00a19d1 100644 --- a/src/Network.hh +++ b/src/Network.hh @@ -8,6 +8,8 @@ #include #include +namespace phosg { + std::pair make_sockaddr_storage( const std::string& addr, uint16_t port); @@ -66,3 +68,5 @@ std::pair socketpair( int protocol = 0); std::unordered_map get_network_interfaces(); + +} // namespace phosg diff --git a/src/ParseData.cc b/src/ParseData.cc index 6e632f1..658db2e 100644 --- a/src/ParseData.cc +++ b/src/ParseData.cc @@ -8,6 +8,7 @@ #include "Strings.hh" using namespace std; +using namespace phosg; int main(int argc, char** argv) { const char* src_filename = (argc > 1) ? argv[1] : nullptr; diff --git a/src/PhosgPNGConv.cc b/src/PhosgPNGConv.cc index 006bbe8..1368c69 100644 --- a/src/PhosgPNGConv.cc +++ b/src/PhosgPNGConv.cc @@ -9,6 +9,7 @@ #include "Strings.hh" using namespace std; +using namespace phosg; int main(int argc, char** argv) { const char* src_filename = (argc > 1) ? argv[1] : nullptr; diff --git a/src/Platform.hh b/src/Platform.hh index b05ee19..51047d6 100644 --- a/src/Platform.hh +++ b/src/Platform.hh @@ -2,6 +2,8 @@ #include +namespace phosg { + #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__CYGWIN__) #define PHOSG_WINDOWS @@ -58,3 +60,5 @@ // Define some useful shortcuts #define ATTR_PRINTF(x, y) __attribute__((format(printf, x, y))) + +} // namespace phosg diff --git a/src/Process.cc b/src/Process.cc index 502ca76..6a1c444 100644 --- a/src/Process.cc +++ b/src/Process.cc @@ -28,6 +28,8 @@ using namespace std; +namespace phosg { + unique_ptr popen_unique(const string& command, const string& mode) { unique_ptr f( @@ -508,8 +510,7 @@ SubprocessResult run_process(const vector& cmd, const string* stdin_data struct Buffer { const string* buf; size_t offset; - Buffer(const string* buf) : buf(buf), - offset(0) {} + Buffer(const string* buf) : buf(buf), offset(0) {} }; unordered_map write_fd_to_buffer; unordered_map read_fd_to_buffer; @@ -526,15 +527,14 @@ SubprocessResult run_process(const vector& cmd, const string* stdin_data read_fd_to_buffer.emplace(sp.stderr_fd(), &ret.stderr_contents); p.add(sp.stderr_fd(), POLLIN); - // read/write to pipes as long as the process is running + // Read and write to pipes as long as the process is running while ((ret.exit_status = sp.wait(true)) == -1) { for (const auto& pfd : p.poll(1000)) { if (pfd.second & POLLIN) { string* buf = read_fd_to_buffer.at(pfd.first); size_t read_offset = buf->size(); buf->resize(read_offset + READ_BLOCK_SIZE); - ssize_t bytes_read = read(pfd.first, buf->data() + read_offset, - READ_BLOCK_SIZE); + ssize_t bytes_read = ::read(pfd.first, buf->data() + read_offset, READ_BLOCK_SIZE); if (bytes_read > 0) { buf->resize(read_offset + bytes_read); } else if (bytes_read < 0) { @@ -585,13 +585,12 @@ SubprocessResult run_process(const vector& cmd, const string* stdin_data } ret.elapsed_time = now() - ret.elapsed_time; - // read any leftover data after termination + // Read any leftover data after termination for (auto& it : read_fd_to_buffer) { for (;;) { size_t read_offset = it.second->size(); it.second->resize(read_offset + READ_BLOCK_SIZE); - ssize_t bytes_read = read(it.first, it.second->data() + read_offset, - READ_BLOCK_SIZE); + ssize_t bytes_read = ::read(it.first, it.second->data() + read_offset, READ_BLOCK_SIZE); if (bytes_read > 0) { it.second->resize(it.second->size() - READ_BLOCK_SIZE + bytes_read); } else if (bytes_read < 0) { @@ -614,3 +613,5 @@ SubprocessResult run_process(const vector& cmd, const string* stdin_data return ret; } + +} // namespace phosg diff --git a/src/Process.hh b/src/Process.hh index 240de2e..0eb1d32 100644 --- a/src/Process.hh +++ b/src/Process.hh @@ -11,8 +11,9 @@ #include #include -std::unique_ptr popen_unique(const std::string& command, - const std::string& mode); +namespace phosg { + +std::unique_ptr popen_unique(const std::string& command, const std::string& mode); std::string name_for_pid(pid_t pid); pid_t pid_for_name(const std::string& name, bool search_commands = true, bool exclude_self = true); @@ -89,3 +90,5 @@ SubprocessResult run_process(const std::vector& cmd, const std::string* cwd = nullptr, const std::unordered_map* env = nullptr, size_t timeout_secs = 0); + +} // namespace phosg diff --git a/src/ProcessTest.cc b/src/ProcessTest.cc index a807611..8209db6 100644 --- a/src/ProcessTest.cc +++ b/src/ProcessTest.cc @@ -16,6 +16,7 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; #ifndef PHOSG_WINDOWS diff --git a/src/Random.cc b/src/Random.cc index 45bdc5c..ccd3cc8 100644 --- a/src/Random.cc +++ b/src/Random.cc @@ -11,6 +11,8 @@ using namespace std; +namespace phosg { + string random_data(size_t bytes) { string ret(bytes, '\0'); random_data(ret.data(), ret.size()); @@ -46,3 +48,5 @@ int64_t random_int(int64_t low, int64_t high) { return low + (random_object() % range); } } + +} // namespace phosg diff --git a/src/Random.hh b/src/Random.hh index 885b320..a41942c 100644 --- a/src/Random.hh +++ b/src/Random.hh @@ -5,6 +5,8 @@ #include +namespace phosg { + std::string random_data(size_t bytes); void random_data(void* data, size_t bytes); @@ -16,3 +18,5 @@ T random_object() { } int64_t random_int(int64_t low, int64_t high); + +} // namespace phosg diff --git a/src/Strings.cc b/src/Strings.cc index 2c23738..118e2a7 100644 --- a/src/Strings.cc +++ b/src/Strings.cc @@ -22,6 +22,8 @@ using namespace std; +namespace phosg { + unique_ptr malloc_unique(size_t size) { return unique_ptr(malloc(size), free); } @@ -44,7 +46,7 @@ string toupper(const string& s) { string ret; ret.reserve(s.size()); for (char ch : s) { - ret.push_back(toupper(ch)); + ret.push_back(::toupper(ch)); } return ret; } @@ -53,7 +55,7 @@ string tolower(const string& s) { string ret; ret.reserve(s.size()); for (char ch : s) { - ret.push_back(tolower(ch)); + ret.push_back(::tolower(ch)); } return ret; } @@ -1763,3 +1765,5 @@ void BlockStringWriter::write_vprintf(const char* fmt, va_list va) { string BlockStringWriter::close(const char* separator) { return join(this->blocks, separator); } + +} // namespace phosg diff --git a/src/Strings.hh b/src/Strings.hh index 289ecc0..f8fef64 100644 --- a/src/Strings.hh +++ b/src/Strings.hh @@ -15,6 +15,8 @@ #include "Filesystem.hh" #include "Platform.hh" +namespace phosg { + std::unique_ptr malloc_unique(size_t size); bool starts_with(const std::string& s, const std::string& start); @@ -810,3 +812,5 @@ T* data_at(std::string& s, size_t offset = 0) { } size_t count_zeroes(const void* vdata, size_t size, size_t stride = 1); + +} // namespace phosg diff --git a/src/StringsTest.cc b/src/StringsTest.cc index aa13c6b..a87c310 100644 --- a/src/StringsTest.cc +++ b/src/StringsTest.cc @@ -8,6 +8,7 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; template void print_data_test_case(const string& expected_output, ArgTs... args) { diff --git a/src/Time.cc b/src/Time.cc index 95db7e1..51f8edf 100644 --- a/src/Time.cc +++ b/src/Time.cc @@ -10,6 +10,8 @@ using namespace std; +namespace phosg { + uint64_t now() { timeval t; gettimeofday(&t, nullptr); @@ -118,3 +120,5 @@ struct timeval usecs_to_timeval(uint64_t usecs) { uint64_t timeval_to_usecs(struct timeval& tv) { return (tv.tv_sec * 1000000) + tv.tv_usec; } + +} // namespace phosg diff --git a/src/Time.hh b/src/Time.hh index 21e3834..e509995 100644 --- a/src/Time.hh +++ b/src/Time.hh @@ -4,6 +4,8 @@ #include +namespace phosg { + uint64_t now(); std::string format_time(uint64_t t); @@ -12,3 +14,5 @@ std::string format_duration(uint64_t usecs, int8_t subsecond_precision = -1); struct timeval usecs_to_timeval(uint64_t usecs); uint64_t timeval_to_usecs(struct timeval& tv); + +} // namespace phosg diff --git a/src/TimeTest.cc b/src/TimeTest.cc index 8e57c66..962750d 100644 --- a/src/TimeTest.cc +++ b/src/TimeTest.cc @@ -5,9 +5,9 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; int main(int, char**) { - fprintf(stderr, "-- usleep\n"); uint64_t start_time = now(); expect_ge(now(), start_time); diff --git a/src/Tools.cc b/src/Tools.cc index b2c8fbf..3edb7b7 100644 --- a/src/Tools.cc +++ b/src/Tools.cc @@ -2,8 +2,12 @@ using namespace std; +namespace phosg { + CallOnDestroy::CallOnDestroy(function f) : f(f) {} CallOnDestroy::~CallOnDestroy() { this->f(); } + +} // namespace phosg diff --git a/src/Tools.hh b/src/Tools.hh index 95e2fbb..5859e92 100644 --- a/src/Tools.hh +++ b/src/Tools.hh @@ -13,6 +13,8 @@ #include "Strings.hh" #include "Time.hh" +namespace phosg { + class CallOnDestroy { public: CallOnDestroy(std::function f); @@ -111,3 +113,5 @@ IntT parallel_range( return result_value; } + +} // namespace phosg diff --git a/src/ToolsTest.cc b/src/ToolsTest.cc index ec92407..5d04eb5 100644 --- a/src/ToolsTest.cc +++ b/src/ToolsTest.cc @@ -4,6 +4,7 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; int main(int, char**) { printf("-- on_close_scope\n"); diff --git a/src/Types.hh b/src/Types.hh index 82604f6..d827669 100644 --- a/src/Types.hh +++ b/src/Types.hh @@ -3,6 +3,8 @@ #include #include +namespace phosg { + inline std::partial_ordering partial_order_for_strong_order(std::strong_ordering st_order) { if (st_order < 0) { return std::partial_ordering::less; @@ -29,3 +31,5 @@ const char* name_for_enum(T) { static_assert(always_false::v, "unspecialized name_for_enum should never be called"); throw std::logic_error("unspecialized name_for_enum should never be called"); } + +} // namespace phosg diff --git a/src/UnitTest.cc b/src/UnitTest.cc index 5e2dffa..f3417ad 100644 --- a/src/UnitTest.cc +++ b/src/UnitTest.cc @@ -6,6 +6,8 @@ using namespace std; +namespace phosg { + expectation_failed::expectation_failed(const char* msg, const char* file, uint64_t line) : logic_error(string_printf("failure at %s:%" PRIu64 ": %s", file, line, msg)), msg(msg), @@ -34,3 +36,5 @@ void expect_raises(std::function fn) { expect_msg(false, "incorrect exception type raised"); } }; + +} // namespace phosg diff --git a/src/UnitTest.hh b/src/UnitTest.hh index de89b4a..59eee30 100644 --- a/src/UnitTest.hh +++ b/src/UnitTest.hh @@ -7,6 +7,8 @@ #include "Strings.hh" +namespace phosg { + class expectation_failed : public std::logic_error { public: expectation_failed(const char* msg, const char* file, uint64_t line); @@ -48,3 +50,5 @@ void expect_raises(std::function fn) { template <> void expect_raises(std::function fn); + +} // namespace phosg diff --git a/src/UnitTestTest.cc b/src/UnitTestTest.cc index 2d93c1b..9cfa65f 100644 --- a/src/UnitTestTest.cc +++ b/src/UnitTestTest.cc @@ -3,6 +3,7 @@ #include "UnitTest.hh" using namespace std; +using namespace phosg; #define expect_fails(x) \ do { \ diff --git a/src/Vector-inl.hh b/src/Vector-inl.hh index ee4295c..b80edc7 100644 --- a/src/Vector-inl.hh +++ b/src/Vector-inl.hh @@ -6,6 +6,8 @@ #include "Strings.hh" +namespace phosg { + template Vector2::Vector2() : x(0), @@ -811,3 +813,5 @@ std::string Matrix4::str() const { this->m[0][2], this->m[1][2], this->m[2][2], this->m[3][2], this->m[0][3], this->m[1][3], this->m[2][3], this->m[3][3]); } + +} // namespace phosg diff --git a/src/Vector.hh b/src/Vector.hh index 3a0cdb1..8fde407 100644 --- a/src/Vector.hh +++ b/src/Vector.hh @@ -2,6 +2,8 @@ #include +namespace phosg { + template struct Vector2 { union { @@ -211,4 +213,6 @@ struct Matrix4 { std::string str() const; }; +} // namespace phosg + #include "Vector-inl.hh"