From 21fe376d1d9ffa6064cf89faab7860d443d9f7fd Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 17 Jan 2013 00:43:27 +0100 Subject: path: convert to C++ --- Makefile.am | 5 +- src/CommandLine.cxx | 1 - src/ConfigFile.cxx | 2 +- src/DatabaseSave.cxx | 5 +- src/Directory.cxx | 1 - src/ExcludeList.cxx | 5 +- src/InotifyUpdate.cxx | 5 +- src/Main.cxx | 2 +- src/Mapper.cxx | 5 +- src/Path.cxx | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Path.hxx | 57 ++++++++++++++++++++++ src/PlaylistFile.cxx | 2 +- src/PlaylistSave.cxx | 2 +- src/PlaylistSong.cxx | 2 +- src/SongFilter.cxx | 1 - src/UpdateWalk.cxx | 2 +- src/path.c | 131 -------------------------------------------------- src/path.h | 55 --------------------- 18 files changed, 200 insertions(+), 214 deletions(-) create mode 100644 src/Path.cxx create mode 100644 src/Path.hxx delete mode 100644 src/path.c delete mode 100644 src/path.h diff --git a/Makefile.am b/Makefile.am index 36684f990..ab1c5d261 100644 --- a/Makefile.am +++ b/Makefile.am @@ -98,7 +98,6 @@ mpd_headers = \ src/daemon.h \ src/AudioCompress/config.h \ src/AudioCompress/compress.h \ - src/path.h \ src/open.h \ src/page.h \ src/Playlist.hxx \ @@ -246,7 +245,7 @@ src_mpd_SOURCES = \ src/MusicBuffer.cxx src/MusicBuffer.hxx \ src/MusicPipe.cxx src/MusicPipe.hxx \ src/MusicChunk.cxx src/MusicChunk.hxx \ - src/path.c \ + src/Path.cxx src/Path.hxx \ src/Mapper.cxx src/Mapper.hxx \ src/page.c \ src/Partition.hxx \ @@ -1060,7 +1059,7 @@ test_DumpDatabase_SOURCES = test/DumpDatabase.cxx \ src/DatabaseLock.cxx src/DatabaseSave.cxx \ src/Song.cxx src/song_sort.c src/SongSave.cxx \ src/Tag.cxx src/TagNames.c src/TagPool.cxx src/TagSave.cxx \ - src/path.c \ + src/Path.cxx \ src/SongFilter.cxx \ src/TextFile.cxx \ src/ConfigFile.cxx src/tokenizer.c src/utils.c src/string_util.c diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index a8608af9c..e9766dfae 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "CommandLine.hxx" -#include "path.h" #include "ls.hxx" #include "Log.hxx" #include "conf.h" diff --git a/src/ConfigFile.cxx b/src/ConfigFile.cxx index fd08f52b1..ce71b21d2 100644 --- a/src/ConfigFile.cxx +++ b/src/ConfigFile.cxx @@ -26,7 +26,7 @@ extern "C" { #include "tokenizer.h" } -#include "path.h" +#include "Path.hxx" #include "mpd_error.h" #include diff --git a/src/DatabaseSave.cxx b/src/DatabaseSave.cxx index 5100c2a0b..029189ae5 100644 --- a/src/DatabaseSave.cxx +++ b/src/DatabaseSave.cxx @@ -26,10 +26,7 @@ #include "TextFile.hxx" #include "TagInternal.hxx" #include "tag.h" - -extern "C" { -#include "path.h" -} +#include "Path.hxx" #include diff --git a/src/Directory.cxx b/src/Directory.cxx index f27b3d474..2b1a34cbf 100644 --- a/src/Directory.cxx +++ b/src/Directory.cxx @@ -26,7 +26,6 @@ extern "C" { #include "song.h" #include "song_sort.h" -#include "path.h" #include "util/list_sort.h" } diff --git a/src/ExcludeList.cxx b/src/ExcludeList.cxx index 4beb7afc3..2c2dc952f 100644 --- a/src/ExcludeList.cxx +++ b/src/ExcludeList.cxx @@ -24,10 +24,7 @@ #include "config.h" #include "ExcludeList.hxx" - -extern "C" { -#include "path.h" -} +#include "Path.hxx" #include #include diff --git a/src/InotifyUpdate.cxx b/src/InotifyUpdate.cxx index 7ed0e84ba..a1c3e0393 100644 --- a/src/InotifyUpdate.cxx +++ b/src/InotifyUpdate.cxx @@ -23,10 +23,7 @@ #include "InotifyQueue.hxx" #include "Mapper.hxx" #include "Main.hxx" - -extern "C" { -#include "path.h" -} +#include "Path.hxx" #include #include diff --git a/src/Main.cxx b/src/Main.cxx index 6ccede6f1..dd269354d 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -47,10 +47,10 @@ #include "InputInit.hxx" #include "event/Loop.hxx" #include "IOThread.hxx" +#include "Path.hxx" extern "C" { #include "daemon.h" -#include "path.h" #include "stats.h" #include "audio_config.h" #include "pcm_resample.h" diff --git a/src/Mapper.cxx b/src/Mapper.cxx index 4d863418b..1a5ebe111 100644 --- a/src/Mapper.cxx +++ b/src/Mapper.cxx @@ -25,10 +25,7 @@ #include "Mapper.hxx" #include "Directory.hxx" #include "song.h" - -extern "C" { -#include "path.h" -} +#include "Path.hxx" #include diff --git a/src/Path.cxx b/src/Path.cxx new file mode 100644 index 000000000..ab8a4e3cc --- /dev/null +++ b/src/Path.cxx @@ -0,0 +1,131 @@ +/* + * 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 "Path.hxx" +#include "conf.h" +#include "mpd_error.h" +#include "gcc.h" + +#include + +#include +#include + +#ifdef G_OS_WIN32 +#include // for GetACP() +#include // for sprintf() +#endif + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "path" + +static char *fs_charset; + +char * +fs_charset_to_utf8(const char *path_fs) +{ + return g_convert(path_fs, -1, + "utf-8", fs_charset, + NULL, NULL, NULL); +} + +char * +utf8_to_fs_charset(const char *path_utf8) +{ + gchar *p; + + p = g_convert(path_utf8, -1, + fs_charset, "utf-8", + NULL, NULL, NULL); + if (p == NULL) + /* fall back to UTF-8 */ + p = g_strdup(path_utf8); + + return p; +} + +gcc_pure +static bool +IsSupportedCharset(const char *charset) +{ + /* convert a space to check if the charset is valid */ + char *test = g_convert(" ", 1, charset, "UTF-8", NULL, NULL, NULL); + if (test == NULL) + return false; + + g_free(test); + return true; +} + +static void +path_set_fs_charset(const char *charset) +{ + assert(charset != NULL); + + if (!IsSupportedCharset(charset)) + MPD_ERROR("invalid filesystem charset: %s", charset); + + g_free(fs_charset); + fs_charset = g_strdup(charset); + + g_debug("path_set_fs_charset: fs charset is: %s", fs_charset); +} + +const char *path_get_fs_charset(void) +{ + return fs_charset; +} + +void path_global_init(void) +{ + const char *charset = NULL; + + charset = config_get_string(CONF_FS_CHARSET, NULL); + if (charset == NULL) { +#ifndef G_OS_WIN32 + const gchar **encodings; + g_get_filename_charsets(&encodings); + + if (encodings[0] != NULL && *encodings[0] != '\0') + charset = encodings[0]; +#else /* G_OS_WIN32 */ + /* Glib claims that file system encoding is always utf-8 + * on native Win32 (i.e. not Cygwin). + * However this is true only if helpers are used. + * MPD uses regular functions. + * Those functions use encoding determined by GetACP(). */ + static char win_charset[13]; + sprintf(win_charset, "cp%u", GetACP()); + charset = win_charset; +#endif + } + + if (charset) { + path_set_fs_charset(charset); + } else { + g_message("setting filesystem charset to ISO-8859-1"); + path_set_fs_charset("ISO-8859-1"); + } +} + +void path_global_finish(void) +{ + g_free(fs_charset); +} diff --git a/src/Path.hxx b/src/Path.hxx new file mode 100644 index 000000000..db3f95961 --- /dev/null +++ b/src/Path.hxx @@ -0,0 +1,57 @@ +/* + * 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_PATH_HXX +#define MPD_PATH_HXX + +#include "check.h" + +#include + +#if !defined(MPD_PATH_MAX) +# if defined(MAXPATHLEN) +# define MPD_PATH_MAX MAXPATHLEN +# elif defined(PATH_MAX) +# define MPD_PATH_MAX PATH_MAX +# else +# define MPD_PATH_MAX 256 +# endif +#endif + +void path_global_init(); + +void path_global_finish(); + +/** + * Converts a file name in the filesystem charset to UTF-8. Returns + * NULL on failure. + */ +char * +fs_charset_to_utf8(const char *path_fs); + +/** + * Converts a file name in UTF-8 to the filesystem charset. Returns a + * duplicate of the UTF-8 string on failure. + */ +char * +utf8_to_fs_charset(const char *path_utf8); + +const char *path_get_fs_charset(); + +#endif diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index d79bea9c5..486c93994 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -30,9 +30,9 @@ #include "TextFile.hxx" #include "conf.h" #include "Idle.hxx" +#include "Path.hxx" extern "C" { -#include "path.h" #include "uri.h" } diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index 745744490..5f47d73c0 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -24,9 +24,9 @@ #include "song.h" #include "Mapper.hxx" #include "Idle.hxx" +#include "Path.hxx" extern "C" { -#include "path.h" #include "uri.h" } diff --git a/src/PlaylistSong.cxx b/src/PlaylistSong.cxx index 118fb5715..ec27656f2 100644 --- a/src/PlaylistSong.cxx +++ b/src/PlaylistSong.cxx @@ -24,11 +24,11 @@ #include "DatabaseGlue.hxx" #include "ls.hxx" #include "tag.h" +#include "Path.hxx" extern "C" { #include "song.h" #include "uri.h" -#include "path.h" } #include diff --git a/src/SongFilter.cxx b/src/SongFilter.cxx index 6d6841675..0e138386b 100644 --- a/src/SongFilter.cxx +++ b/src/SongFilter.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "SongFilter.hxx" -#include "path.h" #include "song.h" #include "tag.h" diff --git a/src/UpdateWalk.cxx b/src/UpdateWalk.cxx index 208e6290a..1716862b5 100644 --- a/src/UpdateWalk.cxx +++ b/src/UpdateWalk.cxx @@ -31,10 +31,10 @@ #include "Mapper.hxx" #include "ExcludeList.hxx" #include "conf.h" +#include "Path.hxx" extern "C" { #include "uri.h" -#include "path.h" #include "playlist_list.h" } diff --git a/src/path.c b/src/path.c deleted file mode 100644 index 21a4dd41e..000000000 --- a/src/path.c +++ /dev/null @@ -1,131 +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 "path.h" -#include "conf.h" -#include "mpd_error.h" -#include "gcc.h" - -#include - -#include -#include - -#ifdef G_OS_WIN32 -#include // for GetACP() -#include // for sprintf() -#endif - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "path" - -static char *fs_charset; - -char * -fs_charset_to_utf8(const char *path_fs) -{ - return g_convert(path_fs, -1, - "utf-8", fs_charset, - NULL, NULL, NULL); -} - -char * -utf8_to_fs_charset(const char *path_utf8) -{ - gchar *p; - - p = g_convert(path_utf8, -1, - fs_charset, "utf-8", - NULL, NULL, NULL); - if (p == NULL) - /* fall back to UTF-8 */ - p = g_strdup(path_utf8); - - return p; -} - -gcc_pure -static bool -IsSupportedCharset(const char *charset) -{ - /* convert a space to check if the charset is valid */ - char *test = g_convert(" ", 1, charset, "UTF-8", NULL, NULL, NULL); - if (test == NULL) - return false; - - g_free(test); - return true; -} - -static void -path_set_fs_charset(const char *charset) -{ - assert(charset != NULL); - - if (!IsSupportedCharset(charset)) - MPD_ERROR("invalid filesystem charset: %s", charset); - - g_free(fs_charset); - fs_charset = g_strdup(charset); - - g_debug("path_set_fs_charset: fs charset is: %s", fs_charset); -} - -const char *path_get_fs_charset(void) -{ - return fs_charset; -} - -void path_global_init(void) -{ - const char *charset = NULL; - - charset = config_get_string(CONF_FS_CHARSET, NULL); - if (charset == NULL) { -#ifndef G_OS_WIN32 - const gchar **encodings; - g_get_filename_charsets(&encodings); - - if (encodings[0] != NULL && *encodings[0] != '\0') - charset = encodings[0]; -#else /* G_OS_WIN32 */ - /* Glib claims that file system encoding is always utf-8 - * on native Win32 (i.e. not Cygwin). - * However this is true only if helpers are used. - * MPD uses regular functions. - * Those functions use encoding determined by GetACP(). */ - static char win_charset[13]; - sprintf(win_charset, "cp%u", GetACP()); - charset = win_charset; -#endif - } - - if (charset) { - path_set_fs_charset(charset); - } else { - g_message("setting filesystem charset to ISO-8859-1"); - path_set_fs_charset("ISO-8859-1"); - } -} - -void path_global_finish(void) -{ - g_free(fs_charset); -} diff --git a/src/path.h b/src/path.h deleted file mode 100644 index 00c368e70..000000000 --- a/src/path.h +++ /dev/null @@ -1,55 +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_PATH_H -#define MPD_PATH_H - -#include - -#if !defined(MPD_PATH_MAX) -# if defined(MAXPATHLEN) -# define MPD_PATH_MAX MAXPATHLEN -# elif defined(PATH_MAX) -# define MPD_PATH_MAX PATH_MAX -# else -# define MPD_PATH_MAX 256 -# endif -#endif - -void path_global_init(void); - -void path_global_finish(void); - -/** - * Converts a file name in the filesystem charset to UTF-8. Returns - * NULL on failure. - */ -char * -fs_charset_to_utf8(const char *path_fs); - -/** - * Converts a file name in UTF-8 to the filesystem charset. Returns a - * duplicate of the UTF-8 string on failure. - */ -char * -utf8_to_fs_charset(const char *path_utf8); - -const char *path_get_fs_charset(void); - -#endif -- cgit v1.2.3