fix some warnings
All checks were successful
build_test / build (push) Successful in 3m57s

This commit is contained in:
PoliEcho 2025-06-03 13:27:04 +02:00
parent d330878f8a
commit 00f261696f
2 changed files with 344 additions and 340 deletions

View File

@ -1,23 +1,23 @@
#include <ncurses.h> #include "strings.h"
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <ncurses.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp> #include <nlohmann/json_fwd.hpp>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include "strings.h"
using nlohmann::json; using nlohmann::json;
void save_document(std::string& text, std::vector<std::string>& lines) { void save_document(std::string &text, std::vector<std::string> &lines) {
text = ""; text = "";
for (const std::string& line : lines) { for (const std::string &line : lines) {
text.append(line); text.append(line);
text.append("\n"); text.append("\n");
} }
} }
void editor_hard(std::string& text) { void editor_hard(std::string &text) {
uint16_t cursor_x = 0; uint16_t cursor_x = 0;
uint16_t cursor_y = 0; uint16_t cursor_y = 0;
std::vector<std::string> lines; std::vector<std::string> lines;
@ -48,7 +48,8 @@ void editor_hard(std::string& text) {
while (true) { while (true) {
clear(); clear();
// Draw text lines // Draw text lines
for (size_t i = 0; i < window_height - 1 && i < lines.size(); i++) { for (size_t i = 0;
i < static_cast<size_t>(window_height) - 1 && i < lines.size(); i++) {
mvprintw(i, 0, "%s", lines[i].c_str()); mvprintw(i, 0, "%s", lines[i].c_str());
} }

View File

@ -1,19 +1,3 @@
#include <curses.h>
#include <menu.h>
#include <ncurses.h>
#include <array>
#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <sstream>
#include <string>
#include <vector>
#include "color.h" #include "color.h"
#include "const.h" #include "const.h"
#include "cups.h" #include "cups.h"
@ -24,13 +8,31 @@
#include "signal.h" #include "signal.h"
#include "strings.h" #include "strings.h"
#include "types.h" #include "types.h"
#include <array>
#include <cerrno>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <curses.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <limits>
#include <menu.h>
#include <ncurses.h>
#include <nlohmann/json.hpp>
#include <sstream>
#include <string>
#include <vector>
using nlohmann::json; using nlohmann::json;
std::vector<allocation> main_menu_allocated; std::vector<allocation> main_menu_allocated;
#define COMPLAINTS_DIR "complaints" #define COMPLAINTS_DIR "complaints"
std::array<std::string, 2> get_name_date_from_item(ITEM* item) { std::array<std::string, 2> get_name_date_from_item(ITEM *item) {
std::stringstream ss(item_description(item)); std::stringstream ss(item_description(item));
std::array<std::string, 2> name_date; std::array<std::string, 2> name_date;
name_date[0] = item_name(item); name_date[0] = item_name(item);
@ -38,7 +40,7 @@ std::array<std::string, 2> get_name_date_from_item(ITEM* item) {
return name_date; return name_date;
} }
void reload_menu_from_directory(complete_menu& main_menu) { void reload_menu_from_directory(complete_menu &main_menu) {
// Clean up existing menu items and allocated memory // Clean up existing menu items and allocated memory
if (main_menu.menu != nullptr) { if (main_menu.menu != nullptr) {
unpost_menu(main_menu.menu); unpost_menu(main_menu.menu);
@ -61,7 +63,7 @@ void reload_menu_from_directory(complete_menu& main_menu) {
for (auto it = main_menu_allocated.begin(); for (auto it = main_menu_allocated.begin();
it != main_menu_allocated.end();) { it != main_menu_allocated.end();) {
if (it->type == GENERIC_TYPE) { if (it->type == GENERIC_TYPE) {
delete[] static_cast<char*>(it->ptr); delete[] static_cast<char *>(it->ptr);
it = main_menu_allocated.erase(it); it = main_menu_allocated.erase(it);
} else { } else {
++it; ++it;
@ -69,10 +71,10 @@ void reload_menu_from_directory(complete_menu& main_menu) {
} }
// Recreate items from directory (same logic as initialization) // Recreate items from directory (same logic as initialization)
std::vector<ITEM*> items; std::vector<ITEM *> items;
if (std::filesystem::exists(COMPLAINTS_DIR)) { if (std::filesystem::exists(COMPLAINTS_DIR)) {
for (const std::filesystem::directory_entry& 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::ifstream file(directroy_entry.path());
@ -96,9 +98,9 @@ void reload_menu_from_directory(complete_menu& main_menu) {
loc_strings loc_strings
->status_strings[complaint_json["status"].get<uint8_t>()]); ->status_strings[complaint_json["status"].get<uint8_t>()]);
char* name = new char[name_date[0].length() + 1]; 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());
@ -112,9 +114,9 @@ void reload_menu_from_directory(complete_menu& main_menu) {
items.push_back(nullptr); items.push_back(nullptr);
// Create new menu items array // Create new menu items array
main_menu.items = new ITEM*[items.size()]; main_menu.items = new ITEM *[items.size()];
main_menu.items_size = items.size() - 1; main_menu.items_size = items.size() - 1;
memcpy(main_menu.items, items.data(), (items.size() * sizeof(ITEM*))); memcpy(main_menu.items, items.data(), (items.size() * sizeof(ITEM *)));
// Create new menu // Create new menu
main_menu.menu = new_menu(main_menu.items); main_menu.menu = new_menu(main_menu.items);
@ -149,9 +151,9 @@ 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 std::filesystem::directory_entry& 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::ifstream file(directroy_entry.path());
@ -173,9 +175,9 @@ void menu() {
loc_strings loc_strings
->status_strings[complaint_json["status"].get<uint8_t>()]); ->status_strings[complaint_json["status"].get<uint8_t>()]);
char* name = new char[name_date[0].length() + 1]; 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());
@ -186,9 +188,9 @@ 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()];
main_menu.items_size = items.size() - 1; main_menu.items_size = items.size() - 1;
memcpy(main_menu.items, items.data(), (items.size() * sizeof(ITEM*))); memcpy(main_menu.items, items.data(), (items.size() * sizeof(ITEM *)));
} }
/* Crate menu */ /* Crate menu */
@ -239,23 +241,21 @@ void menu() {
// DONT FORGET TO PRINT ACTUAL DOCUMENT // DONT FORGET TO PRINT ACTUAL DOCUMENT
std::array<std::string, 2> name_date = std::array<std::string, 2> name_date =
get_name_date_from_item(current_item(main_menu.menu)); get_name_date_from_item(current_item(main_menu.menu));
std::ifstream selected_file(COMPLAINTS_DIR "/" + name_date[0] + std::ifstream selected_file(COMPLAINTS_DIR "/" + name_date[0] + '_' +
'_' + name_date[1]); name_date[1]);
if (!selected_file.is_open()) { if (!selected_file.is_open()) {
std::clog << selected_file.rdstate() << "\n"; std::clog << selected_file.rdstate() << "\n";
std::cerr << RED "[ERROR] " RESET << loc_strings->invalid_input std::cerr << RED "[ERROR] " RESET << loc_strings->invalid_input
<< "\n" << "\n"
<< "File: " << "File: "
<< COMPLAINTS_DIR "/" + name_date[0] + '_' + << COMPLAINTS_DIR "/" + name_date[0] + '_' + name_date[1]
name_date[1]
<< "\n"; << "\n";
exit(EINVAL); exit(EINVAL);
} }
json selected_file_json = json::parse(selected_file); json selected_file_json = json::parse(selected_file);
printDocument( printDocument(selected_file_json["complaint_text"].get<std::string>());
selected_file_json["complaint_text"].get<std::string>());
current_allocated = &main_menu_allocated; current_allocated = &main_menu_allocated;
break; break;
} }
@ -263,12 +263,12 @@ void menu() {
case "ee"_sh: { case "ee"_sh: {
const std::string name = spawncmd(loc_strings->reference_name); const std::string name = spawncmd(loc_strings->reference_name);
if (name == "") { if (name == "") {
print_in_middle(main_menu.win, 10, 0, 40, print_in_middle(main_menu.win, 10, 0, 40, loc_strings->invalid_input,
loc_strings->invalid_input, COLOR_PAIR(1)); COLOR_PAIR(1));
} }
const time_t current_time = time(nullptr); const time_t current_time = time(nullptr);
const tm* local_time = localtime(&current_time); const tm *local_time = localtime(&current_time);
char formatted_date[11]; char formatted_date[11];
strftime(formatted_date, sizeof(formatted_date), "%d-%m-%Y", strftime(formatted_date, sizeof(formatted_date), "%d-%m-%Y",
@ -292,15 +292,14 @@ void menu() {
case "eh"_sh: { case "eh"_sh: {
std::array<std::string, 2> name_date = std::array<std::string, 2> name_date =
get_name_date_from_item(current_item(main_menu.menu)); get_name_date_from_item(current_item(main_menu.menu));
std::ifstream selected_file(COMPLAINTS_DIR "/" + name_date[0] + std::ifstream selected_file(COMPLAINTS_DIR "/" + name_date[0] + '_' +
'_' + name_date[1]); name_date[1]);
if (!selected_file.is_open()) { if (!selected_file.is_open()) {
std::clog << selected_file.rdstate() << "\n"; std::clog << selected_file.rdstate() << "\n";
std::cerr << RED "[ERROR] " RESET << loc_strings->invalid_input std::cerr << RED "[ERROR] " RESET << loc_strings->invalid_input
<< "\n" << "\n"
<< "File: " << "File: "
<< COMPLAINTS_DIR "/" + name_date[0] + '_' + << COMPLAINTS_DIR "/" + name_date[0] + '_' + name_date[1]
name_date[1]
<< "\n"; << "\n";
exit(EINVAL); exit(EINVAL);
} }
@ -321,8 +320,8 @@ void menu() {
break; break;
} }
default: default:
print_in_middle(main_menu.win, 10, 0, 40, print_in_middle(main_menu.win, 10, 0, 40, loc_strings->unknown_command,
loc_strings->unknown_command, COLOR_PAIR(1)); COLOR_PAIR(1));
break; break;
} }
break; break;
@ -333,16 +332,16 @@ void menu() {
{ {
uint8_t n = 0; uint8_t n = 0;
size_t indexes[2]; size_t indexes[2] = {ULONG_MAX, ULONG_MAX}; // fix compiler warning
for (size_t i = 0; (i < main_menu_allocated.size()); i++) { for (size_t i = 0; (i < main_menu_allocated.size()); i++) {
if (main_menu_allocated[i].ptr == if (main_menu_allocated[i].ptr ==
item_name(current_item(main_menu.menu))) { item_name(current_item(main_menu.menu))) {
delete[] static_cast<char*>(main_menu_allocated[i].ptr); delete[] static_cast<char *>(main_menu_allocated[i].ptr);
indexes[0] = i; indexes[0] = i;
n++; n++;
} else if (main_menu_allocated[i].ptr == } else if (main_menu_allocated[i].ptr ==
item_description(current_item(main_menu.menu))) { item_description(current_item(main_menu.menu))) {
delete[] static_cast<char*>(main_menu_allocated[i].ptr); delete[] static_cast<char *>(main_menu_allocated[i].ptr);
indexes[1] = i; indexes[1] = i;
n++; n++;
} }
@ -350,6 +349,10 @@ void menu() {
break; break;
} }
} }
if (indexes[0] == ULONG_MAX || indexes[1] == ULONG_MAX) {
std::cerr << RED "[ERROR]" << RESET " HOW DID THIS EVEN HAPPEN\n";
safe_exit(84);
}
if (indexes[0] > indexes[1]) { if (indexes[0] > indexes[1]) {
std::swap(indexes[0], indexes[1]); std::swap(indexes[0], indexes[1]);
} }
@ -378,8 +381,8 @@ void menu() {
: selected_file_json["status"] = : selected_file_json["status"] =
selected_file_json["status"].get<uint8_t>() + 1; selected_file_json["status"].get<uint8_t>() + 1;
std::ofstream selected_file(COMPLAINTS_DIR "/" + name_date[0] + std::ofstream selected_file(COMPLAINTS_DIR "/" + name_date[0] + '_' +
'_' + name_date[1]); name_date[1]);
selected_file << selected_file_json; selected_file << selected_file_json;
selected_file.close(); selected_file.close();
} else { } else {
@ -390,13 +393,13 @@ void menu() {
name_date[1].append(" "); name_date[1].append(" ");
name_date[1].append( name_date[1].append(
loc_strings->status_strings[selected_file_json["status"] loc_strings
.get<uint8_t>()]); ->status_strings[selected_file_json["status"].get<uint8_t>()]);
char* date_status = new char[name_date[1].length() + 1]; char *date_status = new char[name_date[1].length() + 1];
main_menu_allocated.push_back({GENERIC_TYPE, date_status, 1}); main_menu_allocated.push_back({GENERIC_TYPE, date_status, 1});
strlcpy(date_status, name_date[1].c_str(), name_date[1].length() + 1); strlcpy(date_status, name_date[1].c_str(), name_date[1].length() + 1);
char* name = new char[name_date[0].length() + 1]; 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});
strlcpy(name, name_date[0].c_str(), name_date[0].length() + 1); strlcpy(name, name_date[0].c_str(), name_date[0].length() + 1);
@ -408,7 +411,7 @@ void menu() {
} }
} }
} }
WINDOW* old_win_sub = menu_sub(main_menu.menu); WINDOW *old_win_sub = menu_sub(main_menu.menu);
unpost_menu(main_menu.menu); unpost_menu(main_menu.menu);
free_menu(main_menu.menu); free_menu(main_menu.menu);
main_menu.menu = new_menu(main_menu.items); main_menu.menu = new_menu(main_menu.items);