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++ --- src/ConfigData.cxx | 5 +-- src/ConfigGlobal.cxx | 6 +-- src/ConfigPath.cxx | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/ConfigPath.hxx | 28 ++++++++++++ src/utils.c | 120 --------------------------------------------------- src/utils.h | 28 ------------ 6 files changed, 150 insertions(+), 157 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 (limited to 'src') 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