44#include < string>
55
66#include " fileman.h"
7+ #include " ../utils.h"
8+ #include " stringop.h"
79using namespace std ;
810
911inline 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 ;
0 commit comments