From 2452447c814048ed72e95a459c76b4be65962b5c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 3 Jan 2013 10:12:41 +0100 Subject: text_file: convert to C++ --- src/DatabaseSave.cxx | 2 +- src/DirectorySave.cxx | 5 +--- 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 | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ src/TextFile.hxx | 39 +++++++++++++++++++++++++++ src/text_file.c | 68 ------------------------------------------------ src/text_file.h | 39 --------------------------- 12 files changed, 115 insertions(+), 118 deletions(-) create mode 100644 src/TextFile.cxx create mode 100644 src/TextFile.hxx delete mode 100644 src/text_file.c delete mode 100644 src/text_file.h (limited to 'src') diff --git a/src/DatabaseSave.cxx b/src/DatabaseSave.cxx index 0424fbe29..5bd50f55f 100644 --- a/src/DatabaseSave.cxx +++ b/src/DatabaseSave.cxx @@ -23,10 +23,10 @@ #include "Directory.hxx" #include "DirectorySave.hxx" #include "song.h" +#include "TextFile.hxx" extern "C" { #include "path.h" -#include "text_file.h" #include "tag.h" #include "tag_internal.h" } diff --git a/src/DirectorySave.cxx b/src/DirectorySave.cxx index cd750c9b2..9c5df685f 100644 --- a/src/DirectorySave.cxx +++ b/src/DirectorySave.cxx @@ -23,10 +23,7 @@ #include "song.h" #include "SongSave.hxx" #include "PlaylistDatabase.hxx" - -extern "C" { -#include "text_file.h" -} +#include "TextFile.hxx" #include #include diff --git a/src/PlaylistDatabase.cxx b/src/PlaylistDatabase.cxx index 4604a8956..984af4adc 100644 --- a/src/PlaylistDatabase.cxx +++ b/src/PlaylistDatabase.cxx @@ -20,9 +20,9 @@ #include "config.h" #include "PlaylistDatabase.hxx" #include "PlaylistVector.hxx" +#include "TextFile.hxx" extern "C" { -#include "text_file.h" #include "string_util.h" } diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index 7459bd69a..e03cc5e54 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -25,9 +25,9 @@ #include "song.h" #include "io_error.h" #include "Mapper.hxx" +#include "TextFile.hxx" extern "C" { -#include "text_file.h" #include "path.h" #include "uri.h" #include "idle.h" diff --git a/src/PlaylistState.cxx b/src/PlaylistState.cxx index 0cfd32723..c45c2a834 100644 --- a/src/PlaylistState.cxx +++ b/src/PlaylistState.cxx @@ -25,11 +25,11 @@ #include "config.h" #include "PlaylistState.hxx" #include "QueueSave.hxx" +#include "TextFile.hxx" extern "C" { #include "playlist.h" #include "player_control.h" -#include "text_file.h" #include "conf.h" } diff --git a/src/QueueSave.cxx b/src/QueueSave.cxx index d8b698a9a..a468013f4 100644 --- a/src/QueueSave.cxx +++ b/src/QueueSave.cxx @@ -23,11 +23,11 @@ #include "SongSave.hxx" #include "DatabasePlugin.hxx" #include "DatabaseGlue.hxx" +#include "TextFile.hxx" extern "C" { #include "queue.h" #include "uri.h" -#include "text_file.h" } #include diff --git a/src/SongSave.cxx b/src/SongSave.cxx index 1aaf15481..cbc2536b1 100644 --- a/src/SongSave.cxx +++ b/src/SongSave.cxx @@ -22,10 +22,10 @@ #include "song.h" #include "TagSave.hxx" #include "Directory.hxx" +#include "TextFile.hxx" extern "C" { #include "tag.h" -#include "text_file.h" #include "string_util.h" } diff --git a/src/StateFile.cxx b/src/StateFile.cxx index 6635d9f9d..5af60ddba 100644 --- a/src/StateFile.cxx +++ b/src/StateFile.cxx @@ -22,10 +22,10 @@ #include "OutputState.hxx" #include "playlist.h" #include "PlaylistState.hxx" +#include "TextFile.hxx" extern "C" { #include "volume.h" -#include "text_file.h" } #include diff --git a/src/TextFile.cxx b/src/TextFile.cxx new file mode 100644 index 000000000..ec9c5d442 --- /dev/null +++ b/src/TextFile.cxx @@ -0,0 +1,68 @@ +/* + * 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 +#include + +char * +read_text_line(FILE *file, GString *buffer) +{ + enum { + max_length = 512 * 1024, + step = 1024, + }; + + gsize length = 0, i; + char *p; + + assert(file != NULL); + assert(buffer != NULL); + + if (buffer->allocated_len < step) + g_string_set_size(buffer, step); + + while (buffer->len < max_length) { + p = fgets(buffer->str + length, + buffer->allocated_len - length, file); + if (p == NULL) { + if (length == 0 || ferror(file)) + return NULL; + 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 new file mode 100644 index 000000000..5bd6dbd3c --- /dev/null +++ b/src/TextFile.hxx @@ -0,0 +1,39 @@ +/* + * 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 + +#include + +/** + * 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 NULL on end-of-file or error + */ +char * +read_text_line(FILE *file, GString *buffer); + +#endif diff --git a/src/text_file.c b/src/text_file.c deleted file mode 100644 index 3674e5ce2..000000000 --- a/src/text_file.c +++ /dev/null @@ -1,68 +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. - */ - -#include "config.h" -#include "text_file.h" - -#include -#include - -char * -read_text_line(FILE *file, GString *buffer) -{ - enum { - max_length = 512 * 1024, - step = 1024, - }; - - gsize length = 0, i; - char *p; - - assert(file != NULL); - assert(buffer != NULL); - - if (buffer->allocated_len < step) - g_string_set_size(buffer, step); - - while (buffer->len < max_length) { - p = fgets(buffer->str + length, - buffer->allocated_len - length, file); - if (p == NULL) { - if (length == 0 || ferror(file)) - return NULL; - 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/text_file.h b/src/text_file.h deleted file mode 100644 index 9dd810943..000000000 --- a/src/text_file.h +++ /dev/null @@ -1,39 +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_FILE_H -#define MPD_TEXT_FILE_H - -#include - -#include - -/** - * 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 NULL on end-of-file or error - */ -char * -read_text_line(FILE *file, GString *buffer); - -#endif -- cgit v1.2.3