Skip to content

Commit

Permalink
Port to new file API
Browse files Browse the repository at this point in the history
  • Loading branch information
Daft-Freak committed Feb 22, 2020
1 parent 50be8b2 commit c10ffe2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 45 deletions.
13 changes: 4 additions & 9 deletions buffered-file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@ BufferedFile::BufferedFile()

BufferedFile::~BufferedFile()
{
if(file != -1)
blit::close_file(file);
}

bool BufferedFile::open(std::string filename)
{
bufferFilled = 0;

if(file >= 0)
blit::close_file(file);
file.open(filename);

file = blit::open_file(filename);

if(file < 0)
if(!file.is_open())
return false;

bufferFilled = blit::read_file(file, 0, bufferSize, reinterpret_cast<char *>(buffer));
bufferFilled = file.read(0, bufferSize, reinterpret_cast<char *>(buffer));

if(bufferFilled <= 0)
return false;
Expand All @@ -44,7 +39,7 @@ void BufferedFile::read(int32_t len)

bufferFilled -= len;

auto read = blit::read_file(file, offset, bufferSize - bufferFilled, reinterpret_cast<char *>(buffer) + bufferFilled);
auto read = file.read(offset, bufferSize - bufferFilled, reinterpret_cast<char *>(buffer) + bufferFilled);

if(read <= 0)
return;
Expand Down
4 changes: 3 additions & 1 deletion buffered-file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <cstdint>
#include <string>

#include "engine/file.hpp"

class BufferedFile final
{
public:
Expand All @@ -16,7 +18,7 @@ class BufferedFile final
const uint8_t *getBuffer() const;

private:
int32_t file = -1;
blit::File file;
uint32_t offset = 0;

static const int bufferSize = 1024 * 4;
Expand Down
34 changes: 15 additions & 19 deletions mp3-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,33 @@ static uint32_t getInt32(char *buf)
return ubuf[3] | (ubuf[2] << 8) | (ubuf[1] << 16) | (ubuf[0] << 24);
}

static std::string readString(int32_t file, uint32_t offset, char encoding, int32_t len)
static std::string readString(blit::File &file, uint32_t offset, char encoding, int32_t len)
{
std::string ret;

if(encoding == 0)
{
// ISO-8859-1
char c;
blit::read_file(file, offset++, 1, &c);
file.read(offset++, 1, &c);

for(int i = 1; i < len || (len == -1 && c); i++)
{
// "convert" by throwing away anything non-ascii
if((c & 0x80) == 0)
ret += c;
blit::read_file(file, offset++, 1, &c);
file.read(offset++, 1, &c);
}
}
else if(encoding == 1)
{
// UCS-2
uint16_t bom;
blit::read_file(file, offset, 2, reinterpret_cast<char *>(&bom));
file.read(offset, 2, reinterpret_cast<char *>(&bom));
offset += 2;

uint16_t c;
blit::read_file(file, offset, 2, reinterpret_cast<char *>(&c));
file.read(offset, 2, reinterpret_cast<char *>(&c));
offset += 2;

for(int i = 1; i < (len - 2) / 2 || (len == -1 && c); i++)
Expand All @@ -66,22 +66,22 @@ static std::string readString(int32_t file, uint32_t offset, char encoding, int3
else if((c >> 8) < 0x80)
ret += (c >> 8);

blit::read_file(file, offset, 2, reinterpret_cast<char *>(&c));
file.read(offset, 2, reinterpret_cast<char *>(&c));
offset += 2;
}
}
else if(encoding == 3)
{
// UTF-8
char c;
blit::read_file(file, offset++, 1, &c);
file.read(offset++, 1, &c);

for(int i = 1; i < len || (len == -1 && c); i++)
{
// "convert" by throwing away anything non-ascii
if((c & 0x80) == 0)
ret += c;
blit::read_file(file, offset++, 1, &c);
file.read(offset++, 1, &c);
}
}
else
Expand All @@ -90,10 +90,10 @@ static std::string readString(int32_t file, uint32_t offset, char encoding, int3
return ret;
}

static std::string readTextTag(int32_t file, uint32_t offset, int32_t len)
static std::string readTextTag(blit::File &file, uint32_t offset, int32_t len)
{
char encoding;
blit::read_file(file, offset, 1, &encoding);
file.read(offset, 1, &encoding);

return readString(file, offset + 1, encoding, len);
}
Expand Down Expand Up @@ -132,19 +132,16 @@ MusicTags MP3Stream::parseTags(std::string filename)
{
MusicTags ret;

auto file = blit::open_file(filename);
blit::File file(filename);

if(file == -1)
if(!file.is_open())
return ret;

char buf[10];
blit::read_file(file, 0, 10, buf);
file.read(0, 10, buf);

if(memcmp(buf, "ID3", 3) != 0)
{
blit::close_file(file);
return ret;
}

// version/flags
int versionMajor = buf[3], versionMinor = buf[4];
Expand All @@ -160,14 +157,14 @@ MusicTags MP3Stream::parseTags(std::string filename)
// skip extended header
if(flags & 0x40)
{
blit::read_file(file, offset, 4, buf);
file.read(offset, 4, buf);
offset += getSynchsafe(buf);
}

while(offset < size)
{
// read header
blit::read_file(file, offset, 10, buf);
file.read(offset, 10, buf);

if(buf[0] == 0)
break;
Expand All @@ -191,7 +188,6 @@ MusicTags MP3Stream::parseTags(std::string filename)
offset += frameSize;
}

blit::close_file(file);
return ret;
}

Expand Down
15 changes: 8 additions & 7 deletions stdio-wrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

struct wrap_FILE
{
int32_t file;
blit::File file;
uint32_t offset;

uint8_t getc_buffer[512];
Expand All @@ -19,20 +19,21 @@ inline wrap_FILE *wrap_fopen(const char *filename, const char *mode)
{
auto ret = new wrap_FILE;

ret->file = blit::open_file(filename);
ret->file.open(filename);
ret->offset = 0;

ret->getc_buf_len = ret->getc_buf_off = 0;

if(ret->file == -1)
if(!ret->file.is_open())
return nullptr;

return ret;
}

inline int wrap_fclose(wrap_FILE *file)
{
return blit::close_file(file->file) == -1 ? EOF : 0;
file->file.close();
return 0;
}

inline size_t wrap_fread(void *buffer, size_t size, size_t count, wrap_FILE *file)
Expand All @@ -44,7 +45,7 @@ inline size_t wrap_fread(void *buffer, size_t size, size_t count, wrap_FILE *fil
file->getc_buf_off = file->getc_buf_len = 0;
}

auto ret = blit::read_file(file->file, file->offset, size * count, (char *)buffer);
auto ret = file->file.read(file->offset, size * count, (char *)buffer);
file->offset += ret;

return ret < 0 ? 0 : ret / size;
Expand All @@ -56,7 +57,7 @@ inline int wrap_fgetc(wrap_FILE *file)
if(file->getc_buf_off >= file->getc_buf_len)
{
file->offset += file->getc_buf_off;
file->getc_buf_len = blit::read_file(file->file, file->offset, 512, (char*)file->getc_buffer);
file->getc_buf_len = file->file.read(file->offset, 512, (char*)file->getc_buffer);
file->getc_buf_off = 0;
}

Expand All @@ -80,7 +81,7 @@ inline int wrap_fseek(wrap_FILE *file, long offset, int origin)
else if(origin == SEEK_CUR)
file->offset += offset;
else
file->offset = blit::get_file_length(file->file) - offset;
file->offset = file->file.get_length() - offset;

return 0;
}
Expand Down
14 changes: 5 additions & 9 deletions vorbis-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,13 @@ bool VorbisStream::load(std::string filename)
{
MusicTags ret;
auto file = blit::open_file(filename);
blit::File file(filename);
if(file == -1)
return ret;
//TODO: standalone tag parsing
blit::close_file(file);
return ret;
}*/

Expand Down Expand Up @@ -296,17 +295,17 @@ uint64_t VorbisStream::calcDuration(std::string filename)
{
// scan through the file backwards to find the sample pos of the last page

auto file = blit::open_file(filename);
if(file == -1)
blit::File file(filename);
if(!file.is_open())
return 0;

auto length = blit::get_file_length(file);
auto length = file.get_length();
const int chunkLen = 1024;

for(uint32_t offset = chunkLen; offset < length; offset += chunkLen - 14)
{
uint8_t buf[chunkLen];
blit::read_file(file, length - offset, chunkLen, reinterpret_cast<char *>(buf));
file.read(length - offset, chunkLen, reinterpret_cast<char *>(buf));

for(int i = chunkLen - 14; i >= 0; i--)
{
Expand All @@ -323,13 +322,10 @@ uint64_t VorbisStream::calcDuration(std::string filename)
(static_cast<uint64_t>(buf[i + 12]) << 48) |
(static_cast<uint64_t>(buf[i + 13]) << 56);

blit::close_file(file);
return sampleOff;
}
}
}

blit::close_file(file);

return 0;
}

0 comments on commit c10ffe2

Please sign in to comment.