/*** Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: AuIOHandle.cpp Date: 2023-7-28 Author: Reece ***/ #include #include "AuIOHandle.hpp" namespace Aurora::IO { AFileHandle::~AFileHandle() { if (this->uOSWriteHandle.HasValue() && this->uOSReadHandle.HasValue() && this->uOSReadHandle.Value() == this->uOSWriteHandle.Value()) { this->CloseHandle(this->uOSReadHandle.value()); AuResetMember(this->uOSReadHandle); AuResetMember(this->uOSWriteHandle); } if (this->uOSReadHandle) { this->CloseHandle(this->uOSReadHandle.value()); AuResetMember(this->uOSReadHandle); } if (this->uOSWriteHandle) { this->CloseHandle(this->uOSWriteHandle.value()); AuResetMember(this->uOSWriteHandle); } } bool AFileHandle::InitFromHandle(AuSPtr pHandle) { auto pSrc = AuStaticCast(pHandle); auto pDest = this; if (this->IsValid()) { return false; } pDest->pThat = pHandle; pDest->uOSReadHandle = pSrc->uOSReadHandle; pDest->uOSWriteHandle = pSrc->uOSWriteHandle; pDest->bIsAsync = pSrc->bIsAsync; return true; } bool AFileHandle::InitFromCopy(AuUInt64 uOSHandle) { if (this->IsValid()) { return false; } if (auto uOSWriteHandle = this->DupHandle(uOSHandle, true)) { this->uOSReadHandle = uOSWriteHandle; this->uOSWriteHandle = uOSWriteHandle; return true; } else { return {}; } } bool AFileHandle::InitFromMove(AuUInt64 uOSHandle) { if (this->IsValid()) { return false; } this->uOSReadHandle = uOSHandle; this->uOSWriteHandle = uOSHandle; return true; } bool AFileHandle::InitFromPair(AuUInt64 uOSReadHandle, AuUInt64 uOSWriteHandle) { if (this->IsValid()) { return false; } if (auto uOSReadHandle2 = this->DupHandle(uOSReadHandle, false)) { this->uOSReadHandle = uOSReadHandle2; } else { return {}; } if (auto uOSWriteHandle2 = this->DupHandle(uOSWriteHandle, true)) { this->uOSWriteHandle = uOSWriteHandle2; } else { this->CloseHandle(this->uOSReadHandle); AuResetMember(this->uOSReadHandle); return {}; } return true; } bool AFileHandle::InitFromPairMove(AuOptionalEx uOSReadHandle, AuOptionalEx uOSWriteHandle) { if (this->IsValid()) { return false; } this->uOSReadHandle = uOSReadHandle; this->uOSWriteHandle = uOSWriteHandle; return true; } bool AFileHandle::InitFromStreamEnum(EStandardStream eStream) { switch (eStream) { case EStandardStream::eInputStream: this->InitStdIn(); return this->IsValid(); case EStandardStream::eErrorStream: case EStandardStream::eOutputStream: this->InitStdOut(eStream == EStandardStream::eErrorStream); return this->IsValid(); default: SysPushErrorArg(); return false; } } bool AFileHandle::IsFile() { bool bIsFile {}; if (auto optFile = this->optIsFile) { return optFile.value(); } if (auto optHandle = this->GetOSHandleSafe()) { bIsFile = IsHandleFile(optHandle.value()); } else { SysPushErrorUninitialized(); return false; } this->optIsFile = bIsFile; return bIsFile; } bool AFileHandle::IsTTY() { bool bIsTTY {}; if (auto optTTY = this->optIsTTY) { return optTTY.value(); } if (auto optHandle = this->GetOSHandleSafe()) { bIsTTY = IsHandleTTY(optHandle.value()); } else { SysPushErrorUninitialized(); return false; } this->optIsTTY = bIsTTY; return bIsTTY; } bool AFileHandle::IsPipe() { bool bIsPipe {}; if (auto optPipe = this->optIsPipe) { return optPipe.value(); } if (auto optHandle = this->GetOSHandleSafe()) { bIsPipe = IsHandlePipe(optHandle.value()); } else { SysPushErrorUninitialized(); return false; } this->optIsPipe = bIsPipe; return bIsPipe; } AuOptionalEx AFileHandle::GetOSHandleSafe() { if (auto write = this->uOSWriteHandle) { return write; } if (auto read = this->uOSReadHandle) { return read; } return {}; } AuUInt64 AFileHandle::GetOSHandle() { return this->uOSReadHandle.ValueOr(this->uOSWriteHandle.Value()); } AuUInt64 AFileHandle::GetOSReadHandle() { return this->uOSReadHandle.value(); } AuOptionalEx AFileHandle::GetOSReadHandleSafe() { return this->uOSReadHandle; } AuUInt64 AFileHandle::GetOSWriteHandle() { return this->uOSWriteHandle.Value(); } AuOptionalEx AFileHandle::GetOSWriteHandleSafe() { return this->uOSWriteHandle; } bool AFileHandle::IsValid() { return this->uOSReadHandle.HasValue() || this->uOSWriteHandle.HasValue(); } bool AFileHandle::HasUniqueWriteHandle() { return this->uOSWriteHandle.HasValue(); } bool AFileHandle::IsAsync() { return this->bIsAsync; } AuString AFileHandle::GetPath() { return this->path; } }