//
// parser.h
//
// Created by Taylor Shuler on 4/8/15.
//
//
#ifndef _parser
#define _parser
#include <fstream>
#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class Parser {
public:
// constructors
/// default
Parser();
/// copy
Parser(const Parser &p);
/// custom
Parser(string filename);
// destructor
~Parser();
// public members
string getFilename();
list<string> getContent();
// operators
Parser& operator=(const Parser &p);
Parser& operator-=(const Parser &p);
Parser& operator+=(const Parser &p);
bool operator==(const Parser &p) const;
bool operator!=(const Parser &p) const;
bool operator<(const Parser &p) const;
bool operator>(const Parser &p) const;
bool operator<=(const Parser &p) const;
bool operator>=(const Parser &p) const;
friend Parser operator+(const Parser &p1, const Parser &p2);
friend Parser operator-(const Parser &p1, const Parser &p2);
friend ostream& operator<<(ostream &outstream, const Parser &p);
//friend istream& operator>>(istream &instream, const Parser &p);
protected:
// shared members
void allocFilename(string filename, bool freeMemory = false);
private:
// attributes
char* filename;
list<string> content;
};
#endif /* defined(_parser) */
//
// parser.cpp
//
// Created by Taylor Shuler on 4/8/15.
//
//
#include "parser.h"
void panic(string msg) {
cout << "ERROR: " << msg << endl;
exit(EXIT_FAILURE);
}
void Parser::allocFilename(string Filename, bool freeMemory) {
if (freeMemory) {
delete[] filename;
}
filename = new char[Filename.size()];
strcpy(filename, Filename.c_str());
}
Parser::Parser() {
allocFilename("");
}
Parser::Parser(string Filename) {
allocFilename(Filename);
ifstream file(Filename);
if (file) {
// we have one
string line;
while (getline(file, line)) {
content.push_back(line);
}
} else {
// we don't have one
panic("Could not find the file " + Filename + "!");
}
}
Parser::Parser(const Parser &p) {
// set the filename
allocFilename(p.filename, true);
// clear the destination for the text
content.clear();
// copy p's content over
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
}
Parser::~Parser() {
delete[] filename;
content.clear();
}
string Parser::getFilename() {
return filename;
}
list<string> Parser::getContent() {
return content;
}
Parser& Parser::operator=(const Parser &p) {
if (this == &p) {
return *this;
}
// set the new filename
allocFilename(p.filename);
// make room for the new content
content.clear();
// add in p's content
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
Parser& Parser::operator-=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.remove(*it);
}
return *this;
}
Parser& Parser::operator+=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
bool Parser::operator==(const Parser &p) const {
return content == p.content;
}
bool Parser::operator!=(const Parser &p) const {
return content != p.content;
}
bool Parser::operator<(const Parser &p) const {
return content < p.content;
}
bool Parser::operator>(const Parser &p) const {
return content > p.content;
}
bool Parser::operator<=(const Parser &p) const {
return content <= p.content;
}
bool Parser::operator>=(const Parser &p) const {
return content >= p.content;
}
Parser operator+(const Parser &p1, const Parser &p2) {
Parser sum(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
sum.content.push_back(*it);
}
return sum;
}
Parser operator-(const Parser &p1, const Parser &p2) {
Parser diff(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
diff.content.remove(*it);
}
return diff;
}
ostream& operator<<(ostream &outstream, const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
outstream << *it << endl;
}
return outstream;
}
//
// main.cpp
// parser
//
// Created by Taylor Shuler on 4/8/15.
//
//
#include "parser.h"
int main() {
Parser p1("test.txt"), p2("test2.txt"), p3, hr("hr.txt");
p3 = p1 + p2;
p3 -= hr;
cout << p3;
}
EDIT Here's my final code created with the help of the critique from @glampert and @Snowbody!
FileIO.h
#ifndef FILEIO
#define FILEIO
#include <list>
#include <string>
using std::list;
using std::ostream;
using std::string;
class FileIO {
public:
// constructors
/// default
FileIO();
/// copy
FileIO(const FileIO &io);
/// custom
FileIO(string filename);
// NOTE: Destructor not needed since the class attributes are terminated when they leave the scope
// accessors
string getFilename() const;
list<string> getContent() const;
// NOTE: Mutators not needed since their purpose is fulfilled by the custom constructor
// operators
FileIO& operator=(const FileIO &io);
FileIO& operator+=(const FileIO &io);
FileIO& operator-=(const FileIO &io);
friend FileIO operator+(const FileIO &io1, const FileIO &io2);
friend FileIO operator-(const FileIO &io1, const FileIO &io2);
/// equality
bool operator<(const FileIO &io) const;
bool operator>(const FileIO &io) const;
bool operator<=(const FileIO &io) const;
bool operator>=(const FileIO &io) const;
bool operator==(const FileIO &io) const;
bool operator!=(const FileIO &io) const;
friend ostream& operator<<(ostream& outstream, const FileIO &io);
// member functions
/// binds a new file to a FileIO object
void setFile(string filename);
/// writes the contents of the object to a file
void write();
private:
// error handling
void panic(const string &msg);
// attributes
string filename;
list<string> content;
};
#endif /* defined(FILEIO) */
FileIO.cpp
#include "FileIO.h"
#include <fstream>
#include <iostream>
using std::endl;
using std::ifstream;
using std::ofstream;
using std::runtime_error;
FileIO::FileIO() : filename("null") {}
FileIO::FileIO(const FileIO &io) {
setFile(io.getFilename());
}
FileIO::FileIO(string Filename) {
setFile(Filename);
}
string FileIO::getFilename() const {
return filename;
}
list<string> FileIO::getContent() const {
return content;
}
FileIO& FileIO::operator=(const FileIO &io) {
if (this == &io) {
return *this;
}
// set the filename
filename = io.getFilename();
// create a deep copy of io's content
for (const auto &line : io.getContent()) {
content.push_back(line);
}
return *this;
}
FileIO& FileIO::operator+=(const FileIO &io) {
for (const auto &line : io.getContent()) {
content.push_back(line);
}
}
FileIO& FileIO::operator-=(const FileIO &io) {
for (const auto &line : io.getContent()) {
content.remove(line);
}
}
FileIO operator+(const FileIO &io1, const FileIO &io2) {
FileIO sum(io1);
for (const auto &line : io2.getContent()) {
sum.content.push_back(line);
}
return sum;
}
FileIO operator-(const FileIO &io1, const FileIO &io2) {
FileIO diff(io1);
for (const auto &line : io2.getContent()) {
diff.content.remove(line);
}
return diff;
}
bool FileIO::operator<(const FileIO &io) const {
return content < io.content;
}
bool FileIO::operator>(const FileIO &io) const {
return content > io.content;
}
bool FileIO::operator<=(const FileIO &io) const {
return content <= io.content;
}
bool FileIO::operator>=(const FileIO &io) const {
return content >= io.content;
}
bool FileIO::operator==(const FileIO &io) const {
return content == io.content;
}
bool FileIO::operator!=(const FileIO &io) const {
return content != io.content;
}
ostream& operator<<(ostream &outstream, const FileIO &io) {
for (const auto &line : io.getContent()) {
outstream << line << endl;
}
}
void FileIO::setFile(string Filename) {
// set the filename
filename = Filename;
ifstream in(filename);
if (in) {
// we have a file
string line;
while (getline(in, line)) {
content.push_back(line);
}
} else {
// we don't
panic("No file exists with the name " + filename + "!");
}
}
void FileIO::write() {
ofstream out(filename);
for (const auto &line : getContent()) {
out << line << endl;
}
}
void FileIO::panic(const string &msg) {
throw runtime_error("ERROR: " + msg + "\n");
}
#ifndef _parser
#define _parser
#include <fstream>
#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class Parser {
public:
// constructors
/// default
Parser();
/// copy
Parser(const Parser &p);
/// custom
Parser(string filename);
// destructor
~Parser();
// public members
string getFilename();
list<string> getContent();
// operators
Parser& operator=(const Parser &p);
Parser& operator-=(const Parser &p);
Parser& operator+=(const Parser &p);
bool operator==(const Parser &p) const;
bool operator!=(const Parser &p) const;
bool operator<(const Parser &p) const;
bool operator>(const Parser &p) const;
bool operator<=(const Parser &p) const;
bool operator>=(const Parser &p) const;
friend Parser operator+(const Parser &p1, const Parser &p2);
friend Parser operator-(const Parser &p1, const Parser &p2);
friend ostream& operator<<(ostream &outstream, const Parser &p);
//friend istream& operator>>(istream &instream, const Parser &p);
protected:
// shared members
void allocFilename(string filename, bool freeMemory = false);
private:
// attributes
char* filename;
list<string> content;
};
#endif /* defined(_parser) */
#include "parser.h"
void panic(string msg) {
cout << "ERROR: " << msg << endl;
exit(EXIT_FAILURE);
}
void Parser::allocFilename(string Filename, bool freeMemory) {
if (freeMemory) {
delete[] filename;
}
filename = new char[Filename.size()];
strcpy(filename, Filename.c_str());
}
Parser::Parser() {
allocFilename("");
}
Parser::Parser(string Filename) {
allocFilename(Filename);
ifstream file(Filename);
if (file) {
// we have one
string line;
while (getline(file, line)) {
content.push_back(line);
}
} else {
// we don't have one
panic("Could not find the file " + Filename + "!");
}
}
Parser::Parser(const Parser &p) {
// set the filename
allocFilename(p.filename, true);
// clear the destination for the text
content.clear();
// copy p's content over
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
}
Parser::~Parser() {
delete[] filename;
content.clear();
}
string Parser::getFilename() {
return filename;
}
list<string> Parser::getContent() {
return content;
}
Parser& Parser::operator=(const Parser &p) {
if (this == &p) {
return *this;
}
// set the new filename
allocFilename(p.filename);
// make room for the new content
content.clear();
// add in p's content
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
Parser& Parser::operator-=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.remove(*it);
}
return *this;
}
Parser& Parser::operator+=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
bool Parser::operator==(const Parser &p) const {
return content == p.content;
}
bool Parser::operator!=(const Parser &p) const {
return content != p.content;
}
bool Parser::operator<(const Parser &p) const {
return content < p.content;
}
bool Parser::operator>(const Parser &p) const {
return content > p.content;
}
bool Parser::operator<=(const Parser &p) const {
return content <= p.content;
}
bool Parser::operator>=(const Parser &p) const {
return content >= p.content;
}
Parser operator+(const Parser &p1, const Parser &p2) {
Parser sum(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
sum.content.push_back(*it);
}
return sum;
}
Parser operator-(const Parser &p1, const Parser &p2) {
Parser diff(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
diff.content.remove(*it);
}
return diff;
}
ostream& operator<<(ostream &outstream, const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
outstream << *it << endl;
}
return outstream;
}
#include "parser.h"
int main() {
Parser p1("test.txt"), p2("test2.txt"), p3, hr("hr.txt");
p3 = p1 + p2;
p3 -= hr;
cout << p3;
}
EDIT Here's my final code created with the help of the critique from @glampert and @Snowbody!
FileIO.h
#ifndef FILEIO
#define FILEIO
#include <list>
#include <string>
using std::list;
using std::ostream;
using std::string;
class FileIO {
public:
// constructors
/// default
FileIO();
/// copy
FileIO(const FileIO &io);
/// custom
FileIO(string filename);
// NOTE: Destructor not needed since the class attributes are terminated when they leave the scope
// accessors
string getFilename() const;
list<string> getContent() const;
// NOTE: Mutators not needed since their purpose is fulfilled by the custom constructor
// operators
FileIO& operator=(const FileIO &io);
FileIO& operator+=(const FileIO &io);
FileIO& operator-=(const FileIO &io);
friend FileIO operator+(const FileIO &io1, const FileIO &io2);
friend FileIO operator-(const FileIO &io1, const FileIO &io2);
/// equality
bool operator<(const FileIO &io) const;
bool operator>(const FileIO &io) const;
bool operator<=(const FileIO &io) const;
bool operator>=(const FileIO &io) const;
bool operator==(const FileIO &io) const;
bool operator!=(const FileIO &io) const;
friend ostream& operator<<(ostream& outstream, const FileIO &io);
// member functions
/// binds a new file to a FileIO object
void setFile(string filename);
/// writes the contents of the object to a file
void write();
private:
// error handling
void panic(const string &msg);
// attributes
string filename;
list<string> content;
};
#endif /* defined(FILEIO) */
FileIO.cpp
#include "FileIO.h"
#include <fstream>
#include <iostream>
using std::endl;
using std::ifstream;
using std::ofstream;
using std::runtime_error;
FileIO::FileIO() : filename("null") {}
FileIO::FileIO(const FileIO &io) {
setFile(io.getFilename());
}
FileIO::FileIO(string Filename) {
setFile(Filename);
}
string FileIO::getFilename() const {
return filename;
}
list<string> FileIO::getContent() const {
return content;
}
FileIO& FileIO::operator=(const FileIO &io) {
if (this == &io) {
return *this;
}
// set the filename
filename = io.getFilename();
// create a deep copy of io's content
for (const auto &line : io.getContent()) {
content.push_back(line);
}
return *this;
}
FileIO& FileIO::operator+=(const FileIO &io) {
for (const auto &line : io.getContent()) {
content.push_back(line);
}
}
FileIO& FileIO::operator-=(const FileIO &io) {
for (const auto &line : io.getContent()) {
content.remove(line);
}
}
FileIO operator+(const FileIO &io1, const FileIO &io2) {
FileIO sum(io1);
for (const auto &line : io2.getContent()) {
sum.content.push_back(line);
}
return sum;
}
FileIO operator-(const FileIO &io1, const FileIO &io2) {
FileIO diff(io1);
for (const auto &line : io2.getContent()) {
diff.content.remove(line);
}
return diff;
}
bool FileIO::operator<(const FileIO &io) const {
return content < io.content;
}
bool FileIO::operator>(const FileIO &io) const {
return content > io.content;
}
bool FileIO::operator<=(const FileIO &io) const {
return content <= io.content;
}
bool FileIO::operator>=(const FileIO &io) const {
return content >= io.content;
}
bool FileIO::operator==(const FileIO &io) const {
return content == io.content;
}
bool FileIO::operator!=(const FileIO &io) const {
return content != io.content;
}
ostream& operator<<(ostream &outstream, const FileIO &io) {
for (const auto &line : io.getContent()) {
outstream << line << endl;
}
}
void FileIO::setFile(string Filename) {
// set the filename
filename = Filename;
ifstream in(filename);
if (in) {
// we have a file
string line;
while (getline(in, line)) {
content.push_back(line);
}
} else {
// we don't
panic("No file exists with the name " + filename + "!");
}
}
void FileIO::write() {
ofstream out(filename);
for (const auto &line : getContent()) {
out << line << endl;
}
}
void FileIO::panic(const string &msg) {
throw runtime_error("ERROR: " + msg + "\n");
}
//
// parser.h
//
// Created by Taylor Shuler on 4/8/15.
//
//
#ifndef _parser
#define _parser
#include <fstream>
#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class Parser {
public:
// constructors
/// default
Parser();
/// copy
Parser(const Parser &p);
/// custom
Parser(string filename);
// destructor
~Parser();
// public members
string getFilename();
list<string> getContent();
// operators
Parser& operator=(const Parser &p);
Parser& operator-=(const Parser &p);
Parser& operator+=(const Parser &p);
bool operator==(const Parser &p) const;
bool operator!=(const Parser &p) const;
bool operator<(const Parser &p) const;
bool operator>(const Parser &p) const;
bool operator<=(const Parser &p) const;
bool operator>=(const Parser &p) const;
friend Parser operator+(const Parser &p1, const Parser &p2);
friend Parser operator-(const Parser &p1, const Parser &p2);
friend ostream& operator<<(ostream &outstream, const Parser &p);
//friend istream& operator>>(istream &instream, const Parser &p);
protected:
// shared members
void allocFilename(string filename, bool freeMemory = false);
private:
// attributes
char* filename;
list<string> content;
};
#endif /* defined(_parser) */
//
// parser.cpp
//
// Created by Taylor Shuler on 4/8/15.
//
//
#include "parser.h"
void panic(string msg) {
cout << "ERROR: " << msg << endl;
exit(EXIT_FAILURE);
}
void Parser::allocFilename(string Filename, bool freeMemory) {
if (freeMemory) {
delete[] filename;
}
filename = new char[Filename.size()];
strcpy(filename, Filename.c_str());
}
Parser::Parser() {
allocFilename("");
}
Parser::Parser(string Filename) {
allocFilename(Filename);
ifstream file(Filename);
if (file) {
// we have one
string line;
while (getline(file, line)) {
content.push_back(line);
}
} else {
// we don't have one
panic("Could not find the file " + Filename + "!");
}
}
Parser::Parser(const Parser &p) {
// set the filename
allocFilename(p.filename, true);
// clear the destination for the text
content.clear();
// copy p's content over
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
}
Parser::~Parser() {
delete[] filename;
content.clear();
}
string Parser::getFilename() {
return filename;
}
list<string> Parser::getContent() {
return content;
}
Parser& Parser::operator=(const Parser &p) {
if (this == &p) {
return *this;
}
// set the new filename
allocFilename(p.filename);
// make room for the new content
content.clear();
// add in p's content
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
Parser& Parser::operator-=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.remove(*it);
}
return *this;
}
Parser& Parser::operator+=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
bool Parser::operator==(const Parser &p) const {
return content == p.content;
}
bool Parser::operator!=(const Parser &p) const {
return content != p.content;
}
bool Parser::operator<(const Parser &p) const {
return content < p.content;
}
bool Parser::operator>(const Parser &p) const {
return content > p.content;
}
bool Parser::operator<=(const Parser &p) const {
return content <= p.content;
}
bool Parser::operator>=(const Parser &p) const {
return content >= p.content;
}
Parser operator+(const Parser &p1, const Parser &p2) {
Parser sum(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
sum.content.push_back(*it);
}
return sum;
}
Parser operator-(const Parser &p1, const Parser &p2) {
Parser diff(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
diff.content.remove(*it);
}
return diff;
}
ostream& operator<<(ostream &outstream, const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
outstream << *it << endl;
}
return outstream;
}
//
// main.cpp
// parser
//
// Created by Taylor Shuler on 4/8/15.
//
//
#include "parser.h"
int main() {
Parser p1("test.txt"), p2("test2.txt"), p3, hr("hr.txt");
p3 = p1 + p2;
p3 -= hr;
cout << p3;
}
//
// parser.h
//
// Created by Taylor Shuler on 4/8/15.
//
//
#ifndef _parser
#define _parser
#include <fstream>
#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class Parser {
public:
// constructors
/// default
Parser();
/// copy
Parser(const Parser &p);
/// custom
Parser(string filename);
// destructor
~Parser();
// public members
string getFilename();
list<string> getContent();
// operators
Parser& operator=(const Parser &p);
Parser& operator-=(const Parser &p);
Parser& operator+=(const Parser &p);
bool operator==(const Parser &p) const;
bool operator!=(const Parser &p) const;
bool operator<(const Parser &p) const;
bool operator>(const Parser &p) const;
bool operator<=(const Parser &p) const;
bool operator>=(const Parser &p) const;
friend Parser operator+(const Parser &p1, const Parser &p2);
friend Parser operator-(const Parser &p1, const Parser &p2);
friend ostream& operator<<(ostream &outstream, const Parser &p);
//friend istream& operator>>(istream &instream, const Parser &p);
protected:
// shared members
void allocFilename(string filename, bool freeMemory = false);
private:
// attributes
char* filename;
list<string> content;
};
#endif /* defined(_parser) */
//
// parser.cpp
//
// Created by Taylor Shuler on 4/8/15.
//
//
#include "parser.h"
void panic(string msg) {
cout << "ERROR: " << msg << endl;
exit(EXIT_FAILURE);
}
void Parser::allocFilename(string Filename, bool freeMemory) {
if (freeMemory) {
delete[] filename;
}
filename = new char[Filename.size()];
strcpy(filename, Filename.c_str());
}
Parser::Parser() {
allocFilename("");
}
Parser::Parser(string Filename) {
allocFilename(Filename);
ifstream file(Filename);
if (file) {
// we have one
string line;
while (getline(file, line)) {
content.push_back(line);
}
} else {
// we don't have one
panic("Could not find the file " + Filename + "!");
}
}
Parser::Parser(const Parser &p) {
// set the filename
allocFilename(p.filename, true);
// clear the destination for the text
content.clear();
// copy p's content over
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
}
Parser::~Parser() {
delete[] filename;
content.clear();
}
string Parser::getFilename() {
return filename;
}
list<string> Parser::getContent() {
return content;
}
Parser& Parser::operator=(const Parser &p) {
if (this == &p) {
return *this;
}
// set the new filename
allocFilename(p.filename);
// make room for the new content
content.clear();
// add in p's content
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
Parser& Parser::operator-=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.remove(*it);
}
return *this;
}
Parser& Parser::operator+=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
bool Parser::operator==(const Parser &p) const {
return content == p.content;
}
bool Parser::operator!=(const Parser &p) const {
return content != p.content;
}
bool Parser::operator<(const Parser &p) const {
return content < p.content;
}
bool Parser::operator>(const Parser &p) const {
return content > p.content;
}
bool Parser::operator<=(const Parser &p) const {
return content <= p.content;
}
bool Parser::operator>=(const Parser &p) const {
return content >= p.content;
}
Parser operator+(const Parser &p1, const Parser &p2) {
Parser sum(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
sum.content.push_back(*it);
}
return sum;
}
Parser operator-(const Parser &p1, const Parser &p2) {
Parser diff(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
diff.content.remove(*it);
}
return diff;
}
ostream& operator<<(ostream &outstream, const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
outstream << *it << endl;
}
return outstream;
}
//
// main.cpp
// parser
//
// Created by Taylor Shuler on 4/8/15.
//
//
#include "parser.h"
int main() {
Parser p1("test.txt"), p2("test2.txt"), p3, hr("hr.txt");
p3 = p1 + p2;
p3 -= hr;
cout << p3;
}
EDIT Here's my final code created with the help of the critique from @glampert and @Snowbody!
FileIO.h
#ifndef FILEIO
#define FILEIO
#include <list>
#include <string>
using std::list;
using std::ostream;
using std::string;
class FileIO {
public:
// constructors
/// default
FileIO();
/// copy
FileIO(const FileIO &io);
/// custom
FileIO(string filename);
// NOTE: Destructor not needed since the class attributes are terminated when they leave the scope
// accessors
string getFilename() const;
list<string> getContent() const;
// NOTE: Mutators not needed since their purpose is fulfilled by the custom constructor
// operators
FileIO& operator=(const FileIO &io);
FileIO& operator+=(const FileIO &io);
FileIO& operator-=(const FileIO &io);
friend FileIO operator+(const FileIO &io1, const FileIO &io2);
friend FileIO operator-(const FileIO &io1, const FileIO &io2);
/// equality
bool operator<(const FileIO &io) const;
bool operator>(const FileIO &io) const;
bool operator<=(const FileIO &io) const;
bool operator>=(const FileIO &io) const;
bool operator==(const FileIO &io) const;
bool operator!=(const FileIO &io) const;
friend ostream& operator<<(ostream& outstream, const FileIO &io);
// member functions
/// binds a new file to a FileIO object
void setFile(string filename);
/// writes the contents of the object to a file
void write();
private:
// error handling
void panic(const string &msg);
// attributes
string filename;
list<string> content;
};
#endif /* defined(FILEIO) */
FileIO.cpp
#include "FileIO.h"
#include <fstream>
#include <iostream>
using std::endl;
using std::ifstream;
using std::ofstream;
using std::runtime_error;
FileIO::FileIO() : filename("null") {}
FileIO::FileIO(const FileIO &io) {
setFile(io.getFilename());
}
FileIO::FileIO(string Filename) {
setFile(Filename);
}
string FileIO::getFilename() const {
return filename;
}
list<string> FileIO::getContent() const {
return content;
}
FileIO& FileIO::operator=(const FileIO &io) {
if (this == &io) {
return *this;
}
// set the filename
filename = io.getFilename();
// create a deep copy of io's content
for (const auto &line : io.getContent()) {
content.push_back(line);
}
return *this;
}
FileIO& FileIO::operator+=(const FileIO &io) {
for (const auto &line : io.getContent()) {
content.push_back(line);
}
}
FileIO& FileIO::operator-=(const FileIO &io) {
for (const auto &line : io.getContent()) {
content.remove(line);
}
}
FileIO operator+(const FileIO &io1, const FileIO &io2) {
FileIO sum(io1);
for (const auto &line : io2.getContent()) {
sum.content.push_back(line);
}
return sum;
}
FileIO operator-(const FileIO &io1, const FileIO &io2) {
FileIO diff(io1);
for (const auto &line : io2.getContent()) {
diff.content.remove(line);
}
return diff;
}
bool FileIO::operator<(const FileIO &io) const {
return content < io.content;
}
bool FileIO::operator>(const FileIO &io) const {
return content > io.content;
}
bool FileIO::operator<=(const FileIO &io) const {
return content <= io.content;
}
bool FileIO::operator>=(const FileIO &io) const {
return content >= io.content;
}
bool FileIO::operator==(const FileIO &io) const {
return content == io.content;
}
bool FileIO::operator!=(const FileIO &io) const {
return content != io.content;
}
ostream& operator<<(ostream &outstream, const FileIO &io) {
for (const auto &line : io.getContent()) {
outstream << line << endl;
}
}
void FileIO::setFile(string Filename) {
// set the filename
filename = Filename;
ifstream in(filename);
if (in) {
// we have a file
string line;
while (getline(in, line)) {
content.push_back(line);
}
} else {
// we don't
panic("No file exists with the name " + filename + "!");
}
}
void FileIO::write() {
ofstream out(filename);
for (const auto &line : getContent()) {
out << line << endl;
}
}
void FileIO::panic(const string &msg) {
throw runtime_error("ERROR: " + msg + "\n");
}
//
// parser.h
//
// Created by Taylor Shuler on 4/8/15.
//
//
#ifndef _parser
#define _parser
#include <fstream>
#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class Parser {
public:
// constructors
/// default
Parser();
/// copy
Parser(const Parser &p);
/// custom
Parser(string filename);
// destructor
~Parser();
// public members
string getFilename();
list<string> getContent();
// operators
Parser& operator=(const Parser &p);
Parser& operator-=(const Parser &p);
Parser& operator+=(const Parser &p);
bool operator==(const Parser &p) const;
bool operator!=(const Parser &p) const;
bool operator<(const Parser &p) const;
bool operator>(const Parser &p) const;
bool operator<=(const Parser &p) const;
bool operator>=(const Parser &p) const;
friend Parser operator+(const Parser &p1, const Parser &p2);
friend Parser operator-(const Parser &p1, const Parser &p2);
friend ostream& operator<<(ostream &outstream, const Parser &p);
//friend istream& operator>>(istream &instream, const Parser &p);
protected:
// shared members
void allocFilename(string filename, bool freeMemory = false);
private:
// attributes
char* filename;
list<string> content;
};
#endif /* defined(_parser) */
//
// parser.cpp
//
// Created by Taylor Shuler on 4/8/15.
//
//
#include "parser.h"
void panic(string msg) {
cout << "ERROR: " << msg << endl;
exit(EXIT_FAILURE);
}
void Parser::allocFilename(string Filename, bool freeMemory) {
if (freeMemory) {
delete[] filename;
}
filename = new char[Filename.size()];
strcpy(filename, Filename.c_str());
}
Parser::Parser() {
allocFilename("");
}
Parser::Parser(string Filename) {
allocFilename(Filename);
ifstream file(Filename);
if (file) {
// we have one
string line;
while (getline(file, line)) {
content.push_back(line);
}
} else {
// we don't have one
panic("Could not find the file " + Filename + "!");
}
}
Parser::Parser(const Parser &p) {
// set the filename
allocFilename(p.filename, true);
// clear the destination for the text
content.clear();
// copy p's content over
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
}
Parser::~Parser() {
delete[] filename;
content.clear();
}
string Parser::getFilename() {
return filename;
}
list<string> Parser::getContent() {
return content;
}
Parser& Parser::operator=(const Parser &p) {
if (this == &p) {
return *this;
}
// set the new filename
allocFilename(p.filename);
// make room for the new content
content.clear();
// add in p's content
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
Parser& Parser::operator-=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.remove(*it);
}
return *this;
}
Parser& Parser::operator+=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
bool Parser::operator==(const Parser &p) const {
return content == p.content;
}
bool Parser::operator!=(const Parser &p) const {
return content != p.content;
}
bool Parser::operator<(const Parser &p) const {
return content < p.content;
}
bool Parser::operator>(const Parser &p) const {
return content > p.content;
}
bool Parser::operator<=(const Parser &p) const {
return content <= p.content;
}
bool Parser::operator>=(const Parser &p) const {
return content >= p.content;
}
Parser operator+(const Parser &p1, const Parser &p2) {
Parser sum(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
sum.content.push_back(*it);
}
return sum;
}
Parser operator-(const Parser &p1, const Parser &p2) {
Parser diff(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
diff.content.remove(*it);
}
return diff;
}
ostream& operator<<(ostream &outstream, const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
outstream << *it << endl;
}
return outstream;
}
//
// main.cpp
// parser
//
// Created by Taylor Shuler on 4/8/15.
//
//
#include "parser.h"
int main() {
Parser p1("test.txt"), p2("test2.txt"), p3, hr("hr.txt");
p3 = p1 + p2;
p3 -= hr;
cout << p3;
}
#ifndef _parser
#define _parser
#include <fstream>
#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class Parser {
public:
// constructors
/// default
Parser();
/// copy
Parser(const Parser &p);
/// custom
Parser(string filename);
// destructor
~Parser();
// public members
string getFilename();
list<string> getContent();
// operators
Parser& operator=(const Parser &p);
Parser& operator-=(const Parser &p);
Parser& operator+=(const Parser &p);
bool operator==(const Parser &p) const;
bool operator!=(const Parser &p) const;
bool operator<(const Parser &p) const;
bool operator>(const Parser &p) const;
bool operator<=(const Parser &p) const;
bool operator>=(const Parser &p) const;
friend Parser operator+(const Parser &p1, const Parser &p2);
friend Parser operator-(const Parser &p1, const Parser &p2);
friend ostream& operator<<(ostream &outstream, const Parser &p);
//friend istream& operator>>(istream &instream, const Parser &p);
protected:
// shared members
void allocFilename(string filename, bool freeMemory = false);
private:
// attributes
char* filename;
list<string> content;
};
#endif /* defined(_parser) */
#include "parser.h"
void panic(string msg) {
cout << "ERROR: " << msg << endl;
exit(EXIT_FAILURE);
}
void Parser::allocFilename(string Filename, bool freeMemory) {
if (freeMemory) {
delete[] filename;
}
filename = new char[Filename.size()];
strcpy(filename, Filename.c_str());
}
Parser::Parser() {
allocFilename("");
}
Parser::Parser(string Filename) {
allocFilename(Filename);
ifstream file(Filename);
if (file) {
// we have one
string line;
while (getline(file, line)) {
content.push_back(line);
}
} else {
// we don't have one
panic("Could not find the file " + Filename + "!");
}
}
Parser::Parser(const Parser &p) {
// set the filename
allocFilename(p.filename, true);
// clear the destination for the text
content.clear();
// copy p's content over
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
}
Parser::~Parser() {
delete[] filename;
content.clear();
}
string Parser::getFilename() {
return filename;
}
list<string> Parser::getContent() {
return content;
}
Parser& Parser::operator=(const Parser &p) {
if (this == &p) {
return *this;
}
// set the new filename
allocFilename(p.filename);
// make room for the new content
content.clear();
// add in p's content
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
Parser& Parser::operator-=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.remove(*it);
}
return *this;
}
Parser& Parser::operator+=(const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
content.push_back(*it);
}
return *this;
}
bool Parser::operator==(const Parser &p) const {
return content == p.content;
}
bool Parser::operator!=(const Parser &p) const {
return content != p.content;
}
bool Parser::operator<(const Parser &p) const {
return content < p.content;
}
bool Parser::operator>(const Parser &p) const {
return content > p.content;
}
bool Parser::operator<=(const Parser &p) const {
return content <= p.content;
}
bool Parser::operator>=(const Parser &p) const {
return content >= p.content;
}
Parser operator+(const Parser &p1, const Parser &p2) {
Parser sum(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
sum.content.push_back(*it);
}
return sum;
}
Parser operator-(const Parser &p1, const Parser &p2) {
Parser diff(p1);
for (auto it = p2.content.begin(); it != p2.content.end(); it++) {
diff.content.remove(*it);
}
return diff;
}
ostream& operator<<(ostream &outstream, const Parser &p) {
for (auto it = p.content.begin(); it != p.content.end(); it++) {
outstream << *it << endl;
}
return outstream;
}
#include "parser.h"
int main() {
Parser p1("test.txt"), p2("test2.txt"), p3, hr("hr.txt");
p3 = p1 + p2;
p3 -= hr;
cout << p3;
}
EDIT Here's my final code created with the help of the critique from @glampert and @Snowbody!
FileIO.h
#ifndef FILEIO
#define FILEIO
#include <list>
#include <string>
using std::list;
using std::ostream;
using std::string;
class FileIO {
public:
// constructors
/// default
FileIO();
/// copy
FileIO(const FileIO &io);
/// custom
FileIO(string filename);
// NOTE: Destructor not needed since the class attributes are terminated when they leave the scope
// accessors
string getFilename() const;
list<string> getContent() const;
// NOTE: Mutators not needed since their purpose is fulfilled by the custom constructor
// operators
FileIO& operator=(const FileIO &io);
FileIO& operator+=(const FileIO &io);
FileIO& operator-=(const FileIO &io);
friend FileIO operator+(const FileIO &io1, const FileIO &io2);
friend FileIO operator-(const FileIO &io1, const FileIO &io2);
/// equality
bool operator<(const FileIO &io) const;
bool operator>(const FileIO &io) const;
bool operator<=(const FileIO &io) const;
bool operator>=(const FileIO &io) const;
bool operator==(const FileIO &io) const;
bool operator!=(const FileIO &io) const;
friend ostream& operator<<(ostream& outstream, const FileIO &io);
// member functions
/// binds a new file to a FileIO object
void setFile(string filename);
/// writes the contents of the object to a file
void write();
private:
// error handling
void panic(const string &msg);
// attributes
string filename;
list<string> content;
};
#endif /* defined(FILEIO) */
FileIO.cpp
#include "FileIO.h"
#include <fstream>
#include <iostream>
using std::endl;
using std::ifstream;
using std::ofstream;
using std::runtime_error;
FileIO::FileIO() : filename("null") {}
FileIO::FileIO(const FileIO &io) {
setFile(io.getFilename());
}
FileIO::FileIO(string Filename) {
setFile(Filename);
}
string FileIO::getFilename() const {
return filename;
}
list<string> FileIO::getContent() const {
return content;
}
FileIO& FileIO::operator=(const FileIO &io) {
if (this == &io) {
return *this;
}
// set the filename
filename = io.getFilename();
// create a deep copy of io's content
for (const auto &line : io.getContent()) {
content.push_back(line);
}
return *this;
}
FileIO& FileIO::operator+=(const FileIO &io) {
for (const auto &line : io.getContent()) {
content.push_back(line);
}
}
FileIO& FileIO::operator-=(const FileIO &io) {
for (const auto &line : io.getContent()) {
content.remove(line);
}
}
FileIO operator+(const FileIO &io1, const FileIO &io2) {
FileIO sum(io1);
for (const auto &line : io2.getContent()) {
sum.content.push_back(line);
}
return sum;
}
FileIO operator-(const FileIO &io1, const FileIO &io2) {
FileIO diff(io1);
for (const auto &line : io2.getContent()) {
diff.content.remove(line);
}
return diff;
}
bool FileIO::operator<(const FileIO &io) const {
return content < io.content;
}
bool FileIO::operator>(const FileIO &io) const {
return content > io.content;
}
bool FileIO::operator<=(const FileIO &io) const {
return content <= io.content;
}
bool FileIO::operator>=(const FileIO &io) const {
return content >= io.content;
}
bool FileIO::operator==(const FileIO &io) const {
return content == io.content;
}
bool FileIO::operator!=(const FileIO &io) const {
return content != io.content;
}
ostream& operator<<(ostream &outstream, const FileIO &io) {
for (const auto &line : io.getContent()) {
outstream << line << endl;
}
}
void FileIO::setFile(string Filename) {
// set the filename
filename = Filename;
ifstream in(filename);
if (in) {
// we have a file
string line;
while (getline(in, line)) {
content.push_back(line);
}
} else {
// we don't
panic("No file exists with the name " + filename + "!");
}
}
void FileIO::write() {
ofstream out(filename);
for (const auto &line : getContent()) {
out << line << endl;
}
}
void FileIO::panic(const string &msg) {
throw runtime_error("ERROR: " + msg + "\n");
}
- 3.1k
- 2
- 23
- 44
Text file parsing File IO class in C++
For fun, I've made a basic textpractical file parsingIO class in C++ with the purpose of being light and quick, and extendable for custom parsers. Here is the code:
Text file parsing in C++
For fun, I've made a basic text file parsing class in C++ with the purpose of being light and quick. Here is the code:
File IO class in C++
For fun, I've made a practical file IO class in C++ with the purpose of being light and quick, and extendable for custom parsers. Here is the code: