Commit 73cd5873 authored by cRTrn13's avatar cRTrn13

file abs layer

parent 5a63f951
#include "file.h"
FILE_H* file_open(const char* filename, uint8_t mode)
extern const struct file_type file_linux;
FILE_H *file_open(const char *filename, const char *mode)
{
//return file_open_linux(filename, mode);
return file_linux.open(filename, mode);
}
void file_close(FILE_H* file)
void file_close(FILE_H *file)
{
file->close(file);
}
uint64_t file_seek(FILE_H* file, uint64_t offset, uint64_t origin)
int64_t file_seek(FILE_H *file, int64_t offset, int32_t origin)
{
file->seek(file, offset, origin);
return file->seek(file, offset, origin);
}
uint64_t file_tell(FILE_H* file)
int64_t file_tell(FILE_H *file)
{
file->tell(file);
return file->tell(file);
}
int file_read(FILE_H* file, uint8_t *buf, uint64_t count)
int file_read(FILE_H *file, uint8_t *buf, int64_t count)
{
file->read(file, buf, count);
return file->read(file, buf, count);
}
int file_write(FILE_H* file, uint8_t *buf, uint64_t count)
int file_write(FILE_H *file, uint8_t *buf, int64_t count)
{
file->write(file, buf, count);
return file->write(file, buf, count);
}
......@@ -4,24 +4,35 @@
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
enum OS {
LINUX,
OSX,
BSD,
};
typedef struct file FILE_H;
struct file
{
void* internal;
void (*close)(FILE_H* file);
uint64_t (*seek)(FILE_H* file, uint64_t offset, uint64_t origin);
uint64_t (*tell)(FILE_H* file);
int (*read)(FILE_H* file, uint8_t *buf, uint64_t count);
int (*write)(FILE_H* file, uint8_t *buf, uint64_t count);
void (*close)(FILE_H *file);
int64_t (*seek)(FILE_H *file, int64_t offset, int32_t origin);
int64_t (*tell)(FILE_H *file);
int (*read)(FILE_H *file, uint8_t *buf, int64_t count);
int (*write)(FILE_H *file, uint8_t *buf, int64_t count);
};
struct file_type
{
enum OS os;
FILE_H *(*open)(const char *filename, const char *mode);
};
FILE_H *file_open(const char* filename, uint8_t mode);
void file_close(FILE_H* file);
uint64_t file_seek(FILE_H* file, uint64_t offset, uint64_t origin);
uint64_t file_tell(FILE_H* file);
int file_read(FILE_H* file, uint8_t *buf, uint64_t count);
int file_write(FILE_H* file, uint8_t *buf, uint64_t count);
FILE_H *file_open(const char *filename, const char *mode);
void file_close(FILE_H *file);
int64_t file_seek(FILE_H *file, int64_t offset, int32_t origin);
int64_t file_tell(FILE_H *file);
int file_read(FILE_H *file, uint8_t *buf, int64_t count);
int file_write(FILE_H *file, uint8_t *buf, int64_t count);
#endif /* FILE_H_ */
#include <malloc.h>
#include <stdio.h>
#include "file.h"
void file_close_linux(FILE_H* file);
int64_t file_seek_linux(FILE_H* file, int64_t offset, int64_t origin);
int64_t file_tell_linux(FILE_H* file);
int file_read_linux(FILE_H* file, uint8_t *buf, int64_t count);
int file_write_linux(FILE_H* file, uint8_t *buf, int64_t count);
void file_close_linux(FILE_H *file)
{
fclose((FILE *)file->internal);
}
int64_t file_seek_linux(FILE_H *file, int64_t offset, int32_t origin)
{
return fseek((FILE *)file->internal, offset, origin);
}
int64_t file_tell_linux(FILE_H *file)
{
return ftell((FILE *)file->internal);
}
int file_read_linux(FILE_H *file, uint8_t *buf, int64_t count)
{
return fread(buf, count, 1, (FILE *)file->internal);
}
int file_write_linux(FILE_H *file, uint8_t *buf, int64_t count)
{
return fwrite(buf, count, 1, (FILE *)file->internal);
}
FILE_H *file_open_linux(const char* filename, uint8_t mode)
FILE_H *file_open_linux(const char* filename, const char *mode)
{
FILE *fp = NULL;
FILE_H *file = malloc(sizeof(FILE_H));
file->close = file_close_linux;
......@@ -20,5 +40,16 @@ FILE_H *file_open_linux(const char* filename, uint8_t mode)
file->write = file_write_linux;
file->tell = file_tell_linux;
return file;
if ((fp = fopen(filename, mode))) {
file->internal = fp;
return file;
}
return NULL;
}
const struct file_type file_linux = {
LINUX,
file_open_linux
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment