core/date: Add date_{read|write} functions for date IO

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-28 03:31:09 -04:00
parent af1939a6e7
commit f5d744a75c
4 changed files with 43 additions and 6 deletions

View File

@ -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;

View File

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

View File

@ -4,6 +4,9 @@
#ifndef OCARINA_CORE_DATE_H
#define OCARINA_CORE_DATE_H
#include <core/file.h>
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.
*

View File

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