add flag handling and header guards

This commit is contained in:
PoliEcho 2025-03-06 14:12:10 +01:00
parent 0d11dbec32
commit 98b6b8df7e
13 changed files with 119 additions and 26 deletions

View File

@ -1,3 +1,7 @@
// Header guard
#ifndef RESET
#define RESET "\033[0m" #define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */ #define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */ #define RED "\033[31m" /* Red */
@ -15,3 +19,5 @@
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ #define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ #define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ #define BOLDWHITE "\033[1m\033[37m" /* Bold White */
#endif

View File

@ -1,2 +1,6 @@
#ifndef VERSION
#define VERSION "0.5" #define VERSION "0.5"
#define NAME "bakatui" #define NAME "bakatui"
#endif

24
src/flags.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "const.h"
#include <iostream>
#include "helper_funcs.h"
#include <cstdio>
void PrintHelp() {
std::cout << "Usage: " << NAME << " [OPTIONS]" << "\n"
<< "-h Show this help menu\n"
<< "-V Show version\n"
<< "-v verbose mode\n"
<< "-L Force new login\n"
<< "-S Ignore SSL cert validity\n";
safe_exit(0);
}
void PrintVersion() {
std::cout << NAME" " << VERSION"\n" << "License GPLv3: GNU GPL version 3 <https://gnu.org/licenses/gpl.html>.\n";
safe_exit(0);
}
void DeleteLogin(std::string savedir_path) {
std::remove((savedir_path + "/authfile").c_str());
std::remove((savedir_path + "/urlfile").c_str());
}

10
src/flags.h Normal file
View File

@ -0,0 +1,10 @@
#include <string>
// header guard
#ifndef _ba_fl_hg_
#define _ba_fl_hg_
void PrintHelp();
void PrintVersion();
void DeleteLogin(std::string savedir_path);
#endif

View File

@ -1,5 +1,10 @@
#include <curses.h> #include <curses.h>
#include <string> #include <string>
// header guard
#ifndef _ba_hf_hg_
#define _ba_hf_hg_
void safe_exit(int code); void safe_exit(int code);
std::string bool_to_string(bool bool_in); std::string bool_to_string(bool bool_in);
std::string SoRAuthFile(bool save, std::string data); std::string SoRAuthFile(bool save, std::string data);
@ -7,3 +12,5 @@ void get_input_and_login();
void print_in_middle(WINDOW *win, int starty, int startx, int width, void print_in_middle(WINDOW *win, int starty, int startx, int width,
char *string, chtype color); char *string, chtype color);
std::string rm_tr_le_whitespace(const std::string &s); std::string rm_tr_le_whitespace(const std::string &s);
#endif

View File

@ -2,7 +2,6 @@
#include "color.h" #include "color.h"
#include "helper_funcs.h" #include "helper_funcs.h"
#include "main_menu.h" #include "main_menu.h"
#include "marks.h"
#include "net.h" #include "net.h"
#include <csignal> #include <csignal>
#include <cstdlib> #include <cstdlib>
@ -13,17 +12,12 @@
#include <regex> #include <regex>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
#include "const.h" #include "flags.h"
#include "types.h"
std::string baka_api_url; std::string baka_api_url;
void PrintHelp() { Config config;
std::cout << "Usage: " << NAME << " [OPTIONS]" << "\n"
<< "-h Show this help menu\n"
<< "-v Show version\n"
<< "-L Force new login\n";
safe_exit(0);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
// signal handlers // signal handlers
@ -36,18 +30,22 @@ int main(int argc, char **argv) {
signal(SIGSEGV, safe_exit); signal(SIGSEGV, safe_exit);
{ {
int opt;
while ((opt = getopt(argc, argv, "vf:")) != -1) { // "f:" expects a value
switch (opt) {
case 'h': PrintHelp(); break;
case 'L': break;
case 'v': break;
default: /* handle error */;
}
}
std::string savedir_path = std::getenv("HOME"); std::string savedir_path = std::getenv("HOME");
savedir_path.append("/.local/share/bakatui"); savedir_path.append("/.local/share/bakatui");
int opt;
while ((opt = getopt(argc, argv, "hVvLS:")) != -1) {
switch (opt) {
case 'h': PrintHelp(); break;
case 'V': PrintVersion(); break;
case 'v': config.verbose = true; break;
case 'L': DeleteLogin(savedir_path); break;
case 'S': config.ignoressl = true; break;
default: std::cerr << RED"[ERROR]" << RESET" invalid option: " << (char)optopt << "\ntry: -h\n"; safe_exit(EINVAL);
}
}
std::string urlfile_path = std::string(savedir_path) + "/url"; std::string urlfile_path = std::string(savedir_path) + "/url";
std::ifstream urlfile; std::ifstream urlfile;
urlfile.open(urlfile_path); urlfile.open(urlfile_path);

View File

@ -1,4 +1,11 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <string> #include <string>
#include "types.h"
// header guard
#ifndef _ba_ma_hg_
#define _ba_ma_hg_
extern std::string baka_api_url; extern std::string baka_api_url;
extern Config config;
#endif

View File

@ -1 +1,7 @@
// header guard
#ifndef _ba_mm_hg_
#define _ba_mm_hg_
void main_menu(); void main_menu();
#endif

View File

@ -1 +1,5 @@
// header guard
#ifndef _ba_mp_hg_
#define _ba_mp_hg_
void marks_page(); void marks_page();
#endif

View File

@ -1,6 +1,9 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <string> #include <string>
// header guard
#ifndef _ba_ne_hg_
#define _ba_ne_hg_
using nlohmann::json; using nlohmann::json;
@ -10,3 +13,4 @@ void login(std::string username, std::string password);
void refresh_access_token(); void refresh_access_token();
json get_data_from_endpoint(std::string endpoint); json get_data_from_endpoint(std::string endpoint);
} // namespace bakaapi } // namespace bakaapi
#endif

View File

@ -1,4 +1,8 @@
#include "timetable.h" #include "timetable.h"
#include <nlohmann/json.hpp>
#include "net.h"
using nlohmann::json;
#define NLINES 10 #define NLINES 10
#define NCOLS 40 #define NCOLS 40
@ -9,5 +13,10 @@
#define DEFAULT_PADDING 4 #define DEFAULT_PADDING 4
void timetable_page() { void timetable_page() {
// DONT FORGET TO UNCOMMENT
json resp_from_api = bakaapi::get_data_from_endpoint("api/3/marks");
// std::ifstream f("test-data/marks3.json");
// json resp_from_api = json::parse(f);
// calculate table size
} }

View File

@ -1 +1,6 @@
// header guard
#ifndef _ba_ti_hg_
#define _ba_ti_hg_
void timetable_page(); void timetable_page();
#endif

9
src/types.h Normal file
View File

@ -0,0 +1,9 @@
// header guard
#ifndef _ba_ty_hg_
#define _ba_ty_hg_
struct Config {
bool verbose = false;
bool ignoressl = false;
};
#endif