Blang/src/mkg1m/assemble.cpp

81 lines
1.8 KiB
C++

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstdio>
#include <fstream>
using namespace std;
string header(unsigned int content_size){
char head[32] = {0xAF, 0x90, 0x88, 0x9A, 0x8D, 0xAA, 0xAC, 0xBD,0x31, 0x00, 0x10, 0x00, 0x10, 0x00, (content_size& 1) + 0x41,
0x01, content_size >>24, content_size >>16 , content_size >>8, content_size , (content_size& 1) + 0xB8};
FILE* fichier = NULL;
fichier = fopen("test.hexadecimal", "wb+");
for(int i = 0 ; i<32; i++)
fputc(head[i], fichier);
fclose(fichier);
return head;
}
string intToString(int i) {
string str;
char x;
int n=0;
while((x = (i >> (8*n)) & 0xff) != 0){
n++;
str.insert(0,1,x);
}
return str;
}
string replaceAll(std::string str, const std::string& from, const std::string& to) {
if(from.empty())
return str;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
}
bool build(vector<string> input, vector<string> output, map<string,int> dictionary){
map<string, string> inputContent;
for(int i=0; i<input.size(); i++){
ifstream file(input[i].c_str());
if(file){
string line;
string content;
while(getline(file, line)){
content+= line+"\n";
}
inputContent[input[i].substr(0,8)] = content;
}else{
fprintf (stderr, "%s:No such file\n", input[i].c_str());
return false;
}
}
map<string, string> outputContent;
for(map<string,int>::iterator it = dictionary.begin(); it != dictionary.end(); ++it){
for(map<string,string>::iterator ite = inputContent.begin(); ite != inputContent.end(); ++ite){
replaceAll(ite->second,it->first, intToString(it->second) );
}
}
}