more code

This commit is contained in:
Florian Stinglmayr 2025-04-24 15:29:31 +02:00
parent 5ef447c629
commit 232ae224c2
7 changed files with 167 additions and 33 deletions

View File

@ -3,6 +3,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.24)
FIND_PACKAGE(PkgConfig)
PKG_CHECK_MODULES(JANSSON REQUIRED jansson)
PKG_CHECK_MODULES(GIO2 REQUIRED gio-2.0)
PKG_CHECK_MODULES(GLIB2 REQUIRED glib-2.0)
PKG_CHECK_MODULES(GOBJECT2 REQUIRED gobject-2.0)
@ -15,12 +16,14 @@ SET(SOURCES
INCLUDE_DIRECTORIES(
"include"
${GIO2_INCLUDE_DIRS}
${GLIB2_INCLUDE_DIRS}
${GOBJECT2_INCLUDE_DIRS}
${JANSSON_INCLUDE_DIRS}
)
LINK_DIRECTORIES(
${GIO2_LIBRARY_DIRS}
${GLIB2_LIBRARY_DIRS}
${GOBJECT2_LIBRARY_DIRS}
${JANSSON_LIBRARY_DIRS}
@ -29,6 +32,7 @@ LINK_DIRECTORIES(
ADD_LIBRARY("edapi" SHARED ${SOURCES})
TARGET_LINK_LIBRARIES(
"edapi"
${GIO2_LIBRARIES}
${GLIB2_LIBRARIES}
${GOBJECT2_LIBRARIES}
${JANSSON_LIBRARIES}

View File

@ -16,7 +16,9 @@ EDJournalFile *ed_journal_file_new(void);
EDErrorCode ed_journal_file_parse(EDJournalFile *file, char const *filename);
EDErrorCode ed_journal_file_open(EDJournalFile *file, char const *filename);
EDErrorCode ed_journal_file_open(EDJournalFile *file,
char const *filename,
GError **error);
G_END_DECLS

View File

@ -17,8 +17,20 @@ G_DEFINE_TYPE_EXTENDED(
G_ADD_PRIVATE(EDJournalEntry)
);
static void ed_journal_entry_finalize(GObject *obj)
{
EDJournalEntry *self = ED_JOURNALENTRY(obj);
EDJournalEntryPrivate *p = ed_journal_entry_get_instance_private(self);
json_decref(p->entry);
p = NULL;
G_OBJECT_CLASS(ed_journal_entry_parent_class)->finalize(obj);
}
static void ed_journal_entry_class_init(EDJournalEntryClass *klass)
{
G_OBJECT_CLASS(klass)->finalize = ed_journal_entry_finalize;
}
static void ed_journal_entry_init(EDJournalEntry *self)

View File

@ -1,10 +1,14 @@
#include <edapi/journal/file.h>
#include <edapi/journal/entry.h>
#include <edapi/util.h>
#include <gio/gio.h>
typedef struct {
gchar *filename;
gchar *timestamp;
int index;
GList *entries;
} EDJournalFilePrivate;
struct _EDJournalFile {
@ -34,6 +38,9 @@ static void ed_journal_file_finalize(GObject *obj)
free(p->timestamp);
p->timestamp = NULL;
g_list_free_full(p->entries, g_object_unref);
p->entries = NULL;
G_OBJECT_CLASS(ed_journal_file_parent_class)->finalize(obj);
}
@ -106,7 +113,78 @@ done:
return ret;
}
EDErrorCode ed_journal_file_open(EDJournalFile *file, char const *filename)
static EDErrorCode ed_journal_file_load(EDJournalFile *self,
GError **error)
{
EDErrorCode ret = ed_error_internal;
EDJournalFilePrivate *p = ed_journal_file_get_instance_private(self);
GFile *file = NULL;
GFileIOStream *stream = NULL;
GDataInputStream *reader = NULL;
EDJournalEntry *entry = NULL;
GError *e = NULL;
char *line = NULL;
gsize linelen = 0;
file = g_file_new_for_path(p->filename);
goto_if_true(file == NULL, done);
stream = g_file_open_readwrite(file, NULL, &e);
if (stream == NULL) {
g_propagate_error(error, e);
goto done;
}
reader = g_data_input_stream_new(
g_io_stream_get_input_stream(G_IO_STREAM(stream))
);
if (reader == NULL) {
goto done;
}
while (TRUE) {
line = g_data_input_stream_read_line_utf8(
reader, &linelen, NULL, &e
);
if (line == NULL) {
if (e != NULL) {
g_propagate_error(error, e);
goto done;
} else {
break;
}
}
entry = ed_journal_entry_new();
goto_if_true(entry == NULL, done);
ret = ed_journal_entry_parse(entry, line, NULL);
g_free(line);
line = NULL;
linelen = 0;
if (ED_ERROR(ret)) {
g_clear_object(&entry);
continue;
}
p->entries = g_list_append(p->entries, entry);
}
done:
g_clear_object(&file);
g_clear_object(&stream);
g_clear_object(&reader);
return ret;
}
EDErrorCode ed_journal_file_open(EDJournalFile *file,
char const *filename,
GError **error)
{
EDErrorCode r = ed_error_success;
@ -115,5 +193,10 @@ EDErrorCode ed_journal_file_open(EDJournalFile *file, char const *filename)
return r;
}
r = ed_journal_file_load(file, error);
if (ED_ERROR(r)) {
return r;
}
return r;
}

View File

@ -3,7 +3,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.24)
PKG_CHECK_MODULES(CMOCKA REQUIRED cmocka)
SET(TESTS
"test-journal-file-parse"
"test-journal-file"
)
INCLUDE_DIRECTORIES(

View File

@ -1,30 +0,0 @@
#include <stdarg.h>
#include <setjmp.h>
#include <stddef.h>
#include <cmocka.h>
#include <edapi/journal/file.h>
static void test_new_filename(void **state)
{
char const *filename = "Journal.2023-04-18T061507.01.log";
EDJournalFile *file = ed_journal_file_new();
EDErrorCode ret = 0;
assert_non_null(file);
ret = ed_journal_file_parse(file, filename);
assert_int_equal(ret, ed_error_success);
g_clear_object(&file);
}
int main(int ac, char **av)
{
static const struct CMUnitTest tests[] = {
cmocka_unit_test(test_new_filename),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@ -0,0 +1,63 @@
#include <stdarg.h>
#include <setjmp.h>
#include <stddef.h>
#include <cmocka.h>
#include <stdio.h>
#include <edapi/journal/file.h>
static void test_new_filename(void **state)
{
char const *filename = "Journal.2023-04-18T061507.01.log";
EDJournalFile *file = ed_journal_file_new();
EDErrorCode ret = 0;
assert_non_null(file);
ret = ed_journal_file_parse(file, filename);
assert_int_equal(ret, ed_error_success);
g_clear_object(&file);
}
static void test_valid_load(void **state)
{
char const *filename = "Journal.2023-04-18T061507.01.log";
FILE *F = fopen(filename, "w");
assert_non_null(F);
fprintf(F, "{ \"timestamp\":\"2023-04-18T04:14:56Z\", "
"\"event\":\"Fileheader\", \"part\":1, "
"\"language\":\"English/UK\", \"Odyssey\":true, "
"\"gameversion\":\"4.0.0.1477\", \"build\":\"r291050/r0 \" }\n"
);
fclose(F);
EDJournalFile *file = NULL;
EDErrorCode ret = 0;
GError *error = NULL;
file = ed_journal_file_new();
assert_non_null(file);
ret = ed_journal_file_open(file, filename, &error);
assert_null(error);
assert_int_equal(ret, ed_error_success);
g_clear_object(&file);
}
int main(int ac, char **av)
{
static const struct CMUnitTest tests[] = {
cmocka_unit_test(test_new_filename),
cmocka_unit_test(test_valid_load),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}