From e1f13a7ef4a5fa935d8b88256772598ebe2b47d5 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 21 Feb 2018 16:01:15 -0500 Subject: [PATCH] core/file: Create new functions for reading data from files The readd(), readu(), and readhu() functions are all used to read various forms of integers. The readl() and readw() are for reading either lines or individual whitespace-delimited words Signed-off-by: Anna Schumaker --- core/audio.c | 2 +- core/database.c | 6 ++---- core/date.c | 8 ++++---- core/file.c | 31 +++++++++++++------------------ core/playlists/artist.c | 4 ++-- core/playlists/generic.c | 19 +++++++------------ core/playlists/library.c | 4 ++-- core/playlists/system.c | 13 +++++++------ core/playlists/user.c | 2 +- core/settings.c | 5 +++-- core/tags/album.c | 2 +- core/tags/artist.c | 2 +- core/tags/genre.c | 2 +- core/tags/library.c | 6 +++--- core/tags/track.c | 29 +++++++++++------------------ include/core/file.h | 19 +++++++++++-------- tests/core/database.c | 4 +--- tests/core/file.c | 28 +++++++++++++--------------- tests/core/tags/artist.c | 5 ++--- tests/core/tags/genre.c | 5 ++--- 20 files changed, 88 insertions(+), 108 deletions(-) diff --git a/core/audio.c b/core/audio.c index 8cdd87f0..1a25f16e 100644 --- a/core/audio.c +++ b/core/audio.c @@ -132,7 +132,7 @@ static bool __audio_init_idle(void *data) track = settings_get(SETTINGS_TRACK); __audio_load(track_get(track), LOAD_HISTORY); } else if (file_open(&audio_file, OPEN_READ)) { - data_file_readf(&audio_file, "%u", &track); + track = file_readu(&audio_file); file_close(&audio_file); file_remove(&audio_file); __audio_load(track_get(track), LOAD_HISTORY); diff --git a/core/database.c b/core/database.c index 78b7f008..e4c62a4b 100644 --- a/core/database.c +++ b/core/database.c @@ -36,10 +36,8 @@ static struct db_entry *__dbe_next(const struct database *db, unsigned int index static struct db_entry *__dbe_read(struct database *db, unsigned int index) { struct db_entry *dbe = NULL; - int valid; - data_file_readf(&db->db_file, "%d", &valid); - if (valid) + if (file_readd(&db->db_file)) dbe = db->db_ops->dbe_read(&db->db_file, index); g_ptr_array_index(db->db_entries, index) = dbe; @@ -118,7 +116,7 @@ void db_load(struct database *db) if (file_open(&db->db_file, OPEN_READ) == false) return; - data_file_readf(&db->db_file, "%u", &size); + size = file_readu(&db->db_file); g_ptr_array_set_size(db->db_entries, size); for (unsigned int i = 0; i < size; i++) { if (__dbe_read(db, i)) diff --git a/core/date.c b/core/date.c index 5a112e01..795ac850 100644 --- a/core/date.c +++ b/core/date.c @@ -26,14 +26,14 @@ void date_today(struct date *date) void date_read(struct file *f, struct date *date) { - data_file_readf(f, "%u %u %u", &date->d_year, &date->d_month, &date->d_day); + date->d_year = file_readu(f); + date->d_month = file_readu(f); + date->d_day = file_readu(f); } void date_read_stamp(struct file *f, struct date *date) { - uint32_t stamp; - data_file_readf(f, "%u", &stamp); - date->d_stamp = be32toh(stamp); + date->d_stamp = be32toh(file_readu(f)); } void date_write(struct file *f, struct date *date) diff --git a/core/file.c b/core/file.c index dc251652..81e51d3c 100644 --- a/core/file.c +++ b/core/file.c @@ -130,8 +130,7 @@ static bool __file_open_read(struct file *file, enum open_mode mode) if (mode == OPEN_READ_BINARY) return true; - if (data_file_readf(file, "%u\n", &file->f_prev) != 1) - return false; + file->f_prev = file_readu(file); if (file->f_prev < file->f_min) { REPORT_ERROR(file->f_name, "File too old to be upgraded."); file_close(file); @@ -190,27 +189,23 @@ void file_close(struct file *file) g_free(tmp); } -int data_file_readf(struct file *data, const char *fmt, ...) +gchar *file_readw(struct file *file) { - va_list argp; - int ret; - - va_start(argp, fmt); - ret = vfscanf(data->f_file, fmt, argp); - va_end(argp); - - return ret; + gchar *s; + return fscanf(file->f_file, "%ms%*c", &s) ? s : g_strdup(""); } -gchar *data_file_readl(struct file *data) +gchar *file_readl(struct file *file) { - gchar *res; + gchar *s = NULL; + size_t len = 0; + return getline(&s, &len, file->f_file) ? g_strchomp(s) : g_strdup(""); +} - if (data_file_readf(data, "%m[^\n]\n", &res) == 0) - return g_strdup(""); - - g_strstrip(res); - return res; +unsigned int file_readu(struct file *file) +{ + unsigned int u; + return fscanf(file->f_file, "%u%*c", &u) ? u : 0; } int file_writef(struct file *file, const char *fmt, ...) diff --git a/core/playlists/artist.c b/core/playlists/artist.c index 37bd3cf1..39429ae9 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -50,9 +50,9 @@ static bool __artist_pl_load(void *data) if (!file_open(&artist_file, OPEN_READ)) return true; - data_file_readf(&artist_file, "%u\n", &n); + n = file_readu(&artist_file); for (i = 0; i < n; i++) { - name = data_file_readl(&artist_file); + name = file_readl(&artist_file); playlist = __artist_pl_lookup(name); if (playlist) playlist_generic_load(playlist, &artist_file, diff --git a/core/playlists/generic.c b/core/playlists/generic.c index e707ce09..c92337ba 100644 --- a/core/playlists/generic.c +++ b/core/playlists/generic.c @@ -134,39 +134,34 @@ void playlist_generic_load(struct playlist *playlist, struct file *file, { unsigned int f, n, i, t, it = 0; int field, ascending; - gchar *line; if (!playlist) return; if (flags & PL_SAVE_ITER) - data_file_readf(file, "%u", &it); + it = file_readu(file); if (flags & PL_SAVE_FLAGS) { - data_file_readf(file, "%u %u", &f, &n); + f = file_readu(file); + n = file_readu(file); playlist_clear_sort(playlist); for (i = 0; i < n; i++) { - data_file_readf(file, "%u %d", &field, &ascending); - field += 1; + field = file_readu(file) + 1; + ascending = file_readd(file); if (!ascending) field = -field; playlist->pl_sort = g_slist_append(playlist->pl_sort, GINT_TO_POINTER(field)); } playlist_generic_resort(playlist); - - if (data_file_readf(file, "%m\n", &line)) - g_free(line); } if (flags & PL_SAVE_TRACKS) { - data_file_readf(file, "%u ", &n); + n = file_readu(file); for (i = 0; i < n; i++) { - data_file_readf(file, "%u", &t); + t = file_readu(file); playlist_generic_add(playlist, track_get(t)); } - if (data_file_readf(file, "%m\n", &line)) - g_free(line); } playlist_generic_set_random(playlist, f == PL_RANDOM); diff --git a/core/playlists/library.c b/core/playlists/library.c index 52d28fe7..ed75272d 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -55,9 +55,9 @@ static bool __lib_pl_load(void *data) if (!file_open(&lib_file, OPEN_READ)) return true; - data_file_readf(&lib_file, "%u\n", &n); + n = file_readu(&lib_file); for (i = 0; i < n; i++) { - name = data_file_readl(&lib_file); + name = file_readl(&lib_file); playlist = __lib_pl_lookup(name); if (playlist) playlist_generic_load(playlist, &lib_file, diff --git a/core/playlists/system.c b/core/playlists/system.c index 5b7181a6..58e7d718 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -146,9 +146,9 @@ static bool sys_pl_queued_load() if (!file_open(&sys_deck_f, OPEN_READ)) return true; - data_file_readf(&sys_deck_f, "%u", &num); + num = file_readu(&sys_deck_f); for (i = 0; i < num; i++) { - data_file_readf(&sys_deck_f, "%u", &flags); + flags = file_readu(&sys_deck_f); flags &= PL_RANDOM; if (i == 0) playlist_generic_set_random(playlist, @@ -264,9 +264,10 @@ static bool __sys_pl_load() if (!file_open(&sys_file, OPEN_READ)) return true; - data_file_readf(&sys_file, "%u\n", &n); + n = file_readu(&sys_file); for (i = 0; i < n; i++) { - data_file_readf(&sys_file, "%*u %m[^\n]\n", &name); + file_readu(&sys_file); + name = file_readl(&sys_file); if (string_match(name, "Banned")) { g_free(name); name = g_strdup("Hidden"); @@ -297,10 +298,10 @@ static bool __sys_pl_load_new() return true; } - data_file_readf(&sys_pl_file, "%u\n", &n); + n = file_readu(&sys_pl_file); for (i = 0; i < n; i++) { load = PL_SAVE_METADATA; - name = data_file_readl(&sys_pl_file); + name = file_readl(&sys_pl_file); if (string_match(name, "Banned")) { g_free(name); diff --git a/core/playlists/user.c b/core/playlists/user.c index 076ab599..2408abc9 100644 --- a/core/playlists/user.c +++ b/core/playlists/user.c @@ -39,7 +39,7 @@ static gchar *user_db_key(struct db_entry *dbe) static struct db_entry *user_db_read(struct file *file, unsigned int index) { - gchar *name = data_file_readl(file); + gchar *name = file_readl(file); struct user_playlist *playlist = __user_db_alloc(name, index); playlist_generic_load(&playlist->pl_playlist, file, PL_SAVE_ALL); diff --git a/core/settings.c b/core/settings.c index e0474fc0..1484b573 100644 --- a/core/settings.c +++ b/core/settings.c @@ -29,9 +29,10 @@ static void __settings_read() unsigned int num, i, value; gchar *key; - data_file_readf(&gui_settings_file, "%u\n", &num); + num = file_readu(&gui_settings_file); for (i = 0; i < num; i++) { - data_file_readf(&gui_settings_file, "%m[^ ] %u\n", &key, &value); + key = file_readw(&gui_settings_file); + value = file_readu(&gui_settings_file); g_hash_table_insert(gui_settings, key, GUINT_TO_POINTER(value)); } diff --git a/core/tags/album.c b/core/tags/album.c index 41ea0fb4..bac7096b 100644 --- a/core/tags/album.c +++ b/core/tags/album.c @@ -258,7 +258,7 @@ static struct db_entry *album_read(struct file *file, unsigned int index) struct album *album; gchar *line, *name; - line = data_file_readl(file); + line = file_readl(file); if (file_version(file) == 0) { album = __album_parse_v0(line); album_db_upgraded = true; diff --git a/core/tags/artist.c b/core/tags/artist.c index fd094460..49cacb77 100644 --- a/core/tags/artist.c +++ b/core/tags/artist.c @@ -39,7 +39,7 @@ static gchar *artist_key(struct db_entry *dbe) struct db_entry *artist_read(struct file *file, unsigned int index) { - return &__artist_alloc(data_file_readl(file))->ar_dbe; + return &__artist_alloc(file_readl(file))->ar_dbe; } static void artist_write(struct file *file, struct db_entry *dbe) diff --git a/core/tags/genre.c b/core/tags/genre.c index 144506e0..c9ae253c 100644 --- a/core/tags/genre.c +++ b/core/tags/genre.c @@ -37,7 +37,7 @@ static gchar *genre_key(struct db_entry *dbe) static struct db_entry *genre_read(struct file *file, unsigned int index) { - return &__genre_alloc(data_file_readl(file))->ge_dbe; + return &__genre_alloc(file_readl(file))->ge_dbe; } static void genre_write(struct file *file, struct db_entry *dbe) diff --git a/core/tags/library.c b/core/tags/library.c index c4999680..08a56181 100644 --- a/core/tags/library.c +++ b/core/tags/library.c @@ -35,13 +35,13 @@ static gchar *library_key(struct db_entry *dbe) static struct db_entry *library_read(struct file *file, unsigned int index) { - int enabled; gchar *path; + /* Old "enabled" flag */ if (file_version(file) == 0) - data_file_readf(file, "%d", &enabled); + file_readd(file); - data_file_readf(file, " %m[^\n]", &path); + path = file_readl(file); return &__library_alloc(path)->li_dbe; } diff --git a/core/tags/track.c b/core/tags/track.c index 76eb0b75..dd8f6e35 100644 --- a/core/tags/track.c +++ b/core/tags/track.c @@ -128,19 +128,13 @@ static gchar *track_key(struct db_entry *dbe) static void track_read_v0(struct file *file, struct track *track) { - unsigned int artist_id, album_id, genre_id; - struct artist *artist; - struct genre *genre; - struct album *album; + struct artist *artist = artist_get(file_readu(file)); + struct album *album = album_get( file_readu(file)); + struct genre *genre = genre_get( file_readu(file)); - data_file_readf(file, "%u %u %u %hu", &artist_id, &album_id, &genre_id, - &track->tr_track); + track->tr_track = file_readhu(file); date_read(file, &track->tr_date); - album = album_get(album_id); - artist = artist_get(artist_id); - genre = genre_get(genre_id); - if (album->al_artist != artist || album->al_genre != genre) album = album_find(artist, genre, album->al_name, album->al_year); @@ -150,29 +144,28 @@ static void track_read_v0(struct file *file, struct track *track) static struct db_entry *track_read(struct file *file, unsigned int index) { struct track *track = __track_alloc(); - unsigned int library_id, album_id; - data_file_readf(file, "%u", &library_id); - track->tr_library = library_get(library_id); + track->tr_library = library_get(file_readu(file)); if (file_version(file) == 0) track_read_v0(file, track); else { - data_file_readf(file, "%u %hu", &album_id, &track->tr_track); - track->tr_album = album_get(album_id); + track->tr_album = album_get(file_readu(file)); + track->tr_track = file_readhu(file); date_read_stamp(file, &track->tr_date); } - data_file_readf(file, "%hu %hu", &track->tr_count, &track->tr_length); + track->tr_count = file_readhu(file); + track->tr_length = file_readhu(file); play_count += track->tr_count; if (track->tr_count == 0) unplayed_count++; - track->tr_title = data_file_readl(file); + track->tr_title = file_readl(file); track->tr_tokens = g_str_tokenize_and_fold(track->tr_title, NULL, &track->tr_alts); - track->tr_path = __track_key(track->tr_library, data_file_readl(file)); + track->tr_path = __track_key(track->tr_library, file_readl(file)); return &track->tr_dbe; } diff --git a/include/core/file.h b/include/core/file.h index 26abee86..f8c3b362 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -111,16 +111,19 @@ bool file_open(struct file *, enum open_mode); void file_close(struct file *); /* - * Read an entire line from the file and return it to the caller. - * This function allocates a new string that MUST be freed with g_free(). + * Called to read an unsigned int, signed int, single word, or entire + * line from the file. + * NOTE: file_readw() and file_readl() both return a new string that + * MUST be freed with g_free() */ -gchar *data_file_readl(struct file *); +gchar *file_readw(struct file *); +gchar *file_readl(struct file *); -/* - * Read from a file with an fscanf(3) style format string. - * Returns the number of items matched. - */ -int data_file_readf(struct file *, const char *, ...); +unsigned int file_readu(struct file *); +static inline int file_readd(struct file *file) + { return (int)file_readu(file); } +static inline unsigned short int file_readhu(struct file *file) + { return (unsigned short int)file_readu(file); } /* * Write to a file with an fprintf(3) style format string. diff --git a/tests/core/database.c b/tests/core/database.c index 99b6608d..33b34817 100644 --- a/tests/core/database.c +++ b/tests/core/database.c @@ -46,9 +46,7 @@ static gchar *int_key(struct db_entry *dbe) static struct db_entry *int_read(struct file *f, unsigned int index) { - unsigned int val; - data_file_readf(f, "%u", &val); - return &__int_alloc(val)->ie_dbe; + return &__int_alloc(file_readu(f))->ie_dbe; } static void int_write(struct file *file, struct db_entry *dbe) diff --git a/tests/core/file.c b/tests/core/file.c index 934bf25b..55115e32 100644 --- a/tests/core/file.c +++ b/tests/core/file.c @@ -95,8 +95,6 @@ static void test_io() { struct file fout = FILE_INIT_DATA("", "file.txt", 0); struct file fin = FILE_INIT_DATA("", "file.txt", 1); - char *res = NULL; - unsigned int i; fout.f_version = 1; g_assert_true(file_open(&fout, OPEN_WRITE)); @@ -104,28 +102,28 @@ static void test_io() file_writef(&fout, "2 FGHIJ KLMNO\n"); file_writef(&fout, "3 \n"); file_writef(&fout, "4 5 PQRST\n"); + file_writef(&fout, "-6 UV WX YZ\n"); file_close(&fout); g_assert_true(file_exists(&fout)); g_assert_true(file_open(&fin, OPEN_READ)); g_assert_cmpuint(file_version(&fin), ==, 1); - g_assert_cmpuint(data_file_readf(&fin, "%u %ms\n", &i, &res), ==, 2); - g_assert_cmpuint(i, ==, 1); - g_assert_cmpstr_free(res, ==, "ABCDE"); + g_assert_cmpuint( file_readu(&fin), ==, 1); + g_assert_cmpstr_free(file_readl(&fin), ==, "ABCDE"); - g_assert_cmpuint(data_file_readf(&fin, "%u %m[^\n]\n", &i, &res), ==, 2); - g_assert_cmpuint(i, ==, 2); - g_assert_cmpstr_free(res, ==, "FGHIJ KLMNO"); + g_assert_cmpuint( file_readu(&fin), ==, 2); + g_assert_cmpstr_free(file_readl(&fin), ==, "FGHIJ KLMNO"); - g_assert_cmpuint(data_file_readf(&fin, "%u", &i), ==, 1); - res = data_file_readl(&fin); - g_assert_cmpuint(i, ==, 3); - g_assert_cmpstr_free(res, ==, ""); + g_assert_cmpuint( file_readu(&fin), ==, 3); + g_assert_cmpstr_free(file_readl(&fin), ==, ""); - g_assert_cmpuint(data_file_readf(&fin, "%u %m[^\n]", &i, &res), ==, 2); - g_assert_cmpuint(i, ==, 4); - g_assert_cmpstr_free(res, ==, "5 PQRST"); + g_assert_cmpuint( file_readu(&fin), ==, 4); + g_assert_cmpstr_free(file_readl(&fin), ==, "5 PQRST"); + + g_assert_cmpint( file_readd(&fin), ==, -6); + g_assert_cmpstr_free(file_readw(&fin), ==, "UV"); + g_assert_cmpstr_free(file_readl(&fin), ==, "WX YZ"); file_close(&fin); g_assert_cmpuint(file_version(&fin), ==, OCARINA_MINOR_VERSION); diff --git a/tests/core/tags/artist.c b/tests/core/tags/artist.c index eb9d23d7..6457999f 100644 --- a/tests/core/tags/artist.c +++ b/tests/core/tags/artist.c @@ -31,7 +31,6 @@ static void test_artist() struct file f = FILE_INIT_DATA("", "artist_tag", 0); const struct db_ops *artist_ops = test_artist_ops(); struct artist *artist; - unsigned int i; artist = ARTIST(artist_ops->dbe_alloc("Koji Kondo", 0)); @@ -48,13 +47,13 @@ static void test_artist() artist_ops->dbe_free(&artist->ar_dbe); file_open(&f, OPEN_READ); - data_file_readf(&f, "%u", &i); + file_readu(&f); artist = ARTIST(artist_ops->dbe_read(&f, 0)); test_verify_empty(artist); g_free(artist->ar_name); artist_ops->dbe_free(&artist->ar_dbe); - data_file_readf(&f, "%u", &i); + file_readu(&f); artist = ARTIST(artist_ops->dbe_read(&f, 0)); file_close(&f); test_verify_koji(artist); diff --git a/tests/core/tags/genre.c b/tests/core/tags/genre.c index 9328967f..d88ced92 100644 --- a/tests/core/tags/genre.c +++ b/tests/core/tags/genre.c @@ -30,7 +30,6 @@ static void test_genre() struct file f = FILE_INIT_DATA("", "genre_tag", 0); const struct db_ops *genre_ops = test_genre_ops(); struct genre *genre; - unsigned int i; genre = GENRE(genre_ops->dbe_alloc("Video Game Music", 0)); test_verify_vg(genre); @@ -46,13 +45,13 @@ static void test_genre() genre_ops->dbe_free(&genre->ge_dbe); file_open(&f, OPEN_READ); - data_file_readf(&f, "%u", &i); + file_readu(&f); genre = GENRE(genre_ops->dbe_read(&f, 0)); test_verify_empty(genre); g_free(genre->ge_name); genre_ops->dbe_free(&genre->ge_dbe); - data_file_readf(&f, "%u", &i); + file_readu(&f); genre = GENRE(genre_ops->dbe_read(&f, 0)); file_close(&f); test_verify_vg(genre);