Skip to content

Commit

Permalink
ini : Fixes. pipe : Unix error reporting. (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
p-groarke authored Mar 23, 2024
1 parent 762381f commit 8ce0f03
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
7 changes: 6 additions & 1 deletion include_cpp14/fea/terminal/pipe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
**/
#pragma once
#include "fea/terminal/translation_mode.hpp"
#include "fea/utils/error.hpp"
#include "fea/utils/platform.hpp"
#include "fea/utils/scope.hpp"
#include "fea/utils/unused.hpp"
Expand Down Expand Up @@ -157,7 +158,11 @@ size_t available_pipe_bytes() {
} break;
}
#else
ioctl(fileno(stdin), FIONREAD, &ret);
int n = 0;
if (ioctl(fileno(stdin), FIONREAD, &n) != 0) {
fea::maybe_throw_on_errno(__FUNCTION__, __LINE__);
}
ret = size_t(n);
#endif

return ret;
Expand Down
2 changes: 2 additions & 0 deletions include_cpp20/fea/serialize/ini.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
/*
A simple ini parser.
TODO : Deal with const better : |= etc.
Entries are ordered by declaration order, not sorted alphabetically. Supports
output comments. Supports automatic comments. When parsing an ini file, comments
are dropped.
Expand Down
22 changes: 14 additions & 8 deletions include_cpp20/fea/serialize/ini_details.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,18 @@ inline std::string to_string(const variant_t& v) {

inline std::string to_string(const entry& e, bool var_help) {
constexpr std::string_view endline = "\n";
constexpr std::string_view comment_fmt = " ; {}\n";
static constexpr std::string_view comment_fmt = " ; {}\n";
constexpr std::string_view val_fmt = "{} = {}\n";

assert(!e.entry_name.empty());
assert(!std::holds_alternative<std::nullptr_t>(e.value));

std::string ret;
if (!e.comment.empty()) {
ret += std::format(comment_fmt, e.comment);
ret += "\n";
fea::for_each_line(e.comment, [&](std::string_view line) {
ret += std::format(comment_fmt, line);
});
}

if (var_help) {
Expand All @@ -294,12 +297,14 @@ inline std::string to_string(const entry& e, bool var_help) {

inline std::string to_string(const section& s, bool var_help) {
constexpr std::string_view endline = "\n";
constexpr std::string_view comment_fmt = "\n; {}";
static constexpr std::string_view comment_fmt = "\n; {}";
constexpr std::string_view s_fmt = "\n[{}]\n";

std::string ret;
if (!s.comment.empty()) {
ret += std::format(comment_fmt, s.comment);
fea::for_each_line(s.comment, [&](std::string_view line) {
ret += std::format(comment_fmt, line);
});
}

if (!s.section_name.empty()) {
Expand Down Expand Up @@ -822,10 +827,11 @@ struct section_ret {
double_lit_idx = i;
}
}
if (!(single_lit_idx || double_lit_idx) && is_space(c)) {
// if (single_lit_idx || double_lit_idx) {
// str.push_back(U' ');
// }
// If inside string or section name, don't clean spaces.
// Otherwise, remove spaces.
if (!(single_lit_idx || double_lit_idx
|| section_begin != line.npos)
&& is_space(c)) {
continue;
}

Expand Down
7 changes: 5 additions & 2 deletions tests_cpp20/serialize/ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ a_string = "potato"
[test!][test~]]]]test
shouldbemerged = true
[section with spaces]
)";

TEST(ini, basics) {
Expand All @@ -68,6 +70,7 @@ TEST(ini, basics) {
EXPECT_TRUE(test.contains("🤣.bla"));
EXPECT_TRUE(test.contains("bad_section"));
EXPECT_TRUE(test.contains("type_tests"));
EXPECT_TRUE(test.contains("section with spaces"));
EXPECT_FALSE(test.contains("potato"));

EXPECT_TRUE(test.contains("", "global_var"));
Expand Down Expand Up @@ -155,7 +158,7 @@ TEST(ini, basics) {

// Writing.
{
test["bla"]["bla"] = true, "bla.bla comment";
test["bla"]["bla"] = true, "bla.bla\ncomment";
bool boolval = test["bla"]["bla"];
EXPECT_EQ(boolval, true);

Expand Down Expand Up @@ -269,7 +272,7 @@ TEST(ini, basics) {
std::string got = fea::to_string(test);
EXPECT_NE(got.find("; bla comment"), got.npos);
EXPECT_NE(got.find("; fla comment"), got.npos);
EXPECT_NE(got.find("; bla.bla comment"), got.npos);
EXPECT_NE(got.find("; bla.bla\n ; comment"), got.npos);
EXPECT_NE(got.find("; bla.blee comment"), got.npos);
EXPECT_NE(got.find("; bla.flee comment"), got.npos);
EXPECT_NE(got.find("; fla.flou comment"), got.npos);
Expand Down
7 changes: 6 additions & 1 deletion tests_pipe/terminal/pipe.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <fea/terminal/pipe.hpp>
#include <chrono>
#include <fea/terminal/pipe.hpp>
#include <gtest/gtest.h>
#include <string>
#include <thread>

extern int test_num;

Expand All @@ -9,6 +11,9 @@ const std::string expected = "l1 🙂\nl2\n<>\né\n";
const std::wstring wexpected = L"l1 🙂\nl2\n<>\né\n";

TEST(pipe, basics) {
// Are we too quick?
std::this_thread::sleep_for(std::chrono::milliseconds(100));

switch (test_num) {
case 0: {
std::string str = fea::read_pipe_text();
Expand Down

0 comments on commit 8ce0f03

Please sign in to comment.