-
Notifications
You must be signed in to change notification settings - Fork 20
/
scc_util.c
276 lines (239 loc) · 5.91 KB
/
scc_util.c
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
273
274
275
276
/* ScummC
* Copyright (C) 2004-2006 Alban Bedel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/**
* @file scc_util.c
* @ingroup utils
* @brief Common stuff and portabilty helpers
*/
// This file should contain generic stuff that can be helpfull anywhere.
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include <stdarg.h>
#ifdef IS_MINGW
#include <windows.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "scc_fd.h"
#include "scc_util.h"
int scc_log_level = 2;
void scc_log(int lvl,char* fmt, ...) {
va_list ap;
if(lvl > scc_log_level) return;
va_start(ap, fmt);
vfprintf(stderr,fmt,ap);
va_end(ap);
}
// work around incomplete libc's
#ifndef HAVE_ASPRINTF
int vasprintf(char **strp, const char *fmt, va_list ap) {
/* Guess we need no more than 100 bytes. */
int n, size = 100;
char *p;
if ((p = malloc (size)) == NULL)
return -1;
while (1) {
/* Try to print in the allocated space. */
n = vsnprintf (p, size, fmt, ap);
/* If that worked, return the string. */
if (n > -1 && n < size) {
strp[0] = p;
return n;
}
/* Else try again with more space. */
if (n > -1) /* glibc 2.1 */
size = n+1; /* precisely what is needed */
else /* glibc 2.0 */
size *= 2; /* twice the old size */
if ((p = realloc (p, size)) == NULL)
return -1;
}
}
int asprintf(char **strp, const char *fmt, ...) {
va_list ap;
int n;
va_start(ap, fmt);
n = vasprintf(strp,fmt,ap);
va_end(ap);
return n;
}
#endif
// this should probably go into some common util stuff
// or in scc_fd dunno
scc_data_t* scc_data_load(char* path) {
scc_data_t* data;
scc_fd_t* fd;
int len;
fd = new_scc_fd(path,O_RDONLY,0);
if(!fd) {
scc_log(LOG_ERR,"Failed to open %s.\n",path);
return NULL;
}
// get file size
scc_fd_seek(fd,0,SEEK_END);
len = scc_fd_pos(fd);
scc_fd_seek(fd,0,SEEK_SET);
data = malloc(sizeof(scc_data_t) + len);
data->size = len;
if(scc_fd_read(fd,data->data,len) != len) {
scc_log(LOG_ERR,"Failed to load %s\n",path);
free(data);
scc_fd_close(fd);
return NULL;
}
scc_fd_close(fd);
return data;
}
//
// Windows glob implementation from SoX.
//
#ifdef IS_MINGW
typedef struct file_entry
{
char name[MAX_PATH];
struct file_entry *next;
} file_entry;
static int
insert(
const char* path,
const char* name,
file_entry** phead)
{
int len;
file_entry* cur = malloc(sizeof(file_entry));
if (!cur)
{
return ENOMEM;
}
len = _snprintf(cur->name, MAX_PATH, "%s%s", path, name);
cur->name[MAX_PATH - 1] = 0;
cur->next = *phead;
*phead = cur;
return len < 0 || len >= MAX_PATH ? ENAMETOOLONG : 0;
}
static int
entry_comparer(
const void* pv1,
const void* pv2)
{
const file_entry* const * pe1 = pv1;
const file_entry* const * pe2 = pv2;
return _stricmp((*pe1)->name, (*pe2)->name);
}
int
glob(
const char *pattern,
int flags,
int (*unused)(const char *epath, int eerrno),
glob_t *pglob)
{
char path[MAX_PATH];
file_entry *head = NULL;
int err = 0;
size_t len;
unsigned entries = 0;
WIN32_FIND_DATAA finddata;
HANDLE hfindfile = FindFirstFileA(pattern, &finddata);
if (!pattern || flags != (flags & GLOB_FLAGS) || unused || !pglob)
{
errno = EINVAL;
return EINVAL;
}
path[MAX_PATH - 1] = 0;
strncpy(path, pattern, MAX_PATH);
if (path[MAX_PATH - 1] != 0)
{
errno = ENAMETOOLONG;
return ENAMETOOLONG;
}
len = strlen(path);
while (len > 0 && path[len - 1] != '/' && path[len - 1] != '\\')
len--;
path[len] = 0;
if (hfindfile == INVALID_HANDLE_VALUE)
{
if (flags & GLOB_NOCHECK)
{
err = insert("", pattern, &head);
entries++;
}
}
else
{
do
{
err = insert(path, finddata.cFileName, &head);
entries++;
} while (!err && FindNextFileA(hfindfile, &finddata));
FindClose(hfindfile);
}
if (err == 0)
{
pglob->gl_pathv = malloc((entries + 1) * sizeof(char*));
if (pglob->gl_pathv)
{
pglob->gl_pathc = entries;
pglob->gl_pathv[entries] = NULL;
for (; head; head = head->next, entries--)
pglob->gl_pathv[entries - 1] = (char*)head;
qsort(pglob->gl_pathv, pglob->gl_pathc, sizeof(char*), entry_comparer);
}
else
{
pglob->gl_pathc = 0;
err = ENOMEM;
}
}
else if (pglob)
{
pglob->gl_pathc = 0;
pglob->gl_pathv = NULL;
}
if (err)
{
file_entry *cur;
while (head)
{
cur = head;
head = head->next;
free(cur);
}
errno = err;
}
return err;
}
void
globfree(
glob_t* pglob)
{
if (pglob)
{
char** cur;
for (cur = pglob->gl_pathv; *cur; cur++)
{
free(*cur);
}
pglob->gl_pathc = 0;
pglob->gl_pathv = NULL;
}
}
#endif // IS_MINGW