more code

This commit is contained in:
Florian Stinglmayr 2025-04-27 16:00:18 +02:00
parent 232ae224c2
commit 32d2484637
6 changed files with 167 additions and 2 deletions

View File

@ -1,4 +1,7 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.24)
PROJECT(edterm)
ADD_DEFINITIONS("-Wall -Werror -std=c99")
ENABLE_TESTING()
ADD_SUBDIRECTORY(lib)

View File

@ -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")

View File

@ -0,0 +1,23 @@
#ifndef EDAPI_JOURNAL_JOURNAL_H
#define EDAPI_JOURNAL_JOURNAL_H
#include <edapi/error.h>
#include <glib.h>
#include <glib-object.h>
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

106
lib/src/journal/journal.c Normal file
View File

@ -0,0 +1,106 @@
#include <edapi/journal/journal.h>
#include <edapi/util.h>
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;
}

View File

@ -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()

27
lib/tests/test-journal.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdarg.h>
#include <setjmp.h>
#include <stddef.h>
#include <cmocka.h>
#include <stdio.h>
#include <edapi/journal/journal.h>
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);
}