diff options
Diffstat (limited to 'src/functiondirs.cpp')
-rw-r--r-- | src/functiondirs.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/functiondirs.cpp b/src/functiondirs.cpp new file mode 100644 index 0000000..fb64ef3 --- /dev/null +++ b/src/functiondirs.cpp | |||
@@ -0,0 +1,61 @@ | |||
1 | #include "functiondirs.h" | ||
2 | |||
3 | #include <sys/types.h> | ||
4 | #include <sys/stat.h> | ||
5 | #include <glob.h> | ||
6 | #include <unistd.h> | ||
7 | |||
8 | FunctionDirs::FunctionDirs() | ||
9 | { | ||
10 | } | ||
11 | |||
12 | FunctionDirs::~FunctionDirs() | ||
13 | { | ||
14 | } | ||
15 | |||
16 | Bu::FString FunctionDirs::getName() const | ||
17 | { | ||
18 | return "dirs"; | ||
19 | } | ||
20 | |||
21 | Variable FunctionDirs::call( Variable &/*input*/, VarList lParams ) | ||
22 | { | ||
23 | glob_t globbuf; | ||
24 | |||
25 | int flags = 0; | ||
26 | |||
27 | for( VarList::const_iterator i = lParams.begin(); i; i++ ) | ||
28 | { | ||
29 | switch( (*i).getType() ) | ||
30 | { | ||
31 | case Variable::typeString: | ||
32 | glob( (*i).getString().getStr(), flags, NULL, &globbuf ); | ||
33 | flags |= GLOB_APPEND; | ||
34 | break; | ||
35 | |||
36 | case Variable::typeList: | ||
37 | throw Bu::ExceptionBase("Lists not supported in glob yet."); | ||
38 | break; | ||
39 | |||
40 | default: | ||
41 | throw Bu::ExceptionBase( | ||
42 | "Cannot use a non-string or non-list as a parameter to glob" | ||
43 | ); | ||
44 | break; | ||
45 | } | ||
46 | } | ||
47 | |||
48 | Variable vRet( Variable::typeList ); | ||
49 | struct stat s; | ||
50 | for( size_t j = 0; j < globbuf.gl_pathc; j++ ) | ||
51 | { | ||
52 | stat( globbuf.gl_pathv[j], &s ); | ||
53 | if( S_ISDIR( s.st_mode ) ) | ||
54 | vRet.append( globbuf.gl_pathv[j] ); | ||
55 | } | ||
56 | |||
57 | globfree( &globbuf ); | ||
58 | |||
59 | return vRet; | ||
60 | } | ||
61 | |||