Skip to content

Commit

Permalink
[meatloaf] update MFile/MStream and media libs
Browse files Browse the repository at this point in the history
  • Loading branch information
idolpx committed Dec 14, 2023
1 parent 06ecc69 commit 8916ed8
Show file tree
Hide file tree
Showing 37 changed files with 616 additions and 485 deletions.
20 changes: 10 additions & 10 deletions lib/fnjson/fnjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ string FNJSON::processString(string in)
}
}

#ifdef BUILD_IEC
mstr::toPETSCII(in);
#endif
// #ifdef BUILD_IEC
// mstr::toPETSCII(in);
// #endif

#ifdef BUILD_ATARI
// SIO AUX bits 0+1 control the mapping
Expand Down Expand Up @@ -214,14 +214,14 @@ string FNJSON::getValue(cJSON *item)
item = item->child;
do
{
#ifdef BUILD_IEC
// Convert key to PETSCII
string tempStr = string((const char *)item->string);
mstr::toPETSCII(tempStr);
ss << tempStr;
#else
// #ifdef BUILD_IEC
// // Convert key to PETSCII
// string tempStr = string((const char *)item->string);
// mstr::toPETSCII(tempStr);
// ss << tempStr;
// #else
ss << item->string;
#endif
// #endif

ss << lineEnding + getValue(item);
} while ((item = item->next) != NULL);
Expand Down
28 changes: 7 additions & 21 deletions lib/meatloaf/cbm_media.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void CBMImageStream::close() {

};

uint16_t CBMImageStream::seekFileSize( uint8_t start_track, uint8_t start_sector )
uint32_t CBMImageStream::seekFileSize( uint8_t start_track, uint8_t start_sector )
{
// Calculate file size
seekSector(start_track, start_sector);
Expand All @@ -65,36 +65,20 @@ uint16_t CBMImageStream::seekFileSize( uint8_t start_track, uint8_t start_sector
seekSector( start_track, start_sector );
} while ( start_track > 0 );
blocks--;
return (blocks * (block_size - 2)) + start_sector;
return (blocks * (block_size - 2)) + start_sector - 1;
};


uint32_t CBMImageStream::position() {
return m_position; // return position within "seeked" file, not the D64 image!
};

size_t CBMImageStream::error() {
return m_error; // return position within "seeked" file, not the D64 image!
};

uint32_t CBMImageStream::available() {
// return bytes available in currently "seeked" file
return m_bytesAvailable;
};

uint32_t CBMImageStream::size() {
// size of the "seeked" file, not the image.
return m_length;
};

uint32_t CBMImageStream::write(const uint8_t *buf, uint32_t size) {
return -1;
}


uint32_t CBMImageStream::read(uint8_t* buf, uint32_t size) {
uint32_t bytesRead = 0;

//Debug_printv("seekCalled[%d]", seekCalled);

if(seekCalled) {
// if we have the stream set to a specific file already, either via seekNextEntry or seekPath, return bytes of the file here
// or set the stream to EOF-like state, if whle file is completely read.
Expand All @@ -107,6 +91,8 @@ uint32_t CBMImageStream::read(uint8_t* buf, uint32_t size) {
}

m_position += bytesRead;
m_bytesAvailable = m_length - m_position;

return bytesRead;
};

Expand All @@ -116,4 +102,4 @@ bool CBMImageStream::isOpen() {
};


std::unordered_map<std::string, CBMImageStream*> ImageBroker::repo;
std::unordered_map<std::string, CBMImageStream*> ImageBroker::repo;
22 changes: 7 additions & 15 deletions lib/meatloaf/cbm_media.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class CBMImageStream: public MStream {
// MStream methods
bool open() override;
void close() override;
uint32_t position() override;
size_t error() override;

~CBMImageStream() {
//Debug_printv("close");
Expand Down Expand Up @@ -67,7 +65,7 @@ class CBMImageStream: public MStream {
virtual std::string readString( uint8_t size )
{
uint8_t b[size] = { 0x00 };
uint8_t r = containerStream->read( b, size );
uint32_t r = containerStream->read( b, size );
return std::string((char *)b);
}
// readStringUntil = (delimiter = 0x00) => this.containerStream.readStringUntil(delimiter);
Expand All @@ -80,17 +78,16 @@ class CBMImageStream: public MStream {
bool seekPath(std::string path) override { return false; };
std::string seekNextEntry() override { return ""; };

virtual uint16_t seekFileSize( uint8_t start_track, uint8_t start_sector );
virtual uint32_t seekFileSize( uint8_t start_track, uint8_t start_sector );

uint32_t available() override;
uint32_t size() override;
uint32_t read(uint8_t* buf, uint32_t size) override;
uint32_t write(const uint8_t *buf, uint32_t size);
void reset() {
seekCalled = false;
m_position = 0;
m_length = block_size;
m_bytesAvailable = block_size;
//m_load_address = {0, 0};
}

bool isOpen();
Expand All @@ -102,22 +99,17 @@ class CBMImageStream: public MStream {
std::shared_ptr<MStream> containerStream;

bool m_isOpen = false;
uint32_t m_length = 0;
uint32_t m_bytesAvailable = 0;
uint32_t m_position = 0;
size_t m_error = 0;

CBMImageStream* decodedStream = nullptr;
CBMImageStream* decodedStream;

bool show_hidden = false;

size_t block_size = 256;
size_t media_header_size = 0x00;
size_t entry_index = 0; // Currently selected directory entry
size_t entry_count = -1; // Directory list entry count (-1 unknown)

enum open_modes { OPEN_READ, OPEN_WRITE, OPEN_APPEND, OPEN_MODIFY };
std::string file_type_label[8] = { "del", "seq", "prg", "usr", "rel", "cbm", "dir", "???" };
std::string file_type_label[12] = { "DEL", "SEQ", "PRG", "USR", "REL", "CBM", "DIR", "SUS", "NAT", "CMD", "CFS", "???" };

virtual void seekHeader() = 0;
virtual bool seekNextImageEntry() = 0;
Expand All @@ -130,9 +122,9 @@ class CBMImageStream: public MStream {
virtual uint8_t speedZone( uint8_t track) { return 0; };

virtual bool seekEntry( std::string filename ) { return false; };
virtual bool seekEntry( size_t index ) { return false; };
virtual bool seekEntry( uint16_t index ) { return false; };

virtual size_t readFile(uint8_t* buf, size_t size) = 0;
virtual uint16_t readFile(uint8_t* buf, uint16_t size) = 0;
std::string decodeType(uint8_t file_type, bool show_hidden = false);

private:
Expand Down
49 changes: 26 additions & 23 deletions lib/meatloaf/device/flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ MFile* FlashFileSystem::getFile(std::string path)

bool FlashFile::pathValid(std::string path)
{
std::string s = std::string(basepath + path);
auto apath = s.c_str();
auto apath = std::string(basepath + path).c_str();
while (*apath) {
const char *slash = strchr(apath, '/');
if (!slash) {
Expand Down Expand Up @@ -267,19 +266,19 @@ bool FlashFile::seekEntry( std::string filename )
if ( dirent->d_type != DT_DIR ) // Only want to match files not directories
{
if ( filename == entryFilename )
{
closedir( d );
return true;
}
{
closedir( d );
return true;
}
else if ( filename == "*" || mstr::compare(filename, entryFilename) )
{
// Set filename to this filename
Debug_printv( "Found! file[%s] -> entry[%s]", filename.c_str(), entryFilename.c_str() );
parseUrl(apath + "/" + std::string(dirent->d_name));
closedir( d );
return true;
{
// Set filename to this filename
Debug_printv( "Found! file[%s] -> entry[%s]", filename.c_str(), entryFilename.c_str() );
parseUrl(apath + "/" + std::string(dirent->d_name));
closedir( d );
return true;
}
}
}
}

Debug_printv( "Not Found! file[%s]", filename.c_str() );
Expand Down Expand Up @@ -332,7 +331,8 @@ bool FlashIStream::open() {
// Set file size
fseek(handle->file_h, 0, SEEK_END);
//Debug_printv("IStream: past fseek 1");
_size = ftell(handle->file_h);
m_length = ftell(handle->file_h);
m_bytesAvailable = m_length;
//Debug_printv("IStream: past ftell");
fseek(handle->file_h, 0, SEEK_SET);
//Debug_printv("IStream: past fseek 2");
Expand All @@ -351,24 +351,28 @@ uint32_t FlashIStream::read(uint8_t* buf, uint32_t size) {
return 0;
}

int bytesRead = fread((void*) buf, 1, size, handle->file_h );

if (bytesRead < 0) {
Debug_printv("read rc=%d\r\n", bytesRead);
return 0;
uint32_t bytesRead = 0;
if ( size > m_bytesAvailable )
size = m_bytesAvailable;

if ( size > 0 )
{
bytesRead = fread((void*) buf, 1, size, handle->file_h );
m_position += bytesRead;
m_bytesAvailable = m_length - m_position;
}

return bytesRead;
};


uint32_t FlashIStream::size() {
return _size;
return m_length;
};

uint32_t FlashIStream::available() {
if(!isOpen()) return 0;
return _size - position();
return m_length - position();
};


Expand Down Expand Up @@ -434,10 +438,9 @@ void FlashHandle::obtain(std::string m_path, std::string mode) {
// it will be caught by the real file open later on

char *pathStr = new char[m_path.length()];
strncpy(pathStr, m_path.data(), m_path.length());

if (pathStr) {
strncpy(pathStr, m_path.data(), m_path.length());

// Make dirs up to the final fnamepart
char *ptr = strchr(pathStr, '/');
while (ptr) {
Expand Down
5 changes: 1 addition & 4 deletions lib/meatloaf/device/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ friend class FlashIStream;
if (mstr::contains(name, "?") || mstr::contains(name, "*"))
seekEntry( name );

if (!pathValid(path))
if (!pathValid(path.c_str()))
m_isNull = true;
else
m_isNull = false;
Expand Down Expand Up @@ -161,9 +161,6 @@ class FlashIStream: public MStream {
std::string localPath;

std::unique_ptr<FlashHandle> handle;

private:
size_t _size = 0;
};


Expand Down
2 changes: 1 addition & 1 deletion lib/meatloaf/device/sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "meat_io.h"

#include "../device/flash.h"
#include "flash.h"
#include "fnFsSD.h"

#include "peoples_url_parser.h"
Expand Down
Loading

0 comments on commit 8916ed8

Please sign in to comment.