Compare commits

..

7 Commits

Author SHA1 Message Date
f01422eaf5 fix memory leaks
Some checks are pending
/ sync-to-origin (push) Waiting to run
2025-04-14 09:10:56 +02:00
9b8bdbf850 add asan build rule to Makefile 2025-04-14 09:10:37 +02:00
c9aef90130 add beter regex to gitignore 2025-04-14 09:10:18 +02:00
d9f545ed09 remove random C89 apperence 2025-04-13 19:31:32 +02:00
d533b526bf fix timetable const issue 2025-04-13 14:51:25 +02:00
ea083e31fc make more timetable variables const 2025-04-13 14:46:50 +02:00
05c406ed6a extract komens usage message to function 2025-04-13 14:44:45 +02:00
12 changed files with 198 additions and 119 deletions

2
.gitignore vendored
View File

@ -1,7 +1,7 @@
.cache .cache
.vscode .vscode
compile_commands.json compile_commands.json
core* *core*
build build
test-data test-data
log log

View File

@ -2,6 +2,7 @@
CPPC = g++ CPPC = g++
CPPC_FLAGS = -std=c++23 -s -O3 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings CPPC_FLAGS = -std=c++23 -s -O3 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings
DEBUG_FLAGS = -ggdb -std=c++23 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings DEBUG_FLAGS = -ggdb -std=c++23 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings
DEBUG_ASANITIZE = -fsanitize=address -ggdb -fno-omit-frame-pointer -std=c++23 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings
SRC_PATH := src SRC_PATH := src
@ -19,6 +20,9 @@ all: make-build-dir $(BIN_PATH)/bakatui
debug: CPPC_FLAGS = $(DEBUG_FLAGS) debug: CPPC_FLAGS = $(DEBUG_FLAGS)
debug: make-build-dir $(BIN_PATH)/bakatui debug: make-build-dir $(BIN_PATH)/bakatui
asan: CPPC_FLAGS = $(DEBUG_ASANITIZE)
asan: make-build-dir $(BIN_PATH)/bakatui
make-build-dir: make-build-dir:
mkdir -p $(OBJ_PATH) mkdir -p $(OBJ_PATH)
@ -39,4 +43,4 @@ install:
clean: clean:
rm -fr build rm -fr build
.PHONY: all clean install debug .PHONY: all clean install debug asan

View File

@ -6,6 +6,7 @@
#include <algorithm> #include <algorithm>
#include <codecvt> #include <codecvt>
#include <csignal> #include <csignal>
#include <cstring>
#include <curses.h> #include <curses.h>
#include <dirent.h> #include <dirent.h>
#include <filesystem> #include <filesystem>
@ -207,6 +208,11 @@ char *wchar_to_char(const wchar_t *src) {
size_t len = wcslen(src) + 1; // +1 for null terminator size_t len = wcslen(src) + 1; // +1 for null terminator
char *dest = new char[len * MB_CUR_MAX]; char *dest = new char[len * MB_CUR_MAX];
current_allocated->push_back(allocation{
GENERIC_ARRAY,
dest,
len * MB_CUR_MAX,
});
std::wcstombs(dest, src, len * MB_CUR_MAX); std::wcstombs(dest, src, len * MB_CUR_MAX);
return dest; return dest;

View File

@ -26,22 +26,28 @@ using nlohmann::json;
std::vector<allocation> komens_allocated; std::vector<allocation> komens_allocated;
void insert_content(WINDOW *content_win, WINDOW *attachment_win, void insert_content(WINDOW *content_win, WINDOW *attachment_win,
json &resp_from_api); const json &resp_from_api);
void komens_print_usage_message() {
attron(COLOR_PAIR(COLOR_BLUE));
mvprintw(LINES - 2, 0,
"Use PageUp and PageDown to scoll down or up a page of items");
mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(COLOR_BLUE));
refresh();
}
void komens_page(koment_type type) { void komens_page(koment_type type) {
current_allocated = &komens_allocated; current_allocated = &komens_allocated;
json resp_from_api;
{ const json resp_from_api = [&]() -> json {
/*std::ifstream f("test-data/komens.json");
resp_from_api = json::parse(f);
f.close();*/
const char *types[] = {"/api/3/komens/messages/received", const char *types[] = {"/api/3/komens/messages/received",
"/api/3/komens/messages/sent", "/api/3/komens/messages/sent",
"/api/3/komens/messages/noticeboard"}; "/api/3/komens/messages/noticeboard"};
const std::string endpoint = types[type]; const std::string endpoint = types[type];
resp_from_api = bakaapi::get_data_from_endpoint(endpoint, POST); return bakaapi::get_data_from_endpoint(endpoint, POST);
} }();
/* Initialize curses */ /* Initialize curses */
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
@ -57,14 +63,16 @@ void komens_page(koment_type type) {
} }
complete_menu komens_choise_menu; complete_menu komens_choise_menu;
komens_allocated.push_back({COMPLETE_MENU_TYPE, &komens_choise_menu, 1});
size_t num_of_komens = resp_from_api["Messages"].size(); size_t num_of_komens = resp_from_api["Messages"].size();
komens_choise_menu.items = new ITEM *[num_of_komens + 1]; komens_choise_menu.items = new ITEM *[num_of_komens + 1];
komens_allocated.push_back( komens_choise_menu.items_size = num_of_komens + 1;
{ITEM_ARRAY, komens_choise_menu.items, num_of_komens});
char **title_bufs = new char *[num_of_komens]; char **title_bufs = new char *[num_of_komens];
komens_allocated.push_back({CHAR_PTR_ARRAY, title_bufs, num_of_komens});
char **name_bufs = new char *[num_of_komens]; char **name_bufs = new char *[num_of_komens];
komens_allocated.push_back({CHAR_PTR_ARRAY, name_bufs, num_of_komens});
size_t max_item_lenght; size_t max_item_lenght;
{ {
size_t max_title_lenght = 0; size_t max_title_lenght = 0;
@ -72,10 +80,8 @@ void komens_page(koment_type type) {
size_t tmp_lenght; size_t tmp_lenght;
char tmp_buf[1500]; char tmp_buf[1500];
for (size_t i = 0; i < num_of_komens; i++) { for (size_t i = 0; i < num_of_komens; i++) {
wcstombs(tmp_buf, strlcpy(tmp_buf,
string_to_wstring( resp_from_api["Messages"][i]["Title"].get<std::string>().c_str(),
resp_from_api["Messages"][i]["Title"].get<std::string>())
.c_str(),
sizeof(tmp_buf)); sizeof(tmp_buf));
tmp_lenght = tmp_lenght =
@ -87,10 +93,10 @@ void komens_page(koment_type type) {
title_bufs[i] = new char[strlen(tmp_buf) + 1]; title_bufs[i] = new char[strlen(tmp_buf) + 1];
strlcpy(title_bufs[i], tmp_buf, strlen(tmp_buf) + 1); strlcpy(title_bufs[i], tmp_buf, strlen(tmp_buf) + 1);
wcstombs(
tmp_buf, strlcpy(tmp_buf,
string_to_wstring( resp_from_api["Messages"][i]["Sender"]["Name"]
resp_from_api["Messages"][i]["Sender"]["Name"].get<std::string>()) .get<std::string>()
.c_str(), .c_str(),
sizeof(tmp_buf)); sizeof(tmp_buf));
@ -112,7 +118,6 @@ void komens_page(koment_type type) {
komens_choise_menu.items[num_of_komens] = nullptr; komens_choise_menu.items[num_of_komens] = nullptr;
komens_choise_menu.menu = new_menu(komens_choise_menu.items); komens_choise_menu.menu = new_menu(komens_choise_menu.items);
komens_allocated.push_back({MENU_TYPE, komens_choise_menu.menu, 1});
komens_choise_menu.win = komens_choise_menu.win =
newwin(MAIN_WIN_HIGHT, max_item_lenght + 1, DEFAULT_OFSET, DEFAULT_OFSET); newwin(MAIN_WIN_HIGHT, max_item_lenght + 1, DEFAULT_OFSET, DEFAULT_OFSET);
@ -143,17 +148,13 @@ void komens_page(koment_type type) {
komens_allocated.push_back({WINDOW_TYPE, content_win, 1}); komens_allocated.push_back({WINDOW_TYPE, content_win, 1});
WINDOW *attachment_win = newwin(1, 1, LINES, COLS); WINDOW *attachment_win = newwin(1, 1, LINES, COLS);
komens_allocated.push_back({WINDOW_TYPE, attachment_win, 1});
insert_content(content_win, attachment_win, insert_content(content_win, attachment_win,
resp_from_api["Messages"][item_index( resp_from_api["Messages"][item_index(
current_item(komens_choise_menu.menu))]); current_item(komens_choise_menu.menu))]);
attron(COLOR_PAIR(COLOR_BLUE)); komens_print_usage_message();
mvprintw(LINES - 2, 0,
"Use PageUp and PageDown to scoll down or up a page of items");
mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(COLOR_BLUE));
refresh();
int c; int c;
while ((c = getch()) != KEY_F(1)) { while ((c = getch()) != KEY_F(1)) {
@ -204,6 +205,7 @@ void komens_page(koment_type type) {
komens_choise_menu.menu))]["Attachments"][index]["Id"] komens_choise_menu.menu))]["Attachments"][index]["Id"]
.get<std::string>(), .get<std::string>(),
path); path);
komens_print_usage_message();
} }
} }
break; break;
@ -214,11 +216,13 @@ void komens_page(koment_type type) {
current_item(komens_choise_menu.menu))]); current_item(komens_choise_menu.menu))]);
wrefresh(komens_choise_menu.win); wrefresh(komens_choise_menu.win);
} }
unpost_menu(komens_choise_menu.menu);
endwin();
delete_all(&komens_allocated); delete_all(&komens_allocated);
} }
void insert_content(WINDOW *content_win, WINDOW *attachment_win, void insert_content(WINDOW *content_win, WINDOW *attachment_win,
json &message) { const json &message) {
wclear(content_win); wclear(content_win);
mvwprintw(content_win, 0, 0, "%s", mvwprintw(content_win, 0, 0, "%s",
html_to_string(message.at("Text")).c_str()); html_to_string(message.at("Text")).c_str());

View File

@ -1,6 +1,7 @@
#include "komens_menu.h" #include "komens_menu.h"
#include "helper_funcs.h" #include "helper_funcs.h"
#include "komens.h" #include "komens.h"
#include "memory.h"
#include "net.h" #include "net.h"
#include "types.h" #include "types.h"
#include <cstdlib> #include <cstdlib>
@ -8,15 +9,19 @@
#include <curses.h> #include <curses.h>
#include <menu.h> #include <menu.h>
std::vector<allocation> komens_menu_allocated;
void komens_menu() { void komens_menu() {
current_allocated = &komens_menu_allocated;
wchar_t *choices[] = { wchar_t *choices[] = {
L"received", L"sent", L"noticeboard", L"Exit", nullptr, L"received", L"sent", L"noticeboard", L"Exit", nullptr,
}; };
complete_menu komens_menu; complete_menu komens_menu;
komens_menu_allocated.push_back({COMPLETE_MENU_TYPE, &komens_menu, 1});
int c; int c;
int n_choices, i; int n_choices;
/* Initialize curses */ /* Initialize curses */
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
@ -30,8 +35,11 @@ void komens_menu() {
/* Create items */ /* Create items */
n_choices = ARRAY_SIZE(choices); n_choices = ARRAY_SIZE(choices);
komens_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); komens_menu.items = new ITEM *[ARRAY_SIZE(choices)];
for (i = 0; i < n_choices; ++i) komens_menu.items_size = ARRAY_SIZE(choices);
for (int i = 0; i < n_choices; ++i)
komens_menu.items[i] = komens_menu.items[i] =
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i])); new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
@ -89,6 +97,7 @@ void komens_menu() {
} }
komens_page( komens_page(
static_cast<koment_type>(item_index(current_item(komens_menu.menu)))); static_cast<koment_type>(item_index(current_item(komens_menu.menu))));
current_allocated = &komens_menu_allocated;
pos_menu_cursor(komens_menu.menu); pos_menu_cursor(komens_menu.menu);
refresh(); refresh();
wrefresh(komens_menu.win); wrefresh(komens_menu.win);
@ -100,8 +109,6 @@ close_menu:
/* Unpost and free all the memory taken up */ /* Unpost and free all the memory taken up */
unpost_menu(komens_menu.menu); unpost_menu(komens_menu.menu);
free_menu(komens_menu.menu); delete_all(&komens_menu_allocated);
for (i = 0; i < n_choices; ++i)
free_item(komens_menu.items[i]);
endwin(); endwin();
} }

View File

@ -1,16 +1,21 @@
#include "helper_funcs.h" #include "helper_funcs.h"
#include "komens_menu.h" #include "komens_menu.h"
#include "marks.h" #include "marks.h"
#include "memory.h"
#include "net.h" #include "net.h"
#include "timetable.h" #include "timetable.h"
#include "types.h" #include "types.h"
#include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <curses.h> #include <curses.h>
#include <menu.h> #include <menu.h>
std::vector<allocation> main_menu_allocated;
void main_menu() { void main_menu() {
wchar_t *choices[] = { current_allocated = &main_menu_allocated;
const wchar_t *choices[] = {
L"login", L"Marks", L"timetable", L"Komens", L"login", L"Marks", L"timetable", L"Komens",
L"Homework", L"Absence", L"Exit", nullptr, L"Homework", L"Absence", L"Exit", nullptr,
}; };
@ -18,9 +23,7 @@ void main_menu() {
nullptr, nullptr, nullptr, nullptr}; nullptr, nullptr, nullptr, nullptr};
complete_menu main_menu; complete_menu main_menu;
main_menu_allocated.push_back({COMPLETE_MENU_TYPE, &main_menu, 1});
int c;
int n_choices, i;
/* Initialize curses */ /* Initialize curses */
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
@ -33,14 +36,15 @@ void main_menu() {
init_pair(2, COLOR_CYAN, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* Create items */ /* Create items */
n_choices = ARRAY_SIZE(choices);
main_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); main_menu.items = new ITEM *[ARRAY_SIZE(choices)];
for (i = 0; i < n_choices; ++i) main_menu.items_size = ARRAY_SIZE(choices);
for (size_t i = 0; i < ARRAY_SIZE(choices); ++i) {
main_menu.items[i] = main_menu.items[i] =
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i])); new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
}
/* Crate menu */ /* Crate menu */
main_menu.menu = new_menu((ITEM **)main_menu.items); main_menu.menu = new_menu(main_menu.items);
/* Create the window to be associated with the menu */ /* Create the window to be associated with the menu */
main_menu.win = newwin(12, 40, 4, 4); main_menu.win = newwin(12, 40, 4, 4);
@ -73,6 +77,7 @@ void main_menu() {
attroff(COLOR_PAIR(2)); attroff(COLOR_PAIR(2));
refresh(); refresh();
int c;
while ((c = getch()) != KEY_F(1)) { while ((c = getch()) != KEY_F(1)) {
switch (c) { switch (c) {
case KEY_DOWN: case KEY_DOWN:
@ -88,10 +93,11 @@ void main_menu() {
break; break;
case 10: // ENTER case 10: // ENTER
clear(); clear();
if (item_index(current_item(main_menu.menu)) == n_choices - 1) { if (item_index(current_item(main_menu.menu)) == ARRAY_SIZE(choices) - 1) {
goto close_menu; goto close_menu;
} }
choicesFuncs[item_index(current_item(main_menu.menu))](); choicesFuncs[item_index(current_item(main_menu.menu))]();
current_allocated = &main_menu_allocated;
pos_menu_cursor(main_menu.menu); pos_menu_cursor(main_menu.menu);
refresh(); refresh();
wrefresh(main_menu.win); wrefresh(main_menu.win);
@ -103,8 +109,6 @@ close_menu:
/* Unpost and free all the memory taken up */ /* Unpost and free all the memory taken up */
unpost_menu(main_menu.menu); unpost_menu(main_menu.menu);
free_menu(main_menu.menu); delete_all(&main_menu_allocated);
for (i = 0; i < n_choices; ++i)
free_item(main_menu.items[i]);
endwin(); endwin();
} }

View File

@ -31,22 +31,25 @@ using nlohmann::json;
std::vector<allocation> marks_allocated; std::vector<allocation> marks_allocated;
void init_wins(WINDOW **wins, int n, json marks_json); void init_wins(WINDOW **wins, const int n, const json &marks_json);
void win_show(WINDOW *win, wchar_t *label, int label_color, int width, void win_show(WINDOW *win, const wchar_t *label, const int label_color,
int height, json marks_json, int SubjectIndex); int width, int height, const json &marks_json,
const int SubjectIndex);
void marks_page() { void marks_page() {
current_allocated = &marks_allocated; current_allocated = &marks_allocated;
json resp_from_api;
{ // thanks to lambda i can make this const
std::string endpoint = "api/3/marks"; const json resp_from_api = [&]() -> json {
resp_from_api = bakaapi::get_data_from_endpoint(endpoint, GET); const std::string endpoint = "api/3/marks";
} return bakaapi::get_data_from_endpoint(endpoint, GET);
size_t size_my_wins = resp_from_api["Subjects"].size(); }();
const size_t size_my_wins = resp_from_api["Subjects"].size();
WINDOW **my_wins = new (std::nothrow) WINDOW *[size_my_wins]; WINDOW **my_wins = new (std::nothrow) WINDOW *[size_my_wins];
marks_allocated.push_back({WINDOW_ARRAY, my_wins, size_my_wins}); marks_allocated.push_back({WINDOW_ARRAY, my_wins, size_my_wins});
size_t size_my_panels = resp_from_api["Subjects"].size(); const size_t size_my_panels = size_my_wins;
PANEL **my_panels = new (std::nothrow) PANEL *[size_my_panels]; PANEL **my_panels = new (std::nothrow) PANEL *[size_my_panels];
marks_allocated.push_back({PANEL_ARRAY, my_panels, size_my_panels}); marks_allocated.push_back({PANEL_ARRAY, my_panels, size_my_panels});
@ -132,7 +135,7 @@ void marks_page() {
} }
/* Put all the windows */ /* Put all the windows */
void init_wins(WINDOW **wins, int n, json marks_json) { void init_wins(WINDOW **wins, const int n, const json &marks_json) {
int x, y, i; int x, y, i;
wchar_t label[1500]; wchar_t label[1500];
@ -145,12 +148,12 @@ void init_wins(WINDOW **wins, int n, json marks_json) {
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
// Calculate label and max_text_length to determine window width // Calculate label and max_text_length to determine window width
std::string sub_name = marks_json["Subjects"][i]["Subject"]["Name"]; const std::string sub_name = marks_json["Subjects"][i]["Subject"]["Name"];
std::string sub_avg_s = marks_json["Subjects"][i]["AverageText"]; const std::string sub_avg_s = marks_json["Subjects"][i]["AverageText"];
// Convert to wchar_t // Convert to wchar_t
std::wstring wsub_name = string_to_wstring(sub_name); const std::wstring wsub_name = string_to_wstring(sub_name);
std::wstring wsub_avg_s = string_to_wstring(sub_avg_s); const std::wstring wsub_avg_s = string_to_wstring(sub_avg_s);
// Using swprintf for wide character formatting // Using swprintf for wide character formatting
swprintf(label, sizeof(label) / sizeof(label[0]), L"%ls - avg: %ls", swprintf(label, sizeof(label) / sizeof(label[0]), L"%ls - avg: %ls",
@ -160,17 +163,18 @@ void init_wins(WINDOW **wins, int n, json marks_json) {
for (unsigned int j = 0; j < static_cast<unsigned int>( for (unsigned int j = 0; j < static_cast<unsigned int>(
marks_json["Subjects"][i]["Marks"].size()); marks_json["Subjects"][i]["Marks"].size());
j++) { j++) {
std::string caption = const std::string caption =
rm_tr_le_whitespace(marks_json["Subjects"][i]["Marks"][j]["Caption"]); rm_tr_le_whitespace(marks_json["Subjects"][i]["Marks"][j]["Caption"]);
std::string theme = const std::string theme =
rm_tr_le_whitespace(marks_json["Subjects"][i]["Marks"][j]["Theme"]); rm_tr_le_whitespace(marks_json["Subjects"][i]["Marks"][j]["Theme"]);
std::wstring wcaption = string_to_wstring(caption); const std::wstring wcaption = string_to_wstring(caption);
std::wstring wtheme = string_to_wstring(theme); const std::wstring wtheme = string_to_wstring(theme);
// Some code that does something and fixes some edge cases // Some code that does something and fixes some edge cases
std::string testCaption = caption + std::format(" {{{}}} [{}]", "X", 0); const std::string testCaption =
std::wstring wTestCaption = string_to_wstring(testCaption); caption + std::format(" {{{}}} [{}]", "X", 0);
const std::wstring wTestCaption = string_to_wstring(testCaption);
max_text_length = max_text_length =
std::max({max_text_length, wTestCaption.length(), wtheme.length()}); std::max({max_text_length, wTestCaption.length(), wtheme.length()});
} }
@ -203,8 +207,9 @@ void init_wins(WINDOW **wins, int n, json marks_json) {
} }
/* Show the window with a border and a label */ /* Show the window with a border and a label */
void win_show(WINDOW *win, wchar_t *label, int label_color, int width, void win_show(WINDOW *win, const wchar_t *label, const int label_color,
int height, json marks_json, int SubjectIndex) { int width, int height, const json &marks_json,
const int SubjectIndex) {
// is the compiler smoking weed or something, why is it thinking starty is not // is the compiler smoking weed or something, why is it thinking starty is not
// used ?? // used ??

View File

@ -1,9 +1,34 @@
#include "memory.h" #include "memory.h"
#include "color.h" #include "color.h"
#include "types.h"
#include <curses.h>
#include <iostream> #include <iostream>
#include <menu.h>
#include <ncurses.h> #include <ncurses.h>
#include <panel.h> #include <panel.h>
template <typename T> struct NcursesDeleter {
static void delete_element(T obj);
};
template <> void NcursesDeleter<WINDOW *>::delete_element(WINDOW *win) {
delwin(win);
}
template <> void NcursesDeleter<PANEL *>::delete_element(PANEL *pan) {
del_panel(pan);
}
template <> void NcursesDeleter<ITEM *>::delete_element(ITEM *item) {
free_item(item);
}
template <typename T> void delete_ncurses_arrays(void *ptr, std::size_t size) {
T *array = static_cast<T *>(ptr);
for (std::size_t j = 0; j < size; ++j) {
NcursesDeleter<T>::delete_element(array[j]);
}
delete[] array;
}
void delete_all(std::vector<allocation> *allocated) { void delete_all(std::vector<allocation> *allocated) {
if (allocated == nullptr) { if (allocated == nullptr) {
return; return;
@ -11,27 +36,26 @@ void delete_all(std::vector<allocation> *allocated) {
for (long long i = allocated->size() - 1; i >= 0; i--) { for (long long i = allocated->size() - 1; i >= 0; i--) {
switch (allocated->at(i).type) { switch (allocated->at(i).type) {
case WINDOW_ARRAY: { case WINDOW_ARRAY: {
WINDOW **windows = static_cast<WINDOW **>(allocated->at(i).ptr); delete_ncurses_arrays<WINDOW *>(allocated->at(i).ptr,
for (std::size_t j = 0; j < allocated->at(i).size; j++) { allocated->at(i).size);
delwin(windows[j]);
}
delete[] windows;
break; break;
} }
case PANEL_ARRAY: { case PANEL_ARRAY: {
PANEL **panels = static_cast<PANEL **>(allocated->at(i).ptr); delete_ncurses_arrays<PANEL *>(allocated->at(i).ptr,
for (std::size_t j = 0; j < allocated->at(i).size; j++) { allocated->at(i).size);
del_panel(panels[j]);
}
delete[] panels;
break; break;
} }
case ITEM_ARRAY: { case ITEM_ARRAY: {
ITEM **items = static_cast<ITEM **>(allocated->at(i).ptr); delete_ncurses_arrays<ITEM *>(allocated->at(i).ptr,
for (std::size_t j = 0; j < allocated->at(i).size; j++) { allocated->at(i).size);
free_item(items[j]); break;
} }
delete[] items; case CHAR_PTR_ARRAY: {
char **array = static_cast<char **>(allocated->at(i).ptr);
for (std::size_t j = 0; j < allocated->at(i).size; ++j) {
delete[] array[j];
}
delete[] array;
break; break;
} }
case GENERIC_ARRAY: case GENERIC_ARRAY:
@ -46,6 +70,15 @@ void delete_all(std::vector<allocation> *allocated) {
case MENU_TYPE: case MENU_TYPE:
free_menu(static_cast<MENU *>(allocated->at(i).ptr)); free_menu(static_cast<MENU *>(allocated->at(i).ptr));
break; break;
case COMPLETE_MENU_TYPE: {
free_menu(static_cast<complete_menu *>(allocated->at(i).ptr)->menu);
delwin(static_cast<complete_menu *>(allocated->at(i).ptr)->win);
delete_ncurses_arrays<ITEM *>(
static_cast<complete_menu *>(allocated->at(i).ptr)->items,
static_cast<complete_menu *>(allocated->at(i).ptr)->items_size);
break;
}
case GENERIC_TYPE: case GENERIC_TYPE:
delete static_cast<char *>(allocated->at(i).ptr); delete static_cast<char *>(allocated->at(i).ptr);
break; break;

View File

@ -9,10 +9,12 @@ enum AllocationType {
WINDOW_ARRAY, WINDOW_ARRAY,
PANEL_ARRAY, PANEL_ARRAY,
ITEM_ARRAY, ITEM_ARRAY,
CHAR_PTR_ARRAY,
GENERIC_ARRAY, GENERIC_ARRAY,
WINDOW_TYPE, WINDOW_TYPE,
PANEL_TYPE, PANEL_TYPE,
MENU_TYPE, MENU_TYPE,
COMPLETE_MENU_TYPE,
GENERIC_TYPE GENERIC_TYPE
}; };

View File

@ -9,6 +9,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/header.h>
#include <curses.h> #include <curses.h>
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
@ -111,11 +112,8 @@ std::tuple<std::string, int> send_curl_request(
safe_exit(EINVAL); safe_exit(EINVAL);
} }
} else {
std::cerr << RED "[ERROR] " << RESET "curl not initialised\n";
safe_exit(20);
}
CURLcode curl_return_code = curl_easy_perform(curl); CURLcode curl_return_code = curl_easy_perform(curl);
curl_slist_free_all(headers);
if (curl_return_code != CURLE_OK) { if (curl_return_code != CURLE_OK) {
std::cerr << RED "[ERROR] " << RESET << "curl_easy_perform() failed: " std::cerr << RED "[ERROR] " << RESET << "curl_easy_perform() failed: "
<< curl_easy_strerror(curl_return_code) << "\n"; << curl_easy_strerror(curl_return_code) << "\n";
@ -129,6 +127,14 @@ std::tuple<std::string, int> send_curl_request(
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
return {response, http_code}; return {response, http_code};
} else {
std::cerr << RED "[ERROR] " << RESET "curl not initialised\n";
safe_exit(20);
// prevent compiler warning
return {"", -1};
}
} }
namespace bakaapi { namespace bakaapi {

View File

@ -32,16 +32,18 @@ const wchar_t *day_abriviations[] = {nullptr, L"Mo", L"Tu", L"We",
L"Th", L"Fr", L"Sa", L"Su"}; L"Th", L"Fr", L"Sa", L"Su"};
void draw_days(WINDOW **&day_windows, uint16_t cell_height, uint8_t num_of_days, void draw_days(WINDOW **&day_windows, uint16_t cell_height, uint8_t num_of_days,
json &resp_from_api); const json &resp_from_api);
void draw_lessons(WINDOW **&lesson_windows, uint8_t num_of_columns, void draw_lessons(WINDOW **&lesson_windows, uint8_t num_of_columns,
uint16_t cell_width, std::vector<uint8_t> &HourIdLookupTable, uint16_t cell_width,
json &resp_from_api); const std::vector<uint8_t> &HourIdLookupTable,
const json &resp_from_api);
void draw_cells(uint8_t num_of_columns, uint8_t num_of_days, void draw_cells(uint8_t num_of_columns, uint8_t num_of_days,
uint16_t cell_width, uint16_t cell_height, uint16_t cell_width, uint16_t cell_height,
std::vector<std::vector<WINDOW *>> &cells, std::vector<std::vector<WINDOW *>> &cells,
std::vector<uint8_t> &HourIdLookupTable, json &resp_from_api); const std::vector<uint8_t> &HourIdLookupTable,
const json &resp_from_api);
uint8_t hour_id_to_index(const std::vector<uint8_t> &HourIdLookupTable, uint8_t hour_id_to_index(const std::vector<uint8_t> &HourIdLookupTable,
uint8_t id) { uint8_t id) {
@ -53,8 +55,8 @@ uint8_t hour_id_to_index(const std::vector<uint8_t> &HourIdLookupTable,
return 0; return 0;
} }
json *partial_json_by_id(json &resp_from_api, const std::string &what, const json *partial_json_by_id(const json &resp_from_api,
const std::string &id) { const std::string &what, const std::string &id) {
for (uint8_t i = 0; i < resp_from_api[what].size(); i++) { for (uint8_t i = 0; i < resp_from_api[what].size(); i++) {
if (resp_from_api[what][i]["Id"].get<std::string>() == id) { if (resp_from_api[what][i]["Id"].get<std::string>() == id) {
return &resp_from_api[what][i]; return &resp_from_api[what][i];
@ -63,7 +65,7 @@ json *partial_json_by_id(json &resp_from_api, const std::string &what,
return nullptr; return nullptr;
} }
std::wstring get_data_for_atom(json &resp_from_api, json *atom, std::wstring get_data_for_atom(const json &resp_from_api, const json *atom,
const std::string &from_where, const std::string &from_where,
const std::string &id_key, const std::string &id_key,
const std::string &what) { const std::string &what) {
@ -74,7 +76,8 @@ std::wstring get_data_for_atom(json &resp_from_api, json *atom,
.get<std::string>()); .get<std::string>());
} }
json *find_atom_by_indexes(json &resp_from_api, uint8_t day_index, const json *
find_atom_by_indexes(const json &resp_from_api, uint8_t day_index,
uint8_t hour_index, uint8_t hour_index,
const std::vector<uint8_t> &HourIdLookupTable) { const std::vector<uint8_t> &HourIdLookupTable) {
for (uint8_t k = 0; k < resp_from_api["Days"][day_index]["Atoms"].size(); for (uint8_t k = 0; k < resp_from_api["Days"][day_index]["Atoms"].size();
@ -91,6 +94,7 @@ void timetable_page() {
current_allocated = &timetable_allocated; current_allocated = &timetable_allocated;
auto dateSelected = std::chrono::system_clock::now(); auto dateSelected = std::chrono::system_clock::now();
reload_for_new_week: reload_for_new_week:
clear();
std::time_t date_time_t = std::chrono::system_clock::to_time_t(dateSelected); std::time_t date_time_t = std::chrono::system_clock::to_time_t(dateSelected);
std::tm local_time = *std::localtime(&date_time_t); std::tm local_time = *std::localtime(&date_time_t);
@ -100,7 +104,7 @@ reload_for_new_week:
std::string date_string = "date=" + date_stringstream.str(); std::string date_string = "date=" + date_stringstream.str();
std::string endpoint = "api/3/timetable/actual"; std::string endpoint = "api/3/timetable/actual";
json resp_from_api = const json resp_from_api =
bakaapi::get_data_from_endpoint(endpoint, GET, date_string); bakaapi::get_data_from_endpoint(endpoint, GET, date_string);
// this may be unnecessary but i dont have enaugh data to test it // this may be unnecessary but i dont have enaugh data to test it
@ -384,8 +388,8 @@ reload_for_new_week:
goto reload_for_new_week; goto reload_for_new_week;
break; break;
case 10: // ENTER case 10: // ENTER
json *atom = find_atom_by_indexes(resp_from_api, selected_cell.y, const json *atom = find_atom_by_indexes(
selected_cell.x, HourIdLookupTable); resp_from_api, selected_cell.y, selected_cell.x, HourIdLookupTable);
if (atom == nullptr) { if (atom == nullptr) {
std::cerr << RED "[ERROR]" << RESET " Selector at invalid position\n"; std::cerr << RED "[ERROR]" << RESET " Selector at invalid position\n";
safe_exit(129); safe_exit(129);
@ -535,7 +539,7 @@ reload_for_new_week:
} }
void draw_days(WINDOW **&day_windows, uint16_t cell_height, uint8_t num_of_days, void draw_days(WINDOW **&day_windows, uint16_t cell_height, uint8_t num_of_days,
json &resp_from_api) { const json &resp_from_api) {
for (uint8_t i = 0; i < num_of_days; i++) { for (uint8_t i = 0; i < num_of_days; i++) {
// this wont draw left boarder window making it so it looks partially // this wont draw left boarder window making it so it looks partially
// offscreen // offscreen
@ -549,8 +553,9 @@ void draw_days(WINDOW **&day_windows, uint16_t cell_height, uint8_t num_of_days,
} }
void draw_lessons(WINDOW **&lesson_windows, uint8_t num_of_columns, void draw_lessons(WINDOW **&lesson_windows, uint8_t num_of_columns,
uint16_t cell_width, std::vector<uint8_t> &HourIdLookupTable, uint16_t cell_width,
json &resp_from_api) { const std::vector<uint8_t> &HourIdLookupTable,
const json &resp_from_api) {
for (uint8_t i = 0; i < num_of_columns; i++) { for (uint8_t i = 0; i < num_of_columns; i++) {
wborder(lesson_windows[i], 0, 0, ' ', 0, ACS_VLINE, ACS_VLINE, 0, 0); wborder(lesson_windows[i], 0, 0, ' ', 0, ACS_VLINE, ACS_VLINE, 0, 0);
std::wstring caption; std::wstring caption;
@ -594,11 +599,13 @@ void draw_lessons(WINDOW **&lesson_windows, uint8_t num_of_columns,
void draw_cells(uint8_t num_of_columns, uint8_t num_of_days, void draw_cells(uint8_t num_of_columns, uint8_t num_of_days,
uint16_t cell_width, uint16_t cell_height, uint16_t cell_width, uint16_t cell_height,
std::vector<std::vector<WINDOW *>> &cells, std::vector<std::vector<WINDOW *>> &cells,
std::vector<uint8_t> &HourIdLookupTable, json &resp_from_api) { const std::vector<uint8_t> &HourIdLookupTable,
const json &resp_from_api) {
for (uint8_t i = 0; i < num_of_days; i++) { for (uint8_t i = 0; i < num_of_days; i++) {
for (uint8_t j = 0; j < num_of_columns; j++) { for (uint8_t j = 0; j < num_of_columns; j++) {
json *atom = find_atom_by_indexes(resp_from_api, i, j, HourIdLookupTable); const json *atom =
find_atom_by_indexes(resp_from_api, i, j, HourIdLookupTable);
if (atom == nullptr) { if (atom == nullptr) {
continue; continue;
} }

View File

@ -148,6 +148,7 @@ struct SelectorType {
struct complete_menu { struct complete_menu {
WINDOW *win; WINDOW *win;
ITEM **items; ITEM **items;
size_t items_size;
MENU *menu; MENU *menu;
}; };