MythTV  0.26-pre
file_mythiowrapper.c
Go to the documentation of this file.
00001 #if HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include <fcntl.h>
00006 #include <sys/stat.h>
00007 #include <sys/types.h>
00008 
00009 #include "file.h"
00010 #include "util/macro.h"
00011 #include "util/logging.h"
00012 
00013 #include "mythiowrapper.h"
00014 #include "file_mythiowrapper.h"  // to pass GCC -Werror=missing-prototypes
00015 
00016 static void file_close_mythiowrapper(BD_FILE_H *file)
00017 {
00018     if (file) {
00019         mythfile_close((int)file->internal);
00020 
00021         BD_DEBUG(DBG_FILE, "Closed mythfile file (%p)\n", file);
00022 
00023         X_FREE(file);
00024     }
00025 }
00026 
00027 static int64_t file_seek_mythiowrapper(BD_FILE_H *file, int64_t offset, int32_t origin)
00028 {
00029     return mythfile_seek((int)file->internal, offset, origin);
00030 }
00031 
00032 static int64_t file_tell_mythiowrapper(BD_FILE_H *file)
00033 {
00034     return mythfile_tell((int)file->internal);
00035 }
00036 
00037 static int file_eof_mythiowrapper(BD_FILE_H *file)
00038 {
00039     // FIXME
00040     // return mythfile_eof((FILE *)file->internal);
00041 
00042     fprintf(stderr, "file_eof_mythiowrapper() ERROR UNIMPLEMENTED\n");
00043     return 0;
00044 }
00045 
00046 static int file_stat_mythiowrapper(BD_FILE_H *file, struct stat *buf)
00047 {
00048     return mythfile_stat_fd((int)file->internal, buf);
00049 }
00050 
00051 static int64_t file_read_mythiowrapper(BD_FILE_H *file, uint8_t *buf, int64_t size)
00052 {
00053     return mythfile_read((int)file->internal, buf, size);
00054 }
00055 
00056 static int64_t file_write_mythiowrapper(BD_FILE_H *file, const uint8_t *buf, int64_t size)
00057 {
00058     return mythfile_write((int)file->internal, buf, size);
00059 }
00060 
00061 BD_FILE_H *file_open_mythiowrapper(const char* filename, const char *mode)
00062 {
00063     FILE *fp = NULL;
00064     BD_FILE_H *file = malloc(sizeof(BD_FILE_H));
00065 
00066     BD_DEBUG(DBG_FILE, "Opening mythfile file %s... (%p)\n", filename, file);
00067     file->close = file_close_mythiowrapper;
00068     file->seek = file_seek_mythiowrapper;
00069     file->read = file_read_mythiowrapper;
00070     file->write = file_write_mythiowrapper;
00071     file->tell = file_tell_mythiowrapper;
00072     file->eof = file_eof_mythiowrapper;
00073     file->stat = file_stat_mythiowrapper;
00074 
00075     int fd;
00076     int intMode = O_RDONLY;
00077     if (!strcasecmp(mode, "wb"))
00078         intMode = O_WRONLY;
00079 
00080     if ((fd = mythfile_open(filename, intMode)) >= 0) {
00081         file->internal = (void *)fd;
00082 
00083         return file;
00084     }
00085 
00086     BD_DEBUG(DBG_FILE, "Error opening file! (%p)\n", file);
00087 
00088     X_FREE(file);
00089 
00090     return NULL;
00091 }
00092 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends