diff --git a/.gitignore b/.gitignore index 84838c5..34c0952 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ bin/* tests/* +.vscode/* +.vscode/settings.json diff --git a/libs/fileman.cpp b/libs/fileman.cpp index fc6a26f..ada3e87 100644 --- a/libs/fileman.cpp +++ b/libs/fileman.cpp @@ -4,6 +4,8 @@ #include #include "fileman.h" +#include "../utils.h" +#include "stringop.h" using namespace std; inline bool file_exists (const std::string& name) { @@ -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; @@ -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; diff --git a/libs/fileman.h b/libs/fileman.h index 0a55fb9..df9d4c6 100644 --- a/libs/fileman.h +++ b/libs/fileman.h @@ -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 \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4ce5618..fa5b18e 100644 --- a/main.cpp +++ b/main.cpp @@ -51,7 +51,6 @@ developer. string cmd; string co; int o; - int* dummylol = new int[26]; while (true) { getline(cin, cmd); string cmdu = to_upper(cmd); @@ -59,7 +58,7 @@ developer. 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) { @@ -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); diff --git a/utils.cpp b/utils.cpp index 60642a1..a90951b 100644 --- a/utils.cpp +++ b/utils.cpp @@ -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]; @@ -64,6 +64,7 @@ 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) { @@ -71,4 +72,29 @@ string trimString(string s, int start, int end) { } 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; } \ No newline at end of file diff --git a/utils.h b/utils.h index 11f2330..7e122db 100644 --- a/utils.h +++ b/utils.h @@ -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 \ No newline at end of file