2022-02-15 03:12:20 +00:00
|
|
|
/**
|
|
|
|
* Furnace Tracker - multi-system chiptune tracker
|
|
|
|
* Copyright (C) 2021-2022 tildearrow and contributors
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2021-05-11 20:08:08 +00:00
|
|
|
#ifndef _SAFEREADER_H
|
|
|
|
#define _SAFEREADER_H
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-05-28 20:25:55 +00:00
|
|
|
#include <stdint.h>
|
2021-05-11 20:08:08 +00:00
|
|
|
#include "../ta-utils.h"
|
|
|
|
|
2022-04-10 05:01:55 +00:00
|
|
|
enum Endianness {
|
|
|
|
LittleEndian=0,
|
|
|
|
BigEndian
|
|
|
|
};
|
|
|
|
|
2021-05-11 20:08:08 +00:00
|
|
|
class SafeReader;
|
|
|
|
|
|
|
|
struct EndOfFileException {
|
|
|
|
SafeReader* reader;
|
|
|
|
size_t finalSize;
|
|
|
|
EndOfFileException(SafeReader* r, size_t fs):
|
|
|
|
reader(r),
|
|
|
|
finalSize(fs) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SafeReader {
|
|
|
|
unsigned char* buf;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
size_t curSeek;
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool seek(ssize_t where, int whence);
|
|
|
|
size_t tell();
|
|
|
|
size_t size();
|
|
|
|
|
|
|
|
int read(void* where, size_t count);
|
|
|
|
|
|
|
|
// these functions may throw EndOfFileException.
|
2021-05-11 20:26:38 +00:00
|
|
|
signed char readC();
|
2021-05-11 20:08:08 +00:00
|
|
|
short readS();
|
|
|
|
short readS_BE();
|
|
|
|
int readI();
|
|
|
|
int readI_BE();
|
|
|
|
int64_t readL();
|
|
|
|
int64_t readL_BE();
|
|
|
|
float readF();
|
|
|
|
float readF_BE();
|
|
|
|
double readD();
|
|
|
|
double readD_BE();
|
|
|
|
String readString();
|
|
|
|
String readString(size_t len);
|
2022-04-24 02:31:37 +00:00
|
|
|
String readStringLine();
|
2022-04-30 04:58:21 +00:00
|
|
|
String readStringToken(unsigned char delim, bool stripContiguous);
|
2022-04-24 02:31:37 +00:00
|
|
|
String readStringToken();
|
2022-04-24 04:04:56 +00:00
|
|
|
inline bool isEOF() { return curSeek >= len; };
|
2021-05-11 20:08:08 +00:00
|
|
|
|
|
|
|
SafeReader(void* b, size_t l):
|
|
|
|
buf((unsigned char*)b),
|
|
|
|
len(l),
|
|
|
|
curSeek(0) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|