Statistics
| Branch: | Revision:

root / src / common / fileglobber.cc @ e1750c09

History | View | Annotate | Download (3.78 KB)

1
//==========================================================================
2
//  FILEGLOBBER.CC - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16

    
17

    
18
#include <string.h>
19
#include "platmisc.h"
20
#include "fileglobber.h"
21

    
22
#ifdef _WIN32
23

    
24
#include <io.h>
25
#include <direct.h>
26
#include <stdlib.h> // _MAX_PATH
27

    
28
USING_NAMESPACE
29

    
30

    
31
struct GlobPrivateData
32
{
33
    bool nowildcard;
34
    long handle;
35
    struct _finddata_t fdata;
36
    char dir[_MAX_PATH];
37
    char tmpfname[_MAX_PATH];
38
};
39

    
40
FileGlobber::FileGlobber(const char *filenamepattern)
41
{
42
    fnamepattern = filenamepattern;
43
    data = NULL;
44
}
45

    
46
FileGlobber::~FileGlobber()
47
{
48
    delete data;
49
}
50

    
51
const char *FileGlobber::getNext()
52
{
53
    if (!data)
54
    {
55
        // first call
56
        data = new GlobPrivateData();
57
        if (!strchr(fnamepattern.c_str(),'*') && !strchr(fnamepattern.c_str(),'?'))
58
        {
59
            data->nowildcard = true;
60
            return fnamepattern.c_str();
61
        }
62

    
63
        data->nowildcard = false;
64

    
65
        // remember directory in data->dir
66
        strcpy(data->dir,fnamepattern.c_str());
67
        char *s = data->dir + strlen(data->dir);
68
        while (--s>=data->dir)
69
            if (*s=='/' || *s=='\\')
70
                break;
71
        *(s+1)='\0';  // points (s+1) points either after last "/" or at beg of string.
72

    
73
        if (strchr(data->dir,'*') || strchr(data->dir,'?'))
74
            throw std::runtime_error(std::string(data->dir)+": wildcard characters in directory names are not allowed");
75

    
76
        // get first file
77
        data->handle = _findfirst(fnamepattern.c_str(), &data->fdata);
78
        if (data->handle<0)
79
        {
80
            _findclose(data->handle);
81
            return NULL;
82
        }
83

    
84
        // concat file name on directory
85
        strcpy(data->tmpfname,data->dir);
86
        strcat(data->tmpfname,data->fdata.name);
87
        return data->tmpfname;
88
    }
89
    else
90
    {
91
        // subsequent calls
92
        if (data->nowildcard)
93
            return NULL;
94
        int done=_findnext(data->handle, &data->fdata);
95
        if (done)
96
        {
97
            _findclose(data->handle);
98
            return NULL;
99
        }
100
        // concat file name on directory
101
        strcpy(data->tmpfname,data->dir);
102
        strcat(data->tmpfname,data->fdata.name);
103
        return data->tmpfname;
104
    }
105
}
106

    
107
#else
108

    
109
#include <glob.h>
110

    
111
struct GlobPrivateData
112
{
113
    bool nowildcard;
114
    glob_t globdata;
115
    int globpos;
116
};
117

    
118
FileGlobber::FileGlobber(const char *filenamepattern)
119
{
120
    fnamepattern = filenamepattern;
121
    data = NULL;
122
}
123

    
124
FileGlobber::~FileGlobber()
125
{
126
    globfree(&data->globdata);
127
    delete data;
128
}
129

    
130
const char *FileGlobber::getNext()
131
{
132
    if (!data)
133
    {
134
        data = new GlobPrivateData();
135
        if (!strchr(fnamepattern.c_str(),'*') && !strchr(fnamepattern.c_str(),'?'))
136
        {
137
            data->nowildcard = true;
138
            return fnamepattern.c_str();
139
        }
140

    
141
        data->nowildcard = false;
142
        if (glob(fnamepattern.c_str(), 0, NULL, &data->globdata)!=0)
143
            return NULL;
144
        data->globpos = 0;
145
        return data->globdata.gl_pathv[data->globpos++];
146
    }
147
    else
148
    {
149
        if (data->nowildcard)
150
            return NULL;
151
        return data->globdata.gl_pathv[data->globpos++];
152
    }
153
}
154
#endif
155

    
156

    
157
std::vector<std::string> FileGlobber::getFilenames()
158
{
159
    std::vector<std::string> v;
160
    const char *filename;
161
    while ((filename=getNext())!=NULL)
162
        v.push_back(filename);
163
    return v;
164
}
165

    
166