From a95ec9625646d851bb55598a5cdf777bef825687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 14 Jul 2017 23:05:44 -0700 Subject: [PATCH] Switching code to use FilePath. --- include/bx/crtimpl.h | 18 ++++++++++------ include/bx/filepath.h | 8 ++++++- include/bx/inline/readerwriter.inl | 18 ++++++++++++---- include/bx/inline/string.inl | 6 ++++++ include/bx/readerwriter.h | 21 +++++++++++++----- include/bx/string.h | 3 +++ src/crtimpl.cpp | 34 +++++++++++++++++++----------- src/filepath.cpp | 15 +++++++++++-- 8 files changed, 93 insertions(+), 30 deletions(-) diff --git a/include/bx/crtimpl.h b/include/bx/crtimpl.h index 1bb679d..55031fd 100644 --- a/include/bx/crtimpl.h +++ b/include/bx/crtimpl.h @@ -45,7 +45,7 @@ namespace bx virtual ~FileReader(); /// - virtual bool open(const char* _filePath, Error* _err) BX_OVERRIDE; + virtual bool open(const FilePath& _filePath, Error* _err) BX_OVERRIDE; /// virtual void close() BX_OVERRIDE; @@ -71,7 +71,7 @@ namespace bx virtual ~FileWriter(); /// - virtual bool open(const char* _filePath, bool _append, Error* _err) BX_OVERRIDE; + virtual bool open(const FilePath& _filePath, bool _append, Error* _err) BX_OVERRIDE; /// virtual void close() BX_OVERRIDE; @@ -87,7 +87,10 @@ namespace bx }; /// - class ProcessReader : public ReaderOpenI, public CloserI, public ReaderI + class ProcessReader + : public ProcessOpenI + , public CloserI + , public ReaderI { public: /// @@ -97,7 +100,7 @@ namespace bx ~ProcessReader(); /// - virtual bool open(const char* _command, Error* _err) BX_OVERRIDE; + virtual bool open(const FilePath& _filePath, const StringView& _args, Error* _err) BX_OVERRIDE; /// virtual void close() BX_OVERRIDE; @@ -114,7 +117,10 @@ namespace bx }; /// - class ProcessWriter : public WriterOpenI, public CloserI, public WriterI + class ProcessWriter + : public ProcessOpenI + , public CloserI + , public WriterI { public: /// @@ -124,7 +130,7 @@ namespace bx ~ProcessWriter(); /// - virtual bool open(const char* _command, bool, Error* _err) BX_OVERRIDE; + virtual bool open(const FilePath& _filePath, const StringView& _args, Error* _err) BX_OVERRIDE; /// virtual void close() BX_OVERRIDE; diff --git a/include/bx/filepath.h b/include/bx/filepath.h index d844e07..225011c 100644 --- a/include/bx/filepath.h +++ b/include/bx/filepath.h @@ -27,14 +27,20 @@ namespace bx /// FilePath(); + /// + FilePath(const char* _str); + /// FilePath(const StringView& _str); + /// + FilePath& operator=(const StringView& _rhs); + /// void set(const StringView& _str); /// - const StringView get() const; + const char* get() const; /// If path is `/abv/gd/555/333/pod.mac` returns `/abv/gd/555/333/`. /// diff --git a/include/bx/inline/readerwriter.inl b/include/bx/inline/readerwriter.inl index 72d9651..91ac202 100644 --- a/include/bx/inline/readerwriter.inl +++ b/include/bx/inline/readerwriter.inl @@ -29,6 +29,10 @@ namespace bx { } + inline ProcessOpenI::~ProcessOpenI() + { + } + inline CloserI::~CloserI() { } @@ -293,9 +297,9 @@ namespace bx return _writer->write(_data, _size, _err); } - inline int32_t write(WriterI* _writer, const char* _str, Error* _err) + inline int32_t write(WriterI* _writer, const StringView& _str, Error* _err) { - return write(_writer, _str, strLen(_str), _err); + return write(_writer, _str.getPtr(), _str.getLength(), _err); } inline int32_t writeRep(WriterI* _writer, uint8_t _byte, int32_t _size, Error* _err) @@ -437,18 +441,24 @@ namespace bx return 0; } - inline bool open(ReaderOpenI* _reader, const char* _filePath, Error* _err) + inline bool open(ReaderOpenI* _reader, const FilePath& _filePath, Error* _err) { BX_ERROR_USE_TEMP_WHEN_NULL(_err); return _reader->open(_filePath, _err); } - inline bool open(WriterOpenI* _writer, const char* _filePath, bool _append, Error* _err) + inline bool open(WriterOpenI* _writer, const FilePath& _filePath, bool _append, Error* _err) { BX_ERROR_USE_TEMP_WHEN_NULL(_err); return _writer->open(_filePath, _append, _err); } + inline bool open(ProcessOpenI* _process, const FilePath& _filePath, const StringView& _args, Error* _err) + { + BX_ERROR_USE_TEMP_WHEN_NULL(_err); + return _process->open(_filePath, _args, _err); + } + inline void close(CloserI* _reader) { _reader->close(); diff --git a/include/bx/inline/string.inl b/include/bx/inline/string.inl index 46f2bec..21f9a88 100644 --- a/include/bx/inline/string.inl +++ b/include/bx/inline/string.inl @@ -64,6 +64,12 @@ namespace bx set(_rhs.m_ptr, _rhs.m_len); } + inline StringView& StringView::operator=(const char* _rhs) + { + set(_rhs); + return *this; + } + inline StringView& StringView::operator=(const StringView& _rhs) { set(_rhs.m_ptr, _rhs.m_len); diff --git a/include/bx/readerwriter.h b/include/bx/readerwriter.h index d5c6dce..1f222a4 100644 --- a/include/bx/readerwriter.h +++ b/include/bx/readerwriter.h @@ -9,6 +9,7 @@ #include "allocator.h" #include "error.h" #include "endian.h" +#include "filepath.h" #include "uint32_t.h" BX_ERROR_RESULT(BX_ERROR_READERWRITER_OPEN, BX_MAKEFOURCC('R', 'W', 0, 1) ); @@ -65,14 +66,21 @@ namespace bx struct BX_NO_VTABLE ReaderOpenI { virtual ~ReaderOpenI() = 0; - virtual bool open(const char* _filePath, Error* _err) = 0; + virtual bool open(const FilePath& _filePath, Error* _err) = 0; }; /// struct BX_NO_VTABLE WriterOpenI { virtual ~WriterOpenI() = 0; - virtual bool open(const char* _filePath, bool _append, Error* _err) = 0; + virtual bool open(const FilePath& _filePath, bool _append, Error* _err) = 0; + }; + + /// + struct BX_NO_VTABLE ProcessOpenI + { + virtual ~ProcessOpenI() = 0; + virtual bool open(const FilePath& _filePath, const StringView& _args, Error* _err) = 0; }; /// @@ -248,7 +256,7 @@ namespace bx int32_t write(WriterI* _writer, const void* _data, int32_t _size, Error* _err = NULL); /// Writer string. - inline int32_t write(WriterI* _writer, const char* _str, Error* _err = NULL); + inline int32_t write(WriterI* _writer, const StringView& _str, Error* _err = NULL); /// Write repeat the same value. int32_t writeRep(WriterI* _writer, uint8_t _byte, int32_t _size, Error* _err = NULL); @@ -291,10 +299,13 @@ namespace bx int32_t align(WriterSeekerI* _writer, uint32_t _alignment, Error* _err = NULL); /// - bool open(ReaderOpenI* _reader, const char* _filePath, Error* _err = NULL); + bool open(ReaderOpenI* _reader, const FilePath& _filePath, Error* _err = NULL); /// - bool open(WriterOpenI* _writer, const char* _filePath, bool _append = false, Error* _err = NULL); + bool open(WriterOpenI* _writer, const FilePath& _filePath, bool _append = false, Error* _err = NULL); + + /// + bool open(ProcessOpenI* _process, const FilePath& _filePath, const StringView& _args, Error* _err = NULL); /// void close(CloserI* _reader); diff --git a/include/bx/string.h b/include/bx/string.h index 3cdafa8..c0fd76a 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -31,6 +31,9 @@ namespace bx /// StringView(const StringView& _rhs); + /// + StringView& operator=(const char* _rhs); + /// StringView& operator=(const StringView& _rhs); diff --git a/src/crtimpl.cpp b/src/crtimpl.cpp index 8466509..4b1e5a4 100644 --- a/src/crtimpl.cpp +++ b/src/crtimpl.cpp @@ -116,7 +116,7 @@ namespace bx close(); } - virtual bool open(const char* _filePath, Error* _err) BX_OVERRIDE + virtual bool open(const FilePath& _filePath, Error* _err) BX_OVERRIDE { BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); @@ -126,7 +126,7 @@ namespace bx return false; } - m_file = fopen(_filePath, "rb"); + m_file = fopen(_filePath.get(), "rb"); if (NULL == m_file) { BX_ERROR_SET(_err, BX_ERROR_READERWRITER_OPEN, "FileReader: Failed to open file."); @@ -196,7 +196,7 @@ namespace bx close(); } - virtual bool open(const char* _filePath, bool _append, Error* _err) BX_OVERRIDE + virtual bool open(const FilePath& _filePath, bool _append, Error* _err) BX_OVERRIDE { BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); @@ -206,7 +206,7 @@ namespace bx return false; } - m_file = fopen(_filePath, _append ? "ab" : "wb"); + m_file = fopen(_filePath.get(), _append ? "ab" : "wb"); if (NULL == m_file) { @@ -269,7 +269,7 @@ namespace bx close(); } - virtual bool open(const char* _filePath, Error* _err) BX_OVERRIDE + virtual bool open(const FilePath& _filePath, Error* _err) BX_OVERRIDE { BX_UNUSED(_filePath, _err); return false; @@ -304,7 +304,7 @@ namespace bx close(); } - virtual bool open(const char* _filePath, bool _append, Error* _err) BX_OVERRIDE + virtual bool open(const FilePath& _filePath, bool _append, Error* _err) BX_OVERRIDE { BX_UNUSED(_filePath, _append); return false; @@ -341,7 +341,7 @@ namespace bx impl->~FileReaderImpl(); } - bool FileReader::open(const char* _filePath, Error* _err) + bool FileReader::open(const FilePath& _filePath, Error* _err) { FileReaderImpl* impl = reinterpret_cast(m_internal); return impl->open(_filePath, _err); @@ -377,7 +377,7 @@ namespace bx impl->~FileWriterImpl(); } - bool FileWriter::open(const char* _filePath, bool _append, Error* _err) + bool FileWriter::open(const FilePath& _filePath, bool _append, Error* _err) { FileWriterImpl* impl = reinterpret_cast(m_internal); return impl->open(_filePath, _append, _err); @@ -436,7 +436,7 @@ namespace bx BX_CHECK(NULL == m_file, "Process not closed!"); } - bool ProcessReader::open(const char* _command, Error* _err) + bool ProcessReader::open(const FilePath& _filePath, const StringView& _args, Error* _err) { BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); @@ -446,7 +446,12 @@ namespace bx return false; } - m_file = popen(_command, "r"); + char tmp[kMaxFilePath*2]; + strCopy(tmp, BX_COUNTOF(tmp), _filePath.get() ); + strCat(tmp, BX_COUNTOF(tmp), " "); + strCat(tmp, BX_COUNTOF(tmp), _args); + + m_file = popen(tmp, "r"); if (NULL == m_file) { BX_ERROR_SET(_err, BX_ERROR_READERWRITER_OPEN, "ProcessReader: Failed to open process."); @@ -502,7 +507,7 @@ namespace bx BX_CHECK(NULL == m_file, "Process not closed!"); } - bool ProcessWriter::open(const char* _command, bool, Error* _err) + bool ProcessWriter::open(const FilePath& _filePath, const StringView& _args, Error* _err) { BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); @@ -512,7 +517,12 @@ namespace bx return false; } - m_file = popen(_command, "w"); + char tmp[kMaxFilePath*2]; + strCopy(tmp, BX_COUNTOF(tmp), _filePath.get() ); + strCat(tmp, BX_COUNTOF(tmp), " "); + strCat(tmp, BX_COUNTOF(tmp), _args); + + m_file = popen(tmp, "w"); if (NULL == m_file) { BX_ERROR_SET(_err, BX_ERROR_READERWRITER_OPEN, "ProcessWriter: Failed to open process."); diff --git a/src/filepath.cpp b/src/filepath.cpp index 88ddb93..c010e0b 100644 --- a/src/filepath.cpp +++ b/src/filepath.cpp @@ -133,11 +133,22 @@ namespace bx set(""); } + FilePath::FilePath(const char* _rhs) + { + set(_rhs); + } + FilePath::FilePath(const StringView& _filePath) { set(_filePath); } + FilePath& FilePath::operator=(const StringView& _rhs) + { + set(_rhs); + return *this; + } + void FilePath::set(const StringView& _filePath) { normalizeFilePath( @@ -148,9 +159,9 @@ namespace bx ); } - const StringView FilePath::get() const + const char* FilePath::get() const { - return StringView(m_filePath); + return m_filePath; } const StringView FilePath::getPath() const