From 32d2484637fb2194e94d6451cdcdc74c76e2b0d0 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Sun, 27 Apr 2025 16:00:18 +0200 Subject: [PATCH] more code --- CMakeLists.txt | 3 + lib/CMakeLists.txt | 3 +- lib/include/edapi/journal/journal.h | 23 ++++++ lib/src/journal/journal.c | 106 ++++++++++++++++++++++++++++ lib/tests/CMakeLists.txt | 7 +- lib/tests/test-journal.c | 27 +++++++ 6 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 lib/include/edapi/journal/journal.h create mode 100644 lib/src/journal/journal.c create mode 100644 lib/tests/test-journal.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c43546f..7723c9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.24) PROJECT(edterm) +ADD_DEFINITIONS("-Wall -Werror -std=c99") +ENABLE_TESTING() + ADD_SUBDIRECTORY(lib) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 7e5aa54..1ca215b 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -10,8 +10,10 @@ PKG_CHECK_MODULES(GOBJECT2 REQUIRED gobject-2.0) SET(SOURCES "include/edapi/journal/entry.h" "include/edapi/journal/file.h" + "include/edapi/journal/journal.h" "src/journal/entry.c" "src/journal/file.c" + "src/journal/journal.c" ) INCLUDE_DIRECTORIES( @@ -38,5 +40,4 @@ TARGET_LINK_LIBRARIES( ${JANSSON_LIBRARIES} ) -ENABLE_TESTING() ADD_SUBDIRECTORY("tests") diff --git a/lib/include/edapi/journal/journal.h b/lib/include/edapi/journal/journal.h new file mode 100644 index 0000000..722b587 --- /dev/null +++ b/lib/include/edapi/journal/journal.h @@ -0,0 +1,23 @@ +#ifndef EDAPI_JOURNAL_JOURNAL_H +#define EDAPI_JOURNAL_JOURNAL_H + +#include + +#include +#include + +G_BEGIN_DECLS + +G_DECLARE_FINAL_TYPE( + EDJournal, ed_journal, ED, JOURNAL, GObject); + +#define ED_TYPE_JOURNAL ed_journal_get_type() + +EDJournal *ed_journal_new(void); + +gchar const *ed_journal_get_location(EDJournal *j); +EDErrorCode ed_journal_set_location(EDJournal *j, gchar const *dir); + +G_END_DECLS + +#endif diff --git a/lib/src/journal/journal.c b/lib/src/journal/journal.c new file mode 100644 index 0000000..74994af --- /dev/null +++ b/lib/src/journal/journal.c @@ -0,0 +1,106 @@ +#include +#include + +typedef struct { + gchar *location; +} EDJournalPrivate; + +struct _EDJournal { + GObject parent; +}; + +struct _EDJournalClass { + GObjectClass parent_class; +}; + +G_DEFINE_TYPE_EXTENDED( + EDJournal, + ed_journal, + G_TYPE_OBJECT, + 0, + G_ADD_PRIVATE(EDJournal) + ); + +static void ed_journal_finalize(GObject *obj) +{ + EDJournal *self = ED_JOURNAL(obj); + EDJournalPrivate *p = ed_journal_get_instance_private(self); + + free(p->location); + p->location = NULL; + + G_OBJECT_CLASS(ed_journal_parent_class)->finalize(obj); +} + +static void ed_journal_class_init(EDJournalClass *klass) +{ + G_OBJECT_CLASS(klass)->finalize = ed_journal_finalize; +} + +static EDErrorCode ed_journal_determine_location(EDJournal *self) +{ + EDJournalPrivate *p = ed_journal_get_instance_private(self); + gchar *location = NULL; + +#ifdef G_OS_WIN32 + char const *env = getenv("%USERPROFILE%"); + + if (!S_EMPTY(env)) { + location = g_build_path( + G_DIR_SEPARATOR_S, + env, + "Saved Games", + "Frontier Developments", + "Elite Dangerous", + NULL + ); + } +#endif + + if (S_EMPTY(location)) { + return ed_error_invalid; + } + + if (!g_file_test(location, G_FILE_TEST_IS_DIR)) { + g_free(location); + return ed_error_invalid; + } + + g_free(p->location); + p->location = location; + + return ed_error_success; +} + +static void ed_journal_init(EDJournal *self) +{ + ed_journal_determine_location(self); +} + +EDJournal *ed_journal_new(void) +{ + return g_object_new(ED_TYPE_JOURNAL, NULL); +} + +gchar const *ed_journal_get_location(EDJournal *self) +{ + return_if_true(self == NULL, NULL); + EDJournalPrivate *p = ed_journal_get_instance_private(self); + return p->location; +} + +EDErrorCode ed_journal_set_location(EDJournal *self, gchar const *dir) +{ + return_if_true(self == NULL || dir == NULL, ed_error_args); + + EDJournalPrivate *p = ed_journal_get_instance_private(self); + + if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) { + return ed_error_invalid; + } + + g_free(p->location); + p->location = g_strdup(dir); + + return ed_error_success; +} diff --git a/lib/tests/CMakeLists.txt b/lib/tests/CMakeLists.txt index f12c470..f26a701 100644 --- a/lib/tests/CMakeLists.txt +++ b/lib/tests/CMakeLists.txt @@ -4,6 +4,7 @@ PKG_CHECK_MODULES(CMOCKA REQUIRED cmocka) SET(TESTS "test-journal-file" + "test-journal" ) INCLUDE_DIRECTORIES( @@ -18,5 +19,9 @@ FOREACH(TEST ${TESTS}) "edapi" ${CMOCKA_LIBRARIES} ) - ADD_TEST(NAME ${TEST} COMMAND ${TEST}) + ADD_TEST( + NAME ${TEST} + COMMAND ${TEST} + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/.." + ) ENDFOREACH() diff --git a/lib/tests/test-journal.c b/lib/tests/test-journal.c new file mode 100644 index 0000000..80789d0 --- /dev/null +++ b/lib/tests/test-journal.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +#include +#include + +static void test_new_location(void **state) +{ + EDJournal *journal = ed_journal_new(); + assert_non_null(journal); + + gchar const *location = ed_journal_get_location(journal); + assert_non_null(location); + + g_clear_object(&journal); +} + +int main(int ac, char **av) +{ + static const struct CMUnitTest tests[] = { + cmocka_unit_test(test_new_location), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +}