+#include <iostream>
+#include <fstream>
+#include <boost/tokenizer.hpp>
+#include <boost/program_options.hpp>
+#include <boost/lexical_cast.hpp>
+#include <string>
+#include <map>
+
+#include "disasm.h"
+#include "ccpu.hpp"
+#include "CInstrFactory.hpp"
+
+using boost::lexical_cast;
+using boost::bad_lexical_cast;
+
+using namespace std;
+
+using namespace boost::program_options;
+namespace po = boost::program_options;
+
+std::string progName;
+
+int main(int argc, char* argv[])
+{
+ progName = argv[0];
+ ifstream inFile;
+ try {
+ po::options_description desc("Allowed options");
+ desc.add_options()
+ ("help,h","produce help message")
+ ("file,f",value<string>(), "input file")
+ ;
+
+ po::positional_options_description p;
+ p.add("file",1);
+
+ po::variables_map vm;
+ po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
+ po::notify(vm);
+
+ if(vm.count("help")) {
+ cout << desc << endl;
+ return EXIT_FAILURE;
+ }
+
+ if(vm.count("file")) {
+#ifdef DEBUG
+ cout << "going to open file " << vm["file"].as<string>() << endl;
+#endif
+ inFile.open(vm["file"].as<string>(), ios::in);
+ if(!inFile) {
+ cerr << "Error opening file " << vm["file"].as<string>() << endl;
+ return EXIT_FAILURE;
+ }
+ }
+ else {
+ cout << "not input file given!" << endl << endl;
+ cout << desc << endl;
+ return EXIT_FAILURE;
+ }
+ }
+ catch(std::exception& ex) {
+ cout << ex.what() << endl;
+ }
+
+
+ string dir = "./instr/";
+
+ map<short, Iinstr*> instr;
+
+ CInstrFactory instrFab;
+ try {
+ instrFab.loadLibsIntoMap(instr, dir);
+ }
+ catch(std::bad_alloc& e) {
+ cerr << progName << ": bad_alloc caught " << e.what() << endl;
+ exit(EXIT_FAILURE);
+ }
+ catch(std::string& s) {
+ cerr << progName << ": " << s << endl;
+ exit(EXIT_FAILURE);
+ }
+
+
+ CCpu cpu;
+
+
+ std::string str = "";
+ int addr = 0;
+ boost::char_separator<char> sep(";", "", boost::keep_empty_tokens);
+ boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
+ disasm disasm(instr);
+ while(getline(inFile, str)) {
+ int count = 0;
+ tokens.assign(str);
+ stringstream out;
+ int type = 0;
+ for(auto tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
+ if(tok_iter == tokens.begin()) {
+ try {
+ type = lexical_cast<int>(*tok_iter);
+ count++;
+ continue;
+ }
+ catch(bad_lexical_cast &) {
+ break;
+ }
+ cout << endl;
+ }
+ switch(type) {
+ case 2:
+ if(count == 1) {
+ cout << "; ";
+ }
+ cout << *tok_iter;
+ break;
+ case 3:
+ if((*tok_iter).size() > 0) {
+ if(count > 1) {
+ cout << endl;
+ }
+ cout << *tok_iter << ":";
+ }
+ break;
+ case 1:
+ if(count == 1) {
+ out << "[0x" << *tok_iter << "]: ";
+ addr = lexical_cast<int>(*tok_iter);
+ }
+ else if(count == 2) {
+ cpu.setProg(addr, disasm.decode(*tok_iter));
+ }
+ else if(count == 3) {
+ //code saved in hex-file
+ //cout << *tok_iter ;
+ }
+ else if(count == 4) {
+ /* label */
+ if((*tok_iter).size() > 0) {
+ cout << *tok_iter << ":" << endl;
+ }
+ cout << out.str();
+ }
+ else if(count == 5) {
+ if((*tok_iter).size() > 0) {
+ cout << " ;";
+ }
+ }
+
+ if(count >= 5) {
+ cout << *tok_iter;
+ }
+ break;
+ default:
+ cerr << "i was to lazy to implement the other format types for now" << endl;
+ }
+ count++;
+ }
+ if(type == 1 && count <= 4) {
+ cout << out.str();
+ }
+ cout << endl;
+ }
+ inFile.close();
+
+ cout << endl;
+ return EXIT_SUCCESS;
+}