From a2fbce0e7f31bdebf59bcea5282b4909c9e19837 Mon Sep 17 00:00:00 2001 From: Tim Mayberry Date: Tue, 18 Aug 2015 14:44:53 +1000 Subject: [PATCH] Change return type and name of get_win_special_folder Rename it get_win_special_folder_path to indicate what it is returning Move documentation for the function into the header and use doxygen style comments. Fixes a couple of memory leaks in ArdourVideoToolPaths class although it looks as if there are more. --- gtk2_ardour/video_tool_paths.cc | 22 ++++++++++---------- libs/ardour/search_paths.cc | 16 ++++++--------- libs/pbd/pbd/windows_special_dirs.h | 14 ++++++++++++- libs/pbd/windows_special_dirs.cc | 31 ++++++++++------------------- 4 files changed, 40 insertions(+), 43 deletions(-) diff --git a/gtk2_ardour/video_tool_paths.cc b/gtk2_ardour/video_tool_paths.cc index 6f3d56b48c..2ac4f4d401 100644 --- a/gtk2_ardour/video_tool_paths.cc +++ b/gtk2_ardour/video_tool_paths.cc @@ -66,7 +66,7 @@ ArdourVideoToolPaths::harvid_exe (std::string &harvid_exe) #ifdef PLATFORM_WINDOWS std::string reg; - const char *program_files = PBD::get_win_special_folder (CSIDL_PROGRAM_FILES); + std::string program_files = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILES); #endif harvid_exe = ""; @@ -84,9 +84,9 @@ ArdourVideoToolPaths::harvid_exe (std::string &harvid_exe) { harvid_exe = g_build_filename(reg.c_str(), "harvid.exe", NULL); } - else if (program_files && Glib::file_test(g_build_filename(program_files, "harvid", "harvid.exe", NULL), Glib::FILE_TEST_EXISTS)) + else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "harvid", "harvid.exe", NULL), Glib::FILE_TEST_EXISTS)) { - harvid_exe = g_build_filename(program_files, "harvid", "harvid.exe", NULL); + harvid_exe = g_build_filename(program_files.c_str(), "harvid", "harvid.exe", NULL); } else if (Glib::file_test(X_("C:\\Program Files\\harvid\\harvid.exe"), Glib::FILE_TEST_EXISTS)) { harvid_exe = X_("C:\\Program Files\\harvid\\harvid.exe"); @@ -105,7 +105,7 @@ ArdourVideoToolPaths::xjadeo_exe (std::string &xjadeo_exe) std::string xjadeo_file_path; #ifdef PLATFORM_WINDOWS std::string reg; - const char *program_files = PBD::get_win_special_folder (CSIDL_PROGRAM_FILES); + std::string program_files = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILES); #endif if (getenv("XJREMOTE")) { xjadeo_exe = getenv("XJREMOTE"); @@ -131,9 +131,9 @@ ArdourVideoToolPaths::xjadeo_exe (std::string &xjadeo_exe) { xjadeo_exe = std::string(g_build_filename(reg.c_str(), "xjadeo.exe", NULL)); } - else if (program_files && Glib::file_test(g_build_filename(program_files, "xjadeo", "xjadeo.exe", NULL), Glib::FILE_TEST_EXISTS)) + else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "xjadeo", "xjadeo.exe", NULL), Glib::FILE_TEST_EXISTS)) { - xjadeo_exe = std::string(g_build_filename(program_files, "xjadeo", "xjadeo.exe", NULL)); + xjadeo_exe = std::string(g_build_filename(program_files.c_str(), "xjadeo", "xjadeo.exe", NULL)); } else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjadeo.exe"), Glib::FILE_TEST_EXISTS)) { xjadeo_exe = X_("C:\\Program Files\\xjadeo\\xjadeo.exe"); @@ -151,7 +151,7 @@ ArdourVideoToolPaths::transcoder_exe (std::string &ffmpeg_exe, std::string &ffpr { #ifdef PLATFORM_WINDOWS std::string reg; - const char *program_files = PBD::get_win_special_folder (CSIDL_PROGRAM_FILES); + std::string program_files = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILES); #endif ffmpeg_exe = X_(""); @@ -176,8 +176,8 @@ ArdourVideoToolPaths::transcoder_exe (std::string &ffmpeg_exe, std::string &ffpr if (Glib::file_test(ffmpeg_exe, Glib::FILE_TEST_EXISTS)) { ; } - else if (program_files && Glib::file_test(g_build_filename(program_files, "harvid", "ffmpeg.exe", NULL), Glib::FILE_TEST_EXISTS)) { - ffmpeg_exe = g_build_filename(program_files, "harvid", "ffmpeg.exe", NULL); + else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "harvid", "ffmpeg.exe", NULL), Glib::FILE_TEST_EXISTS)) { + ffmpeg_exe = g_build_filename(program_files.c_str(), "harvid", "ffmpeg.exe", NULL); } else if (Glib::file_test(X_("C:\\Program Files\\ffmpeg\\ffmpeg.exe"), Glib::FILE_TEST_EXISTS)) { ffmpeg_exe = X_("C:\\Program Files\\ffmpeg\\ffmpeg.exe"); @@ -193,8 +193,8 @@ ArdourVideoToolPaths::transcoder_exe (std::string &ffmpeg_exe, std::string &ffpr if (Glib::file_test(ffprobe_exe, Glib::FILE_TEST_EXISTS)) { ; } - else if (program_files && Glib::file_test(g_build_filename(program_files, "harvid", "ffprobe.exe", NULL), Glib::FILE_TEST_EXISTS)) { - ffprobe_exe = g_build_filename(program_files, "harvid", "ffprobe.exe", NULL); + else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "harvid", "ffprobe.exe", NULL), Glib::FILE_TEST_EXISTS)) { + ffprobe_exe = g_build_filename(program_files.c_str(), "harvid", "ffprobe.exe", NULL); } else if (Glib::file_test(X_("C:\\Program Files\\ffmpeg\\ffprobe.exe"), Glib::FILE_TEST_EXISTS)) { ffprobe_exe = X_("C:\\Program Files\\ffmpeg\\ffprobe.exe"); diff --git a/libs/ardour/search_paths.cc b/libs/ardour/search_paths.cc index 6b54ff566c..d9e3bf7bc9 100644 --- a/libs/ardour/search_paths.cc +++ b/libs/ardour/search_paths.cc @@ -196,11 +196,11 @@ vst_search_path () if (p == 0) { char *pVSTx86 = 0; - char *pProgFilesX86 = PBD::get_win_special_folder (CSIDL_PROGRAM_FILESX86); + std::string pProgFilesX86 = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILESX86); - if (pProgFilesX86) { + if (!pProgFilesX86.empty()) { // Look for a VST folder under C:\Program Files (x86) - if ((pVSTx86 = g_build_filename (pProgFilesX86, "Steinberg", "VSTPlugins", NULL))) + if ((pVSTx86 = g_build_filename (pProgFilesX86.c_str(), "Steinberg", "VSTPlugins", NULL))) { if (Glib::file_test (pVSTx86, Glib::FILE_TEST_EXISTS)) if (Glib::file_test (pVSTx86, Glib::FILE_TEST_IS_DIR)) @@ -208,25 +208,21 @@ vst_search_path () g_free (pVSTx86); } - - g_free (pProgFilesX86); } if (p == 0) { // Look for a VST folder under C:\Program Files char *pVST = 0; - char *pProgFiles = PBD::get_win_special_folder (CSIDL_PROGRAM_FILES); + std::string pProgFiles = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILES); - if (pProgFiles) { - if ((pVST = g_build_filename (pProgFiles, "Steinberg", "VSTPlugins", NULL))) { + if (!pProgFiles.empty()) { + if ((pVST = g_build_filename (pProgFiles.c_str(), "Steinberg", "VSTPlugins", NULL))) { if (Glib::file_test (pVST, Glib::FILE_TEST_EXISTS)) if (Glib::file_test (pVST, Glib::FILE_TEST_IS_DIR)) p = g_build_filename (pVST, NULL); g_free (pVST); } - - g_free (pProgFiles); } } } diff --git a/libs/pbd/pbd/windows_special_dirs.h b/libs/pbd/pbd/windows_special_dirs.h index c557ef4c16..0bc5accc96 100644 --- a/libs/pbd/pbd/windows_special_dirs.h +++ b/libs/pbd/pbd/windows_special_dirs.h @@ -20,10 +20,22 @@ #ifndef __libpbd_windows_special_dirs_h__ #define __libpbd_windows_special_dirs_h__ +#include + #include "pbd/libpbd_visibility.h" namespace PBD { - LIBPBD_API char * get_win_special_folder (int csidl); + +/** +* Gets the full path that corresponds of one of the Windows special folders, +* such as "My Documents" and the like. +* +* @param csidl corresponds to CSIDL values, such as CSIDL_SYSTEM etc. +* @return A string containing the name of the special folder or an empty +* string on failure. +*/ +LIBPBD_API std::string get_win_special_folder_path (int csidl); + } #endif /* __libpbd_windows_special_dirs_h__ */ diff --git a/libs/pbd/windows_special_dirs.cc b/libs/pbd/windows_special_dirs.cc index 5e924f57e1..d5138050df 100644 --- a/libs/pbd/windows_special_dirs.cc +++ b/libs/pbd/windows_special_dirs.cc @@ -23,38 +23,27 @@ #include "pbd/windows_special_dirs.h" -//*************************************************************** -// -// get_win_special_folder() -// -// Gets the full path name that corresponds of one of the Windows -// special folders, such as "My Documents" and the like. The input -// parameter must be one of the corresponding CSIDL values, such -// as CSIDL_SYSTEM etc. -// -// Returns: -// -// On Success: A pointer to a newly allocated string containing -// the name of the special folder (must later be freed). -// On Failure: NULL -// - -char * -PBD::get_win_special_folder (int csidl) +std::string +PBD::get_win_special_folder_path (int csidl) { wchar_t path[PATH_MAX+1]; HRESULT hr; LPITEMIDLIST pidl = 0; - char *retval = 0; + char *utf8_folder_path = 0; if (S_OK == (hr = SHGetSpecialFolderLocation (0, csidl, &pidl))) { if (SHGetPathFromIDListW (pidl, path)) { - retval = g_utf16_to_utf8 ((const gunichar2*)path, -1, 0, 0, 0); + utf8_folder_path = g_utf16_to_utf8 ((const gunichar2*)path, -1, 0, 0, 0); } CoTaskMemFree (pidl); } - return retval; + if (utf8_folder_path != NULL) { + std::string folder_path(utf8_folder_path); + g_free (utf8_folder_path); + return folder_path; + } + return std::string(); }