From d4906fd3bd57e1830092eb278b83fe70786cb922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sat, 15 Jul 2017 20:58:27 -0700 Subject: [PATCH] Moving code around and renaming files. --- include/bx/allocator.h | 28 ++- include/bx/crtimpl.h | 151 ------------- include/bx/easing.h | 2 +- include/bx/file.h | 95 ++++++++ include/bx/filepath.h | 7 + include/bx/inline/{fpumath.inl => math.inl} | 0 include/bx/{fpumath.h => math.h} | 2 +- include/bx/os.h | 20 -- include/bx/pixelformat.h | 2 +- include/bx/process.h | 75 ++++++ include/bx/rng.h | 2 +- include/bx/simd_t.h | 2 +- src/allocator.cpp | 74 ++++++ src/amalgamated.cpp | 2 +- src/dtoa.cpp | 2 +- src/{crtimpl.cpp => file.cpp} | 239 +++----------------- src/filepath.cpp | 62 ++++- src/{fpumath.cpp => math.cpp} | 2 +- src/os.cpp | 94 +------- src/process.cpp | 168 ++++++++++++++ tests/filepath_test.cpp | 6 + tests/fpumath_test.cpp | 2 +- tests/os_test.cpp | 7 - tests/simd_test.cpp | 2 +- tests/string_test.cpp | 1 - 25 files changed, 557 insertions(+), 490 deletions(-) delete mode 100644 include/bx/crtimpl.h create mode 100644 include/bx/file.h rename include/bx/inline/{fpumath.inl => math.inl} (100%) rename include/bx/{fpumath.h => math.h} (99%) create mode 100644 include/bx/process.h create mode 100644 src/allocator.cpp rename src/{crtimpl.cpp => file.cpp} (61%) rename src/{fpumath.cpp => math.cpp} (99%) create mode 100644 src/process.cpp diff --git a/include/bx/allocator.h b/include/bx/allocator.h index 67aa7ea..f920c08 100644 --- a/include/bx/allocator.h +++ b/include/bx/allocator.h @@ -53,7 +53,33 @@ namespace bx /// @param[in] _align Alignment. /// @param[in] _file Debug file path info. /// @param[in] _line Debug file line info. - virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) = 0; + virtual void* realloc( + void* _ptr + , size_t _size + , size_t _align + , const char* _file + , uint32_t _line + ) = 0; + }; + + /// + class DefaultAllocator : public AllocatorI + { + public: + /// + DefaultAllocator(); + + /// + virtual ~DefaultAllocator(); + + /// + virtual void* realloc( + void* _ptr + , size_t _size + , size_t _align + , const char* _file + , uint32_t _line + ) override; }; /// Check if pointer is aligned. _align must be power of two. diff --git a/include/bx/crtimpl.h b/include/bx/crtimpl.h deleted file mode 100644 index 69e896e..0000000 --- a/include/bx/crtimpl.h +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright 2010-2017 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bx#license-bsd-2-clause - */ - -#ifndef BX_CRTIMPL_H_HEADER_GUARD -#define BX_CRTIMPL_H_HEADER_GUARD - -#include "allocator.h" -#include "readerwriter.h" - -namespace bx -{ - /// - class DefaultAllocator : public AllocatorI - { - public: - /// - DefaultAllocator(); - - /// - virtual ~DefaultAllocator(); - - /// - virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) override; - }; - - /// - ReaderI* getStdIn(); - - /// - WriterI* getStdOut(); - - /// - WriterI* getStdErr(); - - /// - class FileReader : public FileReaderI - { - public: - /// - FileReader(); - - /// - virtual ~FileReader(); - - /// - virtual bool open(const FilePath& _filePath, Error* _err) override; - - /// - virtual void close() override; - - /// - virtual int64_t seek(int64_t _offset = 0, Whence::Enum _whence = Whence::Current) override; - - /// - virtual int32_t read(void* _data, int32_t _size, Error* _err) override; - - private: - BX_ALIGN_DECL(16, uint8_t) m_internal[64]; - }; - - /// - class FileWriter : public FileWriterI - { - public: - /// - FileWriter(); - - /// - virtual ~FileWriter(); - - /// - virtual bool open(const FilePath& _filePath, bool _append, Error* _err) override; - - /// - virtual void close() override; - - /// - virtual int64_t seek(int64_t _offset = 0, Whence::Enum _whence = Whence::Current) override; - - /// - virtual int32_t write(const void* _data, int32_t _size, Error* _err) override; - - private: - BX_ALIGN_DECL(16, uint8_t) m_internal[64]; - }; - - /// - class ProcessReader - : public ProcessOpenI - , public CloserI - , public ReaderI - { - public: - /// - ProcessReader(); - - /// - ~ProcessReader(); - - /// - virtual bool open(const FilePath& _filePath, const StringView& _args, Error* _err) override; - - /// - virtual void close() override; - - /// - virtual int32_t read(void* _data, int32_t _size, Error* _err) override; - - /// - int32_t getExitCode() const; - - private: - void* m_file; - int32_t m_exitCode; - }; - - /// - class ProcessWriter - : public ProcessOpenI - , public CloserI - , public WriterI - { - public: - /// - ProcessWriter(); - - /// - ~ProcessWriter(); - - /// - virtual bool open(const FilePath& _filePath, const StringView& _args, Error* _err) override; - - /// - virtual void close() override; - - /// - virtual int32_t write(const void* _data, int32_t _size, Error* _err) override; - - /// - int32_t getExitCode() const; - - private: - void* m_file; - int32_t m_exitCode; - }; - -} // namespace bx - -#endif // BX_CRTIMPL_H_HEADER_GUARD diff --git a/include/bx/easing.h b/include/bx/easing.h index 6213157..e0d7ce5 100644 --- a/include/bx/easing.h +++ b/include/bx/easing.h @@ -6,7 +6,7 @@ #ifndef BX_EASING_H_HEADER_GUARD #define BX_EASING_H_HEADER_GUARD -#include "fpumath.h" +#include "math.h" // Reference: // http://easings.net/ diff --git a/include/bx/file.h b/include/bx/file.h new file mode 100644 index 0000000..d0ea3ea --- /dev/null +++ b/include/bx/file.h @@ -0,0 +1,95 @@ +/* + * Copyright 2010-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#ifndef BX_FILE_H_HEADER_GUARD +#define BX_FILE_H_HEADER_GUARD + +#include "filepath.h" +#include "readerwriter.h" + +namespace bx +{ + /// + ReaderI* getStdIn(); + + /// + WriterI* getStdOut(); + + /// + WriterI* getStdErr(); + + /// + class FileReader : public FileReaderI + { + public: + /// + FileReader(); + + /// + virtual ~FileReader(); + + /// + virtual bool open(const FilePath& _filePath, Error* _err) override; + + /// + virtual void close() override; + + /// + virtual int64_t seek(int64_t _offset = 0, Whence::Enum _whence = Whence::Current) override; + + /// + virtual int32_t read(void* _data, int32_t _size, Error* _err) override; + + private: + BX_ALIGN_DECL(16, uint8_t) m_internal[64]; + }; + + /// + class FileWriter : public FileWriterI + { + public: + /// + FileWriter(); + + /// + virtual ~FileWriter(); + + /// + virtual bool open(const FilePath& _filePath, bool _append, Error* _err) override; + + /// + virtual void close() override; + + /// + virtual int64_t seek(int64_t _offset = 0, Whence::Enum _whence = Whence::Current) override; + + /// + virtual int32_t write(const void* _data, int32_t _size, Error* _err) override; + + private: + BX_ALIGN_DECL(16, uint8_t) m_internal[64]; + }; + + /// + struct FileInfo + { + enum Enum + { + Regular, + Directory, + + Count + }; + + uint64_t m_size; + Enum m_type; + }; + + /// + bool stat(const char* _filePath, FileInfo& _fileInfo); + +} // namespace bx + +#endif // BX_FILE_H_HEADER_GUARD diff --git a/include/bx/filepath.h b/include/bx/filepath.h index 225011c..fffae92 100644 --- a/include/bx/filepath.h +++ b/include/bx/filepath.h @@ -11,6 +11,7 @@ namespace bx { const int32_t kMaxFilePath = 1024; + struct TempDir { enum Enum { Tag }; }; /// FilePath parser and helper. /// @@ -27,6 +28,9 @@ namespace bx /// FilePath(); + /// + FilePath(TempDir::Enum); + /// FilePath(const char* _str); @@ -36,6 +40,9 @@ namespace bx /// FilePath& operator=(const StringView& _rhs); + /// + void set(TempDir::Enum); + /// void set(const StringView& _str); diff --git a/include/bx/inline/fpumath.inl b/include/bx/inline/math.inl similarity index 100% rename from include/bx/inline/fpumath.inl rename to include/bx/inline/math.inl diff --git a/include/bx/fpumath.h b/include/bx/math.h similarity index 99% rename from include/bx/fpumath.h rename to include/bx/math.h index 2b4f090..8bf042c 100644 --- a/include/bx/fpumath.h +++ b/include/bx/math.h @@ -488,6 +488,6 @@ namespace bx } // namespace bx -#include "inline/fpumath.inl" +#include "inline/math.inl" #endif // BX_FPU_MATH_H_HEADER_GUARD diff --git a/include/bx/os.h b/include/bx/os.h index 56781b2..a3a873d 100644 --- a/include/bx/os.h +++ b/include/bx/os.h @@ -18,20 +18,6 @@ namespace bx { - struct FileInfo - { - enum Enum - { - Regular, - Directory, - - Count - }; - - uint64_t m_size; - Enum m_type; - }; - /// void sleep(uint32_t _ms); @@ -68,12 +54,6 @@ namespace bx /// char* pwd(char* _buffer, uint32_t _size); - /// - bool getTempPath(char* _out, uint32_t* _inOutSize); - - /// - bool stat(const char* _filePath, FileInfo& _fileInfo); - /// void* exec(const char* const* _argv); diff --git a/include/bx/pixelformat.h b/include/bx/pixelformat.h index 4c96775..a79cf3c 100644 --- a/include/bx/pixelformat.h +++ b/include/bx/pixelformat.h @@ -6,7 +6,7 @@ #ifndef BX_PIXEL_FORMAT_H_HEADER_GUARD #define BX_PIXEL_FORMAT_H_HEADER_GUARD -#include "fpumath.h" +#include "math.h" #include "uint32_t.h" namespace bx diff --git a/include/bx/process.h b/include/bx/process.h new file mode 100644 index 0000000..47a1119 --- /dev/null +++ b/include/bx/process.h @@ -0,0 +1,75 @@ +/* + * Copyright 2010-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#ifndef BX_PROCESS_H_HEADER_GUARD +#define BX_PROCESS_H_HEADER_GUARD + +#include "readerwriter.h" + +namespace bx +{ + /// + class ProcessReader + : public ProcessOpenI + , public CloserI + , public ReaderI + { + public: + /// + ProcessReader(); + + /// + ~ProcessReader(); + + /// + virtual bool open(const FilePath& _filePath, const StringView& _args, Error* _err) override; + + /// + virtual void close() override; + + /// + virtual int32_t read(void* _data, int32_t _size, Error* _err) override; + + /// + int32_t getExitCode() const; + + private: + void* m_file; + int32_t m_exitCode; + }; + + /// + class ProcessWriter + : public ProcessOpenI + , public CloserI + , public WriterI + { + public: + /// + ProcessWriter(); + + /// + ~ProcessWriter(); + + /// + virtual bool open(const FilePath& _filePath, const StringView& _args, Error* _err) override; + + /// + virtual void close() override; + + /// + virtual int32_t write(const void* _data, int32_t _size, Error* _err) override; + + /// + int32_t getExitCode() const; + + private: + void* m_file; + int32_t m_exitCode; + }; + +} // namespace bx + +#endif // BX_PROCESS_H_HEADER_GUARD diff --git a/include/bx/rng.h b/include/bx/rng.h index 56d7674..e2c9ca8 100644 --- a/include/bx/rng.h +++ b/include/bx/rng.h @@ -7,7 +7,7 @@ #define BX_RNG_H_HEADER_GUARD #include "bx.h" -#include "fpumath.h" +#include "math.h" #include "uint32_t.h" namespace bx diff --git a/include/bx/simd_t.h b/include/bx/simd_t.h index d3befc4..f3faad5 100644 --- a/include/bx/simd_t.h +++ b/include/bx/simd_t.h @@ -7,7 +7,7 @@ #define BX_SIMD_T_H_HEADER_GUARD #include "bx.h" -#include "fpumath.h" +#include "math.h" #define BX_SIMD_FORCE_INLINE BX_FORCE_INLINE #define BX_SIMD_INLINE inline diff --git a/src/allocator.cpp b/src/allocator.cpp new file mode 100644 index 0000000..79c22d2 --- /dev/null +++ b/src/allocator.cpp @@ -0,0 +1,74 @@ +/* + * Copyright 2010-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#include + +#include + +#ifndef BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT +# define BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT 8 +#endif // BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT + +namespace bx +{ + DefaultAllocator::DefaultAllocator() + { + } + + DefaultAllocator::~DefaultAllocator() + { + } + + void* DefaultAllocator::realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) + { + if (0 == _size) + { + if (NULL != _ptr) + { + if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) + { + ::free(_ptr); + return NULL; + } + +# if BX_COMPILER_MSVC + BX_UNUSED(_file, _line); + _aligned_free(_ptr); +# else + bx::alignedFree(this, _ptr, _align, _file, _line); +# endif // BX_ + } + + return NULL; + } + else if (NULL == _ptr) + { + if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) + { + return ::malloc(_size); + } + +# if BX_COMPILER_MSVC + BX_UNUSED(_file, _line); + return _aligned_malloc(_size, _align); +# else + return bx::alignedAlloc(this, _size, _align, _file, _line); +# endif // BX_ + } + + if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) + { + return ::realloc(_ptr, _size); + } + +# if BX_COMPILER_MSVC + BX_UNUSED(_file, _line); + return _aligned_realloc(_ptr, _size, _align); +# else + return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line); +# endif // BX_ + } + +} // namespace bx diff --git a/src/amalgamated.cpp b/src/amalgamated.cpp index 1aeb2bb..c70b605 100644 --- a/src/amalgamated.cpp +++ b/src/amalgamated.cpp @@ -9,7 +9,7 @@ #include "crtnone.cpp" #include "debug.cpp" #include "dtoa.cpp" -#include "fpumath.cpp" +#include "math.cpp" #include "mutex.cpp" #include "os.cpp" #include "semaphore.cpp" diff --git a/src/dtoa.cpp b/src/dtoa.cpp index dadaa69..bbac373 100644 --- a/src/dtoa.cpp +++ b/src/dtoa.cpp @@ -4,7 +4,7 @@ */ #include -#include +#include #include #include diff --git a/src/crtimpl.cpp b/src/file.cpp similarity index 61% rename from src/crtimpl.cpp rename to src/file.cpp index 742d87f..3cd70e7 100644 --- a/src/crtimpl.cpp +++ b/src/file.cpp @@ -3,10 +3,10 @@ * License: https://github.com/bkaradzic/bx#license-bsd-2-clause */ -#include +#include #include -#include +#include #ifndef BX_CONFIG_CRT_FILE_READER_WRITER # define BX_CONFIG_CRT_FILE_READER_WRITER !(0 \ @@ -14,80 +14,8 @@ ) #endif // BX_CONFIG_CRT_FILE_READER_WRITER -#ifndef BX_CONFIG_CRT_PROCESS -# define BX_CONFIG_CRT_PROCESS !(0 \ - || BX_CRT_NONE \ - || BX_PLATFORM_EMSCRIPTEN \ - || BX_PLATFORM_PS4 \ - || BX_PLATFORM_WINRT \ - || BX_PLATFORM_XBOXONE \ - ) -#endif // BX_CONFIG_CRT_PROCESS - -#ifndef BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT -# define BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT 8 -#endif // BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT - namespace bx { - DefaultAllocator::DefaultAllocator() - { - } - - DefaultAllocator::~DefaultAllocator() - { - } - - void* DefaultAllocator::realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) - { - if (0 == _size) - { - if (NULL != _ptr) - { - if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) - { - ::free(_ptr); - return NULL; - } - -# if BX_COMPILER_MSVC - BX_UNUSED(_file, _line); - _aligned_free(_ptr); -# else - bx::alignedFree(this, _ptr, _align, _file, _line); -# endif // BX_ - } - - return NULL; - } - else if (NULL == _ptr) - { - if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) - { - return ::malloc(_size); - } - -# if BX_COMPILER_MSVC - BX_UNUSED(_file, _line); - return _aligned_malloc(_size, _align); -# else - return bx::alignedAlloc(this, _size, _align, _file, _line); -# endif // BX_ - } - - if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) - { - return ::realloc(_ptr, _size); - } - -# if BX_COMPILER_MSVC - BX_UNUSED(_file, _line); - return _aligned_realloc(_ptr, _size, _align); -# else - return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line); -# endif // BX_ - } - #if BX_CONFIG_CRT_FILE_READER_WRITER # if BX_CRT_MSVC @@ -423,150 +351,49 @@ namespace bx return &s_stdOut; } -#if BX_CONFIG_CRT_PROCESS - -#if BX_CRT_MSVC -# define popen _popen -# define pclose _pclose -#endif // BX_CRT_MSVC - - ProcessReader::ProcessReader() - : m_file(NULL) + bool stat(const char* _filePath, FileInfo& _fileInfo) { - } + _fileInfo.m_size = 0; + _fileInfo.m_type = FileInfo::Count; - ProcessReader::~ProcessReader() - { - BX_CHECK(NULL == m_file, "Process not closed!"); - } +#if BX_COMPILER_MSVC + struct ::_stat64 st; + int32_t result = ::_stat64(_filePath, &st); - bool ProcessReader::open(const FilePath& _filePath, const StringView& _args, Error* _err) - { - BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); - - if (NULL != m_file) + if (0 != result) { - BX_ERROR_SET(_err, BX_ERROR_READERWRITER_ALREADY_OPEN, "ProcessReader: File is already open."); return false; } - 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) + if (0 != (st.st_mode & _S_IFREG) ) + { + _fileInfo.m_type = FileInfo::Regular; + } + else if (0 != (st.st_mode & _S_IFDIR) ) + { + _fileInfo.m_type = FileInfo::Directory; + } +#else + struct ::stat st; + int32_t result = ::stat(_filePath, &st); + if (0 != result) { - BX_ERROR_SET(_err, BX_ERROR_READERWRITER_OPEN, "ProcessReader: Failed to open process."); return false; } + if (0 != (st.st_mode & S_IFREG) ) + { + _fileInfo.m_type = FileInfo::Regular; + } + else if (0 != (st.st_mode & S_IFDIR) ) + { + _fileInfo.m_type = FileInfo::Directory; + } +#endif // BX_COMPILER_MSVC + + _fileInfo.m_size = st.st_size; + return true; } - void ProcessReader::close() - { - BX_CHECK(NULL != m_file, "Process not open!"); - FILE* file = (FILE*)m_file; - m_exitCode = pclose(file); - m_file = NULL; - } - - int32_t ProcessReader::read(void* _data, int32_t _size, Error* _err) - { - BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); BX_UNUSED(_err); - - FILE* file = (FILE*)m_file; - int32_t size = (int32_t)fread(_data, 1, _size, file); - if (size != _size) - { - if (0 != feof(file) ) - { - BX_ERROR_SET(_err, BX_ERROR_READERWRITER_EOF, "ProcessReader: EOF."); - } - else if (0 != ferror(file) ) - { - BX_ERROR_SET(_err, BX_ERROR_READERWRITER_READ, "ProcessReader: read error."); - } - - return size >= 0 ? size : 0; - } - - return size; - } - - int32_t ProcessReader::getExitCode() const - { - return m_exitCode; - } - - ProcessWriter::ProcessWriter() - : m_file(NULL) - { - } - - ProcessWriter::~ProcessWriter() - { - BX_CHECK(NULL == m_file, "Process not closed!"); - } - - bool ProcessWriter::open(const FilePath& _filePath, const StringView& _args, Error* _err) - { - BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); - - if (NULL != m_file) - { - BX_ERROR_SET(_err, BX_ERROR_READERWRITER_ALREADY_OPEN, "ProcessWriter: File is already open."); - return false; - } - - 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."); - return false; - } - - return true; - } - - void ProcessWriter::close() - { - BX_CHECK(NULL != m_file, "Process not open!"); - FILE* file = (FILE*)m_file; - m_exitCode = pclose(file); - m_file = NULL; - } - - int32_t ProcessWriter::write(const void* _data, int32_t _size, Error* _err) - { - BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); BX_UNUSED(_err); - - FILE* file = (FILE*)m_file; - int32_t size = (int32_t)fwrite(_data, 1, _size, file); - if (size != _size) - { - if (0 != ferror(file) ) - { - BX_ERROR_SET(_err, BX_ERROR_READERWRITER_WRITE, "ProcessWriter: write error."); - } - - return size >= 0 ? size : 0; - } - - return size; - } - - int32_t ProcessWriter::getExitCode() const - { - return m_exitCode; - } -#endif // BX_CONFIG_CRT_PROCESS - } // namespace bx diff --git a/src/filepath.cpp b/src/filepath.cpp index c010e0b..4aaaa58 100644 --- a/src/filepath.cpp +++ b/src/filepath.cpp @@ -3,7 +3,8 @@ * License: https://github.com/bkaradzic/bx#license-bsd-2-clause */ -#include +#include +#include #include namespace bx @@ -128,11 +129,62 @@ namespace bx return size; } + static bool getTempPath(char* _out, uint32_t* _inOutSize) + { +#if BX_PLATFORM_WINDOWS + uint32_t len = ::GetTempPathA(*_inOutSize, _out); + bool result = len != 0 && len < *_inOutSize; + *_inOutSize = len; + return result; +#else + static const char* s_tmp[] = + { + "TMPDIR", + "TMP", + "TEMP", + "TEMPDIR", + + NULL + }; + + for (const char** tmp = s_tmp; *tmp != NULL; ++tmp) + { + uint32_t len = *_inOutSize; + *_out = '\0'; + bool result = getenv(*tmp, _out, &len); + + if (result + && len != 0 + && len < *_inOutSize) + { + *_inOutSize = len; + return result; + } + } + + FileInfo fi; + if (stat("/tmp", fi) + && FileInfo::Directory == fi.m_type) + { + strCopy(_out, *_inOutSize, "/tmp"); + *_inOutSize = 4; + return true; + } + + return false; +#endif // BX_PLATFORM_* + } + FilePath::FilePath() { set(""); } + FilePath::FilePath(TempDir::Enum) + { + set(TempDir::Tag); + } + FilePath::FilePath(const char* _rhs) { set(_rhs); @@ -149,6 +201,14 @@ namespace bx return *this; } + void FilePath::set(TempDir::Enum) + { + char tmp[kMaxFilePath]; + uint32_t len = BX_COUNTOF(tmp); + getTempPath(tmp, &len); + set(StringView(tmp, len) ); + } + void FilePath::set(const StringView& _filePath) { normalizeFilePath( diff --git a/src/fpumath.cpp b/src/math.cpp similarity index 99% rename from src/fpumath.cpp rename to src/math.cpp index 2f52669..ff5a79a 100644 --- a/src/fpumath.cpp +++ b/src/math.cpp @@ -3,7 +3,7 @@ * License: https://github.com/bkaradzic/bx#license-bsd-2-clause */ -#include +#include #include namespace bx diff --git a/src/os.cpp b/src/os.cpp index 525c499..ac99f10 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -3,14 +3,13 @@ * License: https://github.com/bkaradzic/bx#license-bsd-2-clause */ +#include #include #include -#include #if !BX_PLATFORM_NONE #include -#include #if BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT # include @@ -298,97 +297,6 @@ namespace bx #endif // BX_COMPILER_ } - bool getTempPath(char* _out, uint32_t* _inOutSize) - { -#if BX_PLATFORM_WINDOWS - uint32_t len = ::GetTempPathA(*_inOutSize, _out); - bool result = len != 0 && len < *_inOutSize; - *_inOutSize = len; - return result; -#else - static const char* s_tmp[] = - { - "TMPDIR", - "TMP", - "TEMP", - "TEMPDIR", - - NULL - }; - - for (const char** tmp = s_tmp; *tmp != NULL; ++tmp) - { - uint32_t len = *_inOutSize; - *_out = '\0'; - bool result = getenv(*tmp, _out, &len); - - if (result - && len != 0 - && len < *_inOutSize) - { - *_inOutSize = len; - return result; - } - } - - FileInfo fi; - if (stat("/tmp", fi) - && FileInfo::Directory == fi.m_type) - { - strCopy(_out, *_inOutSize, "/tmp"); - *_inOutSize = 4; - return true; - } - - return false; -#endif // BX_PLATFORM_* - } - - bool stat(const char* _filePath, FileInfo& _fileInfo) - { - _fileInfo.m_size = 0; - _fileInfo.m_type = FileInfo::Count; - -#if BX_COMPILER_MSVC - struct ::_stat64 st; - int32_t result = ::_stat64(_filePath, &st); - - if (0 != result) - { - return false; - } - - if (0 != (st.st_mode & _S_IFREG) ) - { - _fileInfo.m_type = FileInfo::Regular; - } - else if (0 != (st.st_mode & _S_IFDIR) ) - { - _fileInfo.m_type = FileInfo::Directory; - } -#else - struct ::stat st; - int32_t result = ::stat(_filePath, &st); - if (0 != result) - { - return false; - } - - if (0 != (st.st_mode & S_IFREG) ) - { - _fileInfo.m_type = FileInfo::Regular; - } - else if (0 != (st.st_mode & S_IFDIR) ) - { - _fileInfo.m_type = FileInfo::Directory; - } -#endif // BX_COMPILER_MSVC - - _fileInfo.m_size = st.st_size; - - return true; - } - void* exec(const char* const* _argv) { #if BX_PLATFORM_LINUX || BX_PLATFORM_HURD diff --git a/src/process.cpp b/src/process.cpp new file mode 100644 index 0000000..4b28b19 --- /dev/null +++ b/src/process.cpp @@ -0,0 +1,168 @@ +/* + * Copyright 2010-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#include + +#include + +#ifndef BX_CONFIG_CRT_PROCESS +# define BX_CONFIG_CRT_PROCESS !(0 \ + || BX_CRT_NONE \ + || BX_PLATFORM_EMSCRIPTEN \ + || BX_PLATFORM_PS4 \ + || BX_PLATFORM_WINRT \ + || BX_PLATFORM_XBOXONE \ + ) +#endif // BX_CONFIG_CRT_PROCESS + +namespace bx +{ +#if BX_CONFIG_CRT_PROCESS + +#if BX_CRT_MSVC +# define popen _popen +# define pclose _pclose +#endif // BX_CRT_MSVC + + ProcessReader::ProcessReader() + : m_file(NULL) + { + } + + ProcessReader::~ProcessReader() + { + BX_CHECK(NULL == m_file, "Process not closed!"); + } + + bool ProcessReader::open(const FilePath& _filePath, const StringView& _args, Error* _err) + { + BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); + + if (NULL != m_file) + { + BX_ERROR_SET(_err, BX_ERROR_READERWRITER_ALREADY_OPEN, "ProcessReader: File is already open."); + return false; + } + + 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."); + return false; + } + + return true; + } + + void ProcessReader::close() + { + BX_CHECK(NULL != m_file, "Process not open!"); + FILE* file = (FILE*)m_file; + m_exitCode = pclose(file); + m_file = NULL; + } + + int32_t ProcessReader::read(void* _data, int32_t _size, Error* _err) + { + BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); BX_UNUSED(_err); + + FILE* file = (FILE*)m_file; + int32_t size = (int32_t)fread(_data, 1, _size, file); + if (size != _size) + { + if (0 != feof(file) ) + { + BX_ERROR_SET(_err, BX_ERROR_READERWRITER_EOF, "ProcessReader: EOF."); + } + else if (0 != ferror(file) ) + { + BX_ERROR_SET(_err, BX_ERROR_READERWRITER_READ, "ProcessReader: read error."); + } + + return size >= 0 ? size : 0; + } + + return size; + } + + int32_t ProcessReader::getExitCode() const + { + return m_exitCode; + } + + ProcessWriter::ProcessWriter() + : m_file(NULL) + { + } + + ProcessWriter::~ProcessWriter() + { + BX_CHECK(NULL == m_file, "Process not closed!"); + } + + bool ProcessWriter::open(const FilePath& _filePath, const StringView& _args, Error* _err) + { + BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); + + if (NULL != m_file) + { + BX_ERROR_SET(_err, BX_ERROR_READERWRITER_ALREADY_OPEN, "ProcessWriter: File is already open."); + return false; + } + + 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."); + return false; + } + + return true; + } + + void ProcessWriter::close() + { + BX_CHECK(NULL != m_file, "Process not open!"); + FILE* file = (FILE*)m_file; + m_exitCode = pclose(file); + m_file = NULL; + } + + int32_t ProcessWriter::write(const void* _data, int32_t _size, Error* _err) + { + BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors."); BX_UNUSED(_err); + + FILE* file = (FILE*)m_file; + int32_t size = (int32_t)fwrite(_data, 1, _size, file); + if (size != _size) + { + if (0 != ferror(file) ) + { + BX_ERROR_SET(_err, BX_ERROR_READERWRITER_WRITE, "ProcessWriter: write error."); + } + + return size >= 0 ? size : 0; + } + + return size; + } + + int32_t ProcessWriter::getExitCode() const + { + return m_exitCode; + } +#endif // BX_CONFIG_CRT_PROCESS + +} // namespace bx diff --git a/tests/filepath_test.cpp b/tests/filepath_test.cpp index 70b32bd..7be97f7 100644 --- a/tests/filepath_test.cpp +++ b/tests/filepath_test.cpp @@ -116,3 +116,9 @@ TEST_CASE("FilePath", "") REQUIRE(test.absolute == fp.isAbsolute() ); }; } + +TEST_CASE("FilePath temp", "") +{ + bx::FilePath tmp(bx::TempDir::Tag); + REQUIRE(0 != bx::strCmp(".", tmp.getPath() ) ); +} diff --git a/tests/fpumath_test.cpp b/tests/fpumath_test.cpp index b2084d2..2005a67 100644 --- a/tests/fpumath_test.cpp +++ b/tests/fpumath_test.cpp @@ -4,7 +4,7 @@ */ #include "test.h" -#include +#include #if !BX_COMPILER_MSVC || BX_COMPILER_MSVC >= 1800 #include diff --git a/tests/os_test.cpp b/tests/os_test.cpp index 6d63d67..ed60e3e 100644 --- a/tests/os_test.cpp +++ b/tests/os_test.cpp @@ -14,13 +14,6 @@ TEST_CASE("getProcessMemoryUsed", "") // DBG("bx::getProcessMemoryUsed %d", bx::getProcessMemoryUsed() ); } -TEST_CASE("getTempPath", "") -{ - char tmpDir[512]; - uint32_t len = BX_COUNTOF(tmpDir); - REQUIRE(bx::getTempPath(tmpDir, &len) ); -} - #if !BX_PLATFORM_OSX TEST_CASE("semaphore_timeout", "") { diff --git a/tests/simd_test.cpp b/tests/simd_test.cpp index 315f53d..97ae9bf 100644 --- a/tests/simd_test.cpp +++ b/tests/simd_test.cpp @@ -5,7 +5,7 @@ #include "test.h" #include -#include +#include #include #if 0 diff --git a/tests/string_test.cpp b/tests/string_test.cpp index e09656c..92d46f4 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -5,7 +5,6 @@ #include "test.h" #include -#include #include bx::AllocatorI* g_allocator;