homelab.cpp, ssem.cpp, machine/z80bin.cpp: Eliminate use of fgetc in quickload processing

This commit is contained in:
AJR 2022-03-31 15:21:08 -04:00
parent f5d61ddcbe
commit 51586b043c
3 changed files with 26 additions and 22 deletions

View File

@ -684,15 +684,16 @@ QUICKLOAD_LOAD_MEMBER(homelab_state::quickload_cb)
u16 args[2];
image.fseek(0x100, SEEK_SET);
u8 ch = image.fgetc();
if (ch != 0xA5)
u8 ch = 0;
u32 bytes = image.fread(&ch, 1);
if (bytes != 1 || ch != 0xA5)
{
image.seterror(image_error::INVALIDIMAGE, "Invalid header");
image.message(" Invalid header");
return image_init_result::FAIL;
}
while((ch = image.fgetc()))
while ((bytes = image.fread(&ch, 1)) != 0 && ch != 0)
{
if (i >= (std::size(pgmname) - 1))
{

View File

@ -12,6 +12,7 @@
#include "emupal.h"
#include "screen.h"
#include "softlist_dev.h"
#include <sstream>
class ssem_state : public driver_device
@ -35,7 +36,7 @@ private:
uint32_t screen_update_ssem(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);
DECLARE_QUICKLOAD_LOAD_MEMBER(quickload_cb);
inline uint32_t reverse(uint32_t v);
std::string read_line(device_image_interface *image);
std::string read_line(device_image_interface &image);
void ssem_map(address_map &map);
@ -516,25 +517,26 @@ uint32_t ssem_state::screen_update_ssem(screen_device &screen, bitmap_rgb32 &bit
* Image helper functions *
\****************************************************/
std::string ssem_state::read_line(device_image_interface *image)
std::string ssem_state::read_line(device_image_interface &image)
{
std::string holder;
std::ostringstream stream;
for (u8 i = 0; i < 100; i++)
{
char c = image->fgetc();
char c = 0;
if (image.fread(&c, 1) != 1)
break;
// convert opcode to lower case
if (c >= 'A' && c <= 'Z')
c += 32;
if (c >= 32)
holder.push_back(c);
stream << c;
else
{
c = image->fgetc(); // skip LF
image.fread(&c, 1); // skip LF
break;
}
}
holder.push_back(0);
return holder;
return std::move(stream).str();
}
@ -548,7 +550,7 @@ QUICKLOAD_LOAD_MEMBER(ssem_state::quickload_cb)
std::string buffer;
u32 num_lines = 0;
std::string image_line = read_line(&image);
std::string image_line = read_line(image);
if (image_line.empty())
{
image.message("No data in line 1");
@ -566,7 +568,7 @@ QUICKLOAD_LOAD_MEMBER(ssem_state::quickload_cb)
for (u32 i = 0; i < num_lines; i++)
{
u32 line = 0, word = 0;
image_line = read_line(&image);
image_line = read_line(image);
u32 length = image_line.length();
if (length < 9)

View File

@ -10,7 +10,6 @@
image_init_result z80bin_load_file(device_image_interface &image, address_space &space, uint16_t &exec_addr, uint16_t &start_addr, uint16_t &end_addr)
{
int ch = 0;
uint16_t args[3]{};
uint16_t i = 0U, j = 0U, size = 0U;
uint8_t data = 0U;
@ -19,15 +18,10 @@ image_init_result z80bin_load_file(device_image_interface &image, address_space
image.fseek(7, SEEK_SET);
while((ch = image.fgetc()) != 0x1A)
char ch = '\0';
uint32_t bytes = 0;
while ((bytes = image.fread(&ch, 1)) != 0 && ch != 0x1A)
{
if (ch == EOF)
{
image.seterror(image_error::INVALIDIMAGE, "Unexpected EOF while getting file name");
image.message(" Unexpected EOF while getting file name");
return image_init_result::FAIL;
}
if (ch != '\0')
{
if (i >= (std::size(pgmname) - 1))
@ -42,6 +36,13 @@ image_init_result z80bin_load_file(device_image_interface &image, address_space
}
}
if (bytes == 0)
{
image.seterror(image_error::INVALIDIMAGE, "Unexpected EOF while getting file name");
image.message(" Unexpected EOF while getting file name");
return image_init_result::FAIL;
}
pgmname[i] = '\0'; /* terminate string with a null */
if (image.fread(args, sizeof(args)) != sizeof(args))