summaryrefslogtreecommitdiffstats
path: root/util/src/platforms/windows/dir_windows.c
blob: 1e0ad8fd6fcb01af31a8cbb1896ef2bcd8db64c6 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <malloc.h>
#include <string.h>
#include <errno.h>

#include <platforms/windows/axutil_dir_windows.h>

/*dirent.h style mehtods for win32*/

AXIS2_DIR *AXIS2_CALL
axis2_opendir(
    const char *_dirname)
{
    AXIS2_DIR *dirp;
    char *filespec;
    long handle;
    int index;

    filespec = malloc(strlen(_dirname) + 2 + 1);
    strcpy(filespec, _dirname);
    index = (int)strlen(filespec) - 1;
    if(index >= 0 && (filespec[index] == '/' || (filespec[index] == '\\' && !IsDBCSLeadByte(
        filespec[index - 1]))))
        filespec[index] = '\0';
    strcat(filespec, "/*");

    dirp = (AXIS2_DIR *)malloc(sizeof(AXIS2_DIR));
    dirp->offset = 0;
    dirp->finished = 0;

    if((handle = (long)_findfirst(filespec, &(dirp->fileinfo))) < 0)
    /* We are sure that the difference lies within the long range */
    {
        if(errno == ENOENT || errno == EINVAL)
            dirp->finished = 1;
        else
        {
            free(dirp);
            free(filespec);
            return NULL;
        }
    }
    /* We are using the ISO C++ conformant name: _strdup, as demanded by VS 2005 */
    dirp->dirname = _strdup(_dirname);
    dirp->handle = handle;
    free(filespec);

    return dirp;
}

int AXIS2_CALL
axis2_closedir(
    AXIS2_DIR * _dirp)
{
    int iret = -1;
    if(!_dirp)
        return iret;
    iret = _findclose(_dirp->handle);
    if(_dirp->dirname)
        free(_dirp->dirname);
    if(_dirp)
        free(_dirp);

    return iret;
}

struct dirent *AXIS2_CALL
axis2_readdir(
    AXIS2_DIR * _dirp)
{
    if(!_dirp || _dirp->finished)
        return NULL;

    if(_dirp->offset != 0)
    {
        if(_findnext(_dirp->handle, &(_dirp->fileinfo)) < 0)
        {
            _dirp->finished = 1;
            return NULL;
        }
    }
    _dirp->offset++;

    strcpy(_dirp->dent.d_name, _dirp->fileinfo.name); /*, _MAX_FNAME+1); */
    _dirp->dent.d_ino = 1;
    _dirp->dent.d_reclen = (unsigned short)strlen(_dirp->dent.d_name);
    _dirp->dent.d_off = _dirp->offset;

    return &(_dirp->dent);
}

int AXIS2_CALL
axis2_readdir_r(
    AXIS2_DIR * _dirp,
    struct dirent *_entry,
    struct dirent **__result)
{
    if(!_dirp || _dirp->finished)
    {
        *__result = NULL;
        return -1;
    }

    if(_dirp->offset != 0)
    {
        if(_findnext(_dirp->handle, &(_dirp->fileinfo)) < 0)
        {
            _dirp->finished = 1;
            *__result = NULL;
            return -1;
        }
    }
    _dirp->offset++;

    strcpy(_dirp->dent.d_name, _dirp->fileinfo.name); /*, _MAX_FNAME+1); */
    _dirp->dent.d_ino = 1;
    _dirp->dent.d_reclen = (unsigned short)strlen(_dirp->dent.d_name);
    _dirp->dent.d_off = _dirp->offset;

    memcpy(_entry, &_dirp->dent, sizeof(*_entry));

    *__result = &_dirp->dent;

    return 0;
}

int AXIS2_CALL
axis2_rewinddir(
    AXIS2_DIR * dirp)
{
    char *filespec;
    long handle;
    int index;

    _findclose(dirp->handle);

    dirp->offset = 0;
    dirp->finished = 0;

    filespec = malloc(strlen(dirp->dirname) + 2 + 1);
    strcpy(filespec, dirp->dirname);
    index = (int)(strlen(filespec) - 1);
    if(index >= 0 && (filespec[index] == '/' || filespec[index] == '\\'))
        filespec[index] = '\0';
    strcat(filespec, "/*");

    if((handle = (long)_findfirst(filespec, &(dirp->fileinfo))) < 0)
    /* We are sure that the difference lies within the int range */
    {
        if(errno == ENOENT || errno == EINVAL)
            dirp->finished = 1;
    }
    dirp->handle = handle;
    free(filespec);

    return 0;
}

int
alphasort(
    const struct dirent **__d1,
    const struct dirent **__d2)
{
    return strcoll((*__d1)->d_name, (*__d2)->d_name);
}

int AXIS2_CALL
axis2_scandir(
    const char *_dirname,
    struct dirent **__namelist[],
    int
    (*selector)(
        const struct dirent * entry),
    int
    (*compare)(
        const struct dirent ** __d1,
        const struct dirent ** __d2))
{
    AXIS2_DIR *dirp = NULL;
    struct dirent **vector = NULL;
    struct dirent *dp = NULL;
    int vector_size = 0;
    int nfiles = 0;

    if(__namelist == NULL)
    {
        return -1;
    }
    dirp = axis2_opendir(_dirname);
    if(!dirp)
    {
        return -1;
    }
    dp = axis2_readdir(dirp);
    while(dp)
    {
        int dsize = 0;
        struct dirent *newdp = NULL;

        if(selector && (*selector)(dp) == 0)
        {
            dp = axis2_readdir(dirp);
            continue;
        }

        if(nfiles == vector_size)
        {
            struct dirent **newv;
            if(vector_size == 0)
            {
                vector_size = 10;
            }
            else
            {
                vector_size *= 2;
            }

            newv = (struct dirent **)realloc(vector, vector_size * sizeof(struct dirent *));
            if(!newv)
            {
                return -1;
            }
            vector = newv;
        }

        /*dsize =
         (int) sizeof(struct dirent) +
         (int) ((strlen(dp->d_name) + 1) * sizeof(char));*/
        dsize = (int)sizeof(struct dirent);
        newdp = (struct dirent *)malloc(dsize);

        if(newdp == NULL)
        {
            while(nfiles-- > 0)
            {
                free(vector[nfiles]);
            }
            free(vector);
            return -1;
        }

        vector[nfiles++] = (struct dirent *)memcpy(newdp, dp, dsize);
        dp = axis2_readdir(dirp);
    }

    axis2_closedir(dirp);

    *__namelist = vector;

    if(compare)
    {
        qsort(*__namelist, nfiles, sizeof(struct dirent *), compare);
    }

    return nfiles;
}