[libbluray-devel] split file_win32.c from file_posix.c

hpi1 git at videolan.org
Sun Mar 2 09:25:50 CET 2014


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Tue Feb 25 14:54:16 2014 +0200| [523b34c31264d0712cc3e8d1b93ae10904b2de8d] | committer: hpi1

split file_win32.c from file_posix.c

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=523b34c31264d0712cc3e8d1b93ae10904b2de8d
---

 src/Makefile.am       |   27 +++++++++--
 src/file/file.c       |   40 ++++++++++++++++
 src/file/file_posix.c |   48 -------------------
 src/file/file_win32.c |  125 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/macro.h      |    1 +
 5 files changed, 189 insertions(+), 52 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index dcbee4a..7423f76 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -70,7 +70,7 @@ libbluray_la_SOURCES = \
 	libbluray/hdmv/mobj_parse.c \
 	libbluray/hdmv/mobj_print.c \
 	file/file.h \
-	file/file_posix.c \
+	file/file.c \
 	file/dirs.h \
 	file/dl.h \
 	file/filesystem.h \
@@ -95,17 +95,20 @@ libbluray_la_SOURCES = \
 
 if HAVE_DARWIN
 libbluray_la_SOURCES+= \
+	file/file_posix.c \
 	file/dir_posix.c \
 	file/dl_posix.c \
 	file/dirs_darwin.c
 else
 if HAVE_WIN32
 libbluray_la_SOURCES+= \
+	file/file_win32.c \
 	file/dir_win32.c \
 	file/dl_win32.c \
 	file/dirs_win32.c
 else
 libbluray_la_SOURCES+= \
+	file/file_posix.c \
 	file/dir_posix.c \
 	file/dl_posix.c \
 	file/dirs_xdg.c
@@ -212,7 +215,7 @@ mpls_dump_SOURCES = \
 	examples/mpls_dump.c \
 	examples/util.c \
 	examples/util.h \
-	file/file_posix.c \
+	file/file.c \
 	libbluray/bdnav/clpi_parse.c \
 	libbluray/bdnav/extdata_parse.c \
 	libbluray/bdnav/mpls_parse.c \
@@ -221,9 +224,11 @@ mpls_dump_SOURCES = \
 	util/strutl.c
 if HAVE_WIN32
 mpls_dump_SOURCES += \
+	file/file_win32.c \
 	file/dir_win32.c
 else
 mpls_dump_SOURCES += \
+	file/file_posix.c \
 	file/dir_posix.c
 endif
 
@@ -237,19 +242,33 @@ clpi_dump_LDADD = libbluray.la
 index_dump_CFLAGS = $(AM_CFLAGS)
 index_dump_SOURCES = \
 	examples/index_dump.c \
-	file/file_posix.c \
+	file/file.c \
 	libbluray/bdnav/index_parse.c \
 	util/bits.c \
 	util/logging.c
+if HAVE_WIN32
+index_dump_SOURCES += \
+	file/file_win32.c
+else
+index_dump_SOURCES += \
+	file/file_posix.c
+endif
 
 mobj_dump_CFLAGS = $(AM_CFLAGS)
 mobj_dump_SOURCES = \
 	examples/mobj_dump.c \
-	file/file_posix.c \
+	file/file.c \
 	libbluray/hdmv/mobj_parse.c \
 	libbluray/hdmv/mobj_print.c \
 	util/bits.c \
 	util/logging.c
+if HAVE_WIN32
+mobj_dump_SOURCES += \
+	file/file_win32.c
+else
+mobj_dump_SOURCES += \
+	file/file_posix.c
+endif
 
 sound_dump_SOURCES = examples/sound_dump.c
 sound_dump_LDADD = libbluray.la
