Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OrcLib: add enum class FileDisposition
- Loading branch information
1 parent
a324640
commit f925e31
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
// | ||
// Copyright © 2021 ANSSI. All Rights Reserved. | ||
// | ||
// Author(s): fabienfl | ||
// | ||
|
||
#include "FileDisposition.h" | ||
|
||
#include <string> | ||
|
||
namespace Orc { | ||
|
||
constexpr std::wstring_view kAppend = L"append"; | ||
constexpr std::wstring_view kTruncate = L"truncate"; | ||
constexpr std::wstring_view kCreateNew = L"create_new"; | ||
constexpr std::wstring_view kUnknown = L"unknown"; | ||
|
||
std::wstring_view ToString(FileDisposition disposition) | ||
{ | ||
switch (disposition) | ||
{ | ||
case FileDisposition::Append: | ||
return kAppend; | ||
case FileDisposition::Truncate: | ||
return kTruncate; | ||
case FileDisposition::CreateNew: | ||
return kCreateNew; | ||
default: | ||
return kUnknown; | ||
} | ||
} | ||
|
||
FileDisposition ToFileDisposition(const std::wstring& disposition, std::error_code& ec) | ||
{ | ||
const std::map<std::wstring_view, FileDisposition> map = { | ||
{kAppend, FileDisposition::Append}, | ||
{kTruncate, FileDisposition::Truncate}, | ||
{kCreateNew, FileDisposition::CreateNew}}; | ||
|
||
auto it = map.find(disposition); | ||
if (it == std::cend(map)) | ||
{ | ||
ec = std::make_error_code(std::errc::invalid_argument); | ||
return FileDisposition::Unknown; | ||
} | ||
|
||
return it->second; | ||
} | ||
|
||
std::ios_base::openmode ToOpenMode(FileDisposition disposition) | ||
{ | ||
switch (disposition) | ||
{ | ||
case FileDisposition::Append: | ||
return std::ios::app; | ||
case FileDisposition::CreateNew: | ||
case FileDisposition::Truncate: | ||
return std::ios_base::trunc; | ||
default: | ||
assert(nullptr); | ||
return std::ios_base::ate; | ||
} | ||
} | ||
|
||
} // namespace Orc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
// | ||
// Copyright © 2021 ANSSI. All Rights Reserved. | ||
// | ||
// Author(s): fabienfl | ||
// | ||
|
||
#pragma once | ||
|
||
#include <string_view> | ||
#include <system_error> | ||
#include <ios> | ||
|
||
namespace Orc { | ||
|
||
enum class FileDisposition | ||
{ | ||
Unknown, | ||
Append, | ||
Truncate, | ||
CreateNew | ||
}; | ||
|
||
std::wstring_view ToString(FileDisposition disposition); | ||
FileDisposition ToFileDisposition(const std::wstring& disposition, std::error_code& ec = std::error_code()); | ||
|
||
std::ios_base::openmode ToOpenMode(FileDisposition disposition); | ||
|
||
} // namespace Orc |