diff --git a/CMakeLists.txt b/CMakeLists.txt index 08235ed0..a49a9720 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -592,7 +592,7 @@ if (NOT CROSSTOOLS) if (NOT DEFINED ANDROID_PLATFORM) if(microhttpd_FOUND AND curl_FOUND) add_executable(pip_test "main.cpp") - target_link_libraries(pip_test pip pip_io_utils pip_client_server pip_http_server pip_http_client) + target_link_libraries(pip_test pip pip_io_utils pip_client_server pip_http_server pip_http_client pip_console) if(sodium_FOUND) add_executable(pip_cloud_test "main_picloud_test.cpp") target_link_libraries(pip_cloud_test pip_cloud) diff --git a/libs/console/piscreen.cpp b/libs/console/piscreen.cpp index 951d182b..1f010459 100644 --- a/libs/console/piscreen.cpp +++ b/libs/console/piscreen.cpp @@ -189,8 +189,8 @@ void PIScreen::SystemConsole::print() { for (int j = 0; j < dh; ++j) { int k = j * dw + i; Cell & c(cells[j + dy0][i + dx0]); - PRIVATE->chars[k].Char.UnicodeChar = 0; - PRIVATE->chars[k].Char.AsciiChar = c.symbol.toConsole1Byte(); + PRIVATE->chars[k].Char.UnicodeChar = c.symbol.unicode16Code(); + // PRIVATE->chars[k].Char.AsciiChar = c.symbol.toConsole1Byte(); PRIVATE->chars[k].Attributes = attributes(c); } // piCout << "draw" << dw << dh; @@ -200,7 +200,7 @@ void PIScreen::SystemConsole::print() { PRIVATE->srect.Top += dy0; PRIVATE->srect.Right -= width - dx1 - 1; PRIVATE->srect.Bottom -= height - dy1 - 1; - WriteConsoleOutput(PRIVATE->hOut, PRIVATE->chars.data(), PRIVATE->bs, PRIVATE->bc, &PRIVATE->srect); + WriteConsoleOutputW(PRIVATE->hOut, PRIVATE->chars.data(), PRIVATE->bs, PRIVATE->bc, &PRIVATE->srect); #else PIString s; int si = 0, sj = 0; diff --git a/main.cpp b/main.cpp index c3749e67..73d33d5b 100644 --- a/main.cpp +++ b/main.cpp @@ -8,405 +8,91 @@ using namespace PICoutManipulators; using namespace PIHTTP; +PIMathVectord data; -struct SN { - int _ii; - complexf _co; - PIIODevice::DeviceMode m; -}; -struct S { - bool _b; - int _i; - float _f; - PIString str; - // SN _sn; - PIVector2D v2d; - PIByteArray ba; - PISystemTime st; - PINetworkAddress na; - PIPointd po; - PILined li; - PIRectd re; -}; - -template<> -PIJSON piSerializeJSON(const SN & v) { - PIJSON ret; - ret["_ii"] = piSerializeJSON(v._ii); - ret["_co"] = piSerializeJSON(v._co); - ret["m"] = piSerializeJSON(v.m); - return ret; -} -template<> -PIJSON piSerializeJSON(const S & v) { - PIJSON ret; - ret["_b"] = piSerializeJSON(v._b); - ret["_i"] = piSerializeJSON(v._i); - ret["_f"] = piSerializeJSON(v._f); - ret["str"] = piSerializeJSON(v.str); - // ret["_sn"] = piSerializeJSON(v._sn); - ret["v2d"] = piSerializeJSON(v.v2d); - ret["ba"] = piSerializeJSON(v.ba); - ret["st"] = piSerializeJSON(v.st); - ret["na"] = piSerializeJSON(v.na); - ret["po"] = piSerializeJSON(v.po); - ret["li"] = piSerializeJSON(v.li); - ret["re"] = piSerializeJSON(v.re); - - return ret; +void _sfplot(const PIMathVectord & sf, PIString * str, const int lines, const int length) { + int offset = (data.size() - length) / 2 / 2; + double max_sf = 0; + for (int i = 0; i < length * 2; i++) { + if (sf[offset + i] > max_sf) { + max_sf = sf[offset + i]; + } + } + static PIVector2D grid; + grid.clear(); + grid.resize(lines * 2, length * 2); + // clang-format off + static const PIChar dots[16] = { + PIChar::fromUTF8(" "), PIChar::fromUTF8("▘"), PIChar::fromUTF8("▝"), PIChar::fromUTF8("▀"), + PIChar::fromUTF8("▖"), PIChar::fromUTF8("▌"), PIChar::fromUTF8("▞"), PIChar::fromUTF8("▛"), + PIChar::fromUTF8("▗"), PIChar::fromUTF8("▚"), PIChar::fromUTF8("▐"), PIChar::fromUTF8("▜"), + PIChar::fromUTF8("▄"), PIChar::fromUTF8("▙"), PIChar::fromUTF8("▟"), PIChar::fromUTF8("█"), + }; + // clang-format on + memset(grid.data(), 0, grid.size()); + for (int c = 0; c < grid.cols(); c++) { + double rind = piClampi(piRound(sf[offset + c] / max_sf * (grid.rows() - 1)), 0, grid.rows() - 1); + grid.element(rind, c) = 1; + } + union helper { + uint index = 0; + struct { + uint tl: 1; + uint tr: 1; + uint bl: 1; + uint br: 1; + }; + }; + for (int i = 0; i < lines; i++) { + str[i].resize(length); + } + helper h; + for (int l = 0; l < lines; l++) { + for (int c = 0; c < length; c++) { + int l2 = l + l; + int c2 = c + c; + h.bl = grid.element(l2, c2); + h.br = grid.element(l2, c2 + 1); + h.tl = grid.element(l2 + 1, c2); + h.tr = grid.element(l2 + 1, c2 + 1); + // piCout << h.index; + str[lines - 1 - l][c] = dots[h.index]; + } + } } -template<> -void piDeserializeJSON(SN & v, const PIJSON & js) { - v = {}; - piDeserializeJSON(v._ii, js["_ii"]); - piDeserializeJSON(v._co, js["_co"]); - piDeserializeJSON(v.m, js["m"]); -} -template<> -void piDeserializeJSON(S & v, const PIJSON & js) { - v = {}; - piDeserializeJSON(v._b, js["_b"]); - piDeserializeJSON(v._i, js["_i"]); - piDeserializeJSON(v._f, js["_f"]); - piDeserializeJSON(v.str, js["str"]); - // piDeserializeJSON(v._sn, js["_sn"]); - piDeserializeJSON(v.v2d, js["v2d"]); - piDeserializeJSON(v.ba, js["ba"]); - piDeserializeJSON(v.st, js["st"]); - piDeserializeJSON(v.na, js["na"]); - piDeserializeJSON(v.po, js["po"]); - piDeserializeJSON(v.li, js["li"]); - piDeserializeJSON(v.re, js["re"]); -} -PIKbdListener kbd; +TileSimple * test_tile; +PIScreen screen; +PIMathVectord asf; int main(int argc, char * argv[]) { - if (argc < 2) return 0; - PIFile f(argv[1], PIIODevice::ReadOnly); - piCout << "read" << f.path(); - auto fc = f.readAll(); - piCout << fc.size(); - if (!fc.isEmpty()) piCout << PIString::fromUTF8(fc.resized(32)); - return 0; + test_tile = new TileSimple(); + screen.rootTile()->addTile(test_tile); + screen.rootTile()->addTile(new TilePICout()); + PITimer _t; + PIString str[9]; + asf.resize(80); + _t.start(20_Hz, [&str] { + static double t = 0; + t += 0.1; + for (uint i = 0; i < asf.size(); ++i) + asf[i] = 1. + sin(t + 2. * i * M_2PI / asf.size()); + _sfplot(asf, str, 9, 28); - - PIHTTPServer server; - server.listen({"127.0.0.1:7777"}); - // server.setBasicAuthRealm("pip"); - // server.setBasicAuthEnabled(true); - // server.setBasicAuthCallback([](const PIString & u, const PIString & p) -> bool { - // piCout << "basic auth" << u << p; - // return (u == "u" && p == "p"); - auto reg = [&server](const PIString & path) { - server.registerPath(path, Method::Get, [path](const PIHTTP::MessageConst & msg) -> PIHTTP::MessageMutable { - piCout << "\nserver rec:\n\tpath: %1\n\t url: %2\n\t args: %3\n\tQ args: %4\n\tP args: %5"_a.arg(path) - .arg(msg.path()) - .arg(piStringify(msg.arguments())) - .arg(piStringify(msg.queryArguments())) - .arg(piStringify(msg.pathArguments())); - return MessageMutable().setCode(Code::Accepted); - }); - }; - // }); - - reg(" /*/a/get "); - reg(" /*/{ID}/get "); - reg(" /*/*/get "); - reg(" /*/*_b/get "); - - reg("/api/{ID}/get"); - reg("/api/1/get"); - // reg("/api/1/bort{bortID}/get"); - // reg("/api/**"); - // reg("/api/1/bort2/get"); - // reg("/api/**/bort2/get"); - - /*server.registerPath("api/{ID}/bort{bortID}/get", Method::Get, [](const PIHTTP::MessageConst & msg) -> PIHTTP::MessageMutable { - piCout << "server rec:\n\tpath: %1\n\targs: %2\n\tQ args: %3\n\tP args: %4\n"_a.arg(msg.path()) - .arg(piStringify(msg.arguments())) - .arg(piStringify(msg.queryArguments())) - .arg(piStringify(msg.pathArguments())); - return MessageMutable().setCode(Code::Accepted); + screen.lock(); + test_tile->content.resize(9); + for (int i = 0; i < 9; i++) { + test_tile->content[i].first = str[i]; + } + screen.unlock(); + piCout << "Время: "_u8 << t; }); - server.registerPath("sendMessage", Method::Post, [](const PIHTTP::MessageConst & msg) -> PIHTTP::MessageMutable { - return MessageMutable().setCode(Code::Accepted); - }); - server.registerUnhandled([](const PIHTTP::MessageConst & msg) -> PIHTTP::MessageMutable { - PIHTTP::MessageMutable ret; - piCout << "server rec:\n\tpath: %1\n\tmethod: %2\n\targs: %3\n\theaders: %4\n\tbody: %5\n"_a.arg(msg.path()) - .arg(PIHTTP::methodName(msg.method())) - .arg(piStringify(msg.arguments())) - .arg(PIStringList(msg.headers().map([](PIString k, PIString v) { return k + " = " + v; })).join("\n\t\t ")) - .arg(PIString::fromUTF8(msg.body())); - ret.setCode(PIHTTP::Code::BadRequest); - ret.setBody(PIByteArray::fromAscii("hello client! 0123456789")); - return ret; - });*/ - kbd.enableExitCapture('Q'); + + screen.enableExitCapture(); WAIT_FOR_EXIT; - return 0; - // PIRegularExpression pire("привет"_u8, PIRegularExpression::CaseInsensitive); - // PIString subj = "the dog ПриВет sat on the cat"_u8; - // PIRegularExpression pire("^(?\\d\\d)/(?\\d\\d)/(?\\d\\d\\d\\d)$"_u8); - // PIString subj = "08/12/1985"_u8; - - - PIString pat = "*.Exe"; - PIRegularExpression re_g = PIRegularExpression::fromGlob(pat, PIRegularExpression::CaseInsensitive); - PIRegularExpression re_p = PIRegularExpression::fromPOSIX(pat, PIRegularExpression::CaseInsensitive); - PIStringList files = { - "(Audio) 20250318-0852-16.8641941.m4a", - "dxwebsetup.exe", - "Firefox Installer.exe", - "LTA8092XS8_R8.pdf", - "SteamSetup.exe", - "TBT_1.41.1325.0.exe", - }; - piCout << " src pat" << pat.quoted(); - piCout << " Glob pat" << re_g.pattern().quoted(); - piCout << "POSIX pat" << re_p.pattern().quoted(); - piCout << "\nG P File"; - for (auto f: files) { - piCout << (re_g.match(f) ? 1 : 0) << (re_p.match(f) ? 1 : 0) << f; - } - // return 0; - PIRegularExpression pire("(?:\\/\\/\\s*)?.*\\n?(?:\\bfunction\\b)\\s*(?\\b\\w+\\b)\\s*(?:\\((?[^;()]*?)\\))", - PIRegularExpression::Multiline); - PIString subj = PIString::fromUTF8(PIFile::readAll("telegram.qs")); - - piCout << "Pattern:" << pire.pattern(); - piCout << "Valid:" << pire.isValid(); - piCout << "Error at" << pire.errorPosition() << ":" << pire.errorString(); - piCout << "Groups count:" << pire.captureGroupsCount(); - piCout << "Named groups:" << pire.captureGroupNames(); - piCout << ""; - - auto mr = pire.matchIterator(subj); - auto pire2 = pire; - - while (mr.next()) { - // piCout << "Subject" << subj; - piCout << "Matched:" << mr.hasMatch(); - - piCout << "By number"; - for (int i = 0; i <= pire.captureGroupsCount(); ++i) - piCout << i << "=" << mr.matchedString(i).trimmed(); - - piCout << "By name"; - for (auto g: pire.captureGroupNames()) - piCout << g.quoted() << "=" << mr.matchedString(g); - - piCout << ""; - } - - piCout << "!!!!!!!!!!!!!!!!!"; - - pire.match("vfsmndvbjbdlgdvb gdgf"); - pire.match(subj); - - { - PIVector vec; - vec << complexf{0.1, 0.2} << complexf{-1, 0.5}; - auto js = PIJSON::serialize(vec); - piCout << vec; - piCout << js; - piCout << PIJSON::deserialize(js); - } - - return 0; - /*PICodeParser parser; - parser.parseFile("c:/work/shstk/pip/test_header.h", false); - - for (const auto * e: parser.entities) { - piCout << e->type << e->name << "{"; - for (const auto & m: e->members) { - piCout << " " << m.type << m.name; - } - piCout << "}"; - } - - return 0;*/ - - // PIJSON j = piSerializeJSON(s); - // piDeserializeJSON(s, j); - PIVector vec; - vec << complexf{0.1, 0.2} << complexf{-1, 0.5}; - auto js = PIJSON::serialize(vec); - piCout << vec; - piCout << js; - piCout << PIJSON::deserialize(js); - - /*PIVector s; - s << S{false, 0, 0.1} << S{true, 1, -10.1}; - PIMap m; - m[1] = S{false, 0, 0.15}; - m[2] = S{true, 1, -10.1}; - // m[1]._sn._co = {3, 4}; - PIJSON j = piSerializeJSON(m); - piCout << j; - piDeserializeJSON(m, j); - piCout << m[1]._f;*/ - // piCout << m[1]._sn._co; - - /*PIVector v({-1, 0, 10, 200}); - PIMap m({ - {-1, -0.1 }, - {0, 0.1 }, - {100, 200.2} - }); - - piCout << v; - piCout << piSerializeJSON(v); - piDeserializeJSON(v, piSerializeJSON(v)); - piCout << v; - piCout << m; - piDeserializeJSON(m, piSerializeJSON(m)); - piCout << piSerializeJSON(m);*/ - - return 0; - - /*auto src = PIByteArray::fromAscii("The quick brown fox jumps over the lazy dog"); - auto key = PIByteArray::fromAscii("key"); - - PIStringList tnl; - int max_size = 0; - for (int t = 0; t < (int)PIDigest::Type::C ount; ++t) { - tnl << PIDigest::typeName((PIDigest::Type)t); - max_size = piMaxi(max_size, tnl.back().size_s()); - } - PIByteArray hs; - piCout << PIString::fromAscii(src); - for (int t = 0; t < (int)PIDigest::Type::Count; ++t) { - hs = PIDigest::calculate(src, (PIDigest::Type)t); - piCout << tnl[t].expandLeftTo(max_size, ' ') << "->" << hs.toHex(); - } - for (int t = 0; t < (int)PIDigest::Type::Count; ++t) { - const int bench_count = 100000; - PITimeMeasurer tm; - piForTimes(bench_count) { - hs = PIDigest::calculate(src, (PIDigest::Type)t); - } - auto el = tm.elapsed(); - piCout << tnl[t].expandLeftTo(max_size, ' ') << "time" << el.toMilliseconds(); - } - - // src.clear(); - // crypto_hash_sha512(sout.data(), src.data(), src.size()); - // piCout << "sod:" << sout.toHex(); - // piCout << "512:" << sha5xx(src, initial_512, 64).toHex(); - return 0;*/ - - /*PIHTTP::MessageMutable req; - req.setBody(PIByteArray::fromAscii("hello server!")).addArgument("a0", "val.0").addArgument("a~r1", "знач,1"_u8); - auto * c = PIHTTPClient::create("http://u:p@127.0.0.1:7777/api", PIHTTP::Method::Get, req); - c->onFinish([](PIHTTP::MessageConst msg) { - piCout << "client rec:\n\tpath: %1\n\tmethod: %2\n\targs: %3\n\theaders: %4\n\tbody: %5\n"_a.arg(msg.path()) - .arg(PIHTTP::methodName(msg.method())) - .arg(piStringify(msg.arguments())) - .arg( - PIStringList(msg.headers().map([](PIString k, PIString v) { return k + " = " + v; })).join("\n\t\t - ")) .arg(PIString::fromUTF8(msg.body())); - }) - ->onError([c](PIHTTP::MessageConst r) { - piCout << "error" << (int)r.code(); - piCout << "msg" << c->lastError(); - }) - ->onAbort([c](PIHTTP::MessageConst r) { - piCout << "abort" << (int)r.code(); - piCout << "msg" << c->lastError(); - }) - ->start();*/ - auto * c = PIHTTPClient::create( - PIString("127.0.0.1:7777/%1").arg("sendMessag"), - Method::Post, - MessageMutable().addHeader(Header::ContentType, "application/json").setBody(PIByteArray::fromAscii("{hello}"))); - c->onFinish([](const PIHTTP::MessageConst & msg) { piCout << "message finish" << (int)msg.code() << PIString::fromUTF8(msg.body()); }) - ->onError([c](const PIHTTP::MessageConst & msg) { piCout << "message error" << c->lastError(); }) - ->onAbort([c](const PIHTTP::MessageConst & msg) { piCout << "aborted"; }) - ->start(); - - piMSleep(1000); - // CurlThreadPool::instance()->destroy(); - // kbd.enableExitCapture(); - // WAIT_FOR_EXIT - // kbd.stopAndWait(); - - // server.stop(); - c->abort(); - piMSleep(10); - - return 0; - - // piCout << PIString::readableSize(PISystemMonitor::usedRAM()); - - /*PIVector vi; - piForTimes(10) { - piSleep(2.); - vi.enlarge(1000000); - piCout << "now" << vi.size() << vi.capacity(); - } - - piSleep(5.);*/ - /*kbd.enableExitCapture(); - - PIHTTPServer server; - - server.setFavicon(PIFile::readAll("logo.png", false)); - // server.setOption(MicrohttpdServer::Option::HTTPSEnabled, true); - server.listen({"127.0.0.1", 7777}); - // server.listen({"192.168.1.10", 7778}); - - server.registerPath("/", MicrohttpdServer::Method::Get, [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply { - MicrohttpdServer::Reply ret; - ret.setBody(PIByteArray::fromAscii(pageTitle)); - return ret; - }); - - server.registerPath("/html", MicrohttpdServer::Method::Get, [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply { - MicrohttpdServer::Reply ret; - ret.setBody("

arg=%1

"_a.arg(r.args.value("a0")).toUTF8()); - return ret; - }); - - server.registerPath("/api", MicrohttpdServer::Method::Put, [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply { - MicrohttpdServer::Reply ret; - ret.setBody(PIByteArray::fromAscii("API")); - return ret; - }); - - server.registerPath("/api/", MicrohttpdServer::Method::Post, [](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply { - MicrohttpdServer::Reply ret; - ret.setBody("API etry %1"_a.arg(r.path).toUTF8()); - ret.setCode(405); - return ret; - }); - - server.registerUnhandled([](const MicrohttpdServer::Request & r) -> MicrohttpdServer::Reply { - MicrohttpdServer::Reply ret; - ret.setBody("Unknown"_a.arg(r.path).toUTF8()); - ret.setCode(404); - return ret; - });*/ - - /*server.setRequestCallback([](MicrohttpdServer::Request r) -> MicrohttpdServer::Reply { - MicrohttpdServer::Reply rep; - piCout << "request" << r.path; - piCout << " header" << r.headers; - piCout << " args" << r.args; - piCout << " body" << r.body; - piCout << ""; - rep.setBody(PIByteArray::fromAscii("[{\"value1\": true, \"value2\": \"ыекштп\"}]")); - return rep; - });*/ - - /*piCout << "start" << server.isListen(); - - WAIT_FOR_EXIT - - server.stop();*/ + screen.stopAndWait(); return 0; }