2008-01-31 07:04:26 +01:00
|
|
|
/*
|
2008-05-20 17:18:38 +02:00
|
|
|
* streamfile.h - definitions for buffered file reading with STREAMFILE
|
|
|
|
*/
|
2008-01-31 07:04:26 +01:00
|
|
|
|
2008-05-06 05:35:37 +02:00
|
|
|
#ifndef _STREAMFILE_H
|
|
|
|
#define _STREAMFILE_H
|
|
|
|
|
2008-04-03 15:40:36 +02:00
|
|
|
#ifdef _MSC_VER
|
2008-04-03 15:56:50 +02:00
|
|
|
#define _CRT_SECURE_NO_DEPRECATE
|
2008-04-03 15:40:36 +02:00
|
|
|
#endif
|
|
|
|
|
2008-01-31 07:04:26 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include "streamtypes.h"
|
2008-01-31 23:26:11 +01:00
|
|
|
#include "util.h"
|
2008-01-31 07:04:26 +01:00
|
|
|
|
2008-04-03 15:40:36 +02:00
|
|
|
#if defined(__MSVCRT__) || defined(_MSC_VER)
|
2008-05-20 17:18:38 +02:00
|
|
|
#include <io.h>
|
2008-02-06 01:27:51 +01:00
|
|
|
#define fseeko fseek
|
|
|
|
#define ftello ftell
|
2008-05-20 17:18:38 +02:00
|
|
|
#define dup _dup
|
|
|
|
#define fileno _fileno
|
|
|
|
#define fdopen _fdopen
|
2008-02-06 01:27:51 +01:00
|
|
|
#endif
|
|
|
|
|
2008-01-31 07:04:26 +01:00
|
|
|
#define STREAMFILE_DEFAULT_BUFFER_SIZE 0x400
|
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
typedef struct _STREAMFILE {
|
|
|
|
size_t (*read)(struct _STREAMFILE *,uint8_t * dest, off_t offset, size_t length);
|
|
|
|
size_t (*get_size)(struct _STREAMFILE *);
|
|
|
|
off_t (*get_offset)(struct _STREAMFILE *);
|
|
|
|
// for dual-file support
|
|
|
|
void (*get_name)(struct _STREAMFILE *,char *name,size_t length);
|
|
|
|
struct _STREAMFILE * (*open)(struct _STREAMFILE *,const char * const filename,size_t buffersize);
|
2008-01-31 07:04:26 +01:00
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
void (*close)(struct _STREAMFILE *);
|
|
|
|
} STREAMFILE;
|
2008-01-31 07:04:26 +01:00
|
|
|
|
|
|
|
/* close a file, destroy the STREAMFILE object */
|
2008-05-20 17:18:38 +02:00
|
|
|
static inline void close_streamfile(STREAMFILE * streamfile) {
|
|
|
|
streamfile->close(streamfile);
|
|
|
|
}
|
2008-01-31 23:26:11 +01:00
|
|
|
|
2008-01-31 07:04:26 +01:00
|
|
|
/* read from a file
|
2008-05-20 17:18:38 +02:00
|
|
|
*
|
|
|
|
* returns number of bytes read
|
|
|
|
*/
|
2008-01-31 23:26:11 +01:00
|
|
|
static inline size_t read_streamfile(uint8_t * dest, off_t offset, size_t length, STREAMFILE * streamfile) {
|
2008-05-20 17:18:38 +02:00
|
|
|
return streamfile->read(streamfile,dest,offset,length);
|
2008-01-31 23:26:11 +01:00
|
|
|
}
|
2008-01-31 07:04:26 +01:00
|
|
|
|
|
|
|
/* return file size */
|
2008-05-20 17:18:38 +02:00
|
|
|
static inline size_t get_streamfile_size(STREAMFILE * streamfile) {
|
|
|
|
return streamfile->get_size(streamfile);
|
|
|
|
}
|
2008-01-31 07:04:26 +01:00
|
|
|
|
|
|
|
/* Sometimes you just need an int, and we're doing the buffering.
|
2008-05-20 17:18:38 +02:00
|
|
|
* Note, however, that if these fail to read they'll return -1,
|
|
|
|
* so that should not be a valid value or there should be some backup. */
|
2008-01-31 23:26:11 +01:00
|
|
|
static inline int16_t read_16bitLE(off_t offset, STREAMFILE * streamfile) {
|
2008-05-20 17:18:38 +02:00
|
|
|
uint8_t buf[2];
|
2008-01-31 23:26:11 +01:00
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_streamfile(buf,offset,2,streamfile)!=2) return -1;
|
|
|
|
return get_16bitLE(buf);
|
2008-01-31 23:26:11 +01:00
|
|
|
}
|
|
|
|
static inline int16_t read_16bitBE(off_t offset, STREAMFILE * streamfile) {
|
2008-05-20 17:18:38 +02:00
|
|
|
uint8_t buf[2];
|
2008-01-31 23:26:11 +01:00
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_streamfile(buf,offset,2,streamfile)!=2) return -1;
|
|
|
|
return get_16bitBE(buf);
|
2008-01-31 23:26:11 +01:00
|
|
|
}
|
|
|
|
static inline int32_t read_32bitLE(off_t offset, STREAMFILE * streamfile) {
|
2008-05-20 17:18:38 +02:00
|
|
|
uint8_t buf[4];
|
2008-01-31 23:26:11 +01:00
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_streamfile(buf,offset,4,streamfile)!=4) return -1;
|
|
|
|
return get_32bitLE(buf);
|
2008-01-31 23:26:11 +01:00
|
|
|
}
|
|
|
|
static inline int32_t read_32bitBE(off_t offset, STREAMFILE * streamfile) {
|
2008-05-20 17:18:38 +02:00
|
|
|
uint8_t buf[4];
|
2008-01-31 23:26:11 +01:00
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
if (read_streamfile(buf,offset,4,streamfile)!=4) return -1;
|
|
|
|
return get_32bitBE(buf);
|
2008-01-31 23:26:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int8_t read_8bit(off_t offset, STREAMFILE * streamfile) {
|
2008-05-20 17:18:38 +02:00
|
|
|
uint8_t buf[1];
|
|
|
|
|
|
|
|
if (read_streamfile(buf,offset,1,streamfile)!=1) return -1;
|
|
|
|
return buf[0];
|
|
|
|
}
|
2008-01-31 23:26:11 +01:00
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
/* open file with a set buffer size, create a STREAMFILE object
|
|
|
|
*
|
|
|
|
* Returns pointer to new STREAMFILE or NULL if open failed
|
|
|
|
*/
|
|
|
|
STREAMFILE * open_stdio_streamfile_buffer(const char * const filename, size_t buffersize);
|
|
|
|
|
|
|
|
/* open file with a default buffer size, create a STREAMFILE object
|
|
|
|
*
|
|
|
|
* Returns pointer to new STREAMFILE or NULL if open failed
|
|
|
|
*/
|
|
|
|
static inline STREAMFILE * open_stdio_streamfile(const char * const filename) {
|
|
|
|
return open_stdio_streamfile_buffer(filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2008-01-31 23:26:11 +01:00
|
|
|
}
|
2008-01-31 07:04:26 +01:00
|
|
|
|
|
|
|
#endif
|