Skip to content

Commit

Permalink
Update SIMH file error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Dec 10, 2024
1 parent 6e85484 commit 00ee74d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions cpp/shared/simh_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,22 @@ uint32_t simh_util::Pad(int length)
return length + (length % 2 ? 1 : 0);
}

void simh_util::WriteFilemark(ostream &file)
bool simh_util::WriteFilemark(ostream &file)
{
const array<uint8_t, 4> &filemark = { 0, 0, 0, 0 };
file.write((const char*)filemark.data(), filemark.size());
return file.good();
}

void simh_util::WriteGoodData(ostream &file, span<const uint8_t> data, int length)
bool simh_util::WriteGoodData(ostream &file, span<const uint8_t> data, int length)
{
const array<uint8_t, 4> good_data = { static_cast<uint8_t>(length & 0xff),
static_cast<uint8_t>((length >> 8) & 0xff), static_cast<uint8_t>((length >> 16) & 0xff),
static_cast<uint8_t>((length >> 24) & 0xff) };
file.write((const char*)good_data.data(), good_data.size());
file.write((const char*)data.data(), length);
file.write((const char*)good_data.data(), good_data.size());
return file.good();
}

simh_util::SimhMetaData simh_util::FromLittleEndian(span<const uint8_t> value)
Expand Down
4 changes: 2 additions & 2 deletions cpp/shared/simh_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ bool IsRecord(const SimhMetaData&);

uint32_t Pad(int);

void WriteFilemark(ostream&);
void WriteGoodData(ostream&, span<const uint8_t>, int);
bool WriteFilemark(ostream&);
bool WriteGoodData(ostream&, span<const uint8_t>, int);

SimhMetaData FromLittleEndian(span<const uint8_t>);
array<uint8_t, 4> ToLittleEndian(const SimhMetaData&);
Expand Down
4 changes: 2 additions & 2 deletions cpp/test/simh_util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ TEST(SimhUtilTest, WriteFilemark)
const string &filename = CreateTempFile();
fstream file(filename);

WriteFilemark(file);
EXPECT_TRUE(WriteFilemark(file));
file.flush();

EXPECT_EQ(4, file_size(filename));
Expand All @@ -96,7 +96,7 @@ TEST(SimhUtilTest, WriteGoodData)
fstream file(filename);

vector<uint8_t> data = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
WriteGoodData(file, data, 8);
EXPECT_TRUE(WriteGoodData(file, data, 8));
file.flush();

EXPECT_EQ(16, file_size(filename));
Expand Down

0 comments on commit 00ee74d

Please sign in to comment.