Skip to content

Commit a76d0cd

Browse files
committed
libavutil: Move avpriv_open to a new file, file_open.c
Signed-off-by: Martin Storsjö <[email protected]>
1 parent cb0244d commit a76d0cd

File tree

3 files changed

+94
-64
lines changed

3 files changed

+94
-64
lines changed

libavutil/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ OBJS = adler32.o \
7070
eval.o \
7171
fifo.o \
7272
file.o \
73+
file_open.o \
7374
float_dsp.o \
7475
frame.o \
7576
hmac.o \

libavutil/file.c

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "internal.h"
2222
#include "log.h"
2323
#include "mem.h"
24-
#include <stdarg.h>
2524
#include <fcntl.h>
2625
#include <sys/stat.h>
2726
#if HAVE_UNISTD_H
@@ -36,69 +35,6 @@
3635
#include <windows.h>
3736
#endif
3837

39-
#if defined(_WIN32) && !defined(__MINGW32CE__)
40-
#undef open
41-
#undef lseek
42-
#undef stat
43-
#undef fstat
44-
#include <windows.h>
45-
#include <share.h>
46-
#include <errno.h>
47-
48-
static int win32_open(const char *filename_utf8, int oflag, int pmode)
49-
{
50-
int fd;
51-
int num_chars;
52-
wchar_t *filename_w;
53-
54-
/* convert UTF-8 to wide chars */
55-
num_chars = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename_utf8, -1, NULL, 0);
56-
if (num_chars <= 0)
57-
goto fallback;
58-
filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
59-
if (!filename_w) {
60-
errno = ENOMEM;
61-
return -1;
62-
}
63-
MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
64-
65-
fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
66-
av_freep(&filename_w);
67-
68-
if (fd != -1 || (oflag & O_CREAT))
69-
return fd;
70-
71-
fallback:
72-
/* filename may be be in CP_ACP */
73-
return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
74-
}
75-
#define open win32_open
76-
#endif
77-
78-
int avpriv_open(const char *filename, int flags, ...)
79-
{
80-
int fd;
81-
unsigned int mode = 0;
82-
va_list ap;
83-
84-
va_start(ap, flags);
85-
if (flags & O_CREAT)
86-
mode = va_arg(ap, unsigned int);
87-
va_end(ap);
88-
89-
#ifdef O_CLOEXEC
90-
flags |= O_CLOEXEC;
91-
#endif
92-
93-
fd = open(filename, flags, mode);
94-
#if HAVE_FCNTL
95-
if (fd != -1)
96-
fcntl(fd, F_SETFD, FD_CLOEXEC);
97-
#endif
98-
99-
return fd;
100-
}
101-
10238
typedef struct {
10339
const AVClass *class;
10440
int log_offset;

libavutil/file_open.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* This file is part of Libav.
3+
*
4+
* Libav is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* Libav is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with Libav; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include "config.h"
20+
#include "internal.h"
21+
#include "mem.h"
22+
#include <stdarg.h>
23+
#include <fcntl.h>
24+
#include <sys/stat.h>
25+
#if HAVE_UNISTD_H
26+
#include <unistd.h>
27+
#endif
28+
#if HAVE_IO_H
29+
#include <io.h>
30+
#endif
31+
32+
#if defined(_WIN32) && !defined(__MINGW32CE__)
33+
#undef open
34+
#undef lseek
35+
#undef stat
36+
#undef fstat
37+
#include <windows.h>
38+
#include <share.h>
39+
#include <errno.h>
40+
41+
static int win32_open(const char *filename_utf8, int oflag, int pmode)
42+
{
43+
int fd;
44+
int num_chars;
45+
wchar_t *filename_w;
46+
47+
/* convert UTF-8 to wide chars */
48+
num_chars = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename_utf8, -1, NULL, 0);
49+
if (num_chars <= 0)
50+
goto fallback;
51+
filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
52+
if (!filename_w) {
53+
errno = ENOMEM;
54+
return -1;
55+
}
56+
MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
57+
58+
fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
59+
av_freep(&filename_w);
60+
61+
if (fd != -1 || (oflag & O_CREAT))
62+
return fd;
63+
64+
fallback:
65+
/* filename may be be in CP_ACP */
66+
return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
67+
}
68+
#define open win32_open
69+
#endif
70+
71+
int avpriv_open(const char *filename, int flags, ...)
72+
{
73+
int fd;
74+
unsigned int mode = 0;
75+
va_list ap;
76+
77+
va_start(ap, flags);
78+
if (flags & O_CREAT)
79+
mode = va_arg(ap, unsigned int);
80+
va_end(ap);
81+
82+
#ifdef O_CLOEXEC
83+
flags |= O_CLOEXEC;
84+
#endif
85+
86+
fd = open(filename, flags, mode);
87+
#if HAVE_FCNTL
88+
if (fd != -1)
89+
fcntl(fd, F_SETFD, FD_CLOEXEC);
90+
#endif
91+
92+
return fd;
93+
}

0 commit comments

Comments
 (0)