diff --git a/src/file/file.c b/src/file/file.c
new file mode 100644
index 0000000..7b0f2c4
--- /dev/null
+++ b/src/file/file.c
@@ -0,0 +1,40 @@
+/*
+ * This file is part of libbluray
+ * Copyright (C) 2014  Petri Hintukainen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "file.h"
+
+#include <stdio.h>
+
+int64_t file_size(BD_FILE_H *fp)
+{
+    int64_t pos    = file_tell(fp);
+    int64_t res1   = file_seek(fp, 0, SEEK_END);
+    int64_t length = file_tell(fp);
+    int64_t res2   = file_seek(fp, pos, SEEK_SET);
+
+    if (res1 < 0 || res2 < 0 || pos < 0 || length < 0) {
+        return -1;
+    }
+
+    return length;
+}
diff --git a/src/file/file_posix.c b/src/file/file_posix.c
index 4bca744..c488fc2 100644
--- a/src/file/file_posix.c
+++ b/src/file/file_posix.c
@@ -22,11 +22,6 @@
 #include "config.h"
 #endif
 
-#if defined(__MINGW32__)
-/* ftello64() and fseeko64() prototypes from stdio.h */
-#   undef __STRICT_ANSI__
-#endif
-
 #include "file.h"
 #include "util/macro.h"
 #include "util/logging.h"
@@ -35,10 +30,6 @@
 #include <stdlib.h>
 #include <inttypes.h>
 
