Fix possible memory leak, split FSLib::Directory into cpp and hpp parts

This commit is contained in:
zvon 2019-01-06 20:40:16 +01:00
parent 715153f13f
commit 9af53e272e
2 changed files with 70 additions and 44 deletions

View File

@ -16,8 +16,10 @@ bool FSLib::isDirectory(const std::string &path) {
std::string FSLib::canonical(const std::string &path) { std::string FSLib::canonical(const std::string &path) {
char *canonical_path = static_cast<char *>(malloc(PATH_MAX)); char *canonical_path = static_cast<char *>(malloc(PATH_MAX));
if( realpath(path.c_str(), canonical_path) == nullptr ) if( realpath(path.c_str(), canonical_path) == nullptr ) {
free(canonical_path);
return ""; return "";
}
std::string canonical_string{canonical_path}; std::string canonical_string{canonical_path};
free(canonical_path); free(canonical_path);
return canonical_string; return canonical_string;
@ -27,3 +29,57 @@ bool FSLib::rename(const std::string &file_a, const std::string &file_b) {
return ::rename( file_a.c_str(), file_b.c_str() ) == 0; return ::rename( file_a.c_str(), file_b.c_str() ) == 0;
} }
FSLib::Directory::iterator FSLib::Directory::begin() {
return Iterator(dir_path);
}
FSLib::Directory::const_iterator FSLib::Directory::begin() const {
return Iterator(dir_path);
}
FSLib::Directory::const_iterator FSLib::Directory::cbegin() const {
return begin();
}
FSLib::Directory::iterator FSLib::Directory::end() {
return Iterator(dir_path, nullptr);
}
FSLib::Directory::const_iterator FSLib::Directory::end() const {
return Iterator(dir_path, nullptr);
}
FSLib::Directory::const_iterator FSLib::Directory::cend() const {
return end();
}
const char *FSLib::Directory::path() const {
return dir_path.c_str();
}
std::string FSLib::Directory::Iterator::operator*() {
return current_entry->d_name;
}
FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() {
if( current_entry == nullptr )
return *this;
current_entry = readdir(d);
if( current_entry != nullptr && (!strcmp(current_entry->d_name, ".") || !strcmp(current_entry->d_name, "..")) )
operator++();
return *this;
}
FSLib::Directory::Iterator FSLib::Directory::Iterator::operator++(int) {
Iterator ret(*this);
operator++();
return ret;
}
bool FSLib::Directory::Iterator::operator==(const Iterator &i_other) {
return i_other.current_entry == current_entry;
}
bool FSLib::Directory::Iterator::operator!=(const Iterator &i_other) {
return i_other.current_entry != current_entry;
}

View File

@ -36,32 +36,15 @@ namespace FSLib{
closedir(d); closedir(d);
} }
std::string operator*() { std::string operator*();
return current_entry->d_name;
}
Iterator &operator++() { Iterator &operator++();
if( current_entry == nullptr )
return *this;
current_entry = readdir(d);
if( current_entry != nullptr && (!strcmp(current_entry->d_name, ".") || !strcmp(current_entry->d_name, "..")) )
operator++();
return *this;
}
Iterator operator++(int) { Iterator operator++(int);
Iterator ret(*this);
operator++();
return ret;
}
bool operator==(const Iterator &i_other) { bool operator==(const Iterator &i_other);
return i_other.current_entry == current_entry;
}
bool operator!=(const Iterator &i_other) { bool operator!=(const Iterator &i_other);
return i_other.current_entry != current_entry;
}
private: private:
DIR *d; DIR *d;
@ -71,33 +54,20 @@ namespace FSLib{
using iterator = Iterator; using iterator = Iterator;
using const_iterator = Iterator; using const_iterator = Iterator;
iterator begin() { iterator begin();
return Iterator(dir_path);
}
const_iterator begin() const { const_iterator begin() const;
return Iterator(dir_path);
}
const_iterator cbegin() const { const_iterator cbegin() const;
return begin();
}
iterator end() { iterator end();
return Iterator(dir_path, nullptr);
}
const_iterator end() const { const_iterator end() const;
return Iterator(dir_path, nullptr);
}
const_iterator cend() const { const_iterator cend() const;
return end();
} const char *path() const;
const char *path() const {
return dir_path.c_str();
}
private: private:
std::string dir_path; std::string dir_path;
}; };