From 87bb21be58d898300ed8021f04f68c447d407adb Mon Sep 17 00:00:00 2001
From: osamu-kj <64986162+0xdeadbeer@users.noreply.github.com>
Date: Sat, 13 May 2023 21:30:01 +0200
Subject: [PATCH] Calendar: Implement basic writing feature
In this commit several features were added as well.
Basic writing feature to a file. Added base64 decoding
to the reading feature. Checks whether or not the terminal
is too small. Basic help page.
---
CMakeLists.txt | 2 +-
src/CMakeLists.txt | 2 +
src/base64/CMakeLists.txt | 1 +
src/base64/base64.c | 209 ++++++++++++++++++++++++++++++++++++++
src/base64/base64.h | 101 ++++++++++++++++++
src/engine/engine.cpp | 69 ++++++++++++-
src/engine/engine.hpp | 8 +-
src/main.cpp | 16 ++-
8 files changed, 398 insertions(+), 10 deletions(-)
create mode 100644 src/base64/CMakeLists.txt
create mode 100644 src/base64/base64.c
create mode 100644 src/base64/base64.h
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f68c2e4..2a7d60e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,7 +18,7 @@ endif()
project(vis
VERSION 1.0
DESCRIPTION "Vi Scheduler (vis) is a simple TUI program built for managing your schedules in a calendar-like grid."
- LANGUAGES CXX)
+ LANGUAGES CXX C)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e77f1ee..a63dd98 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,7 +1,9 @@
add_subdirectory(engine)
add_subdirectory(calendar)
+add_subdirectory(base64)
add_executable(vis main.cpp)
target_link_libraries(vis engine)
target_link_libraries(vis calendar)
+target_link_libraries(vis base64)
diff --git a/src/base64/CMakeLists.txt b/src/base64/CMakeLists.txt
new file mode 100644
index 0000000..dc22ad0
--- /dev/null
+++ b/src/base64/CMakeLists.txt
@@ -0,0 +1 @@
+add_library(base64 SHARED base64.c)
diff --git a/src/base64/base64.c b/src/base64/base64.c
new file mode 100644
index 0000000..eec7d98
--- /dev/null
+++ b/src/base64/base64.c
@@ -0,0 +1,209 @@
+/*
+ * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
+ *
+ * @APPLE_LICENSE_HEADER_START@
+ *
+ * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
+ *
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
+ *
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ *
+ * @APPLE_LICENSE_HEADER_END@
+ */
+/* ====================================================================
+ * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ * software must display the following acknowledgment:
+ * "This product includes software developed by the Apache Group
+ * for use in the Apache HTTP server project (http://www.apache.org/)."
+ *
+ * 4. The names "Apache Server" and "Apache Group" must not be used to
+ * endorse or promote products derived from this software without
+ * prior written permission. For written permission, please contact
+ * apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Group.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ * acknowledgment:
+ * "This product includes software developed by the Apache Group
+ * for use in the Apache HTTP server project (http://www.apache.org/)."
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Group and was originally based
+ * on public domain software written at the National Center for
+ * Supercomputing Applications, University of Illinois, Urbana-Champaign.
+ * For more information on the Apache Group and the Apache HTTP server
+ * project, please see .
+ *
+ */
+
+/* Base64 encoder/decoder. Originally Apache file ap_base64.c
+ */
+
+#include
+
+#include "base64.h"
+
+/* aaaack but it's fast and const should make it shared text page. */
+static const unsigned char pr2six[256] =
+{
+ /* ASCII table */
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
+ 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
+ 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
+ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
+};
+
+int Base64decode_len(const char *bufcoded)
+{
+ int nbytesdecoded;
+ register const unsigned char *bufin;
+ register int nprbytes;
+
+ bufin = (const unsigned char *) bufcoded;
+ while (pr2six[*(bufin++)] <= 63);
+
+ nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
+ nbytesdecoded = ((nprbytes + 3) / 4) * 3;
+
+ return nbytesdecoded + 1;
+}
+
+int Base64decode(char *bufplain, const char *bufcoded)
+{
+ int nbytesdecoded;
+ register const unsigned char *bufin;
+ register unsigned char *bufout;
+ register int nprbytes;
+
+ bufin = (const unsigned char *) bufcoded;
+ while (pr2six[*(bufin++)] <= 63);
+ nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
+ nbytesdecoded = ((nprbytes + 3) / 4) * 3;
+
+ bufout = (unsigned char *) bufplain;
+ bufin = (const unsigned char *) bufcoded;
+
+ while (nprbytes > 4) {
+ *(bufout++) =
+ (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
+ *(bufout++) =
+ (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
+ *(bufout++) =
+ (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
+ bufin += 4;
+ nprbytes -= 4;
+ }
+
+ /* Note: (nprbytes == 1) would be an error, so just ingore that case */
+ if (nprbytes > 1) {
+ *(bufout++) =
+ (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
+ }
+ if (nprbytes > 2) {
+ *(bufout++) =
+ (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
+ }
+ if (nprbytes > 3) {
+ *(bufout++) =
+ (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
+ }
+
+ *(bufout++) = '\0';
+ nbytesdecoded -= (4 - nprbytes) & 3;
+ return nbytesdecoded;
+}
+
+static const char basis_64[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+int Base64encode_len(int len)
+{
+ return ((len + 2) / 3 * 4) + 1;
+}
+
+int Base64encode(char *encoded, const char *string, int len)
+{
+ int i;
+ char *p;
+
+ p = encoded;
+ for (i = 0; i < len - 2; i += 3) {
+ *p++ = basis_64[(string[i] >> 2) & 0x3F];
+ *p++ = basis_64[((string[i] & 0x3) << 4) |
+ ((int) (string[i + 1] & 0xF0) >> 4)];
+ *p++ = basis_64[((string[i + 1] & 0xF) << 2) |
+ ((int) (string[i + 2] & 0xC0) >> 6)];
+ *p++ = basis_64[string[i + 2] & 0x3F];
+ }
+ if (i < len) {
+ *p++ = basis_64[(string[i] >> 2) & 0x3F];
+ if (i == (len - 1)) {
+ *p++ = basis_64[((string[i] & 0x3) << 4)];
+ *p++ = '=';
+ }
+ else {
+ *p++ = basis_64[((string[i] & 0x3) << 4) |
+ ((int) (string[i + 1] & 0xF0) >> 4)];
+ *p++ = basis_64[((string[i + 1] & 0xF) << 2)];
+ }
+ *p++ = '=';
+ }
+
+ *p++ = '\0';
+ return p - encoded;
+}
diff --git a/src/base64/base64.h b/src/base64/base64.h
new file mode 100644
index 0000000..2915796
--- /dev/null
+++ b/src/base64/base64.h
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
+ *
+ * @APPLE_LICENSE_HEADER_START@
+ *
+ * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
+ *
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
+ *
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ *
+ * @APPLE_LICENSE_HEADER_END@
+ */
+/* ====================================================================
+ * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ * software must display the following acknowledgment:
+ * "This product includes software developed by the Apache Group
+ * for use in the Apache HTTP server project (http://www.apache.org/)."
+ *
+ * 4. The names "Apache Server" and "Apache Group" must not be used to
+ * endorse or promote products derived from this software without
+ * prior written permission. For written permission, please contact
+ * apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Group.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ * acknowledgment:
+ * "This product includes software developed by the Apache Group
+ * for use in the Apache HTTP server project (http://www.apache.org/)."
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Group and was originally based
+ * on public domain software written at the National Center for
+ * Supercomputing Applications, University of Illinois, Urbana-Champaign.
+ * For more information on the Apache Group and the Apache HTTP server
+ * project, please see .
+ *
+ */
+
+
+
+#ifndef _BASE64_H_
+#define _BASE64_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int Base64encode_len(int len);
+int Base64encode(char * coded_dst, const char *plain_src,int len_plain_src);
+
+int Base64decode_len(const char * coded_src);
+int Base64decode(char * plain_dst, const char *coded_src);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //_BASE64_H_
diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp
index 0fedbdd..46f02d0 100644
--- a/src/engine/engine.cpp
+++ b/src/engine/engine.cpp
@@ -3,7 +3,6 @@
#include
#include
#include
-#include
#include "engine.hpp"
Engine::Engine(int padding, calendar_view_mode view_mode) {
@@ -21,6 +20,15 @@ Engine::~Engine() {
}
void Engine::ui_draw(WINDOW *win) {
+ int screen_width, screen_height;
+ getmaxyx(win, screen_height, screen_width);
+
+ if (screen_height < 25 || screen_width < 70) {
+ this->ui_warning_draw(win, "Window resolution too small, please resize it."); return;
+ return;
+ }
+
+ this->input_block = false;
this->cells_table.clear();
box(win, 0, 0);
@@ -46,6 +54,14 @@ void Engine::ui_draw(WINDOW *win) {
wrefresh(win);
}
+void Engine::ui_warning_draw(WINDOW *win, const char *message) {
+ this->input_block = true;
+
+ mvwprintw(win, 0, 0, "%s", message);
+
+ wrefresh(win);
+}
+
void Engine::ui_month_draw(WINDOW *win) {
calendar_information date_info = this->calendar->get_info();
int z = COLS - this->padding;
@@ -108,7 +124,10 @@ void Engine::ui_bottom_draw(WINDOW *win) {
void Engine::input_handle(WINDOW *win) {
char key = getch();
-
+
+ if (this->input_block)
+ return;
+
if (this->input_handle_universal(win, key))
return;
@@ -203,9 +222,12 @@ bool Engine::input_handle_universal(WINDOW *win, char key) {
break;
}
case 'w':
+ this->write_calendar();
break;
case 'q':
endwin();
+ this->calendar_file.close();
+
exit(EXIT_SUCCESS);
break;
default:
@@ -283,12 +305,20 @@ void Engine::parse_line(std::string line) {
if (tokens.size() != 2)
return;
+ // decode the event from base64 before adding it into the events_map
calendar_information event_date = this->parse_date(tokens[0]);
- this->events_map[event_date] = tokens[1];
+
+ int decoded_length = Base64decode_len((const char *) tokens[1].c_str());
+ char *event_decoded = (char *) malloc(decoded_length);
+
+ Base64decode(event_decoded, tokens[1].c_str());
+
+ this->events_map[event_date] = event_decoded;
}
void Engine::open_calendar(char *filename) {
- this->calendar_file.open(filename, std::ios::in | std::ios::out);
+ this->calendar_file.open(filename, std::ios::in);
+ this->calendar_file_location = filename;
// If the file does not exist,
// we'll create and reopen it later ('write' shortcut)
@@ -300,3 +330,34 @@ void Engine::open_calendar(char *filename) {
this->parse_line(line);
}
}
+
+bool Engine::write_calendar() {
+ this->calendar_file.close();
+ this->calendar_file.open(this->calendar_file_location, std::ios::out | std::ios::trunc);
+
+ if (this->calendar_file.fail()) {
+ mvprintw(0, 0, "Error saving file.");
+ refresh();
+ getch();
+ return false;
+ }
+
+ for (auto const& event : this->events_map) {
+ std::stringstream output_line;
+
+ calendar_information date = event.first;
+
+ int encoded_length = Base64encode_len(event.second.length());
+ char *event_encoded = (char *) malloc(encoded_length);
+ Base64encode(event_encoded, event.second.c_str(), event.second.length());
+
+ output_line << date.current_day << ".";
+ output_line << date.current_month << ".";
+ output_line << date.current_year << " ";
+ output_line << event_encoded;
+
+ this->calendar_file << output_line.str() << std::endl;
+ }
+
+ return true;
+}
diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp
index e1ff2e3..5bb7639 100644
--- a/src/engine/engine.hpp
+++ b/src/engine/engine.hpp
@@ -2,11 +2,12 @@
#define ENGINE_HPP
#include
+#include
#include
#include
#include
-// #include
#include