From acba9c0f6170b34e2a1c08b2b767554bc65cb5b4 Mon Sep 17 00:00:00 2001 From: Denis Krjuchkov Date: Sat, 7 Dec 2013 03:22:58 +0600 Subject: TextFile: move to fs subsystem --- src/DatabaseSave.cxx | 2 +- src/DirectorySave.cxx | 2 +- src/PlaylistDatabase.cxx | 2 +- src/PlaylistFile.cxx | 2 +- src/PlaylistState.cxx | 2 +- src/QueueSave.cxx | 2 +- src/SongSave.cxx | 2 +- src/StateFile.cxx | 2 +- src/TextFile.cxx | 77 ----------------------------------------- src/TextFile.hxx | 61 -------------------------------- src/db/SimpleDatabasePlugin.cxx | 2 +- src/fs/TextFile.cxx | 77 +++++++++++++++++++++++++++++++++++++++++ src/fs/TextFile.hxx | 61 ++++++++++++++++++++++++++++++++ 13 files changed, 147 insertions(+), 147 deletions(-) delete mode 100644 src/TextFile.cxx delete mode 100644 src/TextFile.hxx create mode 100644 src/fs/TextFile.cxx create mode 100644 src/fs/TextFile.hxx (limited to 'src') diff --git a/src/DatabaseSave.cxx b/src/DatabaseSave.cxx index a9ec281ce..67357a9a9 100644 --- a/src/DatabaseSave.cxx +++ b/src/DatabaseSave.cxx @@ -23,7 +23,7 @@ #include "DatabaseError.hxx" #include "Directory.hxx" #include "DirectorySave.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "tag/Tag.hxx" #include "tag/TagSettings.h" #include "fs/Charset.hxx" diff --git a/src/DirectorySave.cxx b/src/DirectorySave.cxx index 507f5ba47..709184289 100644 --- a/src/DirectorySave.cxx +++ b/src/DirectorySave.cxx @@ -23,7 +23,7 @@ #include "Song.hxx" #include "SongSave.hxx" #include "PlaylistDatabase.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "util/StringUtil.hxx" #include "util/NumberParser.hxx" #include "util/Error.hxx" diff --git a/src/PlaylistDatabase.cxx b/src/PlaylistDatabase.cxx index a6d15e755..e6cdb922b 100644 --- a/src/PlaylistDatabase.cxx +++ b/src/PlaylistDatabase.cxx @@ -20,7 +20,7 @@ #include "config.h" #include "PlaylistDatabase.hxx" #include "PlaylistVector.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "util/StringUtil.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index c3209db0a..3f9845166 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -26,7 +26,7 @@ #include "DatabaseGlue.hxx" #include "Song.hxx" #include "Mapper.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "ConfigGlobal.hxx" #include "ConfigOption.hxx" #include "ConfigDefaults.hxx" diff --git a/src/PlaylistState.cxx b/src/PlaylistState.cxx index 83f4b1143..bb776dce7 100644 --- a/src/PlaylistState.cxx +++ b/src/PlaylistState.cxx @@ -27,7 +27,7 @@ #include "PlaylistError.hxx" #include "Playlist.hxx" #include "QueueSave.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "PlayerControl.hxx" #include "ConfigGlobal.hxx" #include "ConfigOption.hxx" diff --git a/src/QueueSave.cxx b/src/QueueSave.cxx index 829a91de5..f3fc7c6d1 100644 --- a/src/QueueSave.cxx +++ b/src/QueueSave.cxx @@ -25,7 +25,7 @@ #include "SongSave.hxx" #include "DatabasePlugin.hxx" #include "DatabaseGlue.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "util/StringUtil.hxx" #include "util/UriUtil.hxx" #include "util/Error.hxx" diff --git a/src/SongSave.cxx b/src/SongSave.cxx index c4acccc87..9682bf650 100644 --- a/src/SongSave.cxx +++ b/src/SongSave.cxx @@ -21,7 +21,7 @@ #include "SongSave.hxx" #include "Song.hxx" #include "TagSave.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "tag/Tag.hxx" #include "tag/TagBuilder.hxx" #include "util/StringUtil.hxx" diff --git a/src/StateFile.cxx b/src/StateFile.cxx index e2e72c8ce..7ca8b8946 100644 --- a/src/StateFile.cxx +++ b/src/StateFile.cxx @@ -21,7 +21,7 @@ #include "StateFile.hxx" #include "OutputState.hxx" #include "PlaylistState.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "Partition.hxx" #include "Volume.hxx" diff --git a/src/TextFile.cxx b/src/TextFile.cxx deleted file mode 100644 index 4a64ee963..000000000 --- a/src/TextFile.cxx +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2003-2013 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. - */ - -#include "config.h" -#include "TextFile.hxx" -#include "fs/Path.hxx" -#include "fs/FileSystem.hxx" - -#include - -#include -#include - -TextFile::TextFile(Path path_fs) - :file(FOpen(path_fs, FOpenMode::ReadText)), - buffer(g_string_sized_new(step)) {} - -TextFile::~TextFile() -{ - if (file != nullptr) - fclose(file); - - g_string_free(buffer, true); -} - -char * -TextFile::ReadLine() -{ - gsize length = 0, i; - char *p; - - assert(file != nullptr); - assert(buffer != nullptr); - assert(buffer->allocated_len >= step); - - while (buffer->len < max_length) { - p = fgets(buffer->str + length, - buffer->allocated_len - length, file); - if (p == nullptr) { - if (length == 0 || ferror(file)) - return nullptr; - break; - } - - i = strlen(buffer->str + length); - length += i; - if (i < step - 1 || buffer->str[length - 1] == '\n') - break; - - g_string_set_size(buffer, length + step); - } - - /* remove the newline characters */ - if (buffer->str[length - 1] == '\n') - --length; - if (buffer->str[length - 1] == '\r') - --length; - - g_string_set_size(buffer, length); - return buffer->str; -} diff --git a/src/TextFile.hxx b/src/TextFile.hxx deleted file mode 100644 index 9d8608711..000000000 --- a/src/TextFile.hxx +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2003-2013 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_FILE_HXX -#define MPD_TEXT_FILE_HXX - -#include "Compiler.h" - -#include - -class Path; -typedef struct _GString GString; - -class TextFile { - static constexpr size_t max_length = 512 * 1024; - static constexpr size_t step = 1024; - - FILE *const file; - - GString *const buffer; - -public: - TextFile(Path path_fs); - - TextFile(const TextFile &other) = delete; - - ~TextFile(); - - bool HasFailed() const { - return gcc_unlikely(file == nullptr); - } - - /** - * Reads a line from the input file, and strips trailing - * space. There is a reasonable maximum line length, only to - * prevent denial of service. - * - * @param file the source file, opened in text mode - * @param buffer an allocator for the buffer - * @return a pointer to the line, or nullptr on end-of-file or error - */ - char *ReadLine(); -}; - -#endif diff --git a/src/db/SimpleDatabasePlugin.cxx b/src/db/SimpleDatabasePlugin.cxx index d0075ebb4..7fc66ed67 100644 --- a/src/db/SimpleDatabasePlugin.cxx +++ b/src/db/SimpleDatabasePlugin.cxx @@ -26,7 +26,7 @@ #include "DatabaseSave.hxx" #include "DatabaseLock.hxx" #include "DatabaseError.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "ConfigData.hxx" #include "fs/FileSystem.hxx" #include "util/Error.hxx" diff --git a/src/fs/TextFile.cxx b/src/fs/TextFile.cxx new file mode 100644 index 000000000..4a64ee963 --- /dev/null +++ b/src/fs/TextFile.cxx @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2003-2013 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. + */ + +#include "config.h" +#include "TextFile.hxx" +#include "fs/Path.hxx" +#include "fs/FileSystem.hxx" + +#include + +#include +#include + +TextFile::TextFile(Path path_fs) + :file(FOpen(path_fs, FOpenMode::ReadText)), + buffer(g_string_sized_new(step)) {} + +TextFile::~TextFile() +{ + if (file != nullptr) + fclose(file); + + g_string_free(buffer, true); +} + +char * +TextFile::ReadLine() +{ + gsize length = 0, i; + char *p; + + assert(file != nullptr); + assert(buffer != nullptr); + assert(buffer->allocated_len >= step); + + while (buffer->len < max_length) { + p = fgets(buffer->str + length, + buffer->allocated_len - length, file); + if (p == nullptr) { + if (length == 0 || ferror(file)) + return nullptr; + break; + } + + i = strlen(buffer->str + length); + length += i; + if (i < step - 1 || buffer->str[length - 1] == '\n') + break; + + g_string_set_size(buffer, length + step); + } + + /* remove the newline characters */ + if (buffer->str[length - 1] == '\n') + --length; + if (buffer->str[length - 1] == '\r') + --length; + + g_string_set_size(buffer, length); + return buffer->str; +} diff --git a/src/fs/TextFile.hxx b/src/fs/TextFile.hxx new file mode 100644 index 000000000..9d8608711 --- /dev/null +++ b/src/fs/TextFile.hxx @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2003-2013 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_FILE_HXX +#define MPD_TEXT_FILE_HXX + +#include "Compiler.h" + +#include + +class Path; +typedef struct _GString GString; + +class TextFile { + static constexpr size_t max_length = 512 * 1024; + static constexpr size_t step = 1024; + + FILE *const file; + + GString *const buffer; + +public: + TextFile(Path path_fs); + + TextFile(const TextFile &other) = delete; + + ~TextFile(); + + bool HasFailed() const { + return gcc_unlikely(file == nullptr); + } + + /** + * Reads a line from the input file, and strips trailing + * space. There is a reasonable maximum line length, only to + * prevent denial of service. + * + * @param file the source file, opened in text mode + * @param buffer an allocator for the buffer + * @return a pointer to the line, or nullptr on end-of-file or error + */ + char *ReadLine(); +}; + +#endif -- cgit v1.2.3