add io file
This commit is contained in:
34
src/IO.cpp
Normal file
34
src/IO.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "IO.h"
|
||||
|
||||
#include "Assembleur.h"
|
||||
#include <fstream>
|
||||
|
||||
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<const char*>(a_Data.data()), a_Data.size() * sizeof(a_Data.at(0)));
|
||||
}
|
||||
10
src/IO.h
Normal file
10
src/IO.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using BinaryData = std::vector<std::uint32_t>;
|
||||
|
||||
BinaryData ParseFile(const std::string& fileName);
|
||||
|
||||
void OutputFile(const BinaryData& a_Data, const std::string& fileName);
|
||||
38
src/main.cpp
38
src/main.cpp
@@ -1,42 +1,6 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "Assembleur.h"
|
||||
|
||||
using BinaryData = std::vector<std::uint32_t>;
|
||||
|
||||
|
||||
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<const char*>(a_Data.data()), a_Data.size() * sizeof(a_Data.at(0)));
|
||||
}
|
||||
|
||||
#include "IO.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user