-#ifdef _WIN32
-#include <windows.h>
-#endif	//	#ifdef _WIN32
-
 static void file_close_linux(BD_FILE_H *file)
 {
     if (file) {
@@ -52,24 +43,12 @@ static void file_close_linux(BD_FILE_H *file)
 
 static int64_t file_seek_linux(BD_FILE_H *file, int64_t offset, int32_t origin)
 {
-#if defined(__MINGW32__)
-    return fseeko64((FILE *)file->internal, offset, origin);
-#elif defined(_WIN32)
-    return _fseeki64((FILE *)file->internal, offset, origin);
-#else
     return fseeko((FILE *)file->internal, offset, origin);
-#endif
 }
 
 static int64_t file_tell_linux(BD_FILE_H *file)
 {
-#if defined(__MINGW32__)
-    return ftello64((FILE *)file->internal);
-#elif defined(_WIN32)
-    return _ftelli64((FILE *)file->internal);
-#else
     return ftello((FILE *)file->internal);
-#endif
 }
 
 static int file_eof_linux(BD_FILE_H *file)
@@ -77,8 +56,6 @@ static int file_eof_linux(BD_FILE_H *file)
     return feof((FILE *)file->internal);
 }
 
-#define BD_MAX_SSIZE ((int64_t)(((size_t)-1)>>1))
-
 static int64_t file_read_linux(BD_FILE_H *file, uint8_t *buf, int64_t size)
 {
     if (size > 0 && size < BD_MAX_SSIZE) {
@@ -112,14 +89,7 @@ static BD_FILE_H *file_open_linux(const char* filename, const char *mode)
     file->tell = file_tell_linux;
     file->eof = file_eof_linux;
 
-#ifdef _WIN32
-    wchar_t wfilename[MAX_PATH], wmode[8];
-    if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, wfilename, MAX_PATH) &&
-        MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mode, -1, wmode, 8) &&
-        (fp = _wfopen(wfilename, wmode))) {
-#else
     if ((fp = fopen(filename, mode))) {
-#endif
         file->internal = fp;
 
         return file;
@@ -138,21 +108,3 @@ BD_FILE_OPEN file_open_default(void)
 {
     return file_open_linux;
 }
-
-/*
- *
- */
-
-int64_t file_size(BD_FILE_H *fp)
-{
-    int64_t pos    = file_tell(fp);
-    int64_t res1   = file_seek(fp, 0, SEEK_END);
-    int64_t length = file_tell(fp);
-    int64_t res2   = file_seek(fp, pos, SEEK_SET);
-
-    if (res1 < 0 || res2 < 0 || pos < 0 || length < 0) {
-        return -1;
-    }
-
-    return length;
-}
diff --git a/src/file/file_win32.c b/src/file/file_win32.c
new file mode 100644
index 0000000..2418d5a
--- /dev/null
+++ b/src/file/file_win32.c
@@ -0,0 +1,125 @@
+/*
+ * This file is part of libbluray
+ * Copyright (C) 2009-2010  Obliter0n
+ * Copyright (C) 2009-2010  John Stebbins
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if defined(__MINGW32__)
+/* ftello64() and fseeko64() prototypes from stdio.h */
+#   undef __STRICT_ANSI__
+#endif
+
+#include "file.h"
+#include "util/macro.h"
+#include "util/logging.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+
+#include <windows.h>
+
+static void _file_close(BD_FILE_H *file)
+{
+    if (file) {
+        fclose((FILE *)file->internal);
+
+        BD_DEBUG(DBG_FILE, "Closed WIN32 file (%p)\n", (void*)file);
+
+        X_FREE(file);
+    }
+}
+
+static int64_t _file_seek(BD_FILE_H *file, int64_t offset, int32_t origin)
+{
+#if defined(__MINGW32__)
+    return fseeko64((FILE *)file->internal, offset, origin);
+#else
+    return _fseeki64((FILE *)file->internal, offset, origin);
+#endif
+}
+
+static int64_t _file_tell(BD_FILE_H *file)
+{
+#if defined(__MINGW32__)
+    return ftello64((FILE *)file->internal);
+#else
+    return _ftelli64((FILE *)file->internal);
+#endif
+}
+
+static int _file_eof(BD_FILE_H *file)
+{
+    return feof((FILE *)file->internal);
+}
+
+static int64_t _file_read(BD_FILE_H *file, uint8_t *buf, int64_t size)
+{
+    if (size > 0 && size < BD_MAX_SSIZE) {
+        return (int64_t)fread(buf, 1, (size_t)size, (FILE *)file->internal);
+    }
+
+    BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %"PRId64" (%p)\n", size, (void*)file);
+    return 0;
+}
+
+static int64_t _file_write(BD_FILE_H *file, const uint8_t *buf, int64_t size)
+{
+    if (size > 0 && size < BD_MAX_SSIZE) {
+        return (int64_t)fwrite(buf, 1, (size_t)size, (FILE *)file->internal);
+    }
+
+    BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %"PRId64" (%p)\n", size, (void*)file);
+    return 0;
+}
+
+static BD_FILE_H *_file_open(const char* filename, const char *mode)
+{
+    FILE *fp = NULL;
+
+    wchar_t wfilename[MAX_PATH], wmode[8];
+    if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, wfilename, MAX_PATH) &&
+        MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mode, -1, wmode, 8) &&
+        (fp = _wfopen(wfilename, wmode))) {
+
+        BD_FILE_H *file = malloc(sizeof(BD_FILE_H));
+        file->internal = fp;
+        file->close    = _file_close;
+        file->seek     = _file_seek;
+        file->read     = _file_read;
+        file->write    = _file_write;
+        file->tell     = _file_tell;
+        file->eof      = _file_eof;
+
+        BD_DEBUG(DBG_FILE, "Opened WIN32 file %s (%p)\n", filename, (void*)file);
+        return file;
+    }
+
+    BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
+    return NULL;
+}
+
+BD_FILE_H* (*file_open)(const char* filename, const char *mode) = _file_open;
+
+BD_FILE_OPEN file_open_default(void)
+{
+    return _file_open;
+}
diff --git a/src/util/macro.h b/src/util/macro.h
index 60c5c68..47dea24 100644
--- a/src/util/macro.h
+++ b/src/util/macro.h
@@ -33,6 +33,7 @@
 #define BD_MIN(a,b) ((a)<(b)?(a):(b))
 #define BD_MAX(a,b) ((a)>(b)?(a):(b))
 
+#define BD_MAX_SSIZE ((int64_t)(((size_t)-1)>>1))
 
 /*
  * automatic cast from void* (malloc/calloc/realloc)



More information about the libbluray-devel mailing list