Skip to content

Commit

Permalink
Extract methods
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Jan 22, 2024
1 parent 30c0b7f commit 722ebc4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 38 deletions.
95 changes: 57 additions & 38 deletions cpp/s2pexec/s2pexec_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,31 +320,8 @@ string S2pExec::ExecuteCommand()

// Only send data when there is a data file and no receive buffer size has been specified
if (!data_filename.empty() && !buffer.size()) {
fstream in(data_filename, text_data ? ios::in : ios::in | ios::binary);
if (in.fail()) {
return fmt::format("Can't open data input file '{}': {}", data_filename, strerror(errno));
}

size_t size;
if (text_data) {
stringstream ss;
ss << in.rdbuf();
if (in.fail()) {
return fmt::format("Can't read from file '{}': {}", data_filename, strerror(errno));
}

for (const byte b : HexToBytes(ss.str())) {
buffer.emplace_back(static_cast<uint8_t>(b));
}
}
else {
size = file_size(path(data_filename));
buffer.resize(size);

in.read((char*)buffer.data(), size);
if (in.fail()) {
return fmt::format("Can't read from file '{}': {}", data_filename, strerror(errno));
}
if (const string &error = ReadData(); !error.empty()) {
return error;
}

spdlog::debug(fmt::format("Sending {} data bytes", buffer.size()));
Expand All @@ -356,21 +333,11 @@ string S2pExec::ExecuteCommand()
}

const int count = scsi_executor->GetByteCount();

spdlog::debug(fmt::format("Received {} data bytes", count));

if (data_filename.empty()) {
cout << FormatBytes(buffer, count) << '\n';
}
else {
fstream out(data_filename, text_data ? ios::out : ios::out | ios::binary);
if (out.fail()) {
return fmt::format("Can't open data output file '{}': {}", data_filename, strerror(errno));
}

out.write(text_data ? FormatBytes(buffer, count).data() : (const char*)buffer.data(), count);
if (out.fail()) {
return fmt::format("Can't write to file '{}': {}", data_filename, strerror(errno));
}
if (const string &error = WriteData(count); !error.empty()) {
return error;
}

return "";
Expand Down Expand Up @@ -438,6 +405,58 @@ int S2pExec::GenerateOutput(S2pExecExecutor::protobuf_format input_format, const
return EXIT_SUCCESS;
}

string S2pExec::ReadData()
{
fstream in(data_filename, text_data ? ios::in : ios::in | ios::binary);
if (in.fail()) {
return fmt::format("Can't open data input file '{}': {}", data_filename, strerror(errno));
}

size_t size;
if (text_data) {
stringstream ss;
ss << in.rdbuf();
if (in.fail()) {
return fmt::format("Can't read from file '{}': {}", data_filename, strerror(errno));
}

for (const byte b : HexToBytes(ss.str())) {
buffer.emplace_back(static_cast<uint8_t>(b));
}
}
else {
size = file_size(path(data_filename));
buffer.resize(size);

in.read((char*)buffer.data(), size);
if (in.fail()) {
return fmt::format("Can't read from file '{}': {}", data_filename, strerror(errno));
}
}

return "";
}

string S2pExec::WriteData(int count)
{
if (data_filename.empty()) {
cout << FormatBytes(buffer, count) << '\n';
}
else {
fstream out(data_filename, text_data ? ios::out : ios::out | ios::binary);
if (out.fail()) {
return fmt::format("Can't open data output file '{}': {}", data_filename, strerror(errno));
}

out.write(text_data ? FormatBytes(buffer, count).data() : (const char*)buffer.data(), count);
if (out.fail()) {
return fmt::format("Can't write to file '{}': {}", data_filename, strerror(errno));
}
}

return "";
}

bool S2pExec::SetLogLevel() const
{
const level::level_enum l = level::from_str(log_level);
Expand Down
3 changes: 3 additions & 0 deletions cpp/s2pexec/s2pexec_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class S2pExec
int GenerateOutput(S2pExecExecutor::protobuf_format, const string&, S2pExecExecutor::protobuf_format,
const string&);

string ReadData();
string WriteData(int);

bool SetLogLevel() const;

void CleanUp() const;
Expand Down

0 comments on commit 722ebc4

Please sign in to comment.