121 lines
3.1 KiB
C++
121 lines
3.1 KiB
C++
|
#include <iostream>
|
||
|
#include <fstream>
|
||
|
#include "memcard.h"
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
MemoryCard::MemoryCard() {
|
||
|
for( int i = 0; i < 16; i++ ) {
|
||
|
this->blocks[i] = new Block();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool MemoryCard::loadFile( char *fileName ) {
|
||
|
fstream file;
|
||
|
file.open( fileName, ios::in | ios::binary );
|
||
|
if( !file.is_open() ) {
|
||
|
cout << "'" << fileName << "' could not be opened for input, exiting.\n";
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
for( int i = 0; i < 16; i++ ) {
|
||
|
/*
|
||
|
blocks[i] = new Block();
|
||
|
file.read( reinterpret_cast<char*>( blocks[i] ), sizeof( Block ) );
|
||
|
if( file.gcount() != sizeof( Block ) ) {
|
||
|
cout << "Error reading block " << i << ", read count less than initial block size\n";
|
||
|
return false;
|
||
|
}
|
||
|
*/
|
||
|
this->loadBlockFromFile( fileName, i );
|
||
|
}
|
||
|
file.close();
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool MemoryCard::loadBlockFromFile( char *fileName, int srcBlock, int dstBlock, int offset ) {
|
||
|
srcBlock = srcBlock > 15 || srcBlock < 0 ? 0 : srcBlock;
|
||
|
dstBlock = dstBlock > 15 || dstBlock < 0 ? srcBlock : dstBlock;
|
||
|
fstream file;
|
||
|
file.open( fileName, ios::in | ios::binary );
|
||
|
if( !file.is_open() ) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
file.seekg( srcBlock * sizeof( Block ) + offset );
|
||
|
this->blocks[dstBlock] = new Block();
|
||
|
file.read( reinterpret_cast<char*>( this->blocks[dstBlock] ), sizeof( Block ) );
|
||
|
if( file.gcount() != sizeof( Block ) ) {
|
||
|
cout << "Error reading block " << srcBlock << ", read count less than initial block size\n";
|
||
|
return false;
|
||
|
}
|
||
|
file.close();
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool MemoryCard::loadMCS( char* fileName, int dstBlock ) {
|
||
|
if( dstBlock <= 0 ) {
|
||
|
dstBlock = this->directory()->nextOpenBlock();
|
||
|
if( dstBlock <= 0 ) { return false; }
|
||
|
}
|
||
|
fstream file;
|
||
|
file.open( fileName, ios::in | ios::binary );
|
||
|
if( !file.is_open() ) {
|
||
|
return false;
|
||
|
}
|
||
|
DirectoryFrame *dirEntry = new DirectoryFrame();
|
||
|
file.read( reinterpret_cast<char*>( dirEntry ), sizeof( DirectoryFrame ) );
|
||
|
file.close();
|
||
|
|
||
|
if( file.gcount() == sizeof( DirectoryFrame ) ) {
|
||
|
this->loadBlockFromFile( fileName, 0, dstBlock, sizeof( DirectoryFrame ) );
|
||
|
this->directory()->Directory( dstBlock, dirEntry );
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool MemoryCard::saveFile( char* fileName, fileFormatCard format ) {
|
||
|
fstream file;
|
||
|
file.open( fileName, ios::out | ios::binary );
|
||
|
if( !file.is_open() ) {
|
||
|
return false;
|
||
|
}
|
||
|
switch( format ) {
|
||
|
case FORMAT_RAW:
|
||
|
default:
|
||
|
for( int i = 0; i < 16; i++ ) {
|
||
|
file.write( reinterpret_cast<char*>( blocks[i] ), sizeof( Block ) );
|
||
|
}
|
||
|
}
|
||
|
file.close();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool MemoryCard::saveBlockToFile( char* fileName, int block, fileFormatSingle format ) {
|
||
|
fstream file;
|
||
|
file.open( fileName, ios::out | ios::binary );
|
||
|
if( !file.is_open() ) {
|
||
|
return false;
|
||
|
}
|
||
|
switch( format ) {
|
||
|
case FORMAT_MCS:
|
||
|
default:
|
||
|
file.write( reinterpret_cast<char*>( directory()->Directory( block - 1 ) ), sizeof( Frame ) );
|
||
|
file.write( reinterpret_cast<char*>( blocks[block] ), sizeof( Block ) );
|
||
|
break;
|
||
|
}
|
||
|
file.close();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
Block* MemoryCard::getBlock( int index = 0 ) {
|
||
|
index = index > 15 || index < 0 ? 0 : index;
|
||
|
return blocks[index];
|
||
|
}
|
||
|
|
||
|
DirectoryBlock* MemoryCard::directory() {
|
||
|
return (DirectoryBlock*)this->blocks[0];
|
||
|
}
|