-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Replace POSIX directory operations by Boost Filesystem #1090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,6 @@ | |
|
|
||
| #include "cpp-build/generate_geocoding_data.h" | ||
|
|
||
| #include <dirent.h> | ||
| #include <locale> | ||
| #include <sys/stat.h> | ||
| #include <algorithm> | ||
|
|
@@ -31,6 +30,7 @@ | |
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
| #include <boost/filesystem.hpp> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code needs to continue to work without Boost. Please follow the pattern in https://github.com/googlei18n/libphonenumber/blob/master/cpp/src/phonenumbers/base/memory/scoped_ptr.h to use Boost "#if defined(I18N_PHONENUMBERS_USE_BOOST)" and fall back to the current code based on dirent.h. |
||
|
|
||
| #include "base/basictypes.h" | ||
|
|
||
|
|
@@ -43,6 +43,8 @@ using std::vector; | |
| using std::set; | ||
| using std::pair; | ||
|
|
||
| namespace fs = boost::filesystem; | ||
|
|
||
| template <typename ResourceType> class AutoCloser { | ||
| public: | ||
| typedef int (*ReleaseFunction) (ResourceType* resource); | ||
|
|
@@ -79,7 +81,7 @@ enum DirEntryKinds { | |
|
|
||
| class DirEntry { | ||
| public: | ||
| DirEntry(const char* n, DirEntryKinds k) | ||
| DirEntry(const std::string& n, DirEntryKinds k) | ||
| : name_(n), | ||
| kind_(k) | ||
| {} | ||
|
|
@@ -96,36 +98,26 @@ class DirEntry { | |
| // success. | ||
| bool ListDirectory(const string& path, vector<DirEntry>* entries) { | ||
| entries->clear(); | ||
| DIR* dir = opendir(path.c_str()); | ||
| if (!dir) { | ||
| return false; | ||
| } | ||
| AutoCloser<DIR> dir_closer(&dir, closedir); | ||
| struct dirent entry, *dir_result; | ||
| struct stat entry_stat; | ||
| while (true) { | ||
| const int res = readdir_r(dir, &entry, &dir_result); | ||
| if (res) { | ||
| return false; | ||
| } | ||
| if (dir_result == NULL) { | ||
| return true; | ||
| } | ||
| if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) { | ||
| continue; | ||
| } | ||
| const string entry_path = path + "/" + entry.d_name; | ||
| if (stat(entry_path.c_str(), &entry_stat)) { | ||
| return false; | ||
| } | ||
| DirEntryKinds kind = kFile; | ||
| if (S_ISDIR(entry_stat.st_mode)) { | ||
| kind = kDirectory; | ||
| } else if (!S_ISREG(entry_stat.st_mode)) { | ||
| continue; | ||
|
|
||
| try { | ||
| for (fs::directory_iterator it(path); it != fs::directory_iterator(); ++it) { | ||
| DirEntryKinds kind; | ||
|
|
||
| if (fs::is_directory(it->status())) { | ||
| kind = kDirectory; | ||
| } else if (fs::is_regular_file(it->status())) { | ||
| kind = kFile; | ||
| } else { | ||
| continue; | ||
| } | ||
|
|
||
| entries->push_back(DirEntry(it->path().filename().string(), kind)); | ||
| } | ||
| entries->push_back(DirEntry(entry.d_name, kind)); | ||
| } catch (const fs::filesystem_error& ex) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // Returns true if s ends with suffix. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look right to me.
We can't replace "USE_BOOST" check with "USE_BOOST OR BUILD_GEOCODER" when the check is guarding includes for Boost headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I take this back. In this context this is doing the right thing. My initial reading was wrong.