From f5d744a75c20a5a0ca1c2dc091033513be3673ff Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 28 Oct 2015 03:31:09 -0400 Subject: [PATCH] core/date: Add date_{read|write} functions for date IO Signed-off-by: Anna Schumaker --- core/date.c | 10 ++++++++++ core/tags/track.cpp | 14 ++++++++------ include/core/date.h | 9 +++++++++ tests/core/date.c | 16 ++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/core/date.c b/core/date.c index aed10f01..625d7bde 100644 --- a/core/date.c +++ b/core/date.c @@ -17,6 +17,16 @@ void date_today(struct date *date) } } +void date_read(struct file *f, struct date *date) +{ + file_readf(f, "%u %u %u", &date->d_year, &date->d_month, &date->d_day); +} + +void date_write(struct file *f, struct date *date) +{ + file_writef(f, "%u %u %u", date->d_year, date->d_month, date->d_day); +} + int date_compare(const struct date *lhs, const struct date *rhs) { int ret = lhs->d_year - rhs->d_year; diff --git a/core/tags/track.cpp b/core/tags/track.cpp index fcd228e2..4e3781cd 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -112,9 +112,10 @@ void Track :: read(file &file) unsigned int library_id, artist_id, album_id, genre_id; gchar *path; - file_readf(&file, "%u %u %u %u %u %u %u %u %u %u", &library_id, - &artist_id, &album_id, &genre_id, &_track, &_date.d_year, - &_date.d_month, &_date.d_day, &_count, &_length); + file_readf(&file, "%u %u %u %u %u", &library_id, &artist_id, + &album_id, &genre_id, &_track); + date_read(&file, &_date); + file_readf(&file, "%u %u", &_count, &_length); GenericTag :: read(file); path = file_readl(&file); @@ -134,9 +135,10 @@ void Track :: read(file &file) void Track :: write(file &file) { - file_writef(&file, "%u %u %u %u %u %u %u %u %u %u ", _library->index(), - _artist->index(), _album->index(), _genre->index(), _track, - _date.d_year, _date.d_month, _date.d_day, _count, _length); + file_writef(&file, "%u %u %u %u %u ", _library->index(), _artist->index(), + _album->index(), _genre->index(), _track); + date_write(&file, &_date); + file_writef(&file, " %u %u ", _count, _length); GenericTag :: write(file); file_writef(&file, "\n%s\n", _path.c_str()); } diff --git a/include/core/date.h b/include/core/date.h index ca5286ba..9f4bcf89 100644 --- a/include/core/date.h +++ b/include/core/date.h @@ -4,6 +4,9 @@ #ifndef OCARINA_CORE_DATE_H #define OCARINA_CORE_DATE_H +#include + + struct date { unsigned int d_year; unsigned int d_month; @@ -14,6 +17,12 @@ struct date { /* Set the provided date structure to today's date. */ void date_today(struct date *); +/* Read the date from file. */ +void date_read(struct file *, struct date *); + +/* Write the date to file. */ +void date_write(struct file *, struct date *); + /* * Compare two dates. * diff --git a/tests/core/date.c b/tests/core/date.c index 8a48a36f..dc5e51bd 100644 --- a/tests/core/date.c +++ b/tests/core/date.c @@ -16,10 +16,26 @@ void test_date() .d_month = 0, .d_day = 0, }; + struct file f = FILE_INIT("date", 0); date_today(NULL); date_today(&date); + test_equal(date.d_year, today->tm_year + 1900); + test_equal(date.d_month, today->tm_mon + 1); + test_equal(date.d_day, today->tm_mday); + file_open(&f, OPEN_WRITE); + file_writef(&f, "%u %u %u\n", 1988, 6, 17); + date_write(&f, &date); + file_close(&f); + + file_open(&f, OPEN_READ); + date_read(&f, &date); + test_equal(date.d_year, 1988); + test_equal(date.d_month, 6); + test_equal(date.d_day, 17); + + date_read(&f, &date); test_equal(date.d_year, today->tm_year + 1900); test_equal(date.d_month, today->tm_mon + 1); test_equal(date.d_day, today->tm_mday);