diff options
author | Max Kellermann <max@musicpd.org> | 2016-10-29 09:45:34 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2016-10-29 09:45:34 +0200 |
commit | 90a14e14f457e7ec34dd8ff3dcfe710e2dd93208 (patch) | |
tree | d12ec940506c2a51fee437e5499ea248a99b7650 /src | |
parent | dea46e8d5a714a483aee8e14775eb8b3a69aa4ad (diff) |
db/simple/Save: migrate from class Error to C++ exceptions
Diffstat (limited to 'src')
-rw-r--r-- | src/PlaylistDatabase.cxx | 27 | ||||
-rw-r--r-- | src/PlaylistDatabase.hxx | 8 | ||||
-rw-r--r-- | src/SongSave.cxx | 16 | ||||
-rw-r--r-- | src/SongSave.hxx | 6 | ||||
-rw-r--r-- | src/db/plugins/simple/DatabaseSave.cxx | 74 | ||||
-rw-r--r-- | src/db/plugins/simple/DatabaseSave.hxx | 7 | ||||
-rw-r--r-- | src/db/plugins/simple/DirectorySave.cxx | 78 | ||||
-rw-r--r-- | src/db/plugins/simple/DirectorySave.hxx | 7 | ||||
-rw-r--r-- | src/db/plugins/simple/SimpleDatabasePlugin.cxx | 20 | ||||
-rw-r--r-- | src/db/plugins/simple/SimpleDatabasePlugin.hxx | 5 | ||||
-rw-r--r-- | src/queue/QueueSave.cxx | 9 |
11 files changed, 93 insertions, 164 deletions
diff --git a/src/PlaylistDatabase.cxx b/src/PlaylistDatabase.cxx index c0f756fc8..71b554db2 100644 --- a/src/PlaylistDatabase.cxx +++ b/src/PlaylistDatabase.cxx @@ -23,14 +23,11 @@ #include "fs/io/TextFile.hxx" #include "fs/io/BufferedOutputStream.hxx" #include "util/StringUtil.hxx" -#include "util/Error.hxx" -#include "util/Domain.hxx" +#include "util/RuntimeError.hxx" #include <string.h> #include <stdlib.h> -static constexpr Domain playlist_database_domain("playlist_database"); - void playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv) { @@ -41,9 +38,8 @@ playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv) pi.name.c_str(), (long)pi.mtime); } -bool -playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name, - Error &error) +void +playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name) { PlaylistInfo pm(name, 0); @@ -53,24 +49,19 @@ playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name, while ((line = file.ReadLine()) != nullptr && strcmp(line, "playlist_end") != 0) { colon = strchr(line, ':'); - if (colon == nullptr || colon == line) { - error.Format(playlist_database_domain, - "unknown line in db: %s", line); - return false; - } + if (colon == nullptr || colon == line) + throw FormatRuntimeError("unknown line in db: %s", + line); *colon++ = 0; value = StripLeft(colon); if (strcmp(line, "mtime") == 0) pm.mtime = strtol(value, nullptr, 10); - else { - error.Format(playlist_database_domain, - "unknown line in db: %s", line); - return false; - } + else + throw FormatRuntimeError("unknown line in db: %s", + line); } pv.UpdateOrInsert(std::move(pm)); - return true; } diff --git a/src/PlaylistDatabase.hxx b/src/PlaylistDatabase.hxx index a87125c64..fd2be89b5 100644 --- a/src/PlaylistDatabase.hxx +++ b/src/PlaylistDatabase.hxx @@ -32,8 +32,10 @@ class Error; void playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv); -bool -playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name, - Error &error); +/** + * Throws #std::runtime_error on error. + */ +void +playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name); #endif diff --git a/src/SongSave.cxx b/src/SongSave.cxx index d20ad4af1..265b3c286 100644 --- a/src/SongSave.cxx +++ b/src/SongSave.cxx @@ -27,8 +27,7 @@ #include "tag/Tag.hxx" #include "tag/TagBuilder.hxx" #include "util/StringUtil.hxx" -#include "util/Error.hxx" -#include "util/Domain.hxx" +#include "util/RuntimeError.hxx" #include <string.h> #include <stdlib.h> @@ -36,8 +35,6 @@ #define SONG_MTIME "mtime" #define SONG_END "song_end" -static constexpr Domain song_save_domain("song_save"); - static void range_save(BufferedOutputStream &os, unsigned start_ms, unsigned end_ms) { @@ -74,8 +71,7 @@ song_save(BufferedOutputStream &os, const DetachedSong &song) } DetachedSong * -song_load(TextFile &file, const char *uri, - Error &error) +song_load(TextFile &file, const char *uri) { DetachedSong *song = new DetachedSong(uri); @@ -88,9 +84,7 @@ song_load(TextFile &file, const char *uri, if (colon == nullptr || colon == line) { delete song; - error.Format(song_save_domain, - "unknown line in db: %s", line); - return nullptr; + throw FormatRuntimeError("unknown line in db: %s", line); } *colon++ = 0; @@ -118,9 +112,7 @@ song_load(TextFile &file, const char *uri, } else { delete song; - error.Format(song_save_domain, - "unknown line in db: %s", line); - return nullptr; + throw FormatRuntimeError("unknown line in db: %s", line); } } diff --git a/src/SongSave.hxx b/src/SongSave.hxx index db59adb8c..5c29fbfd9 100644 --- a/src/SongSave.hxx +++ b/src/SongSave.hxx @@ -38,11 +38,9 @@ song_save(BufferedOutputStream &os, const DetachedSong &song); * Loads a song from the input file. Reading stops after the * "song_end" line. * - * @param error location to store the error occurring - * @return true on success, false on error + * Throws #std::runtime_error on error. */ DetachedSong * -song_load(TextFile &file, const char *uri, - Error &error); +song_load(TextFile &file, const char *uri); #endif diff --git a/src/db/plugins/simple/DatabaseSave.cxx b/src/db/plugins/simple/DatabaseSave.cxx index fa39164bb..5b93438f9 100644 --- a/src/db/plugins/simple/DatabaseSave.cxx +++ b/src/db/plugins/simple/DatabaseSave.cxx @@ -29,7 +29,7 @@ #include "tag/Settings.hxx" #include "fs/Charset.hxx" #include "util/StringCompare.hxx" -#include "util/Error.hxx" +#include "util/RuntimeError.hxx" #include "Log.hxx" #include <string.h> @@ -66,8 +66,8 @@ db_save_internal(BufferedOutputStream &os, const Directory &music_root) directory_save(os, music_root); } -bool -db_load_internal(TextFile &file, Directory &music_root, Error &error) +void +db_load_internal(TextFile &file, Directory &music_root) { char *line; unsigned format = 0; @@ -76,10 +76,8 @@ db_load_internal(TextFile &file, Directory &music_root, Error &error) /* get initial info */ line = file.ReadLine(); - if (line == nullptr || strcmp(DIRECTORY_INFO_BEGIN, line) != 0) { - error.Set(db_domain, "Database corrupted"); - return false; - } + if (line == nullptr || strcmp(DIRECTORY_INFO_BEGIN, line) != 0) + throw std::runtime_error("Database corrupted"); memset(tags, false, sizeof(tags)); @@ -90,67 +88,49 @@ db_load_internal(TextFile &file, Directory &music_root, Error &error) if ((p = StringAfterPrefix(line, DB_FORMAT_PREFIX))) { format = atoi(p); } else if (StringStartsWith(line, DIRECTORY_MPD_VERSION)) { - if (found_version) { - error.Set(db_domain, "Duplicate version line"); - return false; - } + if (found_version) + throw std::runtime_error("Duplicate version line"); found_version = true; } else if ((p = StringAfterPrefix(line, DIRECTORY_FS_CHARSET))) { - if (found_charset) { - error.Set(db_domain, "Duplicate charset line"); - return false; - } + if (found_charset) + throw std::runtime_error("Duplicate charset line"); found_charset = true; const char *new_charset = p; const char *const old_charset = GetFSCharset(); if (*old_charset != 0 - && strcmp(new_charset, old_charset) != 0) { - error.Format(db_domain, - "Existing database has charset " - "\"%s\" instead of \"%s\"; " - "discarding database file", - new_charset, old_charset); - return false; - } + && strcmp(new_charset, old_charset) != 0) + throw FormatRuntimeError("Existing database has charset " + "\"%s\" instead of \"%s\"; " + "discarding database file", + new_charset, old_charset); } else if ((p = StringAfterPrefix(line, DB_TAG_PREFIX))) { const char *name = p; TagType tag = tag_name_parse(name); - if (tag == TAG_NUM_OF_ITEM_TYPES) { - error.Format(db_domain, - "Unrecognized tag '%s', " - "discarding database file", - name); - return false; - } + if (tag == TAG_NUM_OF_ITEM_TYPES) + throw FormatRuntimeError("Unrecognized tag '%s', " + "discarding database file", + name); tags[tag] = true; } else { - error.Format(db_domain, "Malformed line: %s", line); - return false; + throw FormatRuntimeError("Malformed line: %s", line); } } - if (format < OLDEST_DB_FORMAT || format > DB_FORMAT) { - error.Set(db_domain, - "Database format mismatch, " - "discarding database file"); - return false; - } + if (format < OLDEST_DB_FORMAT || format > DB_FORMAT) + throw std::runtime_error("Database format mismatch, " + "discarding database file"); - for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) { - if (IsTagEnabled(i) && !tags[i]) { - error.Set(db_domain, - "Tag list mismatch, " - "discarding database file"); - return false; - } - } + for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) + if (IsTagEnabled(i) && !tags[i]) + throw std::runtime_error("Tag list mismatch, " + "discarding database file"); LogDebug(db_domain, "reading DB"); const ScopeDatabaseLock protect; - return directory_load(file, music_root, error); + directory_load(file, music_root); } diff --git a/src/db/plugins/simple/DatabaseSave.hxx b/src/db/plugins/simple/DatabaseSave.hxx index 32929a14e..dd5341d32 100644 --- a/src/db/plugins/simple/DatabaseSave.hxx +++ b/src/db/plugins/simple/DatabaseSave.hxx @@ -28,7 +28,10 @@ class Error; void db_save_internal(BufferedOutputStream &os, const Directory &root); -bool -db_load_internal(TextFile &file, Directory &root, Error &error); +/** + * Throws #std::runtime_error on error. + */ +void +db_load_internal(TextFile &file, Directory &root); #endif diff --git a/src/db/plugins/simple/DirectorySave.cxx b/src/db/plugins/simple/DirectorySave.cxx index 83664eb4f..27c602d11 100644 --- a/src/db/plugins/simple/DirectorySave.cxx +++ b/src/db/plugins/simple/DirectorySave.cxx @@ -28,8 +28,7 @@ #include "fs/io/BufferedOutputStream.hxx" #include "util/StringCompare.hxx" #include "util/NumberParser.hxx" -#include "util/Error.hxx" -#include "util/Domain.hxx" +#include "util/RuntimeError.hxx" #include <string.h> @@ -39,8 +38,6 @@ #define DIRECTORY_BEGIN "begin: " #define DIRECTORY_END "end: " -static constexpr Domain directory_domain("directory"); - gcc_const static const char * DeviceToTypeString(unsigned device) @@ -115,49 +112,37 @@ ParseLine(Directory &directory, const char *line) } static Directory * -directory_load_subdir(TextFile &file, Directory &parent, const char *name, - Error &error) +directory_load_subdir(TextFile &file, Directory &parent, const char *name) { - bool success; - - if (parent.FindChild(name) != nullptr) { - error.Format(directory_domain, - "Duplicate subdirectory '%s'", name); - return nullptr; - } + if (parent.FindChild(name) != nullptr) + throw FormatRuntimeError("Duplicate subdirectory '%s'", name); Directory *directory = parent.CreateChild(name); - while (true) { - const char *line = file.ReadLine(); - if (line == nullptr) { - error.Set(directory_domain, "Unexpected end of file"); - directory->Delete(); - return nullptr; - } + try { + while (true) { + const char *line = file.ReadLine(); + if (line == nullptr) + throw std::runtime_error("Unexpected end of file"); - if (StringStartsWith(line, DIRECTORY_BEGIN)) - break; + if (StringStartsWith(line, DIRECTORY_BEGIN)) + break; - if (!ParseLine(*directory, line)) { - error.Format(directory_domain, - "Malformed line: %s", line); - directory->Delete(); - return nullptr; + if (!ParseLine(*directory, line)) + throw FormatRuntimeError("Malformed line: %s", line); } - } - success = directory_load(file, *directory, error); - if (!success) { + directory_load(file, *directory); + } catch (...) { directory->Delete(); - return nullptr; + throw; } return directory; } -bool -directory_load(TextFile &file, Directory &directory, Error &error) +void +directory_load(TextFile &file, Directory &directory) { const char *line; @@ -165,38 +150,23 @@ directory_load(TextFile &file, Directory &directory, Error &error) !StringStartsWith(line, DIRECTORY_END)) { const char *p; if ((p = StringAfterPrefix(line, DIRECTORY_DIR))) { - Directory *subdir = - directory_load_subdir(file, directory, - p, error); - if (subdir == nullptr) - return false; + directory_load_subdir(file, directory, p); } else if ((p = StringAfterPrefix(line, SONG_BEGIN))) { const char *name = p; - if (directory.FindSong(name) != nullptr) { - error.Format(directory_domain, - "Duplicate song '%s'", name); - return false; - } + if (directory.FindSong(name) != nullptr) + throw FormatRuntimeError("Duplicate song '%s'", name); - DetachedSong *song = song_load(file, name, error); - if (song == nullptr) - return false; + DetachedSong *song = song_load(file, name); directory.AddSong(Song::NewFrom(std::move(*song), directory)); delete song; } else if ((p = StringAfterPrefix(line, PLAYLIST_META_BEGIN))) { const char *name = p; - if (!playlist_metadata_load(file, directory.playlists, - name, error)) - return false; + playlist_metadata_load(file, directory.playlists, name); } else { - error.Format(directory_domain, - "Malformed line: %s", line); - return false; + throw FormatRuntimeError("Malformed line: %s", line); } } - - return true; } diff --git a/src/db/plugins/simple/DirectorySave.hxx b/src/db/plugins/simple/DirectorySave.hxx index 6bb781c48..3737710f9 100644 --- a/src/db/plugins/simple/DirectorySave.hxx +++ b/src/db/plugins/simple/DirectorySave.hxx @@ -28,7 +28,10 @@ class Error; void directory_save(BufferedOutputStream &os, const Directory &directory); -bool -directory_load(TextFile &file, Directory &directory, Error &error); +/** + * Throws #std::runtime_error on error. + */ +void +directory_load(TextFile &file, Directory &directory); #endif diff --git a/src/db/plugins/simple/SimpleDatabasePlugin.cxx b/src/db/plugins/simple/SimpleDatabasePlugin.cxx index 768abc57c..54d9372de 100644 --- a/src/db/plugins/simple/SimpleDatabasePlugin.cxx +++ b/src/db/plugins/simple/SimpleDatabasePlugin.cxx @@ -142,22 +142,19 @@ SimpleDatabase::Check() const #endif } -bool -SimpleDatabase::Load(Error &error) +void +SimpleDatabase::Load() { assert(!path.IsNull()); assert(root != nullptr); TextFile file(path); - if (!db_load_internal(file, *root, error)) - return false; + db_load_internal(file, *root); FileInfo fi; if (GetFileInfo(path, fi)) mtime = fi.GetModificationTime(); - - return true; } void @@ -173,16 +170,7 @@ SimpleDatabase::Open() #endif try { - Error error2; - if (!Load(error2)) { - LogError(error2); - - delete root; - - Check(); - - root = Directory::NewRoot(); - } + Load(); } catch (const std::exception &e) { LogError(e); diff --git a/src/db/plugins/simple/SimpleDatabasePlugin.hxx b/src/db/plugins/simple/SimpleDatabasePlugin.hxx index 40815f2c1..0d96fdbba 100644 --- a/src/db/plugins/simple/SimpleDatabasePlugin.hxx +++ b/src/db/plugins/simple/SimpleDatabasePlugin.hxx @@ -136,7 +136,10 @@ private: void Check() const; - bool Load(Error &error); + /** + * Throws #std::runtime_error on error. + */ + void Load(); Database *LockUmountSteal(const char *uri); }; diff --git a/src/queue/QueueSave.cxx b/src/queue/QueueSave.cxx index a67172795..f1fa89497 100644 --- a/src/queue/QueueSave.cxx +++ b/src/queue/QueueSave.cxx @@ -27,7 +27,6 @@ #include "fs/io/TextFile.hxx" #include "fs/io/BufferedOutputStream.hxx" #include "util/StringCompare.hxx" -#include "util/Error.hxx" #include "Log.hxx" #include <stdlib.h> @@ -95,10 +94,10 @@ queue_load_song(TextFile &file, const SongLoader &loader, if ((p = StringAfterPrefix(line, SONG_BEGIN))) { const char *uri = p; - Error error; - song = song_load(file, uri, error); - if (song == nullptr) { - LogError(error); + try { + song = song_load(file, uri); + } catch (const std::runtime_error &e) { + LogError(e); return; } } else { |