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
|
#define _POSIX_C_SOURCE 200809l
#include "common/fmplayer_file.h"
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <strings.h>
#include <iconv.h>
#include <locale.h>
#include <langinfo.h>
static void *fileread(const char *path, size_t maxsize, size_t *filesize, enum fmplayer_file_error *error) {
FILE *f = 0;
void *buf = 0;
f = fopen(path, "rb");
if (!f) {
if (error) *error = FMPLAYER_FILE_ERR_NOTFOUND;
goto err;
}
if (fseek(f, 0, SEEK_END)) {
*error = FMPLAYER_FILE_ERR_FILEIO;
goto err;
}
size_t fsize;
{
long ssize = ftell(f);
if (ssize < 0) {
*error = FMPLAYER_FILE_ERR_FILEIO;
goto err;
}
if (maxsize && ((size_t)ssize > maxsize)) {
*error = FMPLAYER_FILE_ERR_BADFILE_SIZE;
goto err;
}
fsize = ssize;
}
if (fseek(f, 0, SEEK_SET)) {
*error = FMPLAYER_FILE_ERR_FILEIO;
goto err;
}
buf = malloc(fsize);
if (!buf) {
if (error) *error = FMPLAYER_FILE_ERR_NOMEM;
goto err;
}
if (fread(buf, 1, fsize, f) != fsize) {
*error = FMPLAYER_FILE_ERR_FILEIO;
goto err;
}
fclose(f);
*filesize = fsize;
return buf;
err:
free(buf);
if (f) fclose(f);
return 0;
}
void *fmplayer_fileread(const void *pathptr, const char *pcmname, const char *extension,
size_t maxsize, size_t *filesize, enum fmplayer_file_error *error) {
const char *path = pathptr;
if (!pcmname) return fileread(path, maxsize, filesize, error);
char *namebuf = 0;
char *dirbuf = 0;
DIR *d = 0;
if (extension) {
size_t namebuflen = strlen(pcmname) + strlen(extension) + 1;
namebuf = malloc(namebuflen);
if (!namebuf) {
if (error) *error = FMPLAYER_FILE_ERR_NOMEM;
goto err;
}
strcpy(namebuf, pcmname);
strcat(namebuf, extension);
pcmname = namebuf;
}
const char *slash = strrchr(path, '/');
const char *dirpath = 0;
if (slash) {
dirbuf = strdup(path);
if (!dirbuf) {
if (error) *error = FMPLAYER_FILE_ERR_NOMEM;
goto err;
}
*strrchr(dirbuf, '/') = 0;
dirpath = dirbuf;
} else {
dirpath = ".";
}
d = opendir(dirpath);
if (!d) {
*error = FMPLAYER_FILE_ERR_FILEIO;
goto err;
}
const struct dirent *de;
while ((de = readdir(d))) {
if (!strcasecmp(de->d_name, pcmname)) {
size_t pathlen = strlen(dirpath) + 1 + strlen(de->d_name) + 1;
char *pcmpath = malloc(pathlen);
if (!pcmpath) {
if (error) *error = FMPLAYER_FILE_ERR_NOMEM;
goto err;
}
strcpy(pcmpath, dirpath);
strcat(pcmpath, "/");
strcat(pcmpath, de->d_name);
void *buf = fileread(pcmpath, maxsize, filesize, error);
free(pcmpath);
closedir(d);
free(dirbuf);
free(namebuf);
return buf;
}
}
if (error) *error = FMPLAYER_FILE_ERR_NOTFOUND;
err:
if (d) closedir(d);
free(dirbuf);
free(namebuf);
return 0;
}
void *fmplayer_path_dup(const void *path) {
return strdup(path);
}
char *fmplayer_path_filename_sjis(const void *pathptr) {
char *path = (char *)pathptr;
locale_t loc = 0;
iconv_t ic = (iconv_t)-1;
char *pathbuf = 0;
char *fname = strrchr(path, '/');
if (fname) {
if (*fname) fname++;
} else {
fname = path;
}
// use locale from environment values (to guess filesystem path encoding)
loc = newlocale(LC_CTYPE_MASK, "", 0);
if (!loc) {
//perror("newlocale");
goto err;
}
const int pathbuflen = 160;
pathbuf = malloc(pathbuflen);
if (!pathbuf) goto err;
ic = iconv_open("SHIFT_JIS", nl_langinfo_l(CODESET, loc));
if (ic == (iconv_t)-1) {
//perror("iconv_open");
goto err;
}
char *src = fname, *dst = pathbuf;
size_t srcleft = strlen(fname);
size_t dstleft = pathbuflen;
if (iconv(ic, &src, &srcleft, &dst, &dstleft) == (size_t)-1) {
//perror("iconv");
goto err;
}
iconv_close(ic);
*dst = 0;
freelocale(loc);
return pathbuf;
err:
free(pathbuf);
if (ic != (iconv_t)-1) iconv_close(ic);
if (loc) freelocale(loc);
return strdup(fname);
}
|