Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
endif ()

# Find all the required libraries and programs.
if (${USE_BOOST} STREQUAL "ON")
if (${USE_BOOST} STREQUAL "ON" OR ${BUILD_GEOCODER} STREQUAL "ON")
Copy link
Contributor

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.

Copy link
Contributor

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.

add_definitions ("-DI18N_PHONENUMBERS_USE_BOOST")
if (WIN32)
set (Boost_USE_STATIC_LIBS ON)
endif ()
find_package (Boost 1.40.0 COMPONENTS date_time system thread)
find_package (Boost 1.40.0 COMPONENTS date_time filesystem system thread)
if (NOT Boost_FOUND)
print_error ("Boost Date_Time/System/Thread" "Boost")
print_error ("Boost Date_Time/Filesystem/System/Thread" "Boost")
endif ()
include_directories (${Boost_INCLUDE_DIRS})
endif ()
Expand Down
1 change: 1 addition & 0 deletions tools/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ endif ()
include_directories ("src")

add_executable (generate_geocoding_data ${SOURCES})
target_link_libraries (generate_geocoding_data ${Boost_LIBRARIES})

set (TEST_SOURCES
"src/cpp-build/generate_geocoding_data.cc"
Expand Down
52 changes: 22 additions & 30 deletions tools/cpp/src/cpp-build/generate_geocoding_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "cpp-build/generate_geocoding_data.h"

#include <dirent.h>
#include <locale>
#include <sys/stat.h>
#include <algorithm>
Expand All @@ -31,6 +30,7 @@
#include <string>
#include <utility>
#include <vector>
#include <boost/filesystem.hpp>
Copy link
Contributor

Choose a reason for hiding this comment

The 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"

Expand All @@ -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);
Expand Down Expand Up @@ -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)
{}
Expand All @@ -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.
Expand Down