Here's the code:
There's a segmentation fault in the extractToken(buffer&) member function (buffer.h) and for the love of god I can't figure out why
Plz help
/*C++toSB v0.0.1 by Nyap
*C++ to SmileBASIC compiler
*/
#define VERSTRING "0.0.1"
#include "declerations.h"
#include <cstdlib>
#include <fstream>
int main(int argc, char **argv)
{
argv[1] = "lol.txt";
//Print intro
cout << "CPPtoSB " << VERSTRING << " by Nyap\n"
<< "C++ to SmileBASIC compiler\n"
<< "This build was compiled on " << __DATE__ << " at " << __TIME__ << '\n'
<< "#Checking compilation conditions...\n";
//Check if filename argument was passed
if (!argv[1])
{
cerr << "#No filename argument detected! Aborting!\n";
exit(-1);
}
//Make input stream for input file
ifstream input(argv[1]);
//Make output stream for output file
ofstream output("output.txt");
//Check if the input filename even exists
if (!input.is_open())
{
cerr << "#No file of name " << argv[1] << " found.\n";
exit(-1);
}
//Print message to say everything went ok
cout << "#Everything OK!\n";
//Call the compiler
compile(input, output);
//End the program
return 0;
}
*C++ to SmileBASIC compiler
*/
#define VERSTRING "0.0.1"
#include "declerations.h"
#include <cstdlib>
#include <fstream>
int main(int argc, char **argv)
{
argv[1] = "lol.txt";
//Print intro
cout << "CPPtoSB " << VERSTRING << " by Nyap\n"
<< "C++ to SmileBASIC compiler\n"
<< "This build was compiled on " << __DATE__ << " at " << __TIME__ << '\n'
<< "#Checking compilation conditions...\n";
//Check if filename argument was passed
if (!argv[1])
{
cerr << "#No filename argument detected! Aborting!\n";
exit(-1);
}
//Make input stream for input file
ifstream input(argv[1]);
//Make output stream for output file
ofstream output("output.txt");
//Check if the input filename even exists
if (!input.is_open())
{
cerr << "#No file of name " << argv[1] << " found.\n";
exit(-1);
}
//Print message to say everything went ok
cout << "#Everything OK!\n";
//Call the compiler
compile(input, output);
//End the program
return 0;
}
#include "declerations.h"
#include "buffer.h"
void compile(ifstream& input, ofstream& output)
{
cout << "#Compiler called\n";
buffer line{"Hello World!"};
buffer token;
line.extractToken(token);
cout << token;
}
#include "buffer.h"
void compile(ifstream& input, ofstream& output)
{
cout << "#Compiler called\n";
buffer line{"Hello World!"};
buffer token;
line.extractToken(token);
cout << token;
}
#ifndef DECLERATIONS_H
#define DECLERATIONS_H
#include <fstream>
#include <iostream>
#include <sstream>
using std::cout;
using std::cerr;
using std::cin;
using std::ios;
using std::ifstream;
using std::ofstream;
using std::string;
using std::getline;
using std::stringstream;
void compile(ifstream&, ofstream&);
#endif
#define DECLERATIONS_H
#include <fstream>
#include <iostream>
#include <sstream>
using std::cout;
using std::cerr;
using std::cin;
using std::ios;
using std::ifstream;
using std::ofstream;
using std::string;
using std::getline;
using std::stringstream;
void compile(ifstream&, ofstream&);
#endif
class buffer
{
protected:
int bufferSize{};
char* Buffer;
public:
buffer() {}
buffer(char *stringvalue) : Buffer(stringvalue)
{
for (int i{}; Buffer!='\0'; ++i)
++bufferSize;
}
void extractToken(char* target, int targetsize=-1)
{
if (targetsize<0)
cout << "#extractToken() - Target size unkown!";
if (bufferSize==0)
{
cout << "#extractToken() - Nothing in buffer to extract!";
return;
}
int tokenSize;
for (int i{}; Buffer!='\0' && Buffer!=' '; ++i)
++tokenSize;
if ((targetsize>-1 && targetsize>tokenSize) || targetsize<0)
{
for (int i{}; i<=tokenSize; ++i)
target=Buffer;
}
else
{
cout << "#extractToken() - Target too small";
}
}
void extractToken(buffer& target)
{
if (bufferSize==0)
{
cout << "#extractToken() - Nothing in buffer to extract!";
return;
}
target.bufferSize=0;
for (int i{}; Buffer!='\0' && Buffer!=' '; ++i)
{
target.Buffer=Buffer;
++target.bufferSize;
}
}
int size() {return bufferSize;}
char* get() {return Buffer;}
char get(int element) {return Buffer[element];}
buffer operator=(char*);
friend std::ostream&::operator<<(std::ostream&, buffer&);
};
buffer buffer::operator=(char* stringvalue)
{
Buffer=stringvalue;
bufferSize=0;
for (int i{}; Buffer!='\0'; ++i)
++bufferSize;
return *this;
}
std::ostream& operator<<(std::ostream& out, buffer& output)
{
out << output.Buffer;
return out;
}
{
protected:
int bufferSize{};
char* Buffer;
public:
buffer() {}
buffer(char *stringvalue) : Buffer(stringvalue)
{
for (int i{}; Buffer!='\0'; ++i)
++bufferSize;
}
void extractToken(char* target, int targetsize=-1)
{
if (targetsize<0)
cout << "#extractToken() - Target size unkown!";
if (bufferSize==0)
{
cout << "#extractToken() - Nothing in buffer to extract!";
return;
}
int tokenSize;
for (int i{}; Buffer!='\0' && Buffer!=' '; ++i)
++tokenSize;
if ((targetsize>-1 && targetsize>tokenSize) || targetsize<0)
{
for (int i{}; i<=tokenSize; ++i)
target=Buffer;
}
else
{
cout << "#extractToken() - Target too small";
}
}
void extractToken(buffer& target)
{
if (bufferSize==0)
{
cout << "#extractToken() - Nothing in buffer to extract!";
return;
}
target.bufferSize=0;
for (int i{}; Buffer!='\0' && Buffer!=' '; ++i)
{
target.Buffer=Buffer;
++target.bufferSize;
}
}
int size() {return bufferSize;}
char* get() {return Buffer;}
char get(int element) {return Buffer[element];}
buffer operator=(char*);
friend std::ostream&::operator<<(std::ostream&, buffer&);
};
buffer buffer::operator=(char* stringvalue)
{
Buffer=stringvalue;
bufferSize=0;
for (int i{}; Buffer!='\0'; ++i)
++bufferSize;
return *this;
}
std::ostream& operator<<(std::ostream& out, buffer& output)
{
out << output.Buffer;
return out;
}
Plz help