Hello,
I'm learning C++ and all that i learn i try to put in a interpreter of a language that i'm developing, it's only to practice, but here is a thing that i don't learned already, that is how i can get a string that is inside quotes, because i want to build a copy function in my language, here is the source of my interpreter:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main( int argc, char* argv[] )
{
// Error Messages
string extension = argv[ 1 ];
if(argc != 2)
{
cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
return 0;
}
if(extension[extension.length()-3] != '.')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
if(extension[extension.length()-2] != 't')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
if(extension[extension.length()-1] != 'r')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
// End of the error messages
ifstream file(argv[ 1 ]);
if (!file.good()) {
cout << "File " << argv[ 1 ] << " does not exist.\n";
return 0;
}
string linha;
while (!file.eof())
{
getline(file, linha);
if (linha.find("print") != pos)
{
size_t idx = linha.find("\""); //find the first quote on the line
while ( idx != pos )
{
size_t idx_end = linha.find("\"",idx+1); //end of quote
string quotes;
quotes.assign(linha,idx,idx_end-idx+1);
// do not print the start and end " strings
cout << quotes.substr(1,quotes.length()-2) << endl;
//check for another quote on the same line
idx = linha.find("\"",idx_end+1);
}
}
if (linha.find("copy_input") != pos)
{
size_t idx = linha.find("\""); //find the first quote on the line
while ( idx != pos )
{
size_t idx_end = linha.find("\"",idx+1); //end of quote
string quotes;
char* inputFile;
ifstream file_input( inputFile );
quotes.assign(linha,idx,idx_end-idx+1);
Here is the code
idx = linha.find("\"",idx_end+1);
}
}
}
return 0;
}
Here is a template that i use for copying files:
#include <iostream>
#include <fstream>
using namespace std;
int main( int argc, char* argv[] )
{
char* inputFile;
char* outputFile;
inputFile = argv[ 1 ];
outputFile = argv[ 2 ];
ifstream in( inputFile );
if( in.is_open() )
{
ofstream out( outputFile );
out << in.rdbuf();
}
return 0;
}
And here is the syntax of my copy function in my language:
copy_input "The String Will Be Here"
Thanks,
Nathan Paulino Campos