Abstracted hard disc image handling to separate file.

This commit is contained in:
SarahW 2017-07-02 18:10:36 +01:00
commit 6484baaf9f
15 changed files with 251 additions and 280 deletions

85
src/hdd_file.c Normal file
View file

@ -0,0 +1,85 @@
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE
#include <errno.h>
#include "ibm.h"
#include "hdd_file.h"
void hdd_load(hdd_file_t *hdd, int d, const char *fn)
{
if (hdd->f == NULL)
{
/* Try to open existing hard disk image */
hdd->f = fopen64(fn, "rb+");
if (hdd->f == NULL)
{
/* Failed to open existing hard disk image */
if (errno == ENOENT)
{
/* Failed because it does not exist,
so try to create new file */
hdd->f = fopen64(fn, "wb+");
if (hdd->f == NULL)
{
pclog("Cannot create file '%s': %s",
fn, strerror(errno));
return;
}
}
else
{
/* Failed for another reason */
pclog("Cannot open file '%s': %s",
fn, strerror(errno));
return;
}
}
}
hdd->spt = hdc[d].spt;
hdd->hpc = hdc[d].hpc;
hdd->tracks = hdc[d].tracks;
hdd->sectors = hdc[d].spt * hdc[d].hpc * hdc[d].tracks;
}
void hdd_close(hdd_file_t *hdd)
{
if (hdd->f)
{
fclose(hdd->f);
hdd->f = NULL;
}
}
void hdd_read_sectors(hdd_file_t *hdd, int offset, int nr_sectors, void *buffer)
{
off64_t addr;
addr = offset * 512;
fseeko64(hdd->f, addr, SEEK_SET);
fread(buffer, nr_sectors*512, 1, hdd->f);
}
void hdd_write_sectors(hdd_file_t *hdd, int offset, int nr_sectors, void *buffer)
{
off64_t addr;
addr = offset * 512;
fseeko64(hdd->f, addr, SEEK_SET);
fwrite(buffer, nr_sectors*512, 1, hdd->f);
}
void hdd_format_sectors(hdd_file_t *hdd, int offset, int nr_sectors)
{
off64_t addr;
int c;
uint8_t zero_buffer[512];
memset(zero_buffer, 0, 512);
addr = offset * 512;
fseeko64(hdd->f, addr, SEEK_SET);
for (c = 0; c < nr_sectors; c++)
fwrite(zero_buffer, 512, 1, hdd->f);
}