Skip to content

Commit ed32e8c

Browse files
author
Clement Champetier
committed
profile/util: getFilesInDir can browse files on Windows
* Add system.hpp to detect the OS. * Fix #55
1 parent 2020627 commit ed32e8c

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

src/AvTranscoder/common.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _AV_TRANSCODER_COMMON_HPP_
22
#define _AV_TRANSCODER_COMMON_HPP_
33

4+
#include <AvTranscoder/system.hpp>
5+
46
extern "C" {
57
#ifndef __STDC_CONSTANT_MACROS
68
#define __STDC_CONSTANT_MACROS

src/AvTranscoder/profile/util.hpp

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
#ifndef _AV_TRANSCODER_PROFILE_UTIL_HPP_
22
#define _AV_TRANSCODER_PROFILE_UTIL_HPP_
33

4-
#include <string>
4+
#if defined ( __LINUX__ )
5+
6+
#define DIRLIST_SEP_CHARS ":;"
7+
#define DIRSEP "/"
8+
#include <dirent.h>
9+
10+
#elif defined ( __MACOS__ )
11+
12+
#define DIRLIST_SEP_CHARS ";:"
13+
#define DIRSEP "/"
514
#include <dirent.h>
15+
16+
#elif defined ( __WINDOWS__ )
17+
18+
#define DIRLIST_SEP_CHARS ";"
19+
#define DIRSEP "\\"
20+
21+
// CINTERFACE needs to be declared if compiling with VC++
22+
#include <shlobj.h>
23+
#include <tchar.h>
24+
#ifndef _MSC_VER
25+
#define SHGFP_TYPE_CURRENT 0
26+
#endif
27+
28+
#endif
29+
30+
#include <string>
631
#include <iostream>
732

833
namespace avtranscoder
@@ -20,22 +45,49 @@ void split( std::vector< std::string >& splitString, const std::string& inputStr
2045

2146
int getFilesInDir( const std::string& dir, std::vector< std::string >& files )
2247
{
48+
#if defined ( __WINDOWS__ )
49+
WIN32_FIND_DATA findData;
50+
HANDLE findHandle;
51+
52+
#else
2353
DIR *dp;
2454
struct dirent *dirp;
25-
if( ( dp = opendir( dir.c_str() ) ) == NULL )
55+
if( ( dp = opendir( dir.c_str() ) ) == NULL )
2656
{
2757
std::cerr << "Error(" << errno << ") opening " << dir << std::endl;
2858
return errno;
2959
}
60+
#endif
3061

62+
#if defined ( __WINDOWS__ )
63+
findHandle = FindFirstFile( ( dir + "\\*" ).c_str(), &findData );
64+
if( findHandle == INVALID_HANDLE_VALUE )
65+
{
66+
return -1;
67+
}
68+
while(1)
69+
{
70+
const std::string filename( findData.cFileName );
71+
bool isdir = ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0;
72+
if( ! isdir )
73+
files.push_back( filename );
74+
75+
int rval = FindNextFile( findHandle, &findData );
76+
if( rval == 0 )
77+
break;
78+
}
79+
80+
#else
3181
while( ( dirp = readdir( dp ) ) != NULL )
3282
{
33-
std::string filename( dirp->d_name );
83+
const std::string filename( dirp->d_name );
3484
if( filename == "." || filename == ".." )
3585
continue;
3686
files.push_back( filename );
3787
}
3888
closedir( dp );
89+
#endif
90+
3991
return 0;
4092
}
4193

src/AvTranscoder/system.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef _AV_TRANSCODER_SYSTEM_HPP_
2+
#define _AV_TRANSCODER_SYSTEM_HPP_
3+
4+
#if defined( linux ) \
5+
|| defined( __linux ) \
6+
|| defined( LINUX ) \
7+
|| defined( _LINUX ) \
8+
|| defined( __LINUX__ ) \
9+
10+
#ifndef __LINUX__
11+
#define __LINUX__
12+
#endif
13+
14+
15+
#elif defined( macintosh ) \
16+
|| defined( Macintosh ) \
17+
|| defined( __APPLE__ ) \
18+
|| defined( __MACH__ ) \
19+
|| defined( MACOS ) \
20+
|| defined( MACOSX ) \
21+
|| defined( __MACOS__ ) \
22+
23+
#ifndef __MACOS__
24+
#define __MACOS__
25+
#endif
26+
27+
28+
#elif defined( WIN32 ) \
29+
|| defined( _WIN32 ) \
30+
|| defined( __WIN32__ ) \
31+
|| defined( WIN64 ) \
32+
|| defined( _WIN64 ) \
33+
|| defined( __WIN64__ ) \
34+
|| defined( __TOS_WIN__ ) \
35+
|| defined( WINDOWS ) \
36+
|| defined( _WINDOWS ) \
37+
|| defined( __WINDOWS__ ) \
38+
39+
#ifndef __WINDOWS__
40+
#define __WINDOWS__
41+
#endif
42+
43+
#else
44+
45+
#warning "Your operating system is not recognized."
46+
47+
#endif
48+
49+
#endif

0 commit comments

Comments
 (0)