more additions for future file handling
Some checks failed
build_test / build (push) Failing after 3m32s

This commit is contained in:
PoliEcho 2025-05-28 17:58:42 +02:00
parent eaf6f44464
commit 7f427a47fc
5 changed files with 155 additions and 98 deletions

View File

@ -17,4 +17,6 @@ inline constexpr auto operator""_sh(const char* str, size_t len) {
return hash_djb2a(std::string_view{str, len}); return hash_djb2a(std::string_view{str, len});
} }
enum { NOT_SEND, SEND, REJECTED, ACCEPTED, STATUS_COUNT };
#endif #endif

View File

@ -1,19 +1,19 @@
#include "gameske_funkce.h" #include "gameske_funkce.h"
#include "memory.h" #include <curses.h>
#include "strings.h" #include <menu.h>
#include <ncurses.h>
#include <unistd.h>
#include <chrono> #include <chrono>
#include <clocale> #include <clocale>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <curses.h>
#include <format> #include <format>
#include <iostream> #include <iostream>
#include <menu.h>
#include <ncurses.h>
#include <string> #include <string>
#include <thread> #include <thread>
#include <unistd.h> #include "memory.h"
#include "strings.h"
/* /*
size_t spawn_menu(uint16_t begin_y, uint16_t begin_x, const char** choices, size_t spawn_menu(uint16_t begin_y, uint16_t begin_x, const char** choices,
@ -81,10 +81,10 @@ size_t spawn_menu(uint16_t begin_y, uint16_t begin_x, const char** choices,
} }
*/ */
void print_in_middle(WINDOW *win, int starty, int startx, int width, void print_in_middle(WINDOW* win, int starty, int startx, int width,
const char *string, chtype color) { const char* string, chtype color) {
int length, x, y; int length, x, y;
float temp; uint8_t temp;
if (win == NULL) if (win == NULL)
win = stdscr; win = stdscr;
@ -98,7 +98,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width,
length = strlen(string); length = strlen(string);
temp = (width - length) / 2; temp = (width - length) / 2;
x = startx + (int)temp; x = startx + temp;
wattron(win, color); wattron(win, color);
mvwprintw(win, y, x, "%s", string); mvwprintw(win, y, x, "%s", string);
wattroff(win, color); wattroff(win, color);
@ -110,7 +110,7 @@ std::string spawncmd(const std::string prompt) {
int pos = 0; int pos = 0;
int ch; int ch;
WINDOW *cmd_win = newwin(3, 40, LINES - 3, 0); WINDOW* cmd_win = newwin(3, 40, LINES - 3, 0);
if (cmd_win == NULL) if (cmd_win == NULL)
return ""; return "";
@ -134,7 +134,7 @@ std::string spawncmd(const std::string prompt) {
if (pos > 0) { if (pos > 0) {
pos--; pos--;
// Move cursor back and erase the character // Move cursor back and erase the character
wmove(cmd_win, 1, 10 + pos); wmove(cmd_win, 1, prompt.length() + 3 + pos);
waddch(cmd_win, ' '); waddch(cmd_win, ' ');
wmove(cmd_win, 1, 10 + pos); wmove(cmd_win, 1, 10 + pos);
} }
@ -143,7 +143,8 @@ std::string spawncmd(const std::string prompt) {
} else if (isprint(ch)) { } else if (isprint(ch)) {
// Printable character // Printable character
cmd[pos] = ch; cmd[pos] = ch;
mvwaddch(cmd_win, 1, 10 + pos, ch); // Echo the character mvwaddch(cmd_win, 1, prompt.length() + 3 + pos,
ch); // Echo the character
pos++; pos++;
} }
@ -151,7 +152,7 @@ std::string spawncmd(const std::string prompt) {
} }
wattroff(cmd_win, COLOR_PAIR(COLOR_CYAN)); wattroff(cmd_win, COLOR_PAIR(COLOR_CYAN));
cmd[pos] = '\0'; // Ensure null termination cmd[pos] = '\0'; // Ensure null termination
// Restore echo state as needed // Restore echo state as needed
curs_set(0); curs_set(0);
@ -161,7 +162,7 @@ std::string spawncmd(const std::string prompt) {
return std::string(cmd); return std::string(cmd);
} }
void async_clock(WINDOW *win, WINDOW *text_win) { void async_clock(WINDOW* win, WINDOW* text_win) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
bool prev_echo_state; bool prev_echo_state;
uint16_t prev_y, prev_x; uint16_t prev_y, prev_x;
@ -183,7 +184,7 @@ void async_clock(WINDOW *win, WINDOW *text_win) {
if (dot_pos != std::string::npos) { if (dot_pos != std::string::npos) {
time_str.erase(dot_pos); time_str.erase(dot_pos);
} }
} catch (const std::exception &e) { } catch (const std::exception& e) {
std::clog << "Format error: " << e.what() << std::endl; std::clog << "Format error: " << e.what() << std::endl;
time_str = "Error"; time_str = "Error";
} }
@ -205,11 +206,11 @@ void async_clock(WINDOW *win, WINDOW *text_win) {
void async_clock_init() { void async_clock_init() {
// memory leak // memory leak
WINDOW *win = newwin(3, 10, LINES - 3, COLS - 10); WINDOW* win = newwin(3, 10, LINES - 3, COLS - 10);
current_allocated->push_back({WINDOW_TYPE, win, 1}); current_allocated->push_back({WINDOW_TYPE, win, 1});
// memory leak // memory leak
WINDOW *text_win = newwin(3, 52, LINES - 3, COLS - 10 - 44); WINDOW* text_win = newwin(3, 52, LINES - 3, COLS - 10 - 44);
current_allocated->push_back({WINDOW_TYPE, text_win, 1}); current_allocated->push_back({WINDOW_TYPE, text_win, 1});
std::thread clock_thread(async_clock, win, text_win); std::thread clock_thread(async_clock, win, text_win);

View File

@ -1,19 +1,26 @@
#include <curses.h>
#include <menu.h>
#include <ncurses.h>
#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <sstream>
#include <string>
#include <vector>
#include "const.h" #include "const.h"
#include "cups.h" #include "cups.h"
#include "editor_easy.h" #include "editor_easy.h"
#include "gameske_funkce.h" #include "gameske_funkce.h"
#include "memory.h" #include "memory.h"
#include "signal.h"
#include "strings.h" #include "strings.h"
#include "types.h" #include "types.h"
#include <cstdint> using nlohmann::json;
#include <cstring>
#include <curses.h>
#include <filesystem>
#include <iostream>
#include <menu.h>
#include <ncurses.h>
#include <string>
#include <vector>
std::vector<allocation> main_menu_allocated; std::vector<allocation> main_menu_allocated;
#define COMPLAINTS_DIR "complaints" #define COMPLAINTS_DIR "complaints"
@ -26,7 +33,7 @@ void menu() {
start_color(); start_color();
cbreak(); cbreak();
noecho(); noecho();
curs_set(0); // Makes cursor invisible curs_set(0); // Makes cursor invisible
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
for (uint8_t i = 0; i < 8; i++) { for (uint8_t i = 0; i < 8; i++) {
init_pair(i, i, COLOR_BLACK); init_pair(i, i, COLOR_BLACK);
@ -37,19 +44,33 @@ void menu() {
complete_menu main_menu = {nullptr, nullptr, 0, nullptr}; complete_menu main_menu = {nullptr, nullptr, 0, nullptr};
main_menu_allocated.push_back({COMPLETE_MENU_TYPE, &main_menu, 1}); main_menu_allocated.push_back({COMPLETE_MENU_TYPE, &main_menu, 1});
{ {
std::vector<ITEM *> items; std::vector<ITEM*> items;
if (std::filesystem::exists(COMPLAINTS_DIR)) { if (std::filesystem::exists(COMPLAINTS_DIR)) {
for (const auto &directroy_entry : for (const std::filesystem::directory_entry& directroy_entry :
std::filesystem::directory_iterator(COMPLAINTS_DIR)) { std::filesystem::directory_iterator(COMPLAINTS_DIR)) {
if (directroy_entry.is_regular_file()) { if (directroy_entry.is_regular_file()) {
std::ifstream file(directroy_entry.path());
std::string name_date[2]; std::string name_date[2];
std::stringstream ssfn(directroy_entry.path().filename().string()); std::stringstream ssfn(directroy_entry.path().filename().string());
for (uint8_t i = 0; i < 2; i++) { for (uint8_t i = 0; i < ARRAY_SIZE(name_date); i++) {
std::getline(ssfn, name_date[i], '_'); std::getline(ssfn, name_date[i], '_');
} }
char *name = new char[name_date[0].length() + 1]; json complaint_json = json::parse(file);
if (complaint_json["status"].is_null() ||
complaint_json["status"].get<size_t>() >= STATUS_COUNT) {
std::cerr << "Invalid status in file: "
<< directroy_entry.path().filename().string()
<< std::endl;
safe_exit(EINVAL);
}
name_date[1].append(" ");
name_date[1].append(
loc_strings
->status_strings[complaint_json["status"].get<uint8_t>()]);
char* name = new char[name_date[0].length() + 1];
main_menu_allocated.push_back({GENERIC_TYPE, name, 1}); main_menu_allocated.push_back({GENERIC_TYPE, name, 1});
char *date = new char[name_date[1].length() + 1]; char* date = new char[name_date[1].length() + 1];
main_menu_allocated.push_back({GENERIC_TYPE, date, 1}); main_menu_allocated.push_back({GENERIC_TYPE, date, 1});
strcpy(name, name_date[0].c_str()); strcpy(name, name_date[0].c_str());
strcpy(date, name_date[1].c_str()); strcpy(date, name_date[1].c_str());
@ -60,8 +81,8 @@ void menu() {
std::filesystem::create_directory(COMPLAINTS_DIR); std::filesystem::create_directory(COMPLAINTS_DIR);
} }
items.push_back(nullptr); items.push_back(nullptr);
main_menu.items = new ITEM *[items.size()]; main_menu.items = new ITEM*[items.size()];
memcpy(main_menu.items, items.data(), (items.size() * sizeof(ITEM *))); memcpy(main_menu.items, items.data(), (items.size() * sizeof(ITEM*)));
} }
/* Crate menu */ /* Crate menu */
@ -95,40 +116,66 @@ void menu() {
int c; int c;
while ((c = wgetch(main_menu.win)) != KEY_F(1)) { while ((c = wgetch(main_menu.win)) != KEY_F(1)) {
switch (c) { switch (c) {
case KEY_DOWN: case KEY_DOWN:
menu_driver(main_menu.menu, REQ_DOWN_ITEM); menu_driver(main_menu.menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(main_menu.menu, REQ_UP_ITEM);
break;
case 10:
refresh();
break;
case ':':
switch (hash_djb2a(spawncmd())) {
case "print"_sh:
case "p"_sh:
// DONT FORGET TO PRINT ACTUAL DOCUMENT
printDocument("test");
current_allocated = &main_menu_allocated;
break; break;
case "easy_edit"_sh: case KEY_UP:
case "ee"_sh: { menu_driver(main_menu.menu, REQ_UP_ITEM);
std::string name = spawncmd("Reference Name"); break;
if (name == "") { case 10:
print_in_middle(main_menu.win, 10, 0, 40,
loc_strings->unknown_command, COLOR_PAIR(1)); refresh();
break;
case ':':
switch (hash_djb2a(spawncmd())) {
case "print"_sh:
case "p"_sh:
// DONT FORGET TO PRINT ACTUAL DOCUMENT
printDocument("test");
current_allocated = &main_menu_allocated;
break;
case "easy_edit"_sh:
case "ee"_sh: {
const std::string name = spawncmd(loc_strings->reference_name);
if (name == "") {
print_in_middle(main_menu.win, 10, 0, 40,
loc_strings->invalid_input, COLOR_PAIR(1));
}
const time_t current_time = time(nullptr);
const tm* local_time = localtime(&current_time);
char formatted_date[11];
strftime(formatted_date, sizeof(formatted_date), "%d-%m-%Y",
local_time);
std::ofstream complaint_file(COMPLAINTS_DIR "/" + name + "_" +
formatted_date);
const std::string complaint_text = vytvorTrestniOznameni();
json complaint_json = {{"date", formatted_date},
{"status", NOT_SEND},
{"complaint_text", complaint_text}};
complaint_file << complaint_json;
break;
}
default:
print_in_middle(main_menu.win, 10, 0, 40,
loc_strings->unknown_command, COLOR_PAIR(1));
break;
}
break;
case 'm':
std::stringstream item_name_ss(item_name(current_item(main_menu.menu)));
std::string name_date[2];
for (uint8_t i = 0; i < ARRAY_SIZE(name_date); i++) {
std::getline(item_name_ss, name_date[i], ' ');
} }
std::clog << vytvorTrestniOznameni();
break; break;
}
default:
print_in_middle(main_menu.win, 10, 0, 40, loc_strings->unknown_command,
COLOR_PAIR(1));
break;
}
} }
redrawwin(main_menu.win); redrawwin(main_menu.win);
wrefresh(main_menu.win); wrefresh(main_menu.win);

View File

@ -76,7 +76,9 @@ Signature of the complainant: {0}
*This is not a legal document, but only a template for informational purposes. For legal advice, please consult a qualified attorney.* *This is not a legal document, but only a template for informational purposes. For legal advice, please consult a qualified attorney.*
)", )",
.command = "Command", .command = "Command",
.invalid_input = "Invalid input"}; .invalid_input = "Invalid input",
.reference_name = "Reference Name",
.status_strings = {"Not sent", "Sent", "Rejected", "Accepted"}};
constexpr strings czech_strings{ constexpr strings czech_strings{
.invalid_option = "Neplatná volba: ", .invalid_option = "Neplatná volba: ",
@ -152,6 +154,8 @@ Podpis oznamovatele: {0}
*Toto není právní dokument, ale pouze vzor pro informativní účely. Pro právní poradenství se obraťte na kvalifikovaného právníka.* *Toto není právní dokument, ale pouze vzor pro informativní účely. Pro právní poradenství se obraťte na kvalifikovaného právníka.*
)", )",
.command = "Příkaz", .command = "Příkaz",
.invalid_input = "Neplatný vstup"}; .invalid_input = "Neplatný vstup",
.reference_name = "Referenční Název",
.status_strings = {"Nebylo odesláno", "Odesláno", "Zamítnuto", "Přijato"}};
const strings *loc_strings; const strings* loc_strings;

View File

@ -1,44 +1,47 @@
#ifndef PARADOCS_STRINGS_H_ #ifndef PARADOCS_STRINGS_H_
#define PARADOCS_STRINGS_H_ #define PARADOCS_STRINGS_H_
#include "const.h"
typedef struct { typedef struct {
const char *invalid_option; const char* invalid_option;
const char *try_str; const char* try_str;
const char *usage; const char* usage;
const char *options; const char* options;
const char *print_this_help; const char* print_this_help;
const char *print_version; const char* print_version;
const char *version; const char* version;
const char *main_menu; const char* main_menu;
const char *f1_to_exit; const char* f1_to_exit;
const char *unknown_command; const char* unknown_command;
const char *min_terminal_size; const char* min_terminal_size;
const char *name_of_submiter; const char* name_of_submiter;
const char *address_of_submiter; const char* address_of_submiter;
const char *phone_of_submiter; const char* phone_of_submiter;
const char *email_of_submiter; const char* email_of_submiter;
const char *name_of_recipient; const char* name_of_recipient;
const char *address_of_recipient; const char* address_of_recipient;
const char *phone_of_recipient; const char* phone_of_recipient;
const char *email_of_recipient; const char* email_of_recipient;
const char *place_of_incident; const char* place_of_incident;
const char *date_of_incident; const char* date_of_incident;
const char *description_of_incident; const char* description_of_incident;
const char *evidence_of_incident; const char* evidence_of_incident;
const char *additional_info; const char* additional_info;
const char *cease_and_desist_entry_of_information; const char* cease_and_desist_entry_of_information;
const char *enter_all_information_and_press_f10; const char* enter_all_information_and_press_f10;
const char *criminal_complaint_template; const char* criminal_complaint_template;
const char *command; const char* command;
const char *invalid_input; const char* invalid_input;
const char* reference_name;
const char* status_strings[STATUS_COUNT];
} strings; } strings;
extern const strings english_strings; extern const strings english_strings;
extern const strings czech_strings; extern const strings czech_strings;
extern const strings *loc_strings; extern const strings* loc_strings;
#endif #endif