diff options
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/Buffer.cpp | 4 | ||||
| -rw-r--r-- | src/base/Exception.cpp | 19 | ||||
| -rw-r--r-- | src/base/Format.cpp | 8 | ||||
| -rw-r--r-- | src/base/PropertyTree.cpp | 2 | ||||
| -rw-r--r-- | src/base/String.cpp | 10 | ||||
| -rw-r--r-- | src/base/StringToNumberConverter.cpp | 10 | ||||
| -rw-r--r-- | src/base/StringUtil.cpp | 20 | ||||
| -rw-r--r-- | src/base/SubProcess.cpp | 38 | ||||
| -rw-r--r-- | src/base/io/BufferStream.cpp | 2 | ||||
| -rw-r--r-- | src/base/io/CFileStream.cpp | 12 | ||||
| -rw-r--r-- | src/base/io/MemoryStream.cpp | 4 | ||||
| -rw-r--r-- | src/base/io/Resource.cpp | 2 | ||||
| -rw-r--r-- | src/base/io/Stream.cpp | 34 | ||||
| -rw-r--r-- | src/base/platform/unix/PosixSpawnSubProcess.cpp | 34 | ||||
| -rw-r--r-- | src/base/platform/unix/UnixFileStream.cpp | 8 | ||||
| -rw-r--r-- | src/base/platform/win/Win32FileStream.cpp | 4 |
16 files changed, 94 insertions, 117 deletions
diff --git a/src/base/Buffer.cpp b/src/base/Buffer.cpp index 1213364a..386e46cc 100644 --- a/src/base/Buffer.cpp +++ b/src/base/Buffer.cpp @@ -7,7 +7,7 @@ namespace cru { namespace { void CheckSize(Index size) { if (size < 0) { - throw Exception(u"Size of buffer can't be smaller than 0."); + throw Exception("Size of buffer can't be smaller than 0."); } } } // namespace @@ -144,7 +144,7 @@ Index Buffer::PushBack(const std::byte* other, Index other_size, void Buffer::PushBackCount(Index count) { if (count < 0 || count > GetBackFree()) { - throw Exception(u"Count out of range in PushBackCount."); + throw Exception("Count out of range in PushBackCount."); } used_end_ += count; } diff --git a/src/base/Exception.cpp b/src/base/Exception.cpp index 1f03c2ba..5bfe27db 100644 --- a/src/base/Exception.cpp +++ b/src/base/Exception.cpp @@ -10,9 +10,6 @@ namespace cru { Exception::Exception(std::string message, std::shared_ptr<std::exception> inner) : message_(std::move(message)), inner_(std::move(inner)) {} -Exception::Exception(StringView message, std::shared_ptr<std::exception> inner) - : message_(message.ToUtf8()), inner_(std::move(inner)) {} - Exception::~Exception() {} const char* Exception::what() const noexcept { return message_.c_str(); } @@ -31,16 +28,6 @@ void Exception::AppendMessage( if (additional_message) AppendMessage(*additional_message); } -void Exception::SetMessage(StringView message) { SetMessage(message.ToUtf8()); } - -void Exception::AppendMessage(StringView additional_message) { - AppendMessage(additional_message.ToUtf8()); -} - -void Exception::AppendMessage(std::optional<StringView> additional_message) { - if (additional_message) AppendMessage(additional_message->ToUtf8()); -} - ErrnoException::ErrnoException() : ErrnoException(NO_MESSAGE) {} ErrnoException::ErrnoException(int errno_code) @@ -52,10 +39,4 @@ ErrnoException::ErrnoException(std::string_view message) ErrnoException::ErrnoException(std::string_view message, int errno_code) : Exception(std::format("{} Errno is {}.", message, errno_code)), errno_code_(errno_code) {} - -ErrnoException::ErrnoException(StringView message) - : ErrnoException(message.ToUtf8()) {} - -ErrnoException::ErrnoException(StringView message, int errno_code) - : ErrnoException(message.ToUtf8(), errno_code) {} } // namespace cru diff --git a/src/base/Format.cpp b/src/base/Format.cpp index cba4137f..e442e572 100644 --- a/src/base/Format.cpp +++ b/src/base/Format.cpp @@ -9,7 +9,7 @@ FormatToken ParsePlaceHolder(String place_holder_string) { if (place_holder_string.StartWith(u":")) { if (place_holder_string.Find(u':', 1) != -1) { - throw Exception(u"Two ':' inside placeholder."); + throw Exception("Two ':' inside placeholder."); } return FormatToken::NonePlaceHolder(place_holder_string.substr(1)); @@ -27,7 +27,7 @@ FormatToken ParsePlaceHolder(String place_holder_string) { if (index != place_holder_string.size()) { if (place_holder_string[index] != ':') { - throw Exception(u"Invalid placeholder in format."); + throw Exception("Invalid placeholder in format."); } option = place_holder_string.substr(index + 1); @@ -68,7 +68,7 @@ std::vector<FormatToken> ParseToFormatTokenList(StringView str) { is_in_place_holder = false; } else { if (is_in_place_holder) { - throw Exception(u"Invalid format string: '{' inside placeholder."); + throw Exception("Invalid format string: '{' inside placeholder."); } try_to_escape = true; @@ -101,7 +101,7 @@ void FormatAppendFromFormatTokenList( for (Index i = index; i < static_cast<Index>(format_token_list.size()); i++) { const auto& token = format_token_list[i]; if (token.type == FormatTokenType::PlaceHolder) { - throw Exception(u"More placeholder than args."); + throw Exception("More placeholder than args."); } else { current += token.data; } diff --git a/src/base/PropertyTree.cpp b/src/base/PropertyTree.cpp index db17f84f..ea47dabc 100644 --- a/src/base/PropertyTree.cpp +++ b/src/base/PropertyTree.cpp @@ -50,7 +50,7 @@ PropertyTree::PropertyTree(std::unordered_map<std::string, std::string> values) std::string PropertyTree::GetValue(const std::string& key) const { auto it = values_.find(key); if (it == values_.end()) { - throw Exception(u"Property tree has no value."); + throw Exception("Property tree has no value."); } return it->second; } diff --git a/src/base/String.cpp b/src/base/String.cpp index 29cebde3..c90b9a71 100644 --- a/src/base/String.cpp +++ b/src/base/String.cpp @@ -324,7 +324,7 @@ void String::AppendCodePoint(CodePoint code_point) { if (!Utf16EncodeCodePointAppend( code_point, std::bind(&String::push_back, this, std::placeholders::_1))) { - throw TextEncodeException(u"Code point out of range."); + throw TextEncodeException("Code point out of range."); } } @@ -623,7 +623,7 @@ float StringView::ParseToFloat(Index* processed_characters_count, } if (flags & StringToNumberFlags::kThrowOnError && std::isnan(result)) { - throw Exception(u"Result of string to float conversion is NaN"); + throw Exception("Result of string to float conversion is NaN"); } return result; @@ -639,7 +639,7 @@ double StringView::ParseToDouble(Index* processed_characters_count, } if (flags & StringToNumberFlags::kThrowOnError && std::isnan(result)) { - throw Exception(u"Result of string to double conversion is NaN"); + throw Exception("Result of string to double conversion is NaN"); } return result; @@ -651,7 +651,7 @@ std::vector<float> StringView::ParseToFloatList(value_type separator) const { for (auto& item : list) { auto value = item.ParseToFloat(); if (std::isnan(value)) { - throw Exception(u"Invalid double value."); + throw Exception("Invalid double value."); } result.push_back(value); } @@ -664,7 +664,7 @@ std::vector<double> StringView::ParseToDoubleList(value_type separator) const { for (auto& item : list) { auto value = item.ParseToDouble(); if (std::isnan(value)) { - throw Exception(u"Invalid double value."); + throw Exception("Invalid double value."); } result.push_back(value); } diff --git a/src/base/StringToNumberConverter.cpp b/src/base/StringToNumberConverter.cpp index 65aec48e..f7516630 100644 --- a/src/base/StringToNumberConverter.cpp +++ b/src/base/StringToNumberConverter.cpp @@ -34,7 +34,7 @@ StringToIntegerResult StringToIntegerConverter::Parse( *processed_characters_count = 0; } if (throw_on_error) { - throw Exception(u"Empty string (after reading leading spaces)."); + throw Exception("Empty string (after reading leading spaces)."); } else { return {false, 0}; } @@ -54,7 +54,7 @@ StringToIntegerResult StringToIntegerConverter::Parse( *processed_characters_count = 0; } if (throw_on_error) { - throw Exception(u"Empty string (after reading sign)."); + throw Exception("Empty string (after reading sign)."); } else { return {false, 0}; } @@ -89,7 +89,7 @@ StringToIntegerResult StringToIntegerConverter::Parse( *processed_characters_count = 0; } if (throw_on_error) { - throw Exception(u"Empty string (after reading head base indicator)."); + throw Exception("Empty string (after reading head base indicator)."); } else { return {false, 0}; } @@ -136,7 +136,7 @@ StringToIntegerResult StringToIntegerConverter::Parse( *processed_characters_count = 0; } if (throw_on_error) { - throw Exception(String(u"Read invalid character '") + c + u"'."); + throw Exception(std::string("Read invalid character '") + c + "'."); } else { return {false, 0}; } @@ -153,7 +153,7 @@ StringToIntegerResult StringToIntegerConverter::Parse( *processed_characters_count = 0; } if (throw_on_error) { - throw Exception(u"There is trailing junk."); + throw Exception("There is trailing junk."); } else { return {false, 0}; } diff --git a/src/base/StringUtil.cpp b/src/base/StringUtil.cpp index 9053f384..6299acc2 100644 --- a/src/base/StringUtil.cpp +++ b/src/base/StringUtil.cpp @@ -17,13 +17,13 @@ CodePoint Utf8NextCodePoint(const char* ptr, Index size, Index current, auto read_next_folowing_code = [ptr, size, ¤t]() -> CodePoint { if (current == size) throw TextEncodeException( - u"Unexpected end when read continuing byte of multi-byte code " + "Unexpected end when read continuing byte of multi-byte code " "point."); const auto u = static_cast<std::uint8_t>(ptr[current]); if (!(u & (1u << 7)) || (u & (1u << 6))) { throw TextEncodeException( - u"Unexpected bad-format (not 0b10xxxxxx) continuing byte of " + "Unexpected bad-format (not 0b10xxxxxx) continuing byte of " "multi-byte code point."); } @@ -36,7 +36,7 @@ CodePoint Utf8NextCodePoint(const char* ptr, Index size, Index current, if ((1u << 4) & cu0) { // 4-length code point if (cu0 & (1u << 3)) { throw TextEncodeException( - u"Unexpected bad-format begin byte (not 0b11110xxx) of 4-byte" + "Unexpected bad-format begin byte (not 0b11110xxx) of 4-byte" "code point."); } @@ -61,7 +61,7 @@ CodePoint Utf8NextCodePoint(const char* ptr, Index size, Index current, } } else { throw TextEncodeException( - u"Unexpected bad-format (0b10xxxxxx) begin byte of a code point."); + "Unexpected bad-format (0b10xxxxxx) begin byte of a code point."); } } else { result = static_cast<CodePoint>(cu0); @@ -86,13 +86,13 @@ CodePoint Utf16NextCodePoint(const char16_t* ptr, Index size, Index current, } else if (IsUtf16SurrogatePairLeading(cu0)) { // 2-length code point if (current >= size) { throw TextEncodeException( - u"Unexpected end when reading second code unit of surrogate pair."); + "Unexpected end when reading second code unit of surrogate pair."); } const auto cu1 = ptr[current++]; if (!IsUtf16SurrogatePairTrailing(cu1)) { throw TextEncodeException( - u"Unexpected bad-range second code unit of surrogate pair."); + "Unexpected bad-range second code unit of surrogate pair."); } const auto s0 = ExtractBits<std::uint16_t, 10, CodePoint>(cu0) << 10; @@ -102,7 +102,7 @@ CodePoint Utf16NextCodePoint(const char16_t* ptr, Index size, Index current, } else { throw TextEncodeException( - u"Unexpected bad-range first code unit of surrogate pair."); + "Unexpected bad-range first code unit of surrogate pair."); } } @@ -125,13 +125,13 @@ CodePoint Utf16PreviousCodePoint(const char16_t* ptr, Index size, Index current, } else if (IsUtf16SurrogatePairTrailing(cu0)) { // 2-length code point if (current <= 0) { throw TextEncodeException( - u"Unexpected end when reading first code unit of surrogate pair."); + "Unexpected end when reading first code unit of surrogate pair."); } const auto cu1 = ptr[--current]; if (!IsUtf16SurrogatePairLeading(cu1)) { throw TextEncodeException( - u"Unexpected bad-range first code unit of surrogate pair."); + "Unexpected bad-range first code unit of surrogate pair."); } const auto s0 = ExtractBits<std::uint16_t, 10, CodePoint>(cu1) << 10; @@ -141,7 +141,7 @@ CodePoint Utf16PreviousCodePoint(const char16_t* ptr, Index size, Index current, } else { throw TextEncodeException( - u"Unexpected bad-range second code unit of surrogate pair."); + "Unexpected bad-range second code unit of surrogate pair."); } } diff --git a/src/base/SubProcess.cpp b/src/base/SubProcess.cpp index 9a6ee64f..964ae478 100644 --- a/src/base/SubProcess.cpp +++ b/src/base/SubProcess.cpp @@ -19,7 +19,7 @@ void PlatformSubProcess::Start() { std::lock_guard lock_guard(this->lock_); if (this->state_->status != SubProcessStatus::Prepare) { - throw SubProcessException(u"The process has already tried to start."); + throw SubProcessException("The process has already tried to start."); } try { @@ -36,8 +36,8 @@ void PlatformSubProcess::Start() { thread.detach(); } catch (const std::exception& e) { this->state_->status = SubProcessStatus::FailedToStart; - throw SubProcessFailedToStartException(u"Sub-process failed to start. " + - String::FromUtf8(e.what())); + throw SubProcessFailedToStartException( + std::string("Sub-process failed to start. ") + e.what()); } } @@ -46,13 +46,12 @@ void PlatformSubProcess::Wait( std::lock_guard lock_guard(this->lock_); if (this->state_->status == SubProcessStatus::Prepare) { - throw SubProcessException( - u"The process does not start. Can't wait for it."); + throw SubProcessException("The process does not start. Can't wait for it."); } if (this->state_->status == SubProcessStatus::FailedToStart) { throw SubProcessException( - u"The process failed to start. Can't wait for it."); + "The process failed to start. Can't wait for it."); } if (this->state_->status == SubProcessStatus::Exited) { @@ -75,11 +74,11 @@ void PlatformSubProcess::Kill() { std::lock_guard lock_guard(this->lock_); if (this->state_->status == SubProcessStatus::Prepare) { - throw SubProcessException(u"The process does not start. Can't kill it."); + throw SubProcessException("The process does not start. Can't kill it."); } if (this->state_->status == SubProcessStatus::FailedToStart) { - throw SubProcessException(u"The process failed to start. Can't kill it."); + throw SubProcessException("The process failed to start. Can't kill it."); } if (this->state_->status == SubProcessStatus::Exited) { @@ -104,17 +103,16 @@ SubProcessExitResult PlatformSubProcess::GetExitResult() { if (this->state_->status == SubProcessStatus::Prepare) { throw SubProcessException( - u"The process does not start. Can't get exit result."); + "The process does not start. Can't get exit result."); } if (this->state_->status == SubProcessStatus::FailedToStart) { throw SubProcessException( - u"The process failed to start. Can't get exit result."); + "The process failed to start. Can't get exit result."); } if (this->state_->status == SubProcessStatus::Running) { - throw SubProcessException( - u"The process is running. Can't get exit result."); + throw SubProcessException("The process is running. Can't get exit result."); } return this->state_->exit_result; @@ -132,8 +130,9 @@ io::Stream* PlatformSubProcess::GetStderrStream() { return this->state_->impl->GetStderrStream(); } -SubProcess SubProcess::Create(String program, std::vector<String> arguments, - std::unordered_map<String, String> environments) { +SubProcess SubProcess::Create( + std::string program, std::vector<std::string> arguments, + std::unordered_map<std::string, std::string> environments) { SubProcessStartInfo start_info; start_info.program = std::move(program); start_info.arguments = std::move(arguments); @@ -142,8 +141,8 @@ SubProcess SubProcess::Create(String program, std::vector<String> arguments, } SubProcessExitResult SubProcess::Call( - String program, std::vector<String> arguments, - std::unordered_map<String, String> environments) { + std::string program, std::vector<std::string> arguments, + std::unordered_map<std::string, std::string> environments) { auto process = Create(std::move(program), std::move(arguments), std::move(environments)); process.Wait(); @@ -151,11 +150,10 @@ SubProcessExitResult SubProcess::Call( } SubProcess::SubProcess(SubProcessStartInfo start_info) { - #ifdef CRU_PLATFORM_UNIX platform_process_.reset(new PlatformSubProcess( - std::move(start_info), - std::make_shared<platform::unix::PosixSpawnSubProcessImpl>())); + std::move(start_info), + std::make_shared<platform::unix::PosixSpawnSubProcessImpl>())); #else NotImplemented(); #endif @@ -203,7 +201,7 @@ void SubProcess::Detach() { auto p = platform_process_.release(); } void SubProcess::CheckValid() const { if (!IsValid()) { - throw SubProcessException(u"SubProcess instance is invalid."); + throw SubProcessException("SubProcess instance is invalid."); } } diff --git a/src/base/io/BufferStream.cpp b/src/base/io/BufferStream.cpp index 57a8b694..0dbb438b 100644 --- a/src/base/io/BufferStream.cpp +++ b/src/base/io/BufferStream.cpp @@ -57,7 +57,7 @@ Index BufferStream::DoWrite(const std::byte* buffer, Index offset, Index size) { if (eof_) { throw WriteAfterEofException( - u"Stream has been set eof. Can't write to it any more."); + "Stream has been set eof. Can't write to it any more."); } condition_variable_.wait(lock, [this] { diff --git a/src/base/io/CFileStream.cpp b/src/base/io/CFileStream.cpp index 45eb2eaf..f0d4790b 100644 --- a/src/base/io/CFileStream.cpp +++ b/src/base/io/CFileStream.cpp @@ -28,7 +28,7 @@ CFileStream::CFileStream(const char* path, const char* mode) file_(std::fopen(path, mode)), auto_close_(true) { if (file_ == nullptr) { - throw ErrnoException(u"Cannot open file."); + throw ErrnoException("Cannot open file."); } } @@ -36,7 +36,7 @@ CFileStream::CFileStream(std::FILE* file, bool readable, bool writable, bool auto_close) : Stream(true, readable, writable), file_(file), auto_close_(auto_close) { if (file_ == nullptr) { - throw Exception(u"File is NULL."); + throw Exception("File is NULL."); } } @@ -55,13 +55,13 @@ static int ConvertOriginFlag(Stream::SeekOrigin origin) { case Stream::SeekOrigin::End: return SEEK_END; default: - throw Exception(u"Unknown seek origin."); + throw Exception("Unknown seek origin."); } } Index CFileStream::DoSeek(Index offset, SeekOrigin origin) { if (std::fseek(file_, offset, ConvertOriginFlag(origin))) { - throw ErrnoException(u"Seek failed."); + throw ErrnoException("Seek failed."); } return DoTell(); } @@ -69,7 +69,7 @@ Index CFileStream::DoSeek(Index offset, SeekOrigin origin) { Index CFileStream::DoTell() { long position = std::ftell(file_); if (position == -1) { - throw ErrnoException(u"Tell failed."); + throw ErrnoException("Tell failed."); } return position; } @@ -91,7 +91,7 @@ void CFileStream::DoFlush() { std::fflush(file_); } void CFileStream::DoClose() { CRU_STREAM_BEGIN_CLOSE if (auto_close_ && !std::fclose(file_)) { - throw Exception(u"Failed to close FILE."); + throw Exception("Failed to close FILE."); } file_ = nullptr; } diff --git a/src/base/io/MemoryStream.cpp b/src/base/io/MemoryStream.cpp index 4c650f3e..bba0e618 100644 --- a/src/base/io/MemoryStream.cpp +++ b/src/base/io/MemoryStream.cpp @@ -14,10 +14,10 @@ MemoryStream::MemoryStream( position_(0), release_func_(std::move(release_func)) { if (!buffer) { - throw Exception(u"Buffer is nullptr"); + throw Exception("Buffer is nullptr"); } if (size <= 0) { - throw Exception(u"Size is 0 or negative."); + throw Exception("Size is 0 or negative."); } } diff --git a/src/base/io/Resource.cpp b/src/base/io/Resource.cpp index e599f8a9..48045e59 100644 --- a/src/base/io/Resource.cpp +++ b/src/base/io/Resource.cpp @@ -49,7 +49,7 @@ std::filesystem::path GetResourceDir() { } } - throw Exception(u"Failed to find resource directory."); + throw Exception("Failed to find resource directory."); #endif diff --git a/src/base/io/Stream.cpp b/src/base/io/Stream.cpp index 4cb58eca..1aafc839 100644 --- a/src/base/io/Stream.cpp +++ b/src/base/io/Stream.cpp @@ -6,28 +6,24 @@ namespace cru::io { StreamOperationNotSupportedException::StreamOperationNotSupportedException( - StringView operation) - : StreamOperationNotSupportedException(operation.ToUtf8()) {} - -StreamOperationNotSupportedException::StreamOperationNotSupportedException( std::string operation) : Exception(std::format("Stream operation {} not supported.", operation)), operation_(std::move(operation)) {} void StreamOperationNotSupportedException::CheckSeek(bool seekable) { - if (!seekable) throw StreamOperationNotSupportedException(u"seek"); + if (!seekable) throw StreamOperationNotSupportedException("seek"); } void StreamOperationNotSupportedException::CheckRead(bool readable) { - if (!readable) throw StreamOperationNotSupportedException(u"read"); + if (!readable) throw StreamOperationNotSupportedException("read"); } void StreamOperationNotSupportedException::CheckWrite(bool writable) { - if (!writable) throw StreamOperationNotSupportedException(u"write"); + if (!writable) throw StreamOperationNotSupportedException("write"); } StreamClosedException::StreamClosedException() - : Exception(u"Stream is already closed.") {} + : Exception("Stream is already closed.") {} void StreamClosedException::Check(bool closed) { if (closed) throw StreamClosedException(); @@ -122,8 +118,8 @@ bool Stream::DoCanSeek() { return *supported_operations_->can_seek; } else { throw Exception( - u"Can seek is neither set in supported_operations nor implemeted in " - u"virtual function."); + "Can seek is neither set in supported_operations nor implemeted in " + "virtual function."); } } @@ -132,8 +128,8 @@ bool Stream::DoCanRead() { return *supported_operations_->can_read; } else { throw Exception( - u"Can read is neither set in supported_operations nor implemeted in " - u"virtual function."); + "Can read is neither set in supported_operations nor implemeted in " + "virtual function."); } } @@ -142,13 +138,13 @@ bool Stream::DoCanWrite() { return *supported_operations_->can_write; } else { throw Exception( - u"Can write is neither set in supported_operations nor implemeted in " - u"virtual function."); + "Can write is neither set in supported_operations nor implemeted in " + "virtual function."); } } Index Stream::DoSeek(Index offset, SeekOrigin origin) { - throw Exception(u"Stream is seekable but DoSeek is not implemented."); + throw Exception("Stream is seekable but DoSeek is not implemented."); } Index Stream::DoTell() { @@ -171,11 +167,11 @@ Index Stream::DoGetSize() { } Index Stream::DoRead(std::byte* buffer, Index offset, Index size) { - throw Exception(u"Stream is readable but DoRead is not implemented."); + throw Exception("Stream is readable but DoRead is not implemented."); } Index Stream::DoWrite(const std::byte* buffer, Index offset, Index size) { - throw Exception(u"Stream is writable but DoWrite is not implemented."); + throw Exception("Stream is writable but DoWrite is not implemented."); } void Stream::DoFlush() {} @@ -195,8 +191,8 @@ Buffer Stream::ReadToEnd(Index grow_size) { return buffer; } -String Stream::ReadToEndAsUtf8String() { +std::string Stream::ReadToEndAsUtf8String() { auto buffer = ReadToEnd(); - return String::FromUtf8(buffer); + return std::string(buffer.GetUsedBeginPtr(), buffer.GetUsedEndPtr()); } } // namespace cru::io diff --git a/src/base/platform/unix/PosixSpawnSubProcess.cpp b/src/base/platform/unix/PosixSpawnSubProcess.cpp index f99d3224..7362175f 100644 --- a/src/base/platform/unix/PosixSpawnSubProcess.cpp +++ b/src/base/platform/unix/PosixSpawnSubProcess.cpp @@ -1,7 +1,6 @@ #include "cru/base/platform/unix/PosixSpawnSubProcess.h" #include "cru/base/Exception.h" #include "cru/base/Guard.h" -#include "cru/base/String.h" #include "cru/base/SubProcess.h" #include "cru/base/log/Logger.h" @@ -9,6 +8,7 @@ #include <spawn.h> #include <sys/wait.h> #include <unistd.h> +#include <cstring> #include <format> #include <memory> #include <string_view> @@ -20,23 +20,25 @@ PosixSpawnSubProcessImpl::PosixSpawnSubProcessImpl() : pid_(0), exit_code_(0) {} PosixSpawnSubProcessImpl::~PosixSpawnSubProcessImpl() {} namespace { -char** CreateCstrArray(const std::vector<String>& argv) { - std::vector<Buffer> utf8_argv; - for (const auto& arg : argv) { - utf8_argv.push_back(arg.ToUtf8Buffer()); +char** CreateCstrArray(const std::vector<std::string>& argv) { + auto argv_len = argv.size(); + char** result = new char*[argv_len + 1]; + for (int i = 0; i < argv.size(); i++) { + auto len = argv[i].size(); + char* str = new char[len + 1]; + result[i] = str; + std::memcpy(str, argv[i].data(), len); + str[len] = 0; } - char** result = new char*[utf8_argv.size() + 1]; - for (int i = 0; i < utf8_argv.size(); i++) { - result[i] = reinterpret_cast<char*>(utf8_argv[i].Detach()); - } - result[utf8_argv.size()] = nullptr; + result[argv_len] = nullptr; return result; } -char** CreateCstrArray(const std::unordered_map<String, String>& envp) { - std::vector<String> str_array; +char** CreateCstrArray( + const std::unordered_map<std::string, std::string>& envp) { + std::vector<std::string> str_array; for (auto& [key, value] : envp) { - str_array.push_back(key + u"=" + value); + str_array.push_back(key + "=" + value); } return CreateCstrArray(str_array); } @@ -112,8 +114,8 @@ void PosixSpawnSubProcessImpl::PlatformCreateProcess( "Failed to set flag POSIX_SPAWN_CLOEXEC_DEFAULT (osx)."); #endif - auto exe = start_info.program.ToUtf8(); - std::vector<String> arguments{start_info.program}; + auto exe = start_info.program; + std::vector<std::string> arguments{start_info.program}; arguments.insert(arguments.cend(), start_info.arguments.cbegin(), start_info.arguments.cend()); @@ -168,7 +170,7 @@ void PosixSpawnSubProcessImpl::PlatformKillProcess() { int error = kill(pid_, SIGKILL); if (error != 0) { std::unique_ptr<ErrnoException> inner(new ErrnoException(errno)); - throw SubProcessInternalException(u"Failed to call kill on a subprocess.", + throw SubProcessInternalException("Failed to call kill on a subprocess.", std::move(inner)); } } diff --git a/src/base/platform/unix/UnixFileStream.cpp b/src/base/platform/unix/UnixFileStream.cpp index 0772c279..43ff2244 100644 --- a/src/base/platform/unix/UnixFileStream.cpp +++ b/src/base/platform/unix/UnixFileStream.cpp @@ -33,7 +33,7 @@ int MapSeekOrigin(Stream::SeekOrigin origin) { case Stream::SeekOrigin::End: return SEEK_END; default: - throw Exception(u"Invalid seek origin."); + throw Exception("Invalid seek origin."); } } } // namespace @@ -58,7 +58,7 @@ UnixFileStream::~UnixFileStream() { DoClose(); } Index UnixFileStream::DoSeek(Index offset, SeekOrigin origin) { off_t result = ::lseek(file_descriptor_, offset, MapSeekOrigin(origin)); if (result == -1) { - throw ErrnoException(u"Failed to seek file."); + throw ErrnoException("Failed to seek file."); } return result; } @@ -66,7 +66,7 @@ Index UnixFileStream::DoSeek(Index offset, SeekOrigin origin) { Index UnixFileStream::DoRead(std::byte *buffer, Index offset, Index size) { auto result = ::read(file_descriptor_, buffer + offset, size); if (result == -1) { - throw ErrnoException(u"Failed to read file."); + throw ErrnoException("Failed to read file."); } return result; } @@ -75,7 +75,7 @@ Index UnixFileStream::DoWrite(const std::byte *buffer, Index offset, Index size) { auto result = ::write(file_descriptor_, buffer + offset, size); if (result == -1) { - throw ErrnoException(u"Failed to write file."); + throw ErrnoException("Failed to write file."); } return result; } diff --git a/src/base/platform/win/Win32FileStream.cpp b/src/base/platform/win/Win32FileStream.cpp index f3809b3e..341fe9d3 100644 --- a/src/base/platform/win/Win32FileStream.cpp +++ b/src/base/platform/win/Win32FileStream.cpp @@ -29,7 +29,7 @@ Win32FileStream::Win32FileStream(String path, OpenFileFlag flags) if (flags & io::OpenFileFlags::Write) { grfMode |= STGM_WRITE; } else { - throw Exception(u"Stream must be readable or writable."); + throw Exception("Stream must be readable or writable."); } } @@ -80,7 +80,7 @@ Index Win32FileStream::DoRead(std::byte* buffer, Index offset, Index size) { Index Win32FileStream::DoWrite(const std::byte* buffer, Index offset, Index size) { if (size < 0) { - throw Exception(u"Size must be greater than 0."); + throw Exception("Size must be greater than 0."); } CheckClosed(); |
