Skip to content

Development Idea's

Victor Roemer edited this page Aug 17, 2022 · 1 revision

File API

Idea, write a better filesystem abstraction.

Loosely based on the model of idTech4, though I really don't like idTech4's filesystem abstraction either.

File API

#include <cstdint>
#include <cstdio>

class File {
public:
    virtual ~File() {};

    virtual const char* get_name() const = 0;
    virtual size_t get_size() const = 0;
    virtual size_t read(void *buffer, size_t size) = 0;
    virtual size_t write(const void *buffer, size_t size) = 0;
    virtual size_t seek(size_t offset) = 0;
    virtual size_t tell() const = 0;
};

class RegularFile : public File {
public:
    RegularFile(const char *name)
     : name(name)
     {
            FILE* file = fopen(name, "rb");
            if (file) {
                fseek(file, 0, SEEK_END);
                size = ftell(file);
                fseek(file, 0, SEEK_SET);
            }
     };

    virtual ~RegularFile() {};

    virtual const char* get_name() const override
    { 
        return name;
    }

    virtual size_t get_size() const override
    { 
        return size;
    }

    virtual size_t read(void *buffer, size_t size) override 
    {
        return 0
    }

    virtual size_t write(const void *buffer, size_t size) override
    {
        return 0;
    }

    virtual size_t seek(size_t offset) override 
    { 
        return 0;
    }

    virtual size_t tell() const override
    { 
        return 0; 
    }

private:
    FILE* file;
    const char *name;
    size_t size;
};

class ZippedFile : public File {
public:
    ZippedFile(const char *name, unzFile handle) :
        name(name), handle(handle) {};

    virtual ~ZippedFile() {};
    virtual const char* get_name() const { return name; };
    virtual size_t get_size() const { return size; };
    virtual size_t read(void *buffer, size_t size) { return 0; };
    virtual size_t write(const void *buffer, size_t size) { return 0; };
    virtual size_t seek(size_t offset) { return 0; };
    virtual size_t tell() const { return 0; };

private:
    const char *name;
    off_t offset; // position in the zip file
    unzFile handle;
    size_t size;
};