Commit 5accf06b authored by Sergio Costas's avatar Sergio Costas

Now uses streams instead of FILE

Fixed all warnings
parent 262a4b12
......@@ -50,7 +50,10 @@ computer::computer() {
this->port254 = 0;
this->issue = 3;
this->mode128k = 0;
this->turbo = false;
this->sound_bit = 0;
this->tape_write = false;
this->tape_fast_load = true; // fast load by default
this->other_ret = 0;
......
......@@ -116,7 +116,7 @@ public:
unsigned char interr;
unsigned char other_ret; // 0=no change; 1=memory returns RET (201)
unsigned char turbo;
bool turbo;
computer();
~computer();
......
......@@ -43,58 +43,59 @@ char path_snaps[2049];
char path_taps[2049];
char path_mdrs[2049];
unsigned int jump_frames, curr_frames;
char *filenames[5];
string filenames[5];
char *load_a_rom(char **filenames) {
string load_a_rom(string *filenames) {
char **pointer;
string *pointer;
int offset=0;
FILE *fichero;
ifstream *fichero;
for(pointer=filenames;*pointer!=NULL;pointer++) {
fichero=llscreen->myfopen(*pointer,"r");
if(fichero==NULL) {
for(pointer=filenames; *pointer != ""; pointer++) {
fichero=llscreen->myfopen(*pointer,ios::in|ios::binary);
if(fichero == NULL) {
return (*pointer);
}
fread(ordenador->memoria+offset,16384,1,fichero);
fichero->read((char*)ordenador->memoria+offset,16384);
offset+=16384;
fclose(fichero);
fichero->close();
delete fichero;
}
return (NULL);
return "";
}
void load_rom(char type) {
char *retval;
FILE *fichero;
string retval;
ifstream *fichero;
switch(type) {
case 0:
filenames[0]="spectrum-roms/48.rom";
filenames[1]=NULL;
filenames[1]="";
retval=load_a_rom(filenames);
if (retval) {
printf("Can't load file %s\n",retval);
if (retval != "") {
printf("Can't load file %s\n",retval.c_str());
exit(1);
}
break;
case 1:
filenames[0]="spectrum-roms/128-0.rom";
filenames[1]="spectrum-roms/128-1.rom";
filenames[2]=NULL;
filenames[2]="";
retval=load_a_rom(filenames);
if (retval) {
printf("Can't load file %s\n",retval);
if (retval != "") {
printf("Can't load file %s\n",retval.c_str());
exit(1);
}
break;
case 2:
filenames[0]="spectrum-roms/plus2-0.rom";
filenames[1]="spectrum-roms/plus2-1.rom";
filenames[2]=NULL;
filenames[2]="";
retval=load_a_rom(filenames);
if (retval) {
printf("Can't load file %s\n",retval);
if (retval != "") {
printf("Can't load file %s\n",retval.c_str());
exit(1);
}
break;
......@@ -105,26 +106,26 @@ void load_rom(char type) {
filenames[1]="spectrum-roms/plus3-41-1.rom";
filenames[2]="spectrum-roms/plus3-41-2.rom";
filenames[3]="spectrum-roms/plus3-41-3.rom";
filenames[4]=NULL;
filenames[4]="";
retval=load_a_rom(filenames);
if (retval) {
if (retval != "") {
printf("Can't load the Spectrum +3 ROM version 4.1. Trying with version 4.0\n");
filenames[0]="spectrum-roms/plus3-40-0.rom";
filenames[1]="spectrum-roms/plus3-40-1.rom";
filenames[2]="spectrum-roms/plus3-40-2.rom";
filenames[3]="spectrum-roms/plus3-40-3.rom";
filenames[4]=NULL;
filenames[4]="";
retval=load_a_rom(filenames);
if (retval) {
if (retval != "") {
printf("Can't load the Spectrum +3 ROM version 4.0. Trying with legacy filenames\n");
filenames[0]="spectrum-roms/plus3-0.rom";
filenames[1]="spectrum-roms/plus3-1.rom";
filenames[2]="spectrum-roms/plus3-2.rom";
filenames[3]="spectrum-roms/plus3-3.rom";
filenames[4]=NULL;
filenames[4]="";
retval=load_a_rom(filenames);
if (retval) {
printf("Can't load file %s\n",retval);
if (retval != "") {
printf("Can't load file %s\n",retval.c_str());
exit(1);
}
}
......@@ -133,26 +134,29 @@ void load_rom(char type) {
case 4:
filenames[0]="spectrum-roms/128-spanish-0.rom";
filenames[1]="spectrum-roms/128-spanish-1.rom";
filenames[2]=NULL;
filenames[2]="";
retval=load_a_rom(filenames);
if (retval) {
printf("Can't load file %s\n",retval);
if (retval != "") {
printf("Can't load file %s\n",retval.c_str());
exit(1);
}
break;
}
fichero=llscreen->myfopen("spectrum-roms/if1-2.rom","r"); // load Interface1 ROM
if(fichero==NULL) {
fichero=llscreen->myfopen("spectrum-roms/if1-2.rom",ios::in|ios::binary); // load Interface1 ROM
if(fichero == NULL) {
delete fichero;
// try legacy name
fichero=llscreen->myfopen("spectrum-roms/if1-v2.rom","r");
if(fichero==NULL) {
fichero=llscreen->myfopen("spectrum-roms/if1-v2.rom",ios::in|ios::binary);
if(fichero == NULL) {
delete fichero;
printf("Can't open Interface1 ROM file\n");
exit(1);
}
}
fread(ordenador->shadowrom,8192,1,fichero);
fclose(fichero);
fichero->read((char*)ordenador->shadowrom,8192);
fichero->close();
delete fichero;
}
void end_system() {
......
......@@ -109,26 +109,29 @@ LLScreen::LLScreen(int16_t resx,int16_t resy,uint8_t depth,bool fullscreen,bool
printf("Locking screen\n");
}
FILE *LLScreen::myfopen(char *filename,char *mode) {
ifstream *LLScreen::myfopen(string filename,ios_base::openmode mode) {
char tmp[4096];
FILE *fichero;
ifstream *fichero;
fichero=fopen(filename,mode);
if (fichero!=NULL) {
fichero = new ifstream(filename.c_str(),mode);
if (fichero->is_open()) {
return (fichero);
}
sprintf(tmp,"/usr/share/%s",filename);
fichero=fopen(tmp,mode);
if (fichero!=NULL) {
delete(fichero);
sprintf(tmp,"/usr/share/%s",filename.c_str());
fichero = new ifstream(tmp,mode);
if (fichero->is_open()) {
return (fichero);
}
sprintf(tmp,"/usr/local/share/%s",filename);
fichero=fopen(tmp,mode);
if (fichero!=NULL) {
delete(fichero);
sprintf(tmp,"/usr/local/share/%s",filename.c_str());
fichero = new ifstream(tmp,mode);
if (fichero->is_open()) {
return (fichero);
}
return (NULL);
delete(fichero);
return NULL;
}
LLScreen::~LLScreen(){
......@@ -469,14 +472,14 @@ uint8_t LLScreen::printchar(uint8_t carac, int16_t x, int16_t y, uint8_t color,
void LLScreen::paint_picture(string filename) {
FILE *fichero;
ifstream *fichero;
int bucle1,bucle2;
unsigned char *buffer,*buffer2,valor;
buffer=(unsigned char *)this->llscreen->pixels;
this->clear_screen();
fichero=myfopen((char *)filename.c_str(),"r");
fichero = myfopen(filename,ios::in|ios::binary);
if (fichero==NULL) {
osd->set_message("Keymap picture not found",2000);
return;
......@@ -484,7 +487,7 @@ void LLScreen::paint_picture(string filename) {
if (!this->rotate) {
for (bucle1=0;bucle1<344;bucle1++)
for(bucle2=0;bucle2<640;bucle2++) {
fscanf(fichero,"%c",&valor);
fichero->read((char *)&valor,1);
paint_one_pixel(valor,buffer);
buffer += this->bpp;
}
......@@ -493,14 +496,15 @@ void LLScreen::paint_picture(string filename) {
for(bucle1 = 0;bucle1 < 344;bucle1++) {
buffer2 = buffer;
for(bucle2 = 0;bucle2 < 640;bucle2++) {
fscanf(fichero,"%c",&valor);
fichero->read((char *)&valor,1);
paint_one_pixel(valor,buffer);
buffer += (480 * this->bpp);
}
buffer = buffer2 - this->bpp;
}
}
fichero->close();
delete fichero;
}
void LLScreen::clear_screen() {
......
......@@ -26,6 +26,8 @@ using namespace std;
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <iostream>
#include <fstream>
extern struct Charset charset;
......@@ -50,7 +52,7 @@ public:
LLScreen(int16_t resx, int16_t resy, uint8_t depth, bool fullscreen, bool dblbuffer, bool hwsurface);
~LLScreen();
FILE *myfopen(char *filename,char *mode);
ifstream *myfopen(string,ios_base::openmode);
void print_string(string message, int16_t x, int16_t y, uint8_t ink, uint8_t paper);
void set_paletes(bool);
void paint_one_pixel(uint8_t value,unsigned char *address);
......
......@@ -329,10 +329,10 @@ void settings_menu() {
case SDLK_t:
if(ordenador->turbo){
llsound->tst_sample = 3500000 / llsound->freq;
ordenador->turbo = 0;
ordenador->turbo = false;
} else {
llsound->tst_sample = 100000000 / llsound->freq;
ordenador->turbo = 1;
ordenador->turbo = true;
}
}
} while(fin);
......
......@@ -27,8 +27,8 @@
#include "tape.hh"
#include "z80free/Z80free.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
class Tape *OOTape; // Tape object
......@@ -38,48 +38,48 @@ private:
uint8_t signal;
protected:
bool read_8bit(FILE *file, uint8_t &value) {
bool read_8bit(ifstream *file, uint8_t &value) {
uint8_t data;
size_t retval;
retval = fread (&data, 1, 1, file);
if (retval < 1) {
fclose(file); // end-of-file
file->read((char*)&data, 1);
if (file->eof()) {
file->close(); // end-of-file
return true;
}
value = data;
return false;
}
bool read_16bit(FILE *file, uint16_t &value) {
bool read_16bit(ifstream *file, uint16_t &value) {
uint8_t data[2];
size_t retval;
retval = fread (data, 2, 1, file);
if (retval < 1) {
fclose(file); // end-of-file
file->read((char*)data, 2);
if (file->eof()) {
file->close(); // end-of-file
return true;
}
value = ((uint16_t) data[0]) + 256 * ((uint16_t) data[1]);
return false;
}
bool read_24bit(FILE *file, uint32_t &value) {
bool read_24bit(ifstream *file, uint32_t &value) {
uint8_t data[3];
size_t retval;
retval = fread (data, 3, 1, file);
if (retval < 1) {
fclose(file); // end-of-file
file->read((char*)data, 3);
if (file->eof()) {
file->close(); // end-of-file
return true;
}
value = ((uint32_t) data[0]) + 256 * ((uint32_t) data[1]) + 65536 * ((uint32_t) data[2]);
return false;
}
bool read_string(FILE *file, string &value) {
bool read_string(ifstream *file, string &value) {
uint8_t length;
char data[256];
......@@ -88,9 +88,9 @@ protected:
if (this->read_8bit(file,length)) {
return true;
}
retval = fread (data, length, 1, file);
if (retval < 1) {
fclose(file); // end-of-file
file->read(data, length);
if (file->eof()) {
file->close(); // end-of-file
return true;
}
data[length] = 0;
......@@ -118,7 +118,7 @@ public:
}
}
virtual bool load_block(FILE *) {
virtual bool load_block(ifstream *) {
return false;
}
......@@ -236,7 +236,7 @@ private:
class EndLoopBlock *endblock;
public:
bool load_block(FILE *file) {
bool load_block(ifstream *file) {
// read repetitions
if (this->read_16bit(file,this->loop)) {
......@@ -407,7 +407,7 @@ public:
delete[](this->data);
}
}
bool load_block(FILE *file) {
bool load_block(ifstream *file) {
uint16_t size;
size_t retval;
......@@ -416,9 +416,9 @@ public:
return true;
}
this->data = new uint8_t[size];
retval = fread (this->data, size, 1, file);
if (retval != 1) {
fclose(file);
file->read ((char*)this->data, size);
if (file->eof()) {
file->close();
return (true); // end-of-file and error
}
......@@ -456,7 +456,7 @@ public:
delete[](this->data);
}
}
bool load_block(FILE *file) {
bool load_block(ifstream *file) {
uint16_t size;
uint16_t pause;
......@@ -471,9 +471,9 @@ public:
return true;
}
this->data = new uint8_t[size];
retval = fread (this->data, size, 1, file);
if (retval != 1) {
fclose(file);
file->read ((char*)this->data, size);
if (file->eof()) {
file->close();
return (true); // end-of-file and error
}
this->allow_fast_load = false;
......@@ -510,7 +510,7 @@ public:
}
}
bool load_block(FILE *file) {
bool load_block(ifstream *file) {
size_t retval;
uint16_t pause;
......@@ -554,9 +554,9 @@ public:
this->allow_fast_load = false;
this->data = new uint8_t[this->data_size];
retval = fread (this->data, this->data_size, 1, file);
if (retval != 1) {
fclose(file);
file->read ((char*)this->data, this->data_size);
if (file->eof()) {
file->close();
return (true); // end-of-file and error
}
......@@ -591,7 +591,7 @@ public:
delete[](this->data);
}
}
bool load_block (FILE *file) {
bool load_block (ifstream *file) {
int loop;
uint16_t lpause;
......@@ -683,7 +683,7 @@ class ToneBlock : public TapeBlock {
public:
bool load_block(FILE *file) {
bool load_block(ifstream *file) {
// read pilot pulse duration
if (this->read_16bit(file,this->pilot)) {
......@@ -731,7 +731,7 @@ class PulsesBlock : public TapeBlock {
public:
bool load_block(FILE *file) {
bool load_block(ifstream *file) {
uint8_t loop;
uint16_t pulses[255];
......@@ -785,7 +785,7 @@ public:
this->tape = tape;
}
bool read_block(FILE *file) {
bool read_block(ifstream *file) {
uint16_t length;
// read pilot pulse duration
......@@ -833,7 +833,7 @@ class GroupStartBlock : public TapeBlock {
public:
bool load_block(FILE *file) {
bool load_block(ifstream *file) {
if (this->read_string(file,this->name)) {
return true;
......@@ -880,7 +880,7 @@ public:
}
}
bool load_block(FILE *file) {
bool load_block(ifstream *file) {
size_t retval;
......@@ -888,8 +888,8 @@ public:
return true;
}
this->data = new uint8_t[this->size];
retval = fread(this->data,this->size,1,file);
if (retval != 1) {
file->read((char*)this->data,this->size);
if (file->eof()) {
return true;
}
return false;
......@@ -905,6 +905,7 @@ Tape::Tape() {
this->current_block = NULL;
this->paused = true;
this->block_accesed = false;
this->tzx = false;
}
Tape::~Tape() {
......@@ -934,26 +935,30 @@ bool Tape::load_file(string filename) {
this->delete_blocks();
this->paused = true;
FILE *file = fopen(filename.c_str(),"rb");
if (file == NULL) {
ifstream file (filename.c_str(),ios::in|ios::binary);
if (!file.is_open()) {
return true; // error while opening the file
}
fread(char_id,10,1,file); // read the (maybe) TZX header
fclose(file);
file.read(char_id,10); // read the (maybe) TZX header
file.close();
if((!strncmp(char_id,"ZXTape!",7)) && (char_id[7]==0x1A)&&(char_id[8]==1)) {
this->tzx = true;
return this->load_tzx(filename);
} else {
this->tzx = false;
return this->load_tap(filename);
}
}
bool Tape::load_tap(string filename) {
FILE *file;
ifstream *file;
class TAPBlock *block;
file = fopen(filename.c_str(),"rb");
if (file == NULL) {
file = new ifstream(filename.c_str(),ios::in|ios::binary);
if (!file->is_open()) {
delete (file);
return true; // error while opening the file
}
do {
......@@ -967,26 +972,28 @@ bool Tape::load_tap(string filename) {
} while(true);
this->current_block = this->blocks;
delete file;
return false;
}
bool Tape::load_tzx(string filename) {
FILE *file;
ifstream *file;
uint8_t block_type;
uint8_t tmpdata[10];
size_t retval;
class TapeBlock *block;
file = fopen(filename.c_str(),"rb");
if (file == NULL) {
file = new ifstream(filename.c_str(),ios::in|ios::binary);
if (!file->is_open()) {
delete (file);
return true; // error while opening the file
}
fread(tmpdata,10,1,file); // read TZX header
file->read((char*)tmpdata,10); // read TZX header
while(true) {
// read block type
retval = fread (&block_type, 1, 1, file);
if (retval < 1) {
file->read ((char*)&block_type, 1);
if (file->eof()) {
break; // end-of-file
}
switch(block_type) {
......@@ -1025,16 +1032,21 @@ bool Tape::load_tzx(string filename) {
break;
default:
printf("Block unknown: %X\n",block_type);
file->close();
delete (file);
return true;
break;
}
if (block->load_block(file)) {
file->close();
delete (file);
return true;
}
this->add_block(block);
}
fclose(file);
file->close();
delete (file);
this->current_block = this->blocks;
return false;
}
......@@ -1092,7 +1104,9 @@ void Tape::rewind() {
void Tape::set_pause(bool pause) {
this->paused = pause;
osd->set_message("Tape paused",2000);
if (pause) {
osd->set_message("Tape paused",2000);
}
}
bool Tape::get_pause() {
......
......@@ -38,19 +38,6 @@ using namespace std;
#ifndef SRC_TAPE_HPP_
#define SRC_TAPE_HPP_
/*
class TapeSignal {
class Tape *tape_object;
~TapeSignal();
public:
virtual void callback_pause(bool pause) = 0;
void register_tape_object(class Tape *tape);
};
typedef void (TapeSignal::*signal_pause) (bool);
*/
enum FastLoadReturn {FASTLOAD_OK, FASTLOAD_NO_BLOCK, FASTLOAD_NO_TAPE, FASTLOAD_NO_FLAG, FASTLOAD_END_TAPE};
class Tape : public Signals {
......@@ -59,6 +46,7 @@ class Tape : public Signals {
class TapeBlock *current_block;
bool paused;
bool block_accesed;
bool tzx;
void add_block(class TapeBlock *);
void delete_blocks();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment