Skip to content

Commit

Permalink
flags
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jul 4, 2024
1 parent d024ecb commit a287541
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
32 changes: 18 additions & 14 deletions include/cinatra/ylt/coro_io/coro_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,26 @@ enum class read_mode { seq, random };
enum class async_mode { native_async, thread_pool };

constexpr flags to_flags(std::ios::ios_base::openmode mode) {
int access = flags::read_write;
flags access = flags::read_write;

if ((mode & (std::ios::app)) != 0)
if (mode == std::ios::in)
access = flags::read_only;
else if (mode == std::ios::out)
access = flags::write_only;
else if (mode == std::ios::app)
access = flags::append;

if ((mode & (std::ios::trunc)) != 0)
access |= flags::truncate;

if ((mode & (std::ios::in | std::ios::out)) != 0)
access |= read_write;
else if ((mode & std::ios::out) != 0)
access |= flags::create_write;
else if ((mode & std::ios::in) != 0)
access |= flags::read_only;

return (flags)access;
else if (mode == std::ios::trunc)
access = flags::truncate;
else if (mode == (std::ios::in | std::ios::out))
access = flags::read_write;
else if (mode == (std::ios::trunc | std::ios::out))
access = flags::create_write_trunc;
if (mode == (std::ios::in | std::ios::out | std::ios::trunc))
access = create_read_write_trunc;
else if (mode == (std::ios::in | std::ios::out | std::ios::app))
access = create_read_write_append;

return access;
}

#if defined(ENABLE_FILE_IO_URING) || defined(ASIO_WINDOWS)
Expand Down
9 changes: 4 additions & 5 deletions tests/test_corofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ void create_files(const std::vector<std::string> &files, size_t file_size) {
template <coro_io::execution_type execute_type>
void test_random_read_write(std::string_view filename) {
create_files({std::string(filename)}, 190);
coro_io::basic_random_coro_file<execute_type> file(
filename, std::ios::binary | std::ios::in | std::ios::out);
coro_io::basic_random_coro_file<execute_type> file(filename, std::ios::in);
CHECK(file.is_open());
#if defined(ENABLE_FILE_IO_URING) || defined(ASIO_WINDOWS)
if (execute_type == coro_io::execution_type::native_async) {
Expand Down Expand Up @@ -103,7 +102,7 @@ void test_random_read_write(std::string_view filename) {
CHECK(pair.second == 0);

coro_io::basic_random_coro_file<execute_type> file1;
file1.open(filename, std::ios::binary | std::ios::in | std::ios::out);
file1.open(filename, std::ios::out);
CHECK(file1.is_open());
std::string buf1 = "cccccccccc";
async_simple::coro::syncAwait(file1.async_write_at(0, buf1));
Expand All @@ -115,8 +114,8 @@ void test_random_read_write(std::string_view filename) {
template <coro_io::execution_type execute_type>
void test_seq_read_write(std::string_view filename) {
create_files({std::string(filename)}, 190);
coro_io::basic_seq_coro_file<execute_type> file(
filename, std::ios::binary | std::ios::in | std::ios::out);
coro_io::basic_seq_coro_file<execute_type> file(filename,
std::ios::in | std::ios::out);
CHECK(file.is_open());
#if defined(ENABLE_FILE_IO_URING) || defined(ASIO_WINDOWS)
if (execute_type == coro_io::execution_type::native_async) {
Expand Down

0 comments on commit a287541

Please sign in to comment.