Switching code to use FilePath.

This commit is contained in:
Branimir Karadžić
2017-07-14 23:05:44 -07:00
parent 6e252cf889
commit a95ec96256
8 changed files with 93 additions and 30 deletions

View File

@@ -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;

View File

@@ -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/`.
///

View File

@@ -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();

View File

@@ -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);

View File

@@ -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);

View File

@@ -31,6 +31,9 @@ namespace bx
///
StringView(const StringView& _rhs);
///
StringView& operator=(const char* _rhs);
///
StringView& operator=(const StringView& _rhs);

View File

@@ -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<FileReaderImpl*>(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<FileWriterImpl*>(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.");

View File

@@ -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