looper/file_backend.hpp

140 lines
3.4 KiB
C++
Raw Permalink Normal View History

2024-08-08 13:12:37 -07:00
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
2024-09-28 10:31:06 -07:00
#include <optional>
2024-08-08 13:12:37 -07:00
#include <stddef.h>
2024-09-28 10:31:06 -07:00
#include <string.h>
#include <map>
#include <stdlib.h>
2024-08-08 13:12:37 -07:00
#include <SDL.h>
#include <vector>
#include <fmt/core.h>
#include <fmt/format.h>
2024-08-08 13:12:37 -07:00
extern "C" {
#include <vgmstream.h>
}
2024-09-28 10:31:06 -07:00
#include "log.hpp"
#include <stdlib.h>
#include <string.h>
2024-08-08 13:12:37 -07:00
enum class SeekType {
SET,
CUR,
END
};
class CustomException : public std::exception {
const char *message;
protected:
void clear_message(bool free_message = true) {
if (free_message && this->message != nullptr) {
free((void*)this->message);
}
this->message = nullptr;
}
public:
CustomException() {
clear_message(false);
}
CustomException(std::string message) : CustomException() {
set_message(message);
}
void set_message(std::string message) {
clear_message();
this->message = strdup(message.c_str());
}
const char *what() const throw() {
return this->message;
}
~CustomException() {
clear_message();
}
};
class NotFoundException : public CustomException {
public:
NotFoundException(std::string file) : CustomException() {
set_message(fmt::format("File not found: {0}", file));
}
NotFoundException(const char *file) : NotFoundException(std::string(file)) { }
};
2024-08-08 13:12:37 -07:00
class File {
protected:
size_t len;
2024-09-28 10:31:06 -07:00
int64_t pos;
2024-08-08 13:12:37 -07:00
public:
const char *name;
2024-09-28 10:31:06 -07:00
File();
2024-08-08 13:12:37 -07:00
inline virtual void open(const char *fname) { name = fname; }
2024-09-16 15:05:53 -07:00
inline virtual void close() { }
virtual size_t read(void *ptr, size_t size, size_t len) = 0;
2024-09-28 10:31:06 -07:00
virtual void seek(int64_t pos, SeekType seek_type) = 0;
2024-08-08 13:12:37 -07:00
virtual size_t get_len();
2024-09-28 10:31:06 -07:00
virtual int64_t get_pos() = 0;
2024-09-16 15:05:53 -07:00
inline virtual bool is_open() {
return false;
}
inline virtual ~File() {
if (is_open()) close();
}
2024-08-08 13:12:37 -07:00
};
class CFile : public File {
protected:
FILE *file;
public:
2024-09-28 10:31:06 -07:00
CFile();
2024-08-08 13:12:37 -07:00
void open(const char *fname) override;
void close() override;
2024-09-16 15:05:53 -07:00
size_t read(void *ptr, size_t size, size_t len) override;
2024-09-28 10:31:06 -07:00
void seek(int64_t pos, SeekType seek_type) override;
int64_t get_pos() override;
2024-08-08 13:12:37 -07:00
bool is_open() override;
};
2024-09-28 10:31:06 -07:00
class MemFile : public File {
void *ptr;
size_t len;
int64_t pos;
public:
MemFile();
void open(const char *path) override;
void close() override;
size_t read(void *ptr, size_t size, size_t len) override;
void seek(int64_t pos, SeekType seek_type) override;
int64_t get_pos() override;
bool is_open() override;
size_t get_len() override;
};
2024-08-08 13:12:37 -07:00
class HttpFile : public File {
std::vector<uint8_t> data;
public:
2024-09-28 10:31:06 -07:00
HttpFile();
2024-08-08 13:12:37 -07:00
void open(const char *url) override;
void close() override;
2024-09-16 15:05:53 -07:00
size_t read(void *ptr, size_t size, size_t len) override;
2024-09-28 10:31:06 -07:00
void seek(int64_t pos, SeekType type) override;
int64_t get_pos() override;
bool is_open() override;
2024-08-08 13:12:37 -07:00
};
#ifdef __ANDROID__
class AndroidFile : public File {
protected:
void *handle;
public:
2024-09-28 10:31:06 -07:00
AndroidFile();
2024-08-08 13:12:37 -07:00
void open(const char *fname) override;
void close() override;
2024-09-16 15:05:53 -07:00
size_t read(void *ptr, size_t size, size_t len) override;
2024-09-28 10:31:06 -07:00
void seek(int64_t pos, SeekType seek_type) override;
int64_t get_pos() override;
2024-08-08 13:12:37 -07:00
bool is_open() override;
2024-09-28 10:31:06 -07:00
};
2024-08-08 13:12:37 -07:00
#endif
2024-10-19 16:37:43 -07:00
//#ifdef __ANDROID__
//#define FILE_TYPE AndroidFile
//#else
2024-09-28 10:31:06 -07:00
#define FILE_TYPE MemFile
2024-10-19 16:37:43 -07:00
//#endif
2024-08-08 13:12:37 -07:00
STREAMFILE *get_sf_from_file(File *file);
SDL_RWops *get_sdl_file(File *file);
File *open_file(const char *fname);
File *open_url(const char *url);