diff options
author | Max Kellermann <max@duempel.org> | 2013-06-24 16:17:46 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-06-24 16:17:46 +0200 |
commit | ef48eca9cac875f65622daf500bd702e20d666f3 (patch) | |
tree | 52fe455c347fd0f0ab14b471e2482c17c8aed5e6 | |
parent | 906d2fbadf075b7d8e3a5be7134a9bebb09b7285 (diff) | |
parent | e9e55b08127dc45b4c6045e1f42e34115086a521 (diff) | |
download | mpd-ef48eca9cac875f65622daf500bd702e20d666f3.tar.gz mpd-ef48eca9cac875f65622daf500bd702e20d666f3.tar.xz mpd-ef48eca9cac875f65622daf500bd702e20d666f3.zip |
Merge branch 'master' of git://git.musicpd.org/dk/mpd
Diffstat (limited to '')
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile.am | 30 | ||||
-rw-r--r-- | src/ClientFile.cxx | 7 | ||||
-rw-r--r-- | src/ClientFile.hxx | 3 | ||||
-rw-r--r-- | src/DecoderList.cxx | 2 | ||||
-rw-r--r-- | src/ExcludeList.cxx | 6 | ||||
-rw-r--r-- | src/ExcludeList.hxx | 2 | ||||
-rw-r--r-- | src/Mapper.cxx | 2 | ||||
-rw-r--r-- | src/OtherCommands.cxx | 10 | ||||
-rw-r--r-- | src/PlaylistFile.cxx | 51 | ||||
-rw-r--r-- | src/QueueCommands.cxx | 27 | ||||
-rw-r--r-- | src/TextInputStream.cxx (renamed from src/text_input_stream.c) | 75 | ||||
-rw-r--r-- | src/TextInputStream.hxx | 59 | ||||
-rw-r--r-- | src/Timer.cxx (renamed from src/timer.c) | 47 | ||||
-rw-r--r-- | src/Timer.hxx (renamed from src/timer.h) | 44 | ||||
-rw-r--r-- | src/UpdateWalk.cxx | 50 | ||||
-rw-r--r-- | src/clock.h | 8 | ||||
-rw-r--r-- | src/decoder/FlacCommon.cxx (renamed from src/decoder/FLACCommon.cxx) | 8 | ||||
-rw-r--r-- | src/decoder/FlacCommon.hxx (renamed from src/decoder/FLACCommon.hxx) | 4 | ||||
-rw-r--r-- | src/decoder/FlacDecoderPlugin.cxx (renamed from src/decoder/FLACDecoderPlugin.cxx) | 38 | ||||
-rw-r--r-- | src/decoder/FlacDecoderPlugin.h (renamed from src/decoder/FLACDecoderPlugin.h) | 0 | ||||
-rw-r--r-- | src/decoder/FlacIOHandle.cxx (renamed from src/decoder/FLACIOHandle.cxx) | 28 | ||||
-rw-r--r-- | src/decoder/FlacIOHandle.hxx (renamed from src/decoder/FLACIOHandle.hxx) | 4 | ||||
-rw-r--r-- | src/decoder/FlacInput.cxx (renamed from src/decoder/FLACInput.cxx) | 38 | ||||
-rw-r--r-- | src/decoder/FlacInput.hxx (renamed from src/decoder/FLACInput.hxx) | 4 | ||||
-rw-r--r-- | src/decoder/FlacMetadata.cxx (renamed from src/decoder/FLACMetaData.cxx) | 4 | ||||
-rw-r--r-- | src/decoder/FlacMetadata.hxx (renamed from src/decoder/FLACMetaData.hxx) | 14 | ||||
-rw-r--r-- | src/decoder/FlacPcm.cxx (renamed from src/decoder/FLAC_PCM.cxx) | 2 | ||||
-rw-r--r-- | src/decoder/FlacPcm.hxx (renamed from src/decoder/FLAC_PCM.hxx) | 0 | ||||
-rw-r--r-- | src/fs/DirectoryReader.hxx | 8 | ||||
-rw-r--r-- | src/output/FifoOutputPlugin.cxx | 20 | ||||
-rw-r--r-- | src/output/HttpdInternal.hxx | 6 | ||||
-rw-r--r-- | src/output/HttpdOutputPlugin.cxx | 16 | ||||
-rw-r--r-- | src/output/NullOutputPlugin.cxx | 22 | ||||
-rw-r--r-- | src/playlist/CuePlaylistPlugin.cxx | 16 | ||||
-rw-r--r-- | src/playlist/ExtM3uPlaylistPlugin.cxx | 38 | ||||
-rw-r--r-- | src/playlist/M3uPlaylistPlugin.cxx | 27 | ||||
-rw-r--r-- | src/text_input_stream.h | 52 | ||||
-rw-r--r-- | test/dump_text_file.cxx | 22 |
39 files changed, 392 insertions, 404 deletions
diff --git a/.gitignore b/.gitignore index abb37b0db..a20d3c54b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.la *.lo *.o +*.exe .deps .dirstamp Makefile @@ -31,7 +32,6 @@ ltmain.sh missing mkinstalldirs mpd -mpd.exe mpd.service stamp-h1 tags diff --git a/Makefile.am b/Makefile.am index 5fd0a74f7..19db8b644 100644 --- a/Makefile.am +++ b/Makefile.am @@ -70,7 +70,7 @@ mpd_headers = \ src/gcc.h \ src/decoder/pcm_decoder_plugin.h \ src/input_stream.h \ - src/text_input_stream.h \ + src/TextInputStream.hxx \ src/ls.h \ src/mixer_plugin.h \ src/daemon.h \ @@ -94,7 +94,7 @@ mpd_headers = \ src/tag_ape.h \ src/tag_id3.h \ src/tag_rva2.h \ - src/timer.h \ + src/Timer.hxx \ src/mpd_error.h src_mpd_SOURCES = \ @@ -238,12 +238,12 @@ src_mpd_SOURCES = \ src/tag_handler.c src/tag_handler.h \ src/TagFile.cxx src/TagFile.hxx \ src/TextFile.cxx src/TextFile.hxx \ - src/text_input_stream.c \ + src/TextInputStream.cxx \ src/Volume.cxx src/Volume.hxx \ src/SongFilter.cxx src/SongFilter.hxx \ src/SongPointer.hxx \ src/PlaylistFile.cxx src/PlaylistFile.hxx \ - src/timer.c + src/Timer.cxx # # Windows resource file @@ -555,13 +555,13 @@ endif if HAVE_FLAC libdecoder_plugins_a_SOURCES += \ - src/decoder/FLACInput.cxx src/decoder/FLACInput.hxx \ - src/decoder/FLACIOHandle.cxx src/decoder/FLACIOHandle.hxx \ - src/decoder/FLACMetaData.cxx src/decoder/FLACMetaData.hxx \ - src/decoder/FLAC_PCM.cxx src/decoder/FLAC_PCM.hxx \ - src/decoder/FLACCommon.cxx src/decoder/FLACCommon.hxx \ - src/decoder/FLACDecoderPlugin.cxx \ - src/decoder/FLACDecoderPlugin.h + src/decoder/FlacInput.cxx src/decoder/FlacInput.hxx \ + src/decoder/FlacIOHandle.cxx src/decoder/FlacIOHandle.hxx \ + src/decoder/FlacMetadata.cxx src/decoder/FlacMetadata.hxx \ + src/decoder/FlacPcm.cxx src/decoder/FlacPcm.hxx \ + src/decoder/FlacCommon.cxx src/decoder/FlacCommon.hxx \ + src/decoder/FlacDecoderPlugin.cxx \ + src/decoder/FlacDecoderPlugin.h endif if HAVE_AUDIOFILE @@ -1108,7 +1108,7 @@ test_dump_text_file_SOURCES = test/dump_text_file.cxx \ test/stdbin.h \ src/IOThread.cxx \ src/Tag.cxx src/TagNames.c src/TagPool.cxx \ - src/text_input_stream.c \ + src/TextInputStream.cxx \ src/fd_util.c test_dump_playlist_LDADD = \ @@ -1130,14 +1130,14 @@ test_dump_playlist_SOURCES = test/dump_playlist.cxx \ src/Song.cxx src/Tag.cxx src/TagNames.c src/TagPool.cxx src/TagSave.cxx \ src/tag_handler.c src/TagFile.cxx \ src/audio_check.c \ - src/text_input_stream.c \ + src/TextInputStream.cxx \ src/cue/CueParser.cxx src/cue/CueParser.hxx \ src/fd_util.c if HAVE_FLAC test_dump_playlist_SOURCES += \ src/ReplayGainInfo.cxx \ - src/decoder/FLACMetaData.cxx + src/decoder/FlacMetadata.cxx endif test_run_decoder_LDADD = \ @@ -1299,7 +1299,7 @@ test_run_output_SOURCES = test/run_output.cxx \ src/audio_check.c \ src/audio_format.c \ src/AudioParser.cxx \ - src/timer.c src/clock.c \ + src/Timer.cxx src/clock.c \ src/Tag.cxx src/TagNames.c src/TagPool.cxx \ src/Page.cxx \ src/SocketUtil.cxx \ diff --git a/src/ClientFile.cxx b/src/ClientFile.cxx index ca5acb229..07fa767b0 100644 --- a/src/ClientFile.cxx +++ b/src/ClientFile.cxx @@ -17,10 +17,13 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "config.h" #include "ClientFile.hxx" #include "Client.hxx" #include "ack.h" #include "io_error.h" +#include "fs/Path.hxx" +#include "fs/FileSystem.hxx" #include <sys/stat.h> #include <sys/types.h> @@ -28,7 +31,7 @@ #include <unistd.h> bool -client_allow_file(const Client *client, const char *path_fs, +client_allow_file(const Client *client, const Path &path_fs, GError **error_r) { #ifdef WIN32 @@ -53,7 +56,7 @@ client_allow_file(const Client *client, const char *path_fs, } struct stat st; - if (stat(path_fs, &st) < 0) { + if (!StatFile(path_fs, st)) { set_error_errno(error_r); return false; } diff --git a/src/ClientFile.hxx b/src/ClientFile.hxx index 48e00c44f..c2ea25f51 100644 --- a/src/ClientFile.hxx +++ b/src/ClientFile.hxx @@ -25,6 +25,7 @@ #include <stdbool.h> class Client; +class Path; /** * Is this client allowed to use the specified local file? @@ -37,7 +38,7 @@ class Client; * @return true if access is allowed */ bool -client_allow_file(const Client *client, const char *path_fs, +client_allow_file(const Client *client, const Path &path_fs, GError **error_r); #endif diff --git a/src/DecoderList.cxx b/src/DecoderList.cxx index aa37c105e..1d33d5deb 100644 --- a/src/DecoderList.cxx +++ b/src/DecoderList.cxx @@ -25,7 +25,7 @@ #include "decoder/pcm_decoder_plugin.h" #include "decoder/dsdiff_decoder_plugin.h" #include "decoder/dsf_decoder_plugin.h" -#include "decoder/FLACDecoderPlugin.h" +#include "decoder/FlacDecoderPlugin.h" #include "decoder/OpusDecoderPlugin.h" #include "decoder/VorbisDecoderPlugin.h" #include "decoder/AdPlugDecoderPlugin.h" diff --git a/src/ExcludeList.cxx b/src/ExcludeList.cxx index 0daa432dd..ed04b8d38 100644 --- a/src/ExcludeList.cxx +++ b/src/ExcludeList.cxx @@ -63,14 +63,14 @@ ExcludeList::LoadFile(const Path &path_fs) } bool -ExcludeList::Check(const char *name_fs) const +ExcludeList::Check(const Path &name_fs) const { - assert(name_fs != NULL); + assert(!name_fs.IsNull()); /* XXX include full path name in check */ for (const auto &i : patterns) - if (i.Check(name_fs)) + if (i.Check(name_fs.c_str())) return true; return false; diff --git a/src/ExcludeList.hxx b/src/ExcludeList.hxx index f3dc1f057..7111465a3 100644 --- a/src/ExcludeList.hxx +++ b/src/ExcludeList.hxx @@ -73,7 +73,7 @@ public: * Checks whether one of the patterns in the .mpdignore file matches * the specified file name. */ - bool Check(const char *name_fs) const; + bool Check(const Path &name_fs) const; }; diff --git a/src/Mapper.cxx b/src/Mapper.cxx index 559df0d23..40e762e4b 100644 --- a/src/Mapper.cxx +++ b/src/Mapper.cxx @@ -100,7 +100,7 @@ check_directory(const char *path_utf8, const Path &path_fs) #endif const DirectoryReader reader(path_fs); - if (reader.Failed() && errno == EACCES) + if (reader.HasFailed() && errno == EACCES) g_warning("No permission to read directory: %s", path_utf8); } diff --git a/src/OtherCommands.cxx b/src/OtherCommands.cxx index 07fbc8442..7f592ee9f 100644 --- a/src/OtherCommands.cxx +++ b/src/OtherCommands.cxx @@ -34,6 +34,7 @@ #include "ls.hxx" #include "Volume.hxx" #include "util/UriUtil.hxx" +#include "fs/Path.hxx" extern "C" { #include "stats.h" @@ -117,9 +118,16 @@ handle_lsinfo(Client *client, int argc, char *argv[]) if (strncmp(uri, "file:///", 8) == 0) { /* print information about an arbitrary local file */ const char *path_utf8 = uri + 7; + const Path path_fs = Path::FromUTF8(path_utf8); + + if (path_fs.IsNull()) { + command_error(client, ACK_ERROR_NO_EXIST, + "unsupported file name"); + return COMMAND_RETURN_ERROR; + } GError *error = NULL; - if (!client_allow_file(client, path_utf8, &error)) + if (!client_allow_file(client, path_fs, &error)) return print_error(client, error); struct song *song = song_file_load(path_utf8, NULL); diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index 662c4de96..8cc009e0c 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -32,6 +32,7 @@ #include "Idle.hxx" #include "fs/Path.hxx" #include "fs/FileSystem.hxx" +#include "fs/DirectoryReader.hxx" #include "util/UriUtil.hxx" #include <assert.h> @@ -77,7 +78,7 @@ spl_valid_name(const char *name_utf8) strchr(name_utf8, '\r') == NULL; } -static const char * +static const Path & spl_map(GError **error_r) { const Path &path_fs = map_spl_path(); @@ -85,8 +86,7 @@ spl_map(GError **error_r) g_set_error_literal(error_r, playlist_quark(), PLAYLIST_RESULT_DISABLED, "Stored playlists are disabled"); - - return path_fs.c_str(); + return path_fs; } static bool @@ -105,8 +105,7 @@ spl_check_name(const char *name_utf8, GError **error_r) static Path spl_map_to_fs(const char *name_utf8, GError **error_r) { - if (spl_map(error_r) == NULL || - !spl_check_name(name_utf8, error_r)) + if (spl_map(error_r).IsNull() || !spl_check_name(name_utf8, error_r)) return Path::Null(); Path path_fs = map_spl_utf8_to_fs(name_utf8); @@ -139,25 +138,24 @@ playlist_errno(GError **error_r) static bool LoadPlaylistFileInfo(PlaylistInfo &info, - const char *parent_path_fs, const char *name_fs) + const Path &parent_path_fs, const Path &name_fs) { - size_t name_length = strlen(name_fs); + const char *name_fs_str = name_fs.c_str(); + size_t name_length = strlen(name_fs_str); if (name_length < sizeof(PLAYLIST_FILE_SUFFIX) || - memchr(name_fs, '\n', name_length) != NULL) + memchr(name_fs_str, '\n', name_length) != NULL) return false; - if (!g_str_has_suffix(name_fs, PLAYLIST_FILE_SUFFIX)) + if (!g_str_has_suffix(name_fs_str, PLAYLIST_FILE_SUFFIX)) return false; - char *path_fs = g_build_filename(parent_path_fs, name_fs, NULL); + Path path_fs = Path::Build(parent_path_fs, name_fs); struct stat st; - int ret = stat(path_fs, &st); - g_free(path_fs); - if (ret < 0 || !S_ISREG(st.st_mode)) + if (!StatFile(path_fs, st) || !S_ISREG(st.st_mode)) return false; - char *name = g_strndup(name_fs, + char *name = g_strndup(name_fs_str, name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX)); std::string name_utf8 = Path::ToUTF8(name); g_free(name); @@ -174,24 +172,23 @@ ListPlaylistFiles(GError **error_r) { PlaylistVector list; - const char *parent_path_fs = spl_map(error_r); - if (parent_path_fs == NULL) + const Path &parent_path_fs = spl_map(error_r); + if (parent_path_fs.IsNull()) return list; - DIR *dir = opendir(parent_path_fs); - if (dir == NULL) { + DirectoryReader reader(parent_path_fs); + if (reader.HasFailed()) { set_error_errno(error_r); return list; } PlaylistInfo info; - struct dirent *ent; - while ((ent = readdir(dir)) != NULL) { - if (LoadPlaylistFileInfo(info, parent_path_fs, ent->d_name)) + while (reader.ReadEntry()) { + const Path entry = reader.GetEntry(); + if (LoadPlaylistFileInfo(info, parent_path_fs, entry)) list.push_back(std::move(info)); } - closedir(dir); return list; } @@ -201,7 +198,7 @@ SavePlaylistFile(const PlaylistFileContents &contents, const char *utf8path, { assert(utf8path != NULL); - if (spl_map(error_r) == NULL) + if (spl_map(error_r).IsNull()) return false; const Path path_fs = spl_map_to_fs(utf8path, error_r); @@ -226,7 +223,7 @@ LoadPlaylistFile(const char *utf8path, GError **error_r) { PlaylistFileContents contents; - if (spl_map(error_r) == NULL) + if (spl_map(error_r).IsNull()) return contents; const Path path_fs = spl_map_to_fs(utf8path, error_r); @@ -302,7 +299,7 @@ spl_move_index(const char *utf8path, unsigned src, unsigned dest, bool spl_clear(const char *utf8path, GError **error_r) { - if (spl_map(error_r) == NULL) + if (spl_map(error_r).IsNull()) return false; const Path path_fs = spl_map_to_fs(utf8path, error_r); @@ -365,7 +362,7 @@ spl_remove_index(const char *utf8path, unsigned pos, GError **error_r) bool spl_append_song(const char *utf8path, struct song *song, GError **error_r) { - if (spl_map(error_r) == NULL) + if (spl_map(error_r).IsNull()) return false; const Path path_fs = spl_map_to_fs(utf8path, error_r); @@ -454,7 +451,7 @@ spl_rename_internal(const Path &from_path_fs, const Path &to_path_fs, bool spl_rename(const char *utf8from, const char *utf8to, GError **error_r) { - if (spl_map(error_r) == NULL) + if (spl_map(error_r).IsNull()) return false; Path from_path_fs = spl_map_to_fs(utf8from, error_r); diff --git a/src/QueueCommands.cxx b/src/QueueCommands.cxx index aac6d5a51..210b1501a 100644 --- a/src/QueueCommands.cxx +++ b/src/QueueCommands.cxx @@ -32,6 +32,7 @@ #include "protocol/Result.hxx" #include "ls.hxx" #include "util/UriUtil.hxx" +#include "fs/Path.hxx" #include <string.h> @@ -42,13 +43,20 @@ handle_add(Client *client, G_GNUC_UNUSED int argc, char *argv[]) enum playlist_result result; if (strncmp(uri, "file:///", 8) == 0) { - const char *path = uri + 7; + const char *path_utf8 = uri + 7; + const Path path_fs = Path::FromUTF8(path_utf8); + + if (path_fs.IsNull()) { + command_error(client, ACK_ERROR_NO_EXIST, + "unsupported file name"); + return COMMAND_RETURN_ERROR; + } GError *error = NULL; - if (!client_allow_file(client, path, &error)) + if (!client_allow_file(client, path_fs, &error)) return print_error(client, error); - result = client->partition.AppendFile(path); + result = client->partition.AppendFile(path_utf8); return print_playlist_result(client, result); } @@ -78,13 +86,20 @@ handle_addid(Client *client, int argc, char *argv[]) enum playlist_result result; if (strncmp(uri, "file:///", 8) == 0) { - const char *path = uri + 7; + const char *path_utf8 = uri + 7; + const Path path_fs = Path::FromUTF8(path_utf8); + + if (path_fs.IsNull()) { + command_error(client, ACK_ERROR_NO_EXIST, + "unsupported file name"); + return COMMAND_RETURN_ERROR; + } GError *error = NULL; - if (!client_allow_file(client, path, &error)) + if (!client_allow_file(client, path_fs, &error)) return print_error(client, error); - result = client->partition.AppendFile(path, &added_id); + result = client->partition.AppendFile(path_utf8, &added_id); } else { if (uri_has_scheme(uri) && !uri_supported_scheme(uri)) { command_error(client, ACK_ERROR_NO_EXIST, diff --git a/src/text_input_stream.c b/src/TextInputStream.cxx index 4a2eeb817..5e963ae01 100644 --- a/src/text_input_stream.c +++ b/src/TextInputStream.cxx @@ -18,7 +18,7 @@ */ #include "config.h" -#include "text_input_stream.h" +#include "TextInputStream.hxx" #include "input_stream.h" #include "util/fifo_buffer.h" @@ -27,85 +27,66 @@ #include <assert.h> #include <string.h> -struct text_input_stream { - struct input_stream *is; - - struct fifo_buffer *buffer; - - char *line; -}; - -struct text_input_stream * -text_input_stream_new(struct input_stream *is) +TextInputStream::TextInputStream(struct input_stream *_is) + : is(_is), + buffer(fifo_buffer_new(4096)) { - struct text_input_stream *tis = g_new(struct text_input_stream, 1); - - tis->is = is; - tis->buffer = fifo_buffer_new(4096); - tis->line = NULL; - - return tis; } -void -text_input_stream_free(struct text_input_stream *tis) +TextInputStream::~TextInputStream() { - fifo_buffer_free(tis->buffer); - g_free(tis->line); - g_free(tis); + fifo_buffer_free(buffer); } -const char * -text_input_stream_read(struct text_input_stream *tis) +bool TextInputStream::ReadLine(std::string &line) { - GError *error = NULL; + GError *error = nullptr; void *dest; const char *src, *p; size_t length, nbytes; - g_free(tis->line); - tis->line = NULL; - do { - dest = fifo_buffer_write(tis->buffer, &length); - if (dest != NULL && length >= 2) { + dest = fifo_buffer_write(buffer, &length); + if (dest != nullptr && length >= 2) { /* reserve one byte for the null terminator if the last line is not terminated by a newline character */ --length; - nbytes = input_stream_lock_read(tis->is, dest, length, + nbytes = input_stream_lock_read(is, dest, length, &error); if (nbytes > 0) - fifo_buffer_append(tis->buffer, nbytes); - else if (error != NULL) { + fifo_buffer_append(buffer, nbytes); + else if (error != nullptr) { g_warning("%s", error->message); g_error_free(error); - return NULL; + return false; } } else nbytes = 0; - src = fifo_buffer_read(tis->buffer, &length); - if (src == NULL) - return NULL; + auto src_p = fifo_buffer_read(buffer, &length); + src = reinterpret_cast<const char *>(src_p); + + if (src == nullptr) + return false; - p = memchr(src, '\n', length); - if (p == NULL && nbytes == 0) { + p = reinterpret_cast<const char*>(memchr(src, '\n', length)); + if (p == nullptr && nbytes == 0) { /* end of file (or line too long): terminate the current line */ - dest = fifo_buffer_write(tis->buffer, &nbytes); - assert(dest != NULL); + dest = fifo_buffer_write(buffer, &nbytes); + assert(dest != nullptr); *(char *)dest = '\n'; - fifo_buffer_append(tis->buffer, 1); + fifo_buffer_append(buffer, 1); } - } while (p == NULL); + } while (p == nullptr); length = p - src + 1; while (p > src && g_ascii_isspace(p[-1])) --p; - tis->line = g_strndup(src, p - src); - fifo_buffer_consume(tis->buffer, length); - return tis->line; + line = std::string(src, p - src); + fifo_buffer_consume(buffer, length); + return true; } diff --git a/src/TextInputStream.hxx b/src/TextInputStream.hxx new file mode 100644 index 000000000..2608184e2 --- /dev/null +++ b/src/TextInputStream.hxx @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_TEXT_INPUT_STREAM_HXX +#define MPD_TEXT_INPUT_STREAM_HXX + +#include <string> + +struct input_stream; +struct fifo_buffer; + +class TextInputStream { + struct input_stream *is; + struct fifo_buffer *buffer; +public: + /** + * Wraps an existing #input_stream object into a #TextInputStream, + * to read its contents as text lines. + * + * @param _is an open #input_stream object + */ + explicit TextInputStream(struct input_stream *_is); + + /** + * Frees the #TextInputStream object. Does not close or free the + * underlying #input_stream. + */ + ~TextInputStream(); + + TextInputStream(const TextInputStream &) = delete; + TextInputStream& operator=(const TextInputStream &) = delete; + + /** + * Reads the next line from the stream with newline character stripped. + * + * @param line a string to put result to + * @return true if line is read successfully, false on end of file + * or error + */ + bool ReadLine(std::string &line); +}; + +#endif diff --git a/src/timer.c b/src/Timer.cxx index 9a3228465..7ddbda3da 100644 --- a/src/timer.c +++ b/src/Timer.cxx @@ -18,7 +18,7 @@ */ #include "config.h" -#include "timer.h" +#include "Timer.hxx" #include "audio_format.h" #include "clock.h" @@ -28,46 +28,37 @@ #include <limits.h> #include <stddef.h> -struct timer *timer_new(const struct audio_format *af) +Timer::Timer(const struct audio_format &af) + : time(0), + started(false), + rate(af.sample_rate * audio_format_frame_size(&af)) { - struct timer *timer = g_new(struct timer, 1); - timer->time = 0; // us - timer->started = 0; // false - timer->rate = af->sample_rate * audio_format_frame_size(af); // samples per second - - return timer; -} - -void timer_free(struct timer *timer) -{ - g_free(timer); } -void timer_start(struct timer *timer) +void Timer::Start() { - timer->time = monotonic_clock_us(); - timer->started = 1; + time = monotonic_clock_us(); + started = true; } -void timer_reset(struct timer *timer) +void Timer::Reset() { - timer->time = 0; - timer->started = 0; + time = 0; + started = false; } -void timer_add(struct timer *timer, int size) +void Timer::Add(int size) { - assert(timer->started); + assert(started); // (size samples) / (rate samples per second) = duration seconds // duration seconds * 1000000 = duration us - timer->time += ((uint64_t)size * 1000000) / timer->rate; + time += ((uint64_t)size * 1000000) / rate; } -unsigned -timer_delay(const struct timer *timer) +unsigned Timer::GetDelay() const { - int64_t delay = (int64_t)(timer->time - monotonic_clock_us()) / 1000; + int64_t delay = (int64_t)(time - monotonic_clock_us()) / 1000; if (delay < 0) return 0; @@ -77,13 +68,13 @@ timer_delay(const struct timer *timer) return delay; } -void timer_sync(struct timer *timer) +void Timer::Synchronize() const { int64_t sleep_duration; - assert(timer->started); + assert(started); - sleep_duration = timer->time - monotonic_clock_us(); + sleep_duration = time - monotonic_clock_us(); if (sleep_duration > 0) g_usleep(sleep_duration); } diff --git a/src/timer.h b/src/Timer.hxx index 1506c9173..96446a988 100644 --- a/src/timer.h +++ b/src/Timer.hxx @@ -17,43 +17,33 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#ifndef MPD_TIMER_H -#define MPD_TIMER_H +#ifndef MPD_TIMER_HXX +#define MPD_TIMER_HXX #include <stdint.h> struct audio_format; -struct timer { +class Timer { uint64_t time; - int started; - int rate; -}; - -#ifdef __cplusplus -extern "C" { -#endif + bool started; + const int rate; +public: + explicit Timer(const struct audio_format& af); -struct timer *timer_new(const struct audio_format *af); + bool IsStarted() const { return started; } -void timer_free(struct timer *timer); + void Start(); + void Reset(); -void timer_start(struct timer *timer); + void Add(int size); -void timer_reset(struct timer *timer); + /** + * Returns the number of milliseconds to sleep to get back to sync. + */ + unsigned GetDelay() const; -void timer_add(struct timer *timer, int size); - -/** - * Returns the number of milliseconds to sleep to get back to sync. - */ -unsigned -timer_delay(const struct timer *timer); - -void timer_sync(struct timer *timer); - -#ifdef __cplusplus -} -#endif + void Synchronize() const; +}; #endif diff --git a/src/UpdateWalk.cxx b/src/UpdateWalk.cxx index 1d26e0ebd..4b415895e 100644 --- a/src/UpdateWalk.cxx +++ b/src/UpdateWalk.cxx @@ -34,6 +34,7 @@ #include "conf.h" #include "fs/Path.hxx" #include "fs/FileSystem.hxx" +#include "fs/DirectoryReader.hxx" #include "util/UriUtil.hxx" #include <glib.h> @@ -102,7 +103,7 @@ remove_excluded_from_directory(Directory *directory, directory_for_each_child_safe(child, n, directory) { const Path name_fs = Path::FromUTF8(child->GetName()); - if (name_fs.IsNull() || exclude_list.Check(name_fs.c_str())) { + if (name_fs.IsNull() || exclude_list.Check(name_fs)) { delete_directory(child); modified = true; } @@ -113,7 +114,7 @@ remove_excluded_from_directory(Directory *directory, assert(song->parent == directory); const Path name_fs = Path::FromUTF8(song->uri); - if (name_fs.IsNull() || exclude_list.Check(name_fs.c_str())) { + if (name_fs.IsNull() || exclude_list.Check(name_fs)) { delete_song(directory, song); modified = true; } @@ -261,8 +262,9 @@ update_directory_child(Directory *directory, /* we don't look at "." / ".." nor files with newlines in their name */ G_GNUC_PURE -static bool skip_path(const char *path) +static bool skip_path(const Path &path_fs) { + const char *path = path_fs.c_str(); return (path[0] == '.' && path[1] == 0) || (path[0] == '.' && path[1] == '.' && path[2] == 0) || strchr(path, '\n') != NULL; @@ -277,20 +279,11 @@ skip_symlink(const Directory *directory, const char *utf8_name) if (path_fs.IsNull()) return true; - char buffer[MPD_PATH_MAX]; - ssize_t length = readlink(path_fs.c_str(), buffer, sizeof(buffer)); - if (length < 0) + const Path target = ReadLink(path_fs); + if (target.IsNull()) /* don't skip if this is not a symlink */ return errno != EINVAL; - if ((size_t)length >= sizeof(buffer)) - /* skip symlinks when the buffer is too small for the - link target */ - return true; - - /* null-terminate the buffer, because readlink() will not */ - buffer[length] = 0; - if (!follow_inside_symlinks && !follow_outside_symlinks) { /* ignore all symlinks */ return true; @@ -299,16 +292,18 @@ skip_symlink(const Directory *directory, const char *utf8_name) return false; } - if (g_path_is_absolute(buffer)) { + const char *target_str = target.c_str(); + + if (g_path_is_absolute(target_str)) { /* if the symlink points to an absolute path, see if that path is inside the music directory */ - const char *relative = map_to_relative_path(buffer); - return relative > buffer + const char *relative = map_to_relative_path(target_str); + return relative > target_str ? !follow_inside_symlinks : !follow_outside_symlinks; } - const char *p = buffer; + const char *p = target_str; while (*p == '.') { if (p[1] == '.' && G_IS_DIR_SEPARATOR(p[2])) { /* "../" moves to parent directory */ @@ -352,10 +347,12 @@ update_directory(Directory *directory, const struct stat *st) if (path_fs.IsNull()) return false; - DIR *dir = opendir(path_fs.c_str()); - if (!dir) { + DirectoryReader reader(path_fs); + if (reader.HasFailed()) { + int error = errno; + const auto path_utf8 = path_fs.ToUTF8(); g_warning("Failed to open directory %s: %s", - path_fs.c_str(), g_strerror(errno)); + path_utf8.c_str(), g_strerror(error)); return false; } @@ -367,15 +364,16 @@ update_directory(Directory *directory, const struct stat *st) purge_deleted_from_directory(directory); - struct dirent *ent; - while ((ent = readdir(dir))) { + while (reader.ReadEntry()) { std::string utf8; struct stat st2; - if (skip_path(ent->d_name) || exclude_list.Check(ent->d_name)) + const Path entry = reader.GetEntry(); + + if (skip_path(entry) || exclude_list.Check(entry)) continue; - utf8 = Path::ToUTF8(ent->d_name); + utf8 = entry.ToUTF8(); if (utf8.empty()) continue; @@ -390,8 +388,6 @@ update_directory(Directory *directory, const struct stat *st) modified |= delete_name_in(directory, utf8.c_str()); } - closedir(dir); - directory->mtime = st->st_mtime; return true; diff --git a/src/clock.h b/src/clock.h index f1338938f..c98b9a652 100644 --- a/src/clock.h +++ b/src/clock.h @@ -24,6 +24,10 @@ #include <stdint.h> +#ifdef __cplusplus +extern "C" { +#endif + /** * Returns the value of a monotonic clock in milliseconds. */ @@ -38,4 +42,8 @@ gcc_pure uint64_t monotonic_clock_us(void); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/decoder/FLACCommon.cxx b/src/decoder/FlacCommon.cxx index 25fd1f964..425ab55df 100644 --- a/src/decoder/FLACCommon.cxx +++ b/src/decoder/FlacCommon.cxx @@ -22,9 +22,9 @@ */ #include "config.h" -#include "FLACCommon.hxx" -#include "FLACMetaData.hxx" -#include "FLAC_PCM.hxx" +#include "FlacCommon.hxx" +#include "FlacMetadata.hxx" +#include "FlacPcm.hxx" extern "C" { #include "audio_check.h" @@ -36,7 +36,7 @@ extern "C" { flac_data::flac_data(struct decoder *_decoder, struct input_stream *_input_stream) - :FLACInput(_input_stream, _decoder), + :FlacInput(_input_stream, _decoder), initialized(false), unsupported(false), total_frames(0), first_frame(0), next_frame(0), position(0), decoder(_decoder), input_stream(_input_stream), diff --git a/src/decoder/FLACCommon.hxx b/src/decoder/FlacCommon.hxx index 20f93c8c7..0cd295e0d 100644 --- a/src/decoder/FLACCommon.hxx +++ b/src/decoder/FlacCommon.hxx @@ -24,7 +24,7 @@ #ifndef MPD_FLAC_COMMON_HXX #define MPD_FLAC_COMMON_HXX -#include "FLACInput.hxx" +#include "FlacInput.hxx" #include "decoder_api.h" extern "C" { @@ -37,7 +37,7 @@ extern "C" { #undef G_LOG_DOMAIN #define G_LOG_DOMAIN "flac" -struct flac_data : public FLACInput { +struct flac_data : public FlacInput { struct pcm_buffer buffer; /** diff --git a/src/decoder/FLACDecoderPlugin.cxx b/src/decoder/FlacDecoderPlugin.cxx index 43b604097..cd15b4b29 100644 --- a/src/decoder/FLACDecoderPlugin.cxx +++ b/src/decoder/FlacDecoderPlugin.cxx @@ -18,9 +18,9 @@ */ #include "config.h" /* must be first for large file support */ -#include "FLACDecoderPlugin.h" -#include "FLACCommon.hxx" -#include "FLACMetaData.hxx" +#include "FlacDecoderPlugin.h" +#include "FlacCommon.hxx" +#include "FlacMetadata.hxx" #include "OggCodec.hxx" #include <glib.h> @@ -87,7 +87,7 @@ static bool flac_scan_file(const char *file, const struct tag_handler *handler, void *handler_ctx) { - FLACMetadataChain chain; + FlacMetadataChain chain; if (!chain.Read(file)) { g_debug("Failed to read FLAC tags: %s", chain.GetStatusString()); @@ -102,7 +102,7 @@ static bool flac_scan_stream(struct input_stream *is, const struct tag_handler *handler, void *handler_ctx) { - FLACMetadataChain chain; + FlacMetadataChain chain; if (!chain.Read(is)) { g_debug("Failed to read FLAC tags: %s", chain.GetStatusString()); @@ -214,14 +214,14 @@ static FLAC__StreamDecoderInitStatus stream_init_oggflac(FLAC__StreamDecoder *flac_dec, struct flac_data *data) { return FLAC__stream_decoder_init_ogg_stream(flac_dec, - FLACInput::Read, - FLACInput::Seek, - FLACInput::Tell, - FLACInput::Length, - FLACInput::Eof, + FlacInput::Read, + FlacInput::Seek, + FlacInput::Tell, + FlacInput::Length, + FlacInput::Eof, flac_write_cb, flacMetadata, - FLACInput::Error, + FlacInput::Error, data); } @@ -229,14 +229,14 @@ static FLAC__StreamDecoderInitStatus stream_init_flac(FLAC__StreamDecoder *flac_dec, struct flac_data *data) { return FLAC__stream_decoder_init_stream(flac_dec, - FLACInput::Read, - FLACInput::Seek, - FLACInput::Tell, - FLACInput::Length, - FLACInput::Eof, + FlacInput::Read, + FlacInput::Seek, + FlacInput::Tell, + FlacInput::Length, + FlacInput::Eof, flac_write_cb, flacMetadata, - FLACInput::Error, + FlacInput::Error, data); } @@ -298,7 +298,7 @@ static bool oggflac_scan_file(const char *file, const struct tag_handler *handler, void *handler_ctx) { - FLACMetadataChain chain; + FlacMetadataChain chain; if (!chain.ReadOgg(file)) { g_debug("Failed to read OggFLAC tags: %s", chain.GetStatusString()); @@ -313,7 +313,7 @@ static bool oggflac_scan_stream(struct input_stream *is, const struct tag_handler *handler, void *handler_ctx) { - FLACMetadataChain chain; + FlacMetadataChain chain; if (!chain.ReadOgg(is)) { g_debug("Failed to read OggFLAC tags: %s", chain.GetStatusString()); diff --git a/src/decoder/FLACDecoderPlugin.h b/src/decoder/FlacDecoderPlugin.h index c99deeef7..c99deeef7 100644 --- a/src/decoder/FLACDecoderPlugin.h +++ b/src/decoder/FlacDecoderPlugin.h diff --git a/src/decoder/FLACIOHandle.cxx b/src/decoder/FlacIOHandle.cxx index 08ec36e48..16a07a9d1 100644 --- a/src/decoder/FLACIOHandle.cxx +++ b/src/decoder/FlacIOHandle.cxx @@ -18,14 +18,14 @@ */ #include "config.h" -#include "FLACIOHandle.hxx" +#include "FlacIOHandle.hxx" #include "io_error.h" #include "gcc.h" #include <errno.h> static size_t -FLACIORead(void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle) +FlacIORead(void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle) { input_stream *is = (input_stream *)handle; @@ -63,7 +63,7 @@ FLACIORead(void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle) } static int -FLACIOSeek(FLAC__IOHandle handle, FLAC__int64 offset, int whence) +FlacIOSeek(FLAC__IOHandle handle, FLAC__int64 offset, int whence) { input_stream *is = (input_stream *)handle; @@ -71,7 +71,7 @@ FLACIOSeek(FLAC__IOHandle handle, FLAC__int64 offset, int whence) } static FLAC__int64 -FLACIOTell(FLAC__IOHandle handle) +FlacIOTell(FLAC__IOHandle handle) { input_stream *is = (input_stream *)handle; @@ -79,7 +79,7 @@ FLACIOTell(FLAC__IOHandle handle) } static int -FLACIOEof(FLAC__IOHandle handle) +FlacIOEof(FLAC__IOHandle handle) { input_stream *is = (input_stream *)handle; @@ -87,7 +87,7 @@ FLACIOEof(FLAC__IOHandle handle) } static int -FLACIOClose(gcc_unused FLAC__IOHandle handle) +FlacIOClose(gcc_unused FLAC__IOHandle handle) { /* no-op because the libFLAC caller is repsonsible for closing the #input_stream */ @@ -96,19 +96,19 @@ FLACIOClose(gcc_unused FLAC__IOHandle handle) } const FLAC__IOCallbacks flac_io_callbacks = { - FLACIORead, + FlacIORead, nullptr, nullptr, nullptr, - FLACIOEof, - FLACIOClose, + FlacIOEof, + FlacIOClose, }; const FLAC__IOCallbacks flac_io_callbacks_seekable = { - FLACIORead, + FlacIORead, nullptr, - FLACIOSeek, - FLACIOTell, - FLACIOEof, - FLACIOClose, + FlacIOSeek, + FlacIOTell, + FlacIOEof, + FlacIOClose, }; diff --git a/src/decoder/FLACIOHandle.hxx b/src/decoder/FlacIOHandle.hxx index 505d2db1a..3216dafa4 100644 --- a/src/decoder/FLACIOHandle.hxx +++ b/src/decoder/FlacIOHandle.hxx @@ -29,13 +29,13 @@ extern const FLAC__IOCallbacks flac_io_callbacks; extern const FLAC__IOCallbacks flac_io_callbacks_seekable; static inline FLAC__IOHandle -ToFLACIOHandle(input_stream *is) +ToFlacIOHandle(input_stream *is) { return (FLAC__IOHandle)is; } static inline const FLAC__IOCallbacks & -GetFLACIOCallbacks(const input_stream *is) +GetFlacIOCallbacks(const input_stream *is) { return is->seekable ? flac_io_callbacks_seekable diff --git a/src/decoder/FLACInput.cxx b/src/decoder/FlacInput.cxx index ba0a86ce8..f5dedd97b 100644 --- a/src/decoder/FLACInput.cxx +++ b/src/decoder/FlacInput.cxx @@ -18,13 +18,13 @@ */ #include "config.h" -#include "FLACInput.hxx" +#include "FlacInput.hxx" #include "decoder_api.h" #include "gcc.h" #include "InputStream.hxx" FLAC__StreamDecoderReadStatus -FLACInput::Read(FLAC__byte buffer[], size_t *bytes) +FlacInput::Read(FLAC__byte buffer[], size_t *bytes) { size_t r = decoder_read(decoder, input_stream, (void *)buffer, *bytes); *bytes = r; @@ -42,7 +42,7 @@ FLACInput::Read(FLAC__byte buffer[], size_t *bytes) } FLAC__StreamDecoderSeekStatus -FLACInput::Seek(FLAC__uint64 absolute_byte_offset) +FlacInput::Seek(FLAC__uint64 absolute_byte_offset) { if (!input_stream->seekable) return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; @@ -56,7 +56,7 @@ FLACInput::Seek(FLAC__uint64 absolute_byte_offset) } FLAC__StreamDecoderTellStatus -FLACInput::Tell(FLAC__uint64 *absolute_byte_offset) +FlacInput::Tell(FLAC__uint64 *absolute_byte_offset) { if (!input_stream->seekable) return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; @@ -66,7 +66,7 @@ FLACInput::Tell(FLAC__uint64 *absolute_byte_offset) } FLAC__StreamDecoderLengthStatus -FLACInput::Length(FLAC__uint64 *stream_length) +FlacInput::Length(FLAC__uint64 *stream_length) { if (input_stream->size < 0) return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; @@ -76,7 +76,7 @@ FLACInput::Length(FLAC__uint64 *stream_length) } FLAC__bool -FLACInput::Eof() +FlacInput::Eof() { return (decoder != nullptr && decoder_get_command(decoder) != DECODE_COMMAND_NONE && @@ -85,7 +85,7 @@ FLACInput::Eof() } void -FLACInput::Error(FLAC__StreamDecoderErrorStatus status) +FlacInput::Error(FLAC__StreamDecoderErrorStatus status) { if (decoder == nullptr || decoder_get_command(decoder) != DECODE_COMMAND_STOP) @@ -93,56 +93,56 @@ FLACInput::Error(FLAC__StreamDecoderErrorStatus status) } FLAC__StreamDecoderReadStatus -FLACInput::Read(gcc_unused const FLAC__StreamDecoder *flac_decoder, +FlacInput::Read(gcc_unused const FLAC__StreamDecoder *flac_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) { - FLACInput *i = (FLACInput *)client_data; + FlacInput *i = (FlacInput *)client_data; return i->Read(buffer, bytes); } FLAC__StreamDecoderSeekStatus -FLACInput::Seek(gcc_unused const FLAC__StreamDecoder *flac_decoder, +FlacInput::Seek(gcc_unused const FLAC__StreamDecoder *flac_decoder, FLAC__uint64 absolute_byte_offset, void *client_data) { - FLACInput *i = (FLACInput *)client_data; + FlacInput *i = (FlacInput *)client_data; return i->Seek(absolute_byte_offset); } FLAC__StreamDecoderTellStatus -FLACInput::Tell(gcc_unused const FLAC__StreamDecoder *flac_decoder, +FlacInput::Tell(gcc_unused const FLAC__StreamDecoder *flac_decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) { - FLACInput *i = (FLACInput *)client_data; + FlacInput *i = (FlacInput *)client_data; return i->Tell(absolute_byte_offset); } FLAC__StreamDecoderLengthStatus -FLACInput::Length(gcc_unused const FLAC__StreamDecoder *flac_decoder, +FlacInput::Length(gcc_unused const FLAC__StreamDecoder *flac_decoder, FLAC__uint64 *stream_length, void *client_data) { - FLACInput *i = (FLACInput *)client_data; + FlacInput *i = (FlacInput *)client_data; return i->Length(stream_length); } FLAC__bool -FLACInput::Eof(gcc_unused const FLAC__StreamDecoder *flac_decoder, +FlacInput::Eof(gcc_unused const FLAC__StreamDecoder *flac_decoder, void *client_data) { - FLACInput *i = (FLACInput *)client_data; + FlacInput *i = (FlacInput *)client_data; return i->Eof(); } void -FLACInput::Error(gcc_unused const FLAC__StreamDecoder *decoder, +FlacInput::Error(gcc_unused const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) { - FLACInput *i = (FLACInput *)client_data; + FlacInput *i = (FlacInput *)client_data; i->Error(status); } diff --git a/src/decoder/FLACInput.hxx b/src/decoder/FlacInput.hxx index 7661567d1..8fc69f960 100644 --- a/src/decoder/FLACInput.hxx +++ b/src/decoder/FlacInput.hxx @@ -26,13 +26,13 @@ * This class wraps an #input_stream in libFLAC stream decoder * callbacks. */ -class FLACInput { +class FlacInput { struct decoder *decoder; struct input_stream *input_stream; public: - FLACInput(struct input_stream *_input_stream, + FlacInput(struct input_stream *_input_stream, struct decoder *_decoder=nullptr) :decoder(_decoder), input_stream(_input_stream) {} diff --git a/src/decoder/FLACMetaData.cxx b/src/decoder/FlacMetadata.cxx index 8273a230b..694c88c8f 100644 --- a/src/decoder/FLACMetaData.cxx +++ b/src/decoder/FlacMetadata.cxx @@ -18,7 +18,7 @@ */ #include "config.h" -#include "FLACMetaData.hxx" +#include "FlacMetadata.hxx" extern "C" { #include "XiphTags.h" @@ -239,7 +239,7 @@ flac_vorbis_comments_to_tag(struct tag *tag, } void -FLACMetadataChain::Scan(const struct tag_handler *handler, void *handler_ctx) +FlacMetadataChain::Scan(const struct tag_handler *handler, void *handler_ctx) { FLACMetadataIterator iterator(*this); diff --git a/src/decoder/FLACMetaData.hxx b/src/decoder/FlacMetadata.hxx index 0eceec23c..cce34c3a7 100644 --- a/src/decoder/FLACMetaData.hxx +++ b/src/decoder/FlacMetadata.hxx @@ -21,19 +21,19 @@ #define MPD_FLAC_METADATA_H #include "gcc.h" -#include "FLACIOHandle.hxx" +#include "FlacIOHandle.hxx" #include <FLAC/metadata.h> #include <assert.h> -class FLACMetadataChain { +class FlacMetadataChain { FLAC__Metadata_Chain *chain; public: - FLACMetadataChain():chain(::FLAC__metadata_chain_new()) {} + FlacMetadataChain():chain(::FLAC__metadata_chain_new()) {} - ~FLACMetadataChain() { + ~FlacMetadataChain() { ::FLAC__metadata_chain_delete(chain); } @@ -52,7 +52,7 @@ public: } bool Read(input_stream *is) { - return Read(::ToFLACIOHandle(is), ::GetFLACIOCallbacks(is)); + return Read(::ToFlacIOHandle(is), ::GetFlacIOCallbacks(is)); } bool ReadOgg(const char *path) { @@ -66,7 +66,7 @@ public: } bool ReadOgg(input_stream *is) { - return ReadOgg(::ToFLACIOHandle(is), ::GetFLACIOCallbacks(is)); + return ReadOgg(::ToFlacIOHandle(is), ::GetFlacIOCallbacks(is)); } gcc_pure @@ -88,7 +88,7 @@ class FLACMetadataIterator { public: FLACMetadataIterator():iterator(::FLAC__metadata_iterator_new()) {} - FLACMetadataIterator(FLACMetadataChain &chain) + FLACMetadataIterator(FlacMetadataChain &chain) :iterator(::FLAC__metadata_iterator_new()) { ::FLAC__metadata_iterator_init(iterator, (FLAC__Metadata_Chain *)chain); diff --git a/src/decoder/FLAC_PCM.cxx b/src/decoder/FlacPcm.cxx index 303530aa7..82896bd24 100644 --- a/src/decoder/FLAC_PCM.cxx +++ b/src/decoder/FlacPcm.cxx @@ -18,7 +18,7 @@ */ #include "config.h" -#include "FLAC_PCM.hxx" +#include "FlacPcm.hxx" #include <assert.h> diff --git a/src/decoder/FLAC_PCM.hxx b/src/decoder/FlacPcm.hxx index 97d214c17..97d214c17 100644 --- a/src/decoder/FLAC_PCM.hxx +++ b/src/decoder/FlacPcm.hxx diff --git a/src/fs/DirectoryReader.hxx b/src/fs/DirectoryReader.hxx index caa1e90ec..4e4a56345 100644 --- a/src/fs/DirectoryReader.hxx +++ b/src/fs/DirectoryReader.hxx @@ -47,14 +47,14 @@ public: * Destroys this instance. */ ~DirectoryReader() { - if (!Failed()) + if (!HasFailed()) closedir(dirp); } /** * Checks if directory failed to open. */ - bool Failed() const { + bool HasFailed() const { return dirp == nullptr; } @@ -62,7 +62,7 @@ public: * Checks if directory entry is available. */ bool HasEntry() const { - assert(!Failed()); + assert(!HasFailed()); return ent != nullptr; } @@ -70,7 +70,7 @@ public: * Reads next directory entry. */ bool ReadEntry() { - assert(!Failed()); + assert(!HasFailed()); ent = readdir(dirp); return HasEntry(); } diff --git a/src/output/FifoOutputPlugin.cxx b/src/output/FifoOutputPlugin.cxx index ebebc254f..15b107c45 100644 --- a/src/output/FifoOutputPlugin.cxx +++ b/src/output/FifoOutputPlugin.cxx @@ -20,7 +20,7 @@ #include "config.h" #include "FifoOutputPlugin.hxx" #include "output_api.h" -#include "timer.h" +#include "Timer.hxx" #include "fd_util.h" #include "open.h" @@ -44,7 +44,7 @@ struct FifoOutput { int input; int output; bool created; - struct timer *timer; + Timer *timer; FifoOutput() :path(nullptr), input(-1), output(-1), created(false) {} @@ -232,7 +232,7 @@ fifo_output_open(struct audio_output *ao, struct audio_format *audio_format, { FifoOutput *fd = (FifoOutput *)ao; - fd->timer = timer_new(audio_format); + fd->timer = new Timer(*audio_format); return true; } @@ -242,7 +242,7 @@ fifo_output_close(struct audio_output *ao) { FifoOutput *fd = (FifoOutput *)ao; - timer_free(fd->timer); + delete fd->timer; } static void @@ -252,7 +252,7 @@ fifo_output_cancel(struct audio_output *ao) char buf[FIFO_BUFFER_SIZE]; int bytes = 1; - timer_reset(fd->timer); + fd->timer->Reset(); while (bytes > 0 && errno != EINTR) bytes = read(fd->input, buf, FIFO_BUFFER_SIZE); @@ -268,8 +268,8 @@ fifo_output_delay(struct audio_output *ao) { FifoOutput *fd = (FifoOutput *)ao; - return fd->timer->started - ? timer_delay(fd->timer) + return fd->timer->IsStarted() + ? fd->timer->GetDelay() : 0; } @@ -280,9 +280,9 @@ fifo_output_play(struct audio_output *ao, const void *chunk, size_t size, FifoOutput *fd = (FifoOutput *)ao; ssize_t bytes; - if (!fd->timer->started) - timer_start(fd->timer); - timer_add(fd->timer, size); + if (!fd->timer->IsStarted()) + fd->timer->Start(); + fd->timer->Add(size); while (true) { bytes = write(fd->output, chunk, size); diff --git a/src/output/HttpdInternal.hxx b/src/output/HttpdInternal.hxx index e79b9a224..b79a22905 100644 --- a/src/output/HttpdInternal.hxx +++ b/src/output/HttpdInternal.hxx @@ -26,7 +26,7 @@ #define MPD_OUTPUT_HTTPD_INTERNAL_H #include "OutputInternal.hxx" -#include "timer.h" +#include "Timer.hxx" #include "thread/Mutex.hxx" #include "event/ServerSocket.hxx" @@ -72,10 +72,10 @@ struct HttpdOutput final : private ServerSocket { mutable Mutex mutex; /** - * A #timer object to synchronize this output with the + * A #Timer object to synchronize this output with the * wallclock. */ - struct timer *timer; + Timer *timer; /** * The header page, which is sent to every client on connect. diff --git a/src/output/HttpdOutputPlugin.cxx b/src/output/HttpdOutputPlugin.cxx index bb644c318..2c4884827 100644 --- a/src/output/HttpdOutputPlugin.cxx +++ b/src/output/HttpdOutputPlugin.cxx @@ -320,7 +320,7 @@ HttpdOutput::Open(struct audio_format *audio_format, GError **error_r) /* initialize other attributes */ clients_cnt = 0; - timer = timer_new(audio_format); + timer = new Timer(*audio_format); open = true; @@ -346,7 +346,7 @@ HttpdOutput::Close() open = false; - timer_free(timer); + delete timer; clients.clear(); @@ -398,7 +398,7 @@ httpd_output_delay(struct audio_output *ao) then httpd_output_pause() will not do anything, it will not fill the buffer and it will not update the timer; therefore, we reset the timer here */ - timer_reset(httpd->timer); + httpd->timer->Reset(); /* some arbitrary delay that is long enough to avoid consuming too much CPU, and short enough to notice @@ -406,8 +406,8 @@ httpd_output_delay(struct audio_output *ao) return 1000; } - return httpd->timer->started - ? timer_delay(httpd->timer) + return httpd->timer->IsStarted() + ? httpd->timer->GetDelay() : 0; } @@ -463,9 +463,9 @@ httpd_output_play(struct audio_output *ao, const void *chunk, size_t size, return 0; } - if (!httpd->timer->started) - timer_start(httpd->timer); - timer_add(httpd->timer, size); + if (!httpd->timer->IsStarted()) + httpd->timer->Start(); + httpd->timer->Add(size); return size; } diff --git a/src/output/NullOutputPlugin.cxx b/src/output/NullOutputPlugin.cxx index bbcf26c00..b167032b6 100644 --- a/src/output/NullOutputPlugin.cxx +++ b/src/output/NullOutputPlugin.cxx @@ -20,7 +20,7 @@ #include "config.h" #include "NullOutputPlugin.hxx" #include "output_api.h" -#include "timer.h" +#include "Timer.hxx" #include <assert.h> @@ -29,7 +29,7 @@ struct NullOutput { bool sync; - struct timer *timer; + Timer *timer; bool Initialize(const config_param *param, GError **error_r) { return ao_base_init(&base, &null_output_plugin, param, @@ -72,7 +72,7 @@ null_open(struct audio_output *ao, struct audio_format *audio_format, NullOutput *nd = (NullOutput *)ao; if (nd->sync) - nd->timer = timer_new(audio_format); + nd->timer = new Timer(*audio_format); return true; } @@ -83,7 +83,7 @@ null_close(struct audio_output *ao) NullOutput *nd = (NullOutput *)ao; if (nd->sync) - timer_free(nd->timer); + delete nd->timer; } static unsigned @@ -91,8 +91,8 @@ null_delay(struct audio_output *ao) { NullOutput *nd = (NullOutput *)ao; - return nd->sync && nd->timer->started - ? timer_delay(nd->timer) + return nd->sync && nd->timer->IsStarted() + ? nd->timer->GetDelay() : 0; } @@ -101,14 +101,14 @@ null_play(struct audio_output *ao, gcc_unused const void *chunk, size_t size, gcc_unused GError **error) { NullOutput *nd = (NullOutput *)ao; - struct timer *timer = nd->timer; + Timer *timer = nd->timer; if (!nd->sync) return size; - if (!timer->started) - timer_start(timer); - timer_add(timer, size); + if (!timer->IsStarted()) + timer->Start(); + timer->Add(size); return size; } @@ -121,7 +121,7 @@ null_cancel(struct audio_output *ao) if (!nd->sync) return; - timer_reset(nd->timer); + nd->timer->Reset(); } const struct audio_output_plugin null_output_plugin = { diff --git a/src/playlist/CuePlaylistPlugin.cxx b/src/playlist/CuePlaylistPlugin.cxx index 730ef783b..3d8098672 100644 --- a/src/playlist/CuePlaylistPlugin.cxx +++ b/src/playlist/CuePlaylistPlugin.cxx @@ -24,10 +24,7 @@ #include "song.h" #include "input_stream.h" #include "cue/CueParser.hxx" - -extern "C" { -#include "text_input_stream.h" -} +#include "TextInputStream.hxx" #include <glib.h> #include <assert.h> @@ -40,16 +37,15 @@ struct CuePlaylist { struct playlist_provider base; struct input_stream *is; - struct text_input_stream *tis; + TextInputStream tis; CueParser parser; CuePlaylist(struct input_stream *_is) - :is(_is), tis(text_input_stream_new(is)) { + :is(_is), tis(is) { playlist_provider_init(&base, &cue_playlist_plugin); } ~CuePlaylist() { - text_input_stream_free(tis); } }; @@ -76,9 +72,9 @@ cue_playlist_read(struct playlist_provider *_playlist) if (song != NULL) return song; - const char *line; - while ((line = text_input_stream_read(playlist->tis)) != NULL) { - playlist->parser.Feed(line); + std::string line; + while (playlist->tis.ReadLine(line)) { + playlist->parser.Feed(line.c_str()); song = playlist->parser.Get(); if (song != NULL) return song; diff --git a/src/playlist/ExtM3uPlaylistPlugin.cxx b/src/playlist/ExtM3uPlaylistPlugin.cxx index 2043bea52..923536ea9 100644 --- a/src/playlist/ExtM3uPlaylistPlugin.cxx +++ b/src/playlist/ExtM3uPlaylistPlugin.cxx @@ -23,10 +23,7 @@ #include "song.h" #include "tag.h" #include "util/StringUtil.hxx" - -extern "C" { -#include "text_input_stream.h" -} +#include "TextInputStream.hxx" #include <glib.h> @@ -36,20 +33,21 @@ extern "C" { struct ExtM3uPlaylist { struct playlist_provider base; - struct text_input_stream *tis; + TextInputStream *tis; }; static struct playlist_provider * extm3u_open_stream(struct input_stream *is) { ExtM3uPlaylist *playlist = g_new(ExtM3uPlaylist, 1); - playlist->tis = text_input_stream_new(is); + playlist->tis = new TextInputStream(is); - const char *line = text_input_stream_read(playlist->tis); - if (line == NULL || strcmp(line, "#EXTM3U") != 0) { + std::string line; + if (!playlist->tis->ReadLine(line) + || strcmp(line.c_str(), "#EXTM3U") != 0) { /* no EXTM3U header: fall back to the plain m3u plugin */ - text_input_stream_free(playlist->tis); + delete playlist->tis; g_free(playlist); return NULL; } @@ -63,7 +61,7 @@ extm3u_close(struct playlist_provider *_playlist) { ExtM3uPlaylist *playlist = (ExtM3uPlaylist *)_playlist; - text_input_stream_free(playlist->tis); + delete playlist->tis; g_free(playlist); } @@ -112,29 +110,31 @@ extm3u_read(struct playlist_provider *_playlist) { ExtM3uPlaylist *playlist = (ExtM3uPlaylist *)_playlist; struct tag *tag = NULL; - const char *line; + std::string line; + const char *line_s; struct song *song; do { - line = text_input_stream_read(playlist->tis); - if (line == NULL) { + if (!playlist->tis->ReadLine(line)) { if (tag != NULL) tag_free(tag); return NULL; } + + line_s = line.c_str(); - if (g_str_has_prefix(line, "#EXTINF:")) { + if (g_str_has_prefix(line_s, "#EXTINF:")) { if (tag != NULL) tag_free(tag); - tag = extm3u_parse_tag(line + 8); + tag = extm3u_parse_tag(line_s + 8); continue; } - while (*line != 0 && g_ascii_isspace(*line)) - ++line; - } while (line[0] == '#' || *line == 0); + while (*line_s != 0 && g_ascii_isspace(*line_s)) + ++line_s; + } while (line_s[0] == '#' || *line_s == 0); - song = song_remote_new(line); + song = song_remote_new(line_s); song->tag = tag; return song; } diff --git a/src/playlist/M3uPlaylistPlugin.cxx b/src/playlist/M3uPlaylistPlugin.cxx index eeecd2779..e1e0a803b 100644 --- a/src/playlist/M3uPlaylistPlugin.cxx +++ b/src/playlist/M3uPlaylistPlugin.cxx @@ -21,17 +21,14 @@ #include "M3uPlaylistPlugin.hxx" #include "PlaylistPlugin.hxx" #include "song.h" - -extern "C" { -#include "text_input_stream.h" -} +#include "TextInputStream.hxx" #include <glib.h> struct M3uPlaylist { struct playlist_provider base; - struct text_input_stream *tis; + TextInputStream *tis; }; static struct playlist_provider * @@ -40,7 +37,7 @@ m3u_open_stream(struct input_stream *is) M3uPlaylist *playlist = g_new(M3uPlaylist, 1); playlist_provider_init(&playlist->base, &m3u_playlist_plugin); - playlist->tis = text_input_stream_new(is); + playlist->tis = new TextInputStream(is); return &playlist->base; } @@ -50,7 +47,7 @@ m3u_close(struct playlist_provider *_playlist) { M3uPlaylist *playlist = (M3uPlaylist *)_playlist; - text_input_stream_free(playlist->tis); + delete playlist->tis; g_free(playlist); } @@ -58,18 +55,20 @@ static struct song * m3u_read(struct playlist_provider *_playlist) { M3uPlaylist *playlist = (M3uPlaylist *)_playlist; - const char *line; + std::string line; + const char *line_s; do { - line = text_input_stream_read(playlist->tis); - if (line == NULL) + if (!playlist->tis->ReadLine(line)) return NULL; - while (*line != 0 && g_ascii_isspace(*line)) - ++line; - } while (line[0] == '#' || *line == 0); + line_s = line.c_str(); + + while (*line_s != 0 && g_ascii_isspace(*line_s)) + ++line_s; + } while (line_s[0] == '#' || *line_s == 0); - return song_remote_new(line); + return song_remote_new(line_s); } static const char *const m3u_suffixes[] = { diff --git a/src/text_input_stream.h b/src/text_input_stream.h deleted file mode 100644 index 9b3245689..000000000 --- a/src/text_input_stream.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_TEXT_INPUT_STREAM_H -#define MPD_TEXT_INPUT_STREAM_H - -struct input_stream; -struct text_input_stream; - -/** - * Wraps an existing #input_stream object into a #text_input_stream, - * to read its contents as text lines. - * - * @param is an open #input_stream object - * @return the new #text_input_stream object - */ -struct text_input_stream * -text_input_stream_new(struct input_stream *is); - -/** - * Frees the #text_input_stream object. Does not close or free the - * underlying #input_stream. - */ -void -text_input_stream_free(struct text_input_stream *tis); - -/** - * Reads the next line from the stream. - * - * @return a line (newline character stripped), or NULL on end of file - * or error - */ -const char * -text_input_stream_read(struct text_input_stream *tis); - -#endif diff --git a/test/dump_text_file.cxx b/test/dump_text_file.cxx index 19502c7fd..40b3a878f 100644 --- a/test/dump_text_file.cxx +++ b/test/dump_text_file.cxx @@ -23,10 +23,7 @@ #include "InputStream.hxx" #include "conf.h" #include "stdbin.h" - -extern "C" { -#include "text_input_stream.h" -} +#include "TextInputStream.hxx" #ifdef ENABLE_ARCHIVE #include "ArchiveList.hxx" @@ -49,11 +46,11 @@ my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level, } static void -dump_text_file(struct text_input_stream *is) +dump_text_file(TextInputStream &is) { - const char *line; - while ((line = text_input_stream_read(is)) != NULL) - printf("'%s'\n", line); + std::string line; + while (is.ReadLine(line)) + printf("'%s'\n", line.c_str()); } static int @@ -77,11 +74,10 @@ dump_input_stream(struct input_stream *is) /* read data and tags from the stream */ input_stream_unlock(is); - - struct text_input_stream *tis = text_input_stream_new(is); - dump_text_file(tis); - text_input_stream_free(tis); - + { + TextInputStream tis(is); + dump_text_file(tis); + } input_stream_lock(is); if (!input_stream_check(is, &error)) { |