From d3cb3d82de127d26a4550588f0c7353490d708d3 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 23 Oct 2024 11:18:24 +0200 Subject: [PATCH] add io file --- src/IO.cpp | 34 ++++++++++++++++++++++++++++++++++ src/IO.h | 10 ++++++++++ src/main.cpp | 38 +------------------------------------- 3 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 src/IO.cpp create mode 100644 src/IO.h diff --git a/src/IO.cpp b/src/IO.cpp new file mode 100644 index 0000000..9aa9f9c --- /dev/null +++ b/src/IO.cpp @@ -0,0 +1,34 @@ +#include "IO.h" + +#include "Assembleur.h" +#include + +BinaryData ParseFile(const std::string& fileName) { + std::ifstream file{fileName}; + std::uint32_t lineNumber = 0, realLineNumber = 0; + std::string line; + + Assembleur assembleur; + + BinaryData output; + + while (getline(file, line)) { + lineNumber++; + realLineNumber++; + + if (line.find(":") != std::string::npos) { + std::string label = line.substr(0, line.size() - 1); + assembleur.AddLabel(label, lineNumber); + lineNumber--; + } else { + output.push_back(assembleur.ParseInstruction(line, lineNumber, realLineNumber)); + } + } + + return output; +} + +void OutputFile(const BinaryData& a_Data, const std::string& fileName) { + std::ofstream file{fileName}; + file.write(reinterpret_cast(a_Data.data()), a_Data.size() * sizeof(a_Data.at(0))); +} diff --git a/src/IO.h b/src/IO.h new file mode 100644 index 0000000..a62c236 --- /dev/null +++ b/src/IO.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +using BinaryData = std::vector; + +BinaryData ParseFile(const std::string& fileName); + +void OutputFile(const BinaryData& a_Data, const std::string& fileName); diff --git a/src/main.cpp b/src/main.cpp index 2bcb492..5b6c886 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,42 +1,6 @@ -#include #include -#include - -#include "Assembleur.h" - -using BinaryData = std::vector; - - -static BinaryData ParseFile(const std::string& fileName) { - std::ifstream file{fileName}; - std::uint32_t lineNumber = 0, realLineNumber = 0; - std::string line; - - Assembleur assembleur; - - BinaryData output; - - while (getline(file, line)) { - lineNumber++; - realLineNumber++; - - if (line.find(":") != std::string::npos) { - std::string label = line.substr(0, line.size() - 1); - assembleur.AddLabel(label, lineNumber); - lineNumber--; - } else { - output.push_back(assembleur.ParseInstruction(line, lineNumber, realLineNumber)); - } - } - - return output; -} - -static void OutputFile(const BinaryData& a_Data, const std::string& fileName) { - std::ofstream file {fileName}; - file.write(reinterpret_cast(a_Data.data()), a_Data.size() * sizeof(a_Data.at(0))); -} +#include "IO.h" int main(int argc, char** argv) {