Blang/src/mkg1m/main.cpp

121 lines
2.6 KiB
C++

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <unistd.h>
#include <cstdlib>
#include "assemble.hpp"
#include "version.h"
using namespace std;
string getHelpString(){
return "\nUsage : mkg1m input-file [OPTION]\n\n"
" -o output\t\tWrite output to file.\n"
" -l lib\t\tSearch the library named library.\n"
" -L dir\t\tadd all the libraries in the folder dir.\n"
" -v\t\t\tShow version then exit\n"
" -h\t\t\tShow help then exit\n";
}
string getVersionString(){
string str;
str += "g1m-assembler-mkg1m version ";
str += MKG1M_VERSION;
str += " release ";
str += MKG1M_RELEASE;
return str;
}
vector<string> check_cLibraries(vector<string> clibList, vector<string> clibDirList){
vector<string> clib;
for(int i=0; i<clibList.size();i++){
for(int j=0; j<clibDirList.size(); j++){
string str = clibDirList[j]+"/"+clibList[i];
ifstream fichier( str.c_str() );
if(!fichier.fail()){
clib.push_back(str);
}
}
}
return clib;
}
map<string,int> load_cLibraries(vector<string> clibList){
map<string,int> dictionary;
return dictionary;
}
int main(int argc,char *argv[])
{
vector<string> outputFiles;
vector<string> inputFiles;
vector<string> CLibraries;
vector<string> CLibraryDir;
int c;
opterr = 0; // disable getopt error
while ((c = getopt (argc, argv, "o:i:l:L:vh")) != -1){
switch (c){
case 'o':
outputFiles.push_back(optarg); //get output files
break;
case 'i':
inputFiles.push_back(optarg); //get input files
break;
case 'l':
CLibraries.push_back(optarg); //get input files
break;
case 'L':
CLibraryDir.push_back(optarg); //get input files
break;
case 'v':
cout << getVersionString();
return 0;
case 'h':
cout << getHelpString();
return 0;
case '?':
if (optopt == 'o')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
return 1;
default:
return 1;
}
}
//get input files
for (int index = optind; index < argc; index++)
inputFiles.push_back(argv[index]);
if(inputFiles.empty()){
fprintf (stderr, "No input file\n");
return 1;
}
vector<string> clibs = check_cLibraries(CLibraries ,CLibraryDir);
if(clibs.empty()){
fprintf (stderr, "No cLibrary found\n");
return 1;
}
map<string,int> dictionary = load_cLibraries(clibs);
if(!build(inputFiles,outputFiles, dictionary)){
return 1;
}
return 0;
}