From 3f3b26fb0ee090bfc1321572904aa94deca42a84 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 9 Apr 2013 01:17:47 +0200 Subject: utils: convert to C++ --- Makefile.am | 20 ++------- src/ConfigData.cxx | 5 +-- src/ConfigGlobal.cxx | 6 +-- src/ConfigPath.cxx | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/ConfigPath.hxx | 28 ++++++++++++ src/utils.c | 120 --------------------------------------------------- src/utils.h | 28 ------------ 7 files changed, 153 insertions(+), 174 deletions(-) create mode 100644 src/ConfigPath.cxx create mode 100644 src/ConfigPath.hxx delete mode 100644 src/utils.c delete mode 100644 src/utils.h diff --git a/Makefile.am b/Makefile.am index bf00de012..4c6b78e47 100644 --- a/Makefile.am +++ b/Makefile.am @@ -98,7 +98,6 @@ mpd_headers = \ src/tag_ape.h \ src/tag_id3.h \ src/tag_rva2.h \ - src/utils.h \ src/timer.h \ src/mpd_error.h @@ -242,7 +241,6 @@ src_mpd_SOURCES = \ src/TagFile.cxx src/TagFile.hxx \ src/TextFile.cxx src/TextFile.hxx \ src/text_input_stream.c \ - src/utils.c \ src/Volume.cxx src/Volume.hxx \ src/SongFilter.cxx src/SongFilter.hxx \ src/SongPointer.hxx \ @@ -420,6 +418,7 @@ endif # configuration library libconf_a_SOURCES = \ + src/ConfigPath.cxx src/ConfigPath.hxx \ src/ConfigData.cxx src/ConfigData.hxx \ src/ConfigParser.cxx src/ConfigParser.hxx \ src/ConfigGlobal.cxx src/ConfigGlobal.hxx \ @@ -1056,8 +1055,7 @@ test_read_conf_LDADD = \ libutil.a \ libfs.a \ $(GLIB_LIBS) -test_read_conf_SOURCES = test/read_conf.cxx \ - src/utils.c +test_read_conf_SOURCES = test/read_conf.cxx test_run_resolver_LDADD = \ $(GLIB_LIBS) @@ -1079,8 +1077,7 @@ test_DumpDatabase_SOURCES = test/DumpDatabase.cxx \ src/Song.cxx src/song_sort.c src/SongSave.cxx \ src/Tag.cxx src/TagNames.c src/TagPool.cxx src/TagSave.cxx \ src/SongFilter.cxx \ - src/TextFile.cxx \ - src/utils.c + src/TextFile.cxx test_run_input_LDADD = \ $(INPUT_LIBS) \ @@ -1093,7 +1090,6 @@ test_run_input_LDADD = \ test_run_input_SOURCES = test/run_input.cxx \ test/stdbin.h \ src/IOThread.cxx \ - src/utils.c \ src/Tag.cxx src/TagNames.c src/TagPool.cxx src/TagSave.cxx \ src/fd_util.c @@ -1110,7 +1106,6 @@ test_visit_archive_LDADD = \ test_visit_archive_SOURCES = test/visit_archive.cxx \ src/IOThread.cxx \ src/InputStream.cxx \ - src/utils.c \ src/Tag.cxx src/TagNames.c src/TagPool.cxx \ src/fd_util.c @@ -1131,7 +1126,6 @@ test_dump_text_file_LDADD = \ test_dump_text_file_SOURCES = test/dump_text_file.cxx \ test/stdbin.h \ src/IOThread.cxx \ - src/utils.c \ src/Tag.cxx src/TagNames.c src/TagPool.cxx \ src/text_input_stream.c \ src/fd_util.c @@ -1151,7 +1145,6 @@ test_dump_playlist_LDADD = \ test_dump_playlist_SOURCES = test/dump_playlist.cxx \ $(DECODER_SRC) \ src/IOThread.cxx \ - src/utils.c \ 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/pcm_buffer.c \ @@ -1179,7 +1172,6 @@ test_run_decoder_LDADD = \ test_run_decoder_SOURCES = test/run_decoder.cxx \ test/stdbin.h \ src/IOThread.cxx \ - src/utils.c \ src/Tag.cxx src/TagNames.c src/TagPool.cxx src/tag_handler.c \ src/ReplayGainInfo.cxx \ src/fd_util.c \ @@ -1203,7 +1195,6 @@ test_read_tags_LDADD = \ $(GLIB_LIBS) test_read_tags_SOURCES = test/read_tags.cxx \ src/IOThread.cxx \ - src/utils.c \ src/Tag.cxx src/TagNames.c src/TagPool.cxx src/tag_handler.c \ src/ReplayGainInfo.cxx \ src/fd_util.c \ @@ -1231,7 +1222,6 @@ test_run_filter_SOURCES = test/run_filter.cxx \ test/FakeReplayGainConfig.cxx \ test/stdbin.h \ src/FilterPlugin.cxx src/FilterRegistry.cxx \ - src/utils.c \ src/audio_check.c \ src/audio_format.c \ src/AudioParser.cxx \ @@ -1250,7 +1240,6 @@ if ENABLE_ENCODER noinst_PROGRAMS += test/run_encoder test_run_encoder_SOURCES = test/run_encoder.cxx \ test/stdbin.h \ - src/utils.c \ src/Tag.cxx src/TagNames.c src/TagPool.cxx \ src/audio_check.c \ src/audio_format.c \ @@ -1269,7 +1258,6 @@ if ENABLE_VORBIS_ENCODER noinst_PROGRAMS += test/test_vorbis_encoder test_test_vorbis_encoder_SOURCES = test/test_vorbis_encoder.cxx \ test/stdbin.h \ - src/utils.c \ src/Tag.cxx src/TagNames.c src/TagPool.cxx \ src/audio_check.c \ src/audio_format.c \ @@ -1325,7 +1313,6 @@ test_run_output_LDADD = $(MPD_LIBS) \ test_run_output_SOURCES = test/run_output.cxx \ test/FakeReplayGainConfig.cxx \ test/stdbin.h \ - src/utils.c \ src/IOThread.cxx \ src/audio_check.c \ src/audio_format.c \ @@ -1356,7 +1343,6 @@ test_read_mixer_LDADD = \ libfs.a \ $(GLIB_LIBS) test_read_mixer_SOURCES = test/read_mixer.cxx \ - src/utils.c \ src/MixerControl.cxx \ src/MixerInternal.cxx \ src/FilterPlugin.cxx \ diff --git a/src/ConfigData.cxx b/src/ConfigData.cxx index 48e9612d4..dd102a19a 100644 --- a/src/ConfigData.cxx +++ b/src/ConfigData.cxx @@ -19,12 +19,9 @@ #include "ConfigData.hxx" #include "ConfigParser.hxx" +#include "ConfigPath.hxx" #include "mpd_error.h" -extern "C" { -#include "utils.h" -} - #include #include diff --git a/src/ConfigGlobal.cxx b/src/ConfigGlobal.cxx index 9786690d0..a66c03748 100644 --- a/src/ConfigGlobal.cxx +++ b/src/ConfigGlobal.cxx @@ -22,11 +22,7 @@ #include "ConfigParser.hxx" #include "ConfigData.hxx" #include "ConfigFile.hxx" - -extern "C" { -#include "utils.h" -} - +#include "ConfigPath.hxx" #include "mpd_error.h" #include diff --git a/src/ConfigPath.cxx b/src/ConfigPath.cxx new file mode 100644 index 000000000..767115d19 --- /dev/null +++ b/src/ConfigPath.cxx @@ -0,0 +1,120 @@ +/* + * 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 "ConfigPath.hxx" +#include "conf.h" + +#include + +#include +#include +#include +#include +#include + +#ifndef WIN32 +#include +#endif + +#if HAVE_IPV6 && WIN32 +#include +#endif + +#if HAVE_IPV6 && ! WIN32 +#include +#endif + +#ifdef WIN32 +#include +#endif + +G_GNUC_CONST +static inline GQuark +parse_path_quark(void) +{ + return g_quark_from_static_string("path"); +} + +char * +parsePath(const char *path, G_GNUC_UNUSED GError **error_r) +{ + assert(path != nullptr); + assert(error_r == nullptr || *error_r == nullptr); + +#ifndef WIN32 + if (!g_path_is_absolute(path) && path[0] != '~') { + g_set_error(error_r, parse_path_quark(), 0, + "not an absolute path: %s", path); + return nullptr; + } else if (path[0] == '~') { + const char *home; + + if (path[1] == '/' || path[1] == '\0') { + const char *user = config_get_string(CONF_USER, nullptr); + if (user != nullptr) { + struct passwd *passwd = getpwnam(user); + if (!passwd) { + g_set_error(error_r, parse_path_quark(), 0, + "no such user: %s", user); + return nullptr; + } + + home = passwd->pw_dir; + } else { + home = g_get_home_dir(); + if (home == nullptr) { + g_set_error_literal(error_r, parse_path_quark(), 0, + "problems getting home " + "for current user"); + return nullptr; + } + } + + ++path; + } else { + ++path; + + const char *slash = strchr(path, '/'); + char *user = slash != nullptr + ? g_strndup(path, slash - path) + : g_strdup(path); + + struct passwd *passwd = getpwnam(user); + if (!passwd) { + g_set_error(error_r, parse_path_quark(), 0, + "no such user: %s", user); + g_free(user); + return nullptr; + } + + g_free(user); + + home = passwd->pw_dir; + path = slash; + } + + return g_strconcat(home, path, nullptr); + } else { +#endif + return g_strdup(path); +#ifndef WIN32 + } +#endif +} diff --git a/src/ConfigPath.hxx b/src/ConfigPath.hxx new file mode 100644 index 000000000..42e51215f --- /dev/null +++ b/src/ConfigPath.hxx @@ -0,0 +1,28 @@ +/* + * 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_CONFIG_PATH_HXX +#define MPD_CONFIG_PATH_HXX + +#include "gerror.h" + +char * +parsePath(const char *path, GError **error_r); + +#endif diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index 776813c4b..000000000 --- a/src/utils.c +++ /dev/null @@ -1,120 +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 "utils.h" -#include "conf.h" - -#include - -#include -#include -#include -#include -#include - -#ifndef WIN32 -#include -#endif - -#if HAVE_IPV6 && WIN32 -#include -#endif - -#if HAVE_IPV6 && ! WIN32 -#include -#endif - -#ifdef WIN32 -#include -#endif - -G_GNUC_CONST -static inline GQuark -parse_path_quark(void) -{ - return g_quark_from_static_string("path"); -} - -char * -parsePath(const char *path, G_GNUC_UNUSED GError **error_r) -{ - assert(path != NULL); - assert(error_r == NULL || *error_r == NULL); - -#ifndef WIN32 - if (!g_path_is_absolute(path) && path[0] != '~') { - g_set_error(error_r, parse_path_quark(), 0, - "not an absolute path: %s", path); - return NULL; - } else if (path[0] == '~') { - const char *home; - - if (path[1] == '/' || path[1] == '\0') { - const char *user = config_get_string(CONF_USER, NULL); - if (user != NULL) { - struct passwd *passwd = getpwnam(user); - if (!passwd) { - g_set_error(error_r, parse_path_quark(), 0, - "no such user: %s", user); - return NULL; - } - - home = passwd->pw_dir; - } else { - home = g_get_home_dir(); - if (home == NULL) { - g_set_error_literal(error_r, parse_path_quark(), 0, - "problems getting home " - "for current user"); - return NULL; - } - } - - ++path; - } else { - ++path; - - const char *slash = strchr(path, '/'); - char *user = slash != NULL - ? g_strndup(path, slash - path) - : g_strdup(path); - - struct passwd *passwd = getpwnam(user); - if (!passwd) { - g_set_error(error_r, parse_path_quark(), 0, - "no such user: %s", user); - g_free(user); - return NULL; - } - - g_free(user); - - home = passwd->pw_dir; - path = slash; - } - - return g_strconcat(home, path, NULL); - } else { -#endif - return g_strdup(path); -#ifndef WIN32 - } -#endif -} diff --git a/src/utils.h b/src/utils.h deleted file mode 100644 index 059d44fa3..000000000 --- a/src/utils.h +++ /dev/null @@ -1,28 +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_UTILS_H -#define MPD_UTILS_H - -#include "gerror.h" - -char * -parsePath(const char *path, GError **error_r); - -#endif -- cgit v1.2.3