Skip to content

Commit

Permalink
It can now load and save :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthilde committed Aug 27, 2019
1 parent fe6ce6f commit 22cc364
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bin/*
tests/*
.vscode/*
.vscode/settings.json
45 changes: 38 additions & 7 deletions libs/fileman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string>

#include "fileman.h"
#include "../utils.h"
#include "stringop.h"
using namespace std;

inline bool file_exists (const std::string& name) {
Expand All @@ -12,7 +14,7 @@ inline bool file_exists (const std::string& name) {
}

// File managing functions to save/load BAS Files
int saveFile(string name, string* prgmArray, bool overwrite) {
int saveFile(const string name, const string* prgmArray, const bool overwrite) {
string fpname = "programs/" + name;


Expand All @@ -31,15 +33,44 @@ int saveFile(string name, string* prgmArray, bool overwrite) {
}
}

cout << "Done!" << endl;
cout << "[fileman.cpp] Done!" << endl;
ofstream basfile;
basfile.open(fpname);

for (int i = 0; i < 999999; ++i) {
if (prgmArray[i] == "")
continue;

basfile << to_string(i) << " " << prgmArray[i] << "\n";
if (basfile.is_open()) {
for (int i = 0; i < 999999; ++i) {
if (prgmArray[i] == "")
continue;

basfile << to_string(i) << " " << prgmArray[i] << "\n";
}
basfile.close();
} else {
cout << "[fileman.cpp] File Error! " << "Unable to open file." << endl;
return 1;
}
return 0;
}

int loadFile(const string name, string* prgmArray) {
string fpname = "programs/" + name;
if (!file_exists(fpname)) {
cout << "[fileman.cpp] File Error! " << name << "doesn't exist." << endl;
return 1;
}

string line;
ifstream basfile;
basfile.open(fpname);

string tmp;
int linenum;
while (getline(basfile, line)) {
if (is_number(trimString(line, 0, searchChar(' ', line) - 1))) {
tmp = trimString(line, 0, searchChar(' ', line));
int linnum = evalString(tmp, new int[26], false); // Lmao I can't code :D
prgmArray[linnum] = line.erase(0, tmp.size());
}
}

return 0;
Expand Down
3 changes: 2 additions & 1 deletion libs/fileman.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef FILEMAN_H
#define FILEMAN_H

int saveFile(std::string, std::string*, bool);
int saveFile(const std::string, const std::string*, const bool);
int loadFile(const std::string, std::string*);
#endif
12 changes: 9 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ developer.
string cmd;
string co;
int o;
int* dummylol = new int[26];
while (true) {
getline(cin, cmd);
string cmdu = to_upper(cmd);
if (cmdu == "EXIT")
break;
else if (is_number(trimString(cmd, 0, searchChar(' ', cmd) - 1))) {
string a = trimString(cmd, 0, searchChar(' ', cmd));
int linnum = evalString(a, dummylol, false); // Lmao I can't code :D
int linnum = evalString(a, new int[26], false); // Lmao I can't code :D
PrgmMem[linnum] = cmd.erase(0, a.size());
} else if (cmdu == "LIST") {
for (int i = 0; i < 999999; ++i) {
Expand All @@ -77,7 +76,14 @@ developer.
if (co == "")
cout << "!!SYNTAX ERROR" << endl;
else
saveFile(co, PrgmMem, true);
saveFile(co + ".BAS", PrgmMem, true);
} else if (starts_with(cmdu, "LOAD")) {
co = cmdu.erase(0, 4);

if (co == "")
cout << "!!SYNTAX ERROR" << endl;
else
loadFile(co + ".BAS", PrgmMem);
}
else if (cmdu == "RUN") {
ExecuteCode(PrgmMem);
Expand Down
28 changes: 27 additions & 1 deletion utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int searchChar(char target, string s) {
return -1; // Returns -1 if nothing has been found
}

// To split spaces and put every words into a string array
// To split spaces and put every words into a string array. Thanks Stack Overflow lol
std::string* splitSpaces(std::string line)
{
string* arr = new string[20];
Expand All @@ -64,11 +64,37 @@ std::string* splitSpaces(std::string line)
return arr;
}

// To trim strings
string trimString(string s, int start, int end) {
string o = "";
for (int i = start; i <= end; ++i) {
o += s.at(i);
}

return o;
}

// Count how many times there is one specific character
int countChar(const string s, const char c) {
int count = 0;
for (int i = 0; i < s.size(); ++i) {
if (s.at(i) == c)
++count;
}

return count;
}

// To split into array
string* splitString(const string s, const char c) {
string* sarr = new string[countChar(s, c) + 1];
int ptr = 0;
for (int i = 0; i < s.size(); ++i) {
if (s.at(i) == c) {
++ptr; continue;
}
sarr[ptr] += s.at(i);
}

return sarr;
}
1 change: 1 addition & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ int searchChar(char, std::string);
std::string trimString(std::string, int, int);
bool starts_with(const std::string, const std::string);
std::string to_upper(const std::string);
std::string* splitString(const std::string, const char);
#endif

0 comments on commit 22cc364

Please sign in to comment.