aboutsummaryrefslogtreecommitdiff
path: root/src/functionfiles.cpp
blob: 1a010ea784197718bd57e14a926e8c36c4bd078a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (C) 2007-2014 Xagasoft, All rights reserved.
 *
 * This file is part of the Xagasoft Build and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "functionfiles.h"

#include <sys/types.h> 
#include <sys/stat.h> 
#include <glob.h>
#include <unistd.h>

#include <bu/plugger.h>
PluginInterface3( pluginFunctionFiles, files, FunctionFiles, Function,
        "Mike Buland", 0, 1 );

FunctionFiles::FunctionFiles()
{
}

FunctionFiles::~FunctionFiles()
{
}

Bu::String FunctionFiles::getName() const
{
    return "files";
}

Variable FunctionFiles::call( Variable &/*input*/, VarList lParams )
{
    glob_t globbuf;

    int flags = 0;

    for( VarList::const_iterator i = lParams.begin(); i; i++ )
    {
        switch( (*i).getType() )
        {
            case Variable::typeString:
                glob( (*i).getString().getStr(), flags, NULL, &globbuf );
                flags |= GLOB_APPEND;
                break;

            case Variable::typeList:
                throw Bu::ExceptionBase("Lists not supported in glob yet.");
                break;

            default:
                throw Bu::ExceptionBase(
                    "Cannot use a non-string or non-list as a parameter to glob"
                    );
                break;
        }
    }

    Variable vRet( Variable::typeList );
    struct stat s;
    for( size_t j = 0; j < globbuf.gl_pathc; j++ )
    {
        stat( globbuf.gl_pathv[j], &s );
        if( S_ISREG( s.st_mode ) )
            vRet.append( globbuf.gl_pathv[j] );
    }

    globfree( &globbuf );

    return vRet;
}