diff options
author | 2022-03-31 13:45:02 +0200 | |
---|---|---|
committer | 2022-04-01 11:36:55 +0200 | |
commit | e818533b167a24207e1cbfcee04524b215d001ef (patch) | |
tree | 839f3a7e41600020417506bb792c46ffb393a726 /src/lib/util/harddisk.cpp | |
parent | 50fc1c3a58910f4bdf5519804c310cb88d72e075 (diff) |
hard_disk_file: classify
Diffstat (limited to 'src/lib/util/harddisk.cpp')
-rw-r--r-- | src/lib/util/harddisk.cpp | 184 |
1 files changed, 52 insertions, 132 deletions
diff --git a/src/lib/util/harddisk.cpp b/src/lib/util/harddisk.cpp index ad0108092c8..d009317922e 100644 --- a/src/lib/util/harddisk.cpp +++ b/src/lib/util/harddisk.cpp @@ -15,88 +15,51 @@ #include <cstdlib> -/*************************************************************************** - TYPE DEFINITIONS -***************************************************************************/ - -struct hard_disk_file -{ - chd_file * chd; // CHD file - util::random_read_write * fhandle; // file if not a CHD - hard_disk_info info; // hard disk info -}; - - - -/*************************************************************************** - CORE IMPLEMENTATION -***************************************************************************/ - /*------------------------------------------------- - hard_disk_open - open a hard disk handle, + constructor - open a hard disk handle, given a chd_file -------------------------------------------------*/ -hard_disk_file *hard_disk_open(chd_file *chd) +hard_disk_file::hard_disk_file(chd_file *_chd) { - int cylinders, heads, sectors, sectorbytes; - hard_disk_file *file; + chd = _chd; + fhandle = nullptr; + hdinfo.fileoffset = 0; + std::string metadata; std::error_condition err; /* punt if no CHD */ if (chd == nullptr) - return nullptr; + throw nullptr; /* read the hard disk metadata */ - err = chd->read_metadata(HARD_DISK_METADATA_TAG, 0, metadata); + err = _chd->read_metadata(HARD_DISK_METADATA_TAG, 0, metadata); if (err) - return nullptr; + throw nullptr; /* parse the metadata */ - if (sscanf(metadata.c_str(), HARD_DISK_METADATA_FORMAT, &cylinders, &heads, §ors, §orbytes) != 4) - return nullptr; - - /* allocate memory for the hard disk file */ - file = (hard_disk_file *)malloc(sizeof(hard_disk_file)); - if (file == nullptr) - return nullptr; - - /* fill in the data */ - file->chd = chd; - file->fhandle = nullptr; - file->info.cylinders = cylinders; - file->info.heads = heads; - file->info.sectors = sectors; - file->info.sectorbytes = sectorbytes; - file->info.fileoffset = 0; - return file; + if (sscanf(metadata.c_str(), HARD_DISK_METADATA_FORMAT, &hdinfo.cylinders, &hdinfo.heads, &hdinfo.sectors, &hdinfo.sectorbytes) != 4) + throw nullptr; } -hard_disk_file *hard_disk_open(util::random_read_write &corefile, uint32_t skipoffs) +hard_disk_file::hard_disk_file(util::random_read_write &corefile, uint32_t skipoffs) { // bail if getting the file length fails std::uint64_t length; if (corefile.length(length)) - return nullptr; - - hard_disk_file *file; - - // allocate memory for the hard disk file - file = (hard_disk_file *)malloc(sizeof(hard_disk_file)); - if (file == nullptr) - return nullptr; + throw nullptr; - file->chd = nullptr; - file->fhandle = &corefile; - file->info.sectorbytes = 512; - file->info.cylinders = 0; - file->info.heads = 0; - file->info.sectors = 0; - file->info.fileoffset = skipoffs; + chd = nullptr; + fhandle = &corefile; + hdinfo.sectorbytes = 512; + hdinfo.cylinders = 0; + hdinfo.heads = 0; + hdinfo.sectors = 0; + hdinfo.fileoffset = skipoffs; // attempt to guess geometry in case this is an ATA situation - for (uint32_t totalsectors = (length - skipoffs) / file->info.sectorbytes; ; totalsectors++) + for (uint32_t totalsectors = (length - skipoffs) / hdinfo.sectorbytes; ; totalsectors++) for (uint32_t cursectors = 63; cursectors > 1; cursectors--) if (totalsectors % cursectors == 0) { @@ -104,146 +67,103 @@ hard_disk_file *hard_disk_open(util::random_read_write &corefile, uint32_t skipo for (uint32_t curheads = 16; curheads > 1; curheads--) if (totalheads % curheads == 0) { - file->info.cylinders = totalheads / curheads; - file->info.heads = curheads; - file->info.sectors = cursectors; - osd_printf_verbose("Guessed CHS of %d/%d/%d\n", file->info.cylinders, file->info.heads, file->info.sectors); - return file; + hdinfo.cylinders = totalheads / curheads; + hdinfo.heads = curheads; + hdinfo.sectors = cursectors; + osd_printf_verbose("Guessed CHS of %d/%d/%d\n", hdinfo.cylinders, hdinfo.heads, hdinfo.sectors); + return; } } - - return file; } /*------------------------------------------------- - hard_disk_close - close a hard disk handle + destructor - close a hard disk handle -------------------------------------------------*/ -void hard_disk_close(hard_disk_file *file) +hard_disk_file::~hard_disk_file() { - if (file->fhandle) - file->fhandle->flush(); - - free(file); + if (fhandle) + fhandle->flush(); } /*------------------------------------------------- - hard_disk_get_chd - get a handle to a CHD - from a hard disk --------------------------------------------------*/ - -chd_file *hard_disk_get_chd(hard_disk_file *file) -{ - return file->chd; -} - - -/*------------------------------------------------- - hard_disk_get_info - return information about - a hard disk --------------------------------------------------*/ - -/** - * @fn hard_disk_info *hard_disk_get_info(hard_disk_file *file) - * - * @brief Hard disk get information. - * - * @param [in,out] file The hard disk file object to operate on. - * - * @return null if it fails, else a hard_disk_info*. - */ - -hard_disk_info *hard_disk_get_info(hard_disk_file *file) -{ - return &file->info; -} - - -/*------------------------------------------------- - hard_disk_read - read sectors from a hard - disk + read - read sectors from a hard disk -------------------------------------------------*/ /** - * @fn bool hard_disk_read(hard_disk_file *file, uint32_t lbasector, void *buffer) + * @fn bool read(uint32_t lbasector, void *buffer) * * @brief Hard disk read. * - * @param [in,out] file The hard disk file object to operate on. * @param lbasector The sector number (Linear Block Address) to read. * @param buffer The buffer where the hard disk data will be placed. * * @return True if the operation succeeded */ -bool hard_disk_read(hard_disk_file *file, uint32_t lbasector, void *buffer) +bool hard_disk_file::read(uint32_t lbasector, void *buffer) { - std::error_condition err; - if (file->chd) + if (chd) { - err = file->chd->read_units(lbasector, buffer); + std::error_condition err = chd->read_units(lbasector, buffer); return !err; } else { size_t actual = 0; - err = file->fhandle->seek(file->info.fileoffset + (lbasector * file->info.sectorbytes), SEEK_SET); + std::error_condition err = fhandle->seek(hdinfo.fileoffset + (lbasector * hdinfo.sectorbytes), SEEK_SET); if (!err) - err = file->fhandle->read(buffer, file->info.sectorbytes, actual); - return !err && (actual == file->info.sectorbytes); + err = fhandle->read(buffer, hdinfo.sectorbytes, actual); + return !err && (actual == hdinfo.sectorbytes); } } /*------------------------------------------------- - hard_disk_write - write sectors to a hard - disk + write - write sectors to a hard disk -------------------------------------------------*/ /** - * @fn bool hard_disk_write(hard_disk_file *file, uint32_t lbasector, const void *buffer) + * @fn bool write(uint32_t lbasector, const void *buffer) * * @brief Hard disk write. * - * @param [in,out] file The hard disk file object to operate on. * @param lbasector The sector number (Linear Block Address) to write. * @param buffer The buffer containing the data to write. * * @return True if the operation succeeded */ -bool hard_disk_write(hard_disk_file *file, uint32_t lbasector, const void *buffer) +bool hard_disk_file::write(uint32_t lbasector, const void *buffer) { - std::error_condition err; - if (file->chd) + if (chd) { - err = file->chd->write_units(lbasector, buffer); + std::error_condition err = chd->write_units(lbasector, buffer); return !err; } else { size_t actual = 0; - err = file->fhandle->seek(file->info.fileoffset + (lbasector * file->info.sectorbytes), SEEK_SET); + std::error_condition err = fhandle->seek(hdinfo.fileoffset + (lbasector * hdinfo.sectorbytes), SEEK_SET); if (!err) - err = file->fhandle->write(buffer, file->info.sectorbytes, actual); - return !err && (actual == file->info.sectorbytes); + err = fhandle->write(buffer, hdinfo.sectorbytes, actual); + return !err && (actual == hdinfo.sectorbytes); } } /*------------------------------------------------- - hard_disk_set_block_size - sets the block size + set_block_size - sets the block size for a non-CHD-backed hard disk (a bare file). -------------------------------------------------*/ /** - * @fn bool hard_disk_set_block_size(hard_disk_file *file, uint32_t blocksize) + * @fn bool set_block_size(uint32_t blocksize) * * @brief Hard disk set block size (works only for non-CHD-files) * - * @param [in,out] file The hard_disk_file object to operate on. * @param blocksize The block size of this hard disk, in bytes. * * @return true on success, false on failure. Failure means a CHD is in use and the CHD @@ -251,12 +171,12 @@ bool hard_disk_write(hard_disk_file *file, uint32_t lbasector, const void *buffe * sizes match, success is returned). */ -bool hard_disk_set_block_size(hard_disk_file *file, uint32_t blocksize) +bool hard_disk_file::set_block_size(uint32_t blocksize) { - if (file->chd) + if (chd) { // if the CHD block size matches our block size, we're OK. - if (file->chd->unit_bytes() == blocksize) + if (chd->unit_bytes() == blocksize) { return true; } @@ -266,7 +186,7 @@ bool hard_disk_set_block_size(hard_disk_file *file, uint32_t blocksize) } else { - file->info.sectorbytes = blocksize; + hdinfo.sectorbytes = blocksize; return true; } } |