Skip to content

Commit 22cc364

Browse files
author
Matthilde
committed
It can now load and save :D
1 parent fe6ce6f commit 22cc364

6 files changed

Lines changed: 79 additions & 12 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
bin/*
22
tests/*
3+
.vscode/*
4+
.vscode/settings.json

libs/fileman.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <string>
55

66
#include "fileman.h"
7+
#include "../utils.h"
8+
#include "stringop.h"
79
using namespace std;
810

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

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

1820

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

34-
cout << "Done!" << endl;
36+
cout << "[fileman.cpp] Done!" << endl;
3537
ofstream basfile;
3638
basfile.open(fpname);
3739

38-
for (int i = 0; i < 999999; ++i) {
39-
if (prgmArray[i] == "")
40-
continue;
41-
42-
basfile << to_string(i) << " " << prgmArray[i] << "\n";
40+
if (basfile.is_open()) {
41+
for (int i = 0; i < 999999; ++i) {
42+
if (prgmArray[i] == "")
43+
continue;
44+
45+
basfile << to_string(i) << " " << prgmArray[i] << "\n";
46+
}
47+
basfile.close();
48+
} else {
49+
cout << "[fileman.cpp] File Error! " << "Unable to open file." << endl;
50+
return 1;
51+
}
52+
return 0;
53+
}
54+
55+
int loadFile(const string name, string* prgmArray) {
56+
string fpname = "programs/" + name;
57+
if (!file_exists(fpname)) {
58+
cout << "[fileman.cpp] File Error! " << name << "doesn't exist." << endl;
59+
return 1;
60+
}
61+
62+
string line;
63+
ifstream basfile;
64+
basfile.open(fpname);
65+
66+
string tmp;
67+
int linenum;
68+
while (getline(basfile, line)) {
69+
if (is_number(trimString(line, 0, searchChar(' ', line) - 1))) {
70+
tmp = trimString(line, 0, searchChar(' ', line));
71+
int linnum = evalString(tmp, new int[26], false); // Lmao I can't code :D
72+
prgmArray[linnum] = line.erase(0, tmp.size());
73+
}
4374
}
4475

4576
return 0;

libs/fileman.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef FILEMAN_H
22
#define FILEMAN_H
33

4-
int saveFile(std::string, std::string*, bool);
4+
int saveFile(const std::string, const std::string*, const bool);
5+
int loadFile(const std::string, std::string*);
56
#endif

main.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@ developer.
5151
string cmd;
5252
string co;
5353
int o;
54-
int* dummylol = new int[26];
5554
while (true) {
5655
getline(cin, cmd);
5756
string cmdu = to_upper(cmd);
5857
if (cmdu == "EXIT")
5958
break;
6059
else if (is_number(trimString(cmd, 0, searchChar(' ', cmd) - 1))) {
6160
string a = trimString(cmd, 0, searchChar(' ', cmd));
62-
int linnum = evalString(a, dummylol, false); // Lmao I can't code :D
61+
int linnum = evalString(a, new int[26], false); // Lmao I can't code :D
6362
PrgmMem[linnum] = cmd.erase(0, a.size());
6463
} else if (cmdu == "LIST") {
6564
for (int i = 0; i < 999999; ++i) {
@@ -77,7 +76,14 @@ developer.
7776
if (co == "")
7877
cout << "!!SYNTAX ERROR" << endl;
7978
else
80-
saveFile(co, PrgmMem, true);
79+
saveFile(co + ".BAS", PrgmMem, true);
80+
} else if (starts_with(cmdu, "LOAD")) {
81+
co = cmdu.erase(0, 4);
82+
83+
if (co == "")
84+
cout << "!!SYNTAX ERROR" << endl;
85+
else
86+
loadFile(co + ".BAS", PrgmMem);
8187
}
8288
else if (cmdu == "RUN") {
8389
ExecuteCode(PrgmMem);

utils.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int searchChar(char target, string s) {
4848
return -1; // Returns -1 if nothing has been found
4949
}
5050

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

67+
// To trim strings
6768
string trimString(string s, int start, int end) {
6869
string o = "";
6970
for (int i = start; i <= end; ++i) {
7071
o += s.at(i);
7172
}
7273

7374
return o;
75+
}
76+
77+
// Count how many times there is one specific character
78+
int countChar(const string s, const char c) {
79+
int count = 0;
80+
for (int i = 0; i < s.size(); ++i) {
81+
if (s.at(i) == c)
82+
++count;
83+
}
84+
85+
return count;
86+
}
87+
88+
// To split into array
89+
string* splitString(const string s, const char c) {
90+
string* sarr = new string[countChar(s, c) + 1];
91+
int ptr = 0;
92+
for (int i = 0; i < s.size(); ++i) {
93+
if (s.at(i) == c) {
94+
++ptr; continue;
95+
}
96+
sarr[ptr] += s.at(i);
97+
}
98+
99+
return sarr;
74100
}

utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ int searchChar(char, std::string);
77
std::string trimString(std::string, int, int);
88
bool starts_with(const std::string, const std::string);
99
std::string to_upper(const std::string);
10+
std::string* splitString(const std::string, const char);
1011
#endif

0 commit comments

Comments
 (0)