Skip to content

Commit

Permalink
Add MSVC implementation of directory listing to StringFeatures
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed Aug 17, 2016
1 parent 79a3dbc commit 97af9bf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/shogun/features/StringFeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <tchar.h>
#include <strsafe.h>
#include <vector>
#else
#include <unistd.h>

#endif

namespace shogun
{
Expand Down Expand Up @@ -793,7 +798,39 @@ template<class ST> bool CStringFeatures<ST>::load_from_directory(char* dirname)

SG_DEBUG("dirname '%s'\n", dirname)

#ifdef _WIN32
TCHAR search_dir[MAX_PATH];
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
HANDLE h_find = INVALID_HANDLE_VALUE;

StringCchCopy(search_dir, MAX_PATH, dirname);
StringCchCat(search_dir, MAX_PATH, TEXT("\\*"));

h_find = FindFirstFile(search_dir, &ffd);
if (INVALID_HANDLE_VALUE == h_find)
{
SG_ERROR("Error finding finds in %s\n", dirname)
return false;
}

std::vector<struct dirent*> files;
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
{
struct dirent* d = SG_MALLOC(struct dirent, 1);
StringCchCopy(d->d_name, MAX_PATH, ffd.cFileName);
files.push_back(d);
n++;
}
}
while (FindNextFile(h_find, &ffd) != 0);
namelist = &files[0];
FindClose(h_find);
#else
n=scandir(dirname, &namelist, &SGIO::filter, alphasort);
#endif
if (n <= 0)
{
SG_ERROR("error calling scandir - no files found\n")
Expand Down Expand Up @@ -849,6 +886,7 @@ template<class ST> bool CStringFeatures<ST>::load_from_directory(char* dirname)
return true;
}
}

return false;
}

Expand Down
4 changes: 4 additions & 0 deletions src/shogun/io/SGIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ uint32_t SGIO::ss_length(substring s)

char* SGIO::concat_filename(const char* filename)
{
#ifdef WIN32
if (snprintf(file_buffer, FBUFSIZE, "%s\\%s", directory_name, filename) > FBUFSIZE)
#else
if (snprintf(file_buffer, FBUFSIZE, "%s/%s", directory_name, filename) > FBUFSIZE)
#endif
SG_SERROR("filename too long")

SG_SDEBUG("filename=\"%s\"\n", file_buffer)
Expand Down

0 comments on commit 97af9bf

Please sign in to comment.