add memory safety
/ sync-to-origin (push) Waiting to run

This commit is contained in:
2025-03-31 20:26:56 +02:00
parent dfa0774242
commit 096b1d1eb6
6 changed files with 171 additions and 67 deletions
+64 -49
View File
@@ -2,6 +2,7 @@
#include "color.h"
#include "const.h"
#include "helper_funcs.h"
#include "memory.h"
#include "net.h"
#include "types.h"
#include <bits/chrono.h>
@@ -20,13 +21,13 @@
#include <sys/types.h>
#include <vector>
using nlohmann::json;
#define BOTTOM_PADDING 5
#define DEFAULT_OFFSET 3
std::vector<allocation> timetable_allocated;
const wchar_t *day_abriviations[] = {nullptr, L"Mo", L"Tu", L"We",
L"Th", L"Fr", L"Sa", L"Su"};
@@ -87,20 +88,19 @@ json *find_atom_by_indexes(json &resp_from_api, uint8_t day_index,
}
void timetable_page() {
auto dateSelected = std::chrono::system_clock::now();
reload_for_new_week:
std::time_t date_time_t = std::chrono::system_clock::to_time_t(dateSelected);
std::tm local_time = *std::localtime(&date_time_t);
current_allocated = &timetable_allocated;
auto dateSelected = std::chrono::system_clock::now();
reload_for_new_week:
std::time_t date_time_t = std::chrono::system_clock::to_time_t(dateSelected);
std::tm local_time = *std::localtime(&date_time_t);
std::stringstream date_stringstream;
date_stringstream << std::put_time(&local_time, "%Y-%m-%d");
std::stringstream date_stringstream;
date_stringstream << std::put_time(&local_time, "%Y-%m-%d");
std::string date_string = "date=" + date_stringstream.str();
std::string date_string = "date=" + date_stringstream.str();
std::string endpoint = "api/3/timetable/actual";
json resp_from_api =
bakaapi::get_data_from_endpoint(endpoint, date_string);
json resp_from_api = bakaapi::get_data_from_endpoint(endpoint, date_string);
// this may be unnecessary but i dont have enaugh data to test it
// it sorts the hours by start time
@@ -111,6 +111,8 @@ void timetable_page() {
Id_and_Start_time *temp_hour_sorting_array =
new Id_and_Start_time[resp_from_api["Hours"].size()];
timetable_allocated.push_back({GENERIC_ARRAY, temp_hour_sorting_array,
resp_from_api["Hours"].size()});
for (uint8_t i = 0; i < resp_from_api["Hours"].size(); i++) {
temp_hour_sorting_array[i] = std::make_tuple(
@@ -155,6 +157,7 @@ void timetable_page() {
}
delete[] temp_hour_sorting_array;
timetable_allocated.pop_back();
}
// some lambda dark magic
@@ -194,7 +197,11 @@ void timetable_page() {
const uint16_t cell_height = (LINES - BOTTOM_PADDING) / num_of_days;
WINDOW **day_windows = new WINDOW *[num_of_days];
timetable_allocated.push_back({WINDOW_ARRAY, day_windows, num_of_days});
WINDOW **lesson_windows = new WINDOW *[num_of_columns];
timetable_allocated.push_back({WINDOW_ARRAY, lesson_windows, num_of_columns});
std::vector<std::vector<WINDOW *>> cells(
num_of_days, std::vector<WINDOW *>(num_of_columns));
@@ -256,7 +263,11 @@ void timetable_page() {
selector_windows[i] =
newwin(1, 1, DEFAULT_OFFSET + y_offset, DEFAULT_OFFSET + x_offset);
timetable_allocated.push_back({WINDOW_TYPE, selector_windows[i], 1});
selector_panels[i] = new_panel(selector_windows[i]);
timetable_allocated.push_back({PANEL_TYPE, selector_panels[i], 1});
wattron(selector_windows[i], COLOR_PAIR(COLOR_RED));
mvwaddch(selector_windows[i], 0, 0, corners[i]);
wattroff(selector_windows[i], COLOR_PAIR(COLOR_RED));
@@ -264,45 +275,50 @@ void timetable_page() {
}
attron(COLOR_PAIR(COLOR_BLUE));
mvprintw(LINES - 2, 0,
"Arrows/hjkl to select | ENTER to show info | p/n to select weeks |F1 to exit");
"Arrows/hjkl to select | ENTER to show info | p/n to select weeks "
"|F1 to exit");
{
std::tm end_week = local_time;
std::tm start_week = local_time;
std::tm start_week = local_time;
// Convert tm_wday (0-6) to API day format (1-7)
int current_wday = (local_time.tm_wday == 0) ? 7 : local_time.tm_wday;
// Convert tm_wday (0-6) to API day format (1-7)
int current_wday = (local_time.tm_wday == 0) ? 7 : local_time.tm_wday;
// Get days of week from API (1-7 format where Monday is 1)
uint8_t start_day = resp_from_api["Days"][0]["DayOfWeek"].get<uint8_t>();
uint8_t end_day = resp_from_api["Days"][resp_from_api["Days"].size() - 1]["DayOfWeek"].get<uint8_t>();
// Get days of week from API (1-7 format where Monday is 1)
uint8_t start_day = resp_from_api["Days"][0]["DayOfWeek"].get<uint8_t>();
uint8_t end_day =
resp_from_api["Days"][resp_from_api["Days"].size() - 1]["DayOfWeek"]
.get<uint8_t>();
// Calculate days back to start day (handles week wraparound)
int days_back = (current_wday >= start_day)
? (current_wday - start_day)
: (current_wday + 7 - start_day);
// Calculate days back to start day (handles week wraparound)
int days_back = (current_wday >= start_day)
? (current_wday - start_day)
: (current_wday + 7 - start_day);
// Calculate days forward to end day (handles week wraparound)
int days_forward = (current_wday <= end_day)
? (end_day - current_wday)
: (end_day + 7 - current_wday);
// Calculate days forward to end day (handles week wraparound)
int days_forward = (current_wday <= end_day) ? (end_day - current_wday)
: (end_day + 7 - current_wday);
// Adjust dates
start_week.tm_mday -= days_back;
end_week.tm_mday += days_forward;
// Adjust dates
start_week.tm_mday -= days_back;
end_week.tm_mday += days_forward;
// Normalize the dates
std::mktime(&start_week);
std::mktime(&end_week);
// Normalize the dates
std::mktime(&start_week);
std::mktime(&end_week);
// Format the dates as strings
std::stringstream start_week_strstream, end_week_strstream;
start_week_strstream << std::put_time(&start_week, "%d.%m.%Y");
end_week_strstream << std::put_time(&end_week, "%d.%m.%Y");
// kern. developer approved ↓↓
mvprintw(LINES - 2, COLS - (start_week_strstream.str().length() + 3 + end_week_strstream.str().length()),
"%s", (start_week_strstream.str() + " - " + end_week_strstream.str()).c_str());
// Format the dates as strings
std::stringstream start_week_strstream, end_week_strstream;
start_week_strstream << std::put_time(&start_week, "%d.%m.%Y");
end_week_strstream << std::put_time(&end_week, "%d.%m.%Y");
// kern. developer approved ↓↓
mvprintw(LINES - 2,
COLS - (start_week_strstream.str().length() + 3 +
end_week_strstream.str().length()),
"%s",
(start_week_strstream.str() + " - " + end_week_strstream.str())
.c_str());
}
attroff(COLOR_PAIR(COLOR_BLUE));
@@ -360,13 +376,12 @@ mvprintw(LINES - 2, COLS - (start_week_strstream.str().length() + 3 + end_week_s
selected_cell.x++;
break;
case 'p':
dateSelected = dateSelected - std::chrono::days(7);
goto reload_for_new_week;
break;
case 'n':
dateSelected = dateSelected + std::chrono::days(7);
(ch == 'p') ? dateSelected = dateSelected - std::chrono::days(7)
: dateSelected = dateSelected + std::chrono::days(7);
delete_all(&timetable_allocated);
goto reload_for_new_week;
break;
break;
case 10: // ENTER
json *atom = find_atom_by_indexes(resp_from_api, selected_cell.y,
selected_cell.x, HourIdLookupTable);
@@ -513,8 +528,8 @@ mvprintw(LINES - 2, COLS - (start_week_strstream.str().length() + 3 + end_week_s
}
}
}
delete[] day_windows;
delete[] lesson_windows;
delete_all(&timetable_allocated);
clear();
endwin();
}
@@ -661,4 +676,4 @@ void draw_cells(uint8_t num_of_columns, uint8_t num_of_days,
}
}
}
}
}