diff options
author | Tim Mayberry <mojofunk@gmail.com> | 2014-06-16 20:39:45 +1000 |
---|---|---|
committer | Tim Mayberry <mojofunk@gmail.com> | 2014-06-17 21:13:05 +1000 |
commit | 0e96d84079c1792523d99b6bbec5878d11f5c8e4 (patch) | |
tree | b4faae743beff6136031953c12b3858d6b44cea8 /libs/pbd | |
parent | e426c603b679903502989b2b36966e3fb2facd23 (diff) |
Change PBD::PathScanner API to return results by value to avoid inadvertent memory leaks
Diffstat (limited to 'libs/pbd')
-rw-r--r-- | libs/pbd/file_utils.cc | 14 | ||||
-rw-r--r-- | libs/pbd/pathscanner.cc | 92 | ||||
-rw-r--r-- | libs/pbd/pbd/pathscanner.h | 78 |
3 files changed, 84 insertions, 100 deletions
diff --git a/libs/pbd/file_utils.cc b/libs/pbd/file_utils.cc index 756bd24fe4..552012d227 100644 --- a/libs/pbd/file_utils.cc +++ b/libs/pbd/file_utils.cc @@ -229,16 +229,12 @@ void copy_files(const std::string & from_path, const std::string & to_dir) { PathScanner scanner; - vector<string*>* files = scanner (from_path, accept_all_files, 0, true, false); + vector<string> files = scanner (from_path, accept_all_files, 0, true, false); - if (files) { - for (vector<string*>::iterator i = files->begin(); i != files->end(); ++i) { - std::string from = Glib::build_filename (from_path, **i); - std::string to = Glib::build_filename (to_dir, **i); - copy_file (from, to); - } - vector_delete (files); - delete (files); + for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) { + std::string from = Glib::build_filename (from_path, *i); + std::string to = Glib::build_filename (to_dir, *i); + copy_file (from, to); } } diff --git a/libs/pbd/pathscanner.cc b/libs/pbd/pathscanner.cc index b60e1eee1a..bd010e66c5 100644 --- a/libs/pbd/pathscanner.cc +++ b/libs/pbd/pathscanner.cc @@ -40,12 +40,11 @@ using PBD::closedir; #include "pbd/error.h" #include "pbd/pathexpand.h" #include "pbd/pathscanner.h" -#include "pbd/stl_delete.h" using namespace std; using namespace PBD; -vector<string *> * +vector<string> PathScanner::operator() (const string &dirpath, const string ®exp, bool match_fullpath, bool return_fullpath, long limit, bool recurse) @@ -65,7 +64,7 @@ PathScanner::operator() (const string &dirpath, const string ®exp, << ")" << endmsg; - return 0; + return vector<string>(); } return run_scan (dirpath, &PathScanner::regexp_filter, @@ -76,7 +75,7 @@ PathScanner::operator() (const string &dirpath, const string ®exp, limit, recurse); } -vector<string *> * +vector<string> PathScanner::run_scan (const string &dirpath, bool (PathScanner::*memberfilter)(const string &), bool (*filter)(const string &, void *), @@ -85,11 +84,13 @@ PathScanner::run_scan (const string &dirpath, long limit, bool recurse) { - return run_scan_internal ((vector<string*>*) 0, dirpath, memberfilter, filter, arg, match_fullpath, return_fullpath, limit, recurse); + vector<string> result; + run_scan_internal (result, dirpath, memberfilter, filter, arg, match_fullpath, return_fullpath, limit, recurse); + return result; } -vector<string *> * -PathScanner::run_scan_internal (vector<string *> *result, +void +PathScanner::run_scan_internal (vector<string>& result, const string &dirpath, bool (PathScanner::*memberfilter)(const string &), bool (*filter)(const string &, void *), @@ -104,18 +105,13 @@ PathScanner::run_scan_internal (vector<string *> *result, char *thisdir; string fullpath; string search_str; - string *newstr; long nfound = 0; char *saveptr; if ((thisdir = strtok_r (pathcopy, G_SEARCHPATH_SEPARATOR_S, &saveptr)) == 0 || strlen (thisdir) == 0) { free (pathcopy); - return 0; - } - - if (result == 0) { - result = new vector<string *>; + return; } do { @@ -161,12 +157,11 @@ PathScanner::run_scan_internal (vector<string *> *result, } if (return_fullpath) { - newstr = new string (fullpath); + result.push_back(fullpath); } else { - newstr = new string (finfo->d_name); + result.push_back(finfo->d_name); } - result->push_back (newstr); nfound++; } } @@ -175,17 +170,16 @@ PathScanner::run_scan_internal (vector<string *> *result, } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok_r (0, G_SEARCHPATH_SEPARATOR_S, &saveptr))); free (pathcopy); - return result; + return; } -string * +string PathScanner::find_first (const string &dirpath, const string ®exp, bool match_fullpath, bool return_fullpath) { - vector<string *> *res; - string *ret; + vector<string> res; int err; char msg[256]; @@ -197,51 +191,45 @@ PathScanner::find_first (const string &dirpath, error << "Cannot compile soundfile regexp for use (" << msg << ")" << endmsg; - return 0; } - res = run_scan (dirpath, - &PathScanner::regexp_filter, - (bool (*)(const string &, void *)) 0, - 0, - match_fullpath, - return_fullpath, - 1); + run_scan_internal (res, dirpath, + &PathScanner::regexp_filter, + (bool (*)(const string &, void *)) 0, + 0, + match_fullpath, + return_fullpath, + 1); - if (res->size() == 0) { - ret = 0; - } else { - ret = res->front(); + if (res.size() == 0) { + return string(); } - vector_delete (res); - delete res; - return ret; + + return res.front(); } -string * +string PathScanner::find_first (const string &dirpath, bool (*filter)(const string &, void *), void * /*arg*/, bool match_fullpath, bool return_fullpath) { - vector<string *> *res; - string *ret; - - res = run_scan (dirpath, - (bool (PathScanner::*)(const string &)) 0, - filter, - 0, - match_fullpath, - return_fullpath, 1); + vector<string> res; + string ret; + + run_scan_internal (res, + dirpath, + (bool (PathScanner::*)(const string &)) 0, + filter, + 0, + match_fullpath, + return_fullpath, 1); - if (res->size() == 0) { - ret = 0; - } else { - ret = res->front(); + if (res.size() == 0) { + return string(); } - vector_delete (res); - delete res; - return ret; + + return res.front(); } diff --git a/libs/pbd/pbd/pathscanner.h b/libs/pbd/pbd/pathscanner.h index d62203c008..990aa44c77 100644 --- a/libs/pbd/pbd/pathscanner.h +++ b/libs/pbd/pbd/pathscanner.h @@ -34,13 +34,13 @@ class LIBPBD_API PathScanner { public: - std::vector<std::string *> *operator() (const std::string &dirpath, - bool (*filter)(const std::string &, void *arg), - void *arg, - bool match_fullpath = true, - bool return_fullpath = true, - long limit = -1, - bool recurse = false) { + std::vector<std::string> operator() (const std::string &dirpath, + bool (*filter)(const std::string &, void *arg), + void *arg, + bool match_fullpath = true, + bool return_fullpath = true, + long limit = -1, + bool recurse = false) { return run_scan (dirpath, (bool (PathScanner::*)(const std::string &)) 0, filter, @@ -50,23 +50,23 @@ class LIBPBD_API PathScanner limit, recurse); } - std::vector<std::string *> *operator() (const std::string &dirpath, - const std::string ®exp, - bool match_fullpath = true, - bool return_fullpath = true, - long limit = -1, - bool recurse = false); + std::vector<std::string> operator() (const std::string &dirpath, + const std::string ®exp, + bool match_fullpath = true, + bool return_fullpath = true, + long limit = -1, + bool recurse = false); - std::string *find_first (const std::string &dirpath, - const std::string ®exp, - bool match_fullpath = true, - bool return_fullpath = true); + std::string find_first (const std::string &dirpath, + const std::string ®exp, + bool match_fullpath = true, + bool return_fullpath = true); - std::string *find_first (const std::string &dirpath, - bool (*filter)(const std::string &, void *), - void *arg, - bool match_fullpath = true, - bool return_fullpath = true); + std::string find_first (const std::string &dirpath, + bool (*filter)(const std::string &, void *), + void *arg, + bool match_fullpath = true, + bool return_fullpath = true); private: regex_t compiled_pattern; @@ -75,24 +75,24 @@ class LIBPBD_API PathScanner return regexec (&compiled_pattern, str.c_str(), 0, 0, 0) == 0; } - std::vector<std::string *> *run_scan (const std::string &dirpath, - bool (PathScanner::*mfilter) (const std::string &), - bool (*filter)(const std::string &, void *), - void *arg, - bool match_fullpath, - bool return_fullpath, - long limit, - bool recurse = false); + std::vector<std::string> run_scan (const std::string &dirpath, + bool (PathScanner::*mfilter) (const std::string &), + bool (*filter)(const std::string &, void *), + void *arg, + bool match_fullpath, + bool return_fullpath, + long limit, + bool recurse = false); - std::vector<std::string *> *run_scan_internal (std::vector<std::string*>*, - const std::string &dirpath, - bool (PathScanner::*mfilter) (const std::string &), - bool (*filter)(const std::string &, void *), - void *arg, - bool match_fullpath, - bool return_fullpath, - long limit, - bool recurse = false); + void run_scan_internal (std::vector<std::string>&, + const std::string &dirpath, + bool (PathScanner::*mfilter) (const std::string &), + bool (*filter)(const std::string &, void *), + void *arg, + bool match_fullpath, + bool return_fullpath, + long limit, + bool recurse = false); }; #endif // __libmisc_pathscanner_h__ |