Skip to content

Commit

Permalink
directvt#400 WIP: Get rid of C arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
o-sdn-o committed Oct 27, 2023
1 parent 0a7d47b commit 2f71e9d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 43 deletions.
16 changes: 8 additions & 8 deletions src/netxs/desktopio/canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,17 +389,17 @@ namespace netxs
i == tint16::greenlt ? _vt16<greenlt> :
i == tint16::yellowlt ? _vt16<yellowlt> : 0;

static constexpr ui32 vtm16[] =
static constexpr auto vtm16 = std::to_array(
{
_vtm16<0>, _vtm16<1>, _vtm16<2>, _vtm16<3>, _vtm16<4>, _vtm16<5>, _vtm16<6>, _vtm16<7>,
_vtm16<8>, _vtm16<9>, _vtm16<10>, _vtm16<11>, _vtm16<12>, _vtm16<13>, _vtm16<14>, _vtm16<15>,
};
static constexpr ui32 vga16[] =
});
static constexpr auto vga16 = std::to_array(
{
_vt16<blackdk>, _vt16<bluedk>, _vt16<greendk>, _vt16<cyandk>, _vt16<reddk>, _vt16<magentadk>, _vt16<yellowdk>, _vt16<whitedk>,
_vt16<blacklt>, _vt16<bluelt>, _vt16<greenlt>, _vt16<cyanlt>, _vt16<redlt>, _vt16<magentalt>, _vt16<yellowlt>, _vt16<whitelt>,
};
static constexpr ui32 vt256[] =
});
static constexpr auto vt256 = std::to_array(
{
_vt16<0>, _vt16<1>, _vt16<2>, _vt16<3>, _vt16<4>, _vt16<5>, _vt16<6>, _vt16<7>,
_vt16<8>, _vt16<9>, _vt16<10>, _vt16<11>, _vt16<12>, _vt16<13>, _vt16<14>, _vt16<15>,
Expand Down Expand Up @@ -451,7 +451,7 @@ namespace netxs
0xFF444444, 0xFF4E4E4E, 0xFF585858, 0xFF626262, 0xFF6C6C6C, 0xFF767676,
0xFF808080, 0xFF8A8A8A, 0xFF949494, 0xFF9E9E9E, 0xFFA8A8A8, 0xFFB2B2B2,
0xFFBCBCBC, 0xFFC6C6C6, 0xFFD0D0D0, 0xFFDADADA, 0xFFE4E4E4, 0xFFEEEEEE,
};
});
friend auto& operator << (std::ostream& s, rgba c)
{
return s << "{" << (int)c.chan.r
Expand Down Expand Up @@ -563,7 +563,7 @@ namespace netxs
}();
static auto cache_bg = cache_fg;
auto& cache = fg ? cache_fg : cache_bg; // Fg and Bg are sorted differently.
auto c = lookup(cache, std::span{ rgba::vt256, 16 });
auto c = lookup(cache, std::span{ rgba::vt256.data(), 16});
return netxs::swap_bits<0, 2>(c); // ANSI<->DOS color scheme reindex.
}
auto to_vtm16(bool fg = true) const // rgba: 4-bit Foreground color (vtm 16-color palette).
Expand Down Expand Up @@ -645,7 +645,7 @@ namespace netxs
});
return table;
}();
return lookup(cache, std::span{ rgba::vtm16, 8 });
return lookup(cache, std::span{ rgba::vtm16.data(), 8 });
}
};

Expand Down
1 change: 1 addition & 0 deletions src/netxs/desktopio/intmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include <array>
#include <optional>
#include <algorithm>
#include <limits>
Expand Down
33 changes: 18 additions & 15 deletions src/netxs/desktopio/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ namespace netxs::os
fdata.op = KD_FONT_OP_SET;
if (!ok(::ioctl(os::stdout_fd, KDFONTOP, &fdata), "::ioctl(KDFONTOP, KD_FONT_OP_SET)", os::unexpected)) return;

auto max_sz = std::numeric_limits<unsigned short>::max();
auto max_sz = ui16max;
auto spairs = std::vector<unipair>(max_sz);
auto dpairs = std::vector<unipair>(max_sz);
auto srcmap = unimapdesc{ max_sz, spairs.data() };
Expand All @@ -1027,17 +1027,20 @@ namespace netxs::os
&& smap.unicode != 0x2584) *dstptr++ = smap;
}
dstmap.entry_ct = dstptr - dstmap.entries;
unipair new_recs[] = {{ 0x2580, 10 },
{ 0x2219, 211 },
{ 0x2022, 211 },
{ 0x25CF, 211 },
{ 0x25A0, 254 },
{ 0x25AE, 254 },
{ 0x2584, 254 }};
if (dstmap.entry_ct < max_sz - std::size(new_recs)) // Add new records.
auto new_recs = std::to_array<unipair>(
{
{ 0x2580, 10 },
{ 0x2219, 211 },
{ 0x2022, 211 },
{ 0x25CF, 211 },
{ 0x25A0, 254 },
{ 0x25AE, 254 },
{ 0x2584, 254 },
});
if (dstmap.entry_ct < max_sz - new_recs.size()) // Add new records.
{
for (auto& p : new_recs) *dstptr++ = p;
dstmap.entry_ct += std::size(new_recs);
dstmap.entry_ct += new_recs.size();
if (!ok(::ioctl(os::stdout_fd, PIO_UNIMAP, &dstmap), "::ioctl(os::stdout_fd, PIO_UNIMAP)", os::unexpected)) return;
}
else log(prompt::os, "VGA font loading failed - 'UNIMAP' is full");
Expand Down Expand Up @@ -2581,11 +2584,11 @@ namespace netxs::os
#elif defined(__FreeBSD__)

auto size = 0_sz;
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
if (::sysctl(name, std::size(name), nullptr, &size, nullptr, 0) == 0)
auto name = std::to_array({ CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 });
if (::sysctl(name.data(), name.size(), nullptr, &size, nullptr, 0) == 0)
{
auto buff = std::vector<char>(size);
if (::sysctl(name, std::size(name), buff.data(), &size, nullptr, 0) == 0)
if (::sysctl(name.data(), name.size(), buff.data(), &size, nullptr, 0) == 0)
{
result = text(buff.data(), size);
}
Expand Down Expand Up @@ -4056,8 +4059,8 @@ namespace netxs::os
oldval = newval;
}
};
fd_t waits[] = { os::stdin_fd, alarm };
while (alive && WAIT_OBJECT_0 == ::WaitForMultipleObjects(2, waits, FALSE, INFINITE))
auto waits = std::to_array({ os::stdin_fd, (fd_t)alarm });
while (alive && WAIT_OBJECT_0 == ::WaitForMultipleObjects(2, waits.data(), FALSE, INFINITE))
{
if (!::GetNumberOfConsoleInputEvents(os::stdin_fd, &count))
{
Expand Down
18 changes: 9 additions & 9 deletions src/netxs/desktopio/unidata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ namespace netxs::unidata
struct base
{
static constexpr auto blocks_size = sz_t{ 4352 };
static constexpr si32 blocks_pack[] =
static constexpr auto blocks_pack = std::to_array<si32>(
{
0, -2, 256, 512, 768, 1024, 1280, 1536, 1792, 2048,
2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608,
Expand Down Expand Up @@ -375,10 +375,10 @@ namespace netxs::unidata
11264, 38912, -12, 15616, -2, 11264, 39168, -5, 15616, -19,
11264, 39424, -2796, 15616, 39680, 39936, -14, 40192, -240, 15616,
-255, 256, 40448, -255, 256, 40448
};
});

static constexpr auto offset_size = sz_t{ 40704 };
static constexpr si32 offset_pack[] =
static constexpr auto offset_pack = std::to_array<si32>(
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
Expand Down Expand Up @@ -654,9 +654,9 @@ namespace netxs::unidata

-386,107,-14, 69,-305,107,-31, 69,-30,107,-226, 69,-75,107,-181, 69,
-32, 95,-96, 68,-128, 95,-240, 68,-272, 95,-254, 0, -2, 69
};
});

static constexpr unidata ucspec[] =
static constexpr auto ucspec = std::to_array<unidata>(
{
{ widths::slim, gbreak::any , cntrls::non_control }, // 0
{ widths::zero, gbreak::ctrl, cntrls::non_control }, // 1
Expand Down Expand Up @@ -797,17 +797,17 @@ namespace netxs::unidata
{ widths::zero, gbreak::ep , cntrls::non_control }, // 136
{ widths::slim, gbreak::ri , cntrls::non_control }, // 137
{ widths::wide, gbreak::ext , cntrls::non_control }, // 138
};
});
};

template<class T, class D>
auto unpack(D const& pack, sz_t size)
{
auto data = std::vector<T>{};
data.reserve(size);
auto iter = pack;
auto tail = pack + std::size(pack);
while (iter < tail)
auto iter = pack.begin();
auto tail = pack.end();
while (iter != tail)
{
auto n = *iter++;
if (n < 0) data.insert(data.end(), -n, *iter++);
Expand Down
18 changes: 9 additions & 9 deletions src/netxs/desktopio/unidatagen.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,31 +396,31 @@
struct base
{{
static constexpr auto blocks_size = sz_t{{ {blocks_size} }};
static constexpr si32 blocks_pack[] =
static constexpr auto blocks_pack = std::to_array<si32>(
{{
{blocks}
}};
}});
static constexpr auto offset_size = sz_t{{ {offset_size} }};
static constexpr si32 offset_pack[] =
static constexpr auto offset_pack = std::to_array<si32>(
{{
{offset}
}};
}});
static constexpr {module} ucspec[] =
static constexpr auto ucspec = std::to_array<{module}>(
{{
{ucspec}
}};
}});
}};
template<class T, class D>
auto unpack(D const& pack, sz_t size)
{{
auto data = std::vector<T>{{}};
data.reserve(size);
auto iter = pack;
auto tail = pack + std::size(pack);
while (iter < tail)
auto iter = pack.begin();
auto tail = pack.end();
while (iter != tail)
{{
auto n = *iter++;
if (n < 0) data.insert(data.end(), -n, *iter++);
Expand Down
4 changes: 2 additions & 2 deletions src/netxs/desktopio/utf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace netxs::utf
};

// utf: First byte based UTF-8 codepoint lengths.
static constexpr int utf8lengths[] =
static constexpr auto utf8lengths = std::to_array(
{ // 0 1 2 3 4 5 6 7 8 9 A B C D E F
/* 0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 1 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Expand All @@ -112,7 +112,7 @@ namespace netxs::utf
/* D */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
/* E */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
/* F */ 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
} ;
});

// utf: Codepoint iterator.
struct cpit
Expand Down

0 comments on commit 2f71e9d

Please sign in to comment.