Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- int main ()
- {
- string Uin;
- ifstream prefs ("files.dat");
- bool fileStdIn, appendLF, charflush, IOmarkers, EXmarkers, commentCHK, inToOut, memloop;
- while (!prefs.is_open())
- {
- std::cerr << "[ERROR] COULD NOT OPEN SETTINGS FILE, PLEASE SPECIFY LOCATION" << endl;
- std::getline (std::cin, Uin);
- prefs.open (Uin);
- }
- if (prefs.good())
- {
- std::getline (prefs, Uin, '\t');
- std::getline (prefs, Uin);
- }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- ifstream srcfile (Uin);
- while (!srcfile.is_open())
- {
- std::cerr << "[ERROR] SOURCE FILE NOT FOUND, PLEASE SPECIFY LOCATION" << std::endl;
- std::getline (std::cin, Uin);
- srcfile.open (Uin);
- }
- cout << "[SRC FILE DETECTED]: " << Uin << std::endl;
- if (prefs.good())
- {
- std::getline (prefs, Uin, '\t');
- std::getline (prefs, Uin);
- }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- ofstream outfile (Uin);
- while (!outfile.is_open())
- {
- std::cerr << "[ERROR] STD OUTPUT FILE NOT FOUND, PLEASE SPECIFY LOCATION" << std::endl;
- std::getline (std::cin, Uin);
- outfile.open (Uin);
- }
- cout << "[STDOUT FILE DETECTED]: " << Uin << std::endl;
- if (prefs.good())
- {
- std::getline (prefs, Uin, '\t');
- std::getline (prefs, Uin);
- }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- ofstream errfile (Uin);
- while (!errfile.is_open())
- {
- std::cerr << "[ERROR] STD ERROR FILE NOT FOUND, PLEASE SPECIFY LOCATION" << std::endl;
- std::getline (std::cin, Uin);
- errfile.open (Uin);
- }
- cout << "[STDERR FILE DETECTED]: " << Uin << std::endl;
- if (prefs.good())
- {
- std::getline (prefs, Uin, '\t');
- std::getline (prefs, Uin);
- }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- ifstream infile (Uin);
- while (!infile.is_open())
- {
- std::cerr << "[ERROR] STD INPUT FILE NOT FOUND, PLEASE SPECIFY LOCATION" << std::endl;
- std::getline (std::cin, Uin);
- infile.open (Uin);
- }
- cout << "[STDIN FILE DETECTED]: " << Uin << std::endl;
- if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- if (Uin == "true" || Uin == "TRUE" || Uin == "1") fileStdIn = true;
- else fileStdIn = false;
- if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- if (Uin == "true" || Uin == "TRUE" || Uin == "1") appendLF = true;
- else appendLF = false;
- if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- if (Uin == "true" || Uin == "TRUE" || Uin == "1") charflush = true;
- else charflush = false;
- if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- if (Uin == "true" || Uin == "TRUE" || Uin == "1") IOmarkers = true;
- else IOmarkers = false;
- if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- if (Uin == "true" || Uin == "TRUE" || Uin == "1") EXmarkers = true;
- else EXmarkers = false;
- if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- if (Uin == "true" || Uin == "TRUE" || Uin == "1") commentCHK = true;
- else commentCHK = false;
- if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- if (Uin == "true" || Uin == "TRUE" || Uin == "1") inToOut = true;
- else inToOut = false;
- if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
- else
- {
- cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- if (Uin == "true" || Uin == "TRUE" || Uin == "1") memloop = true;
- else memloop = false;
- prefs.close();
- std::cout << std::endl;
- if (EXmarkers)
- cout << "USE INPUT FILE FOR STDIN: " << fileStdIn << std::endl
- << "APPEND NEWLINE TO ALL INPUT: " << appendLF << std::endl
- << "FLUSH ALL OUTPUT CHARS: " << charflush << std::endl
- << "OUTPUT IO MARKERS TO CONSOLE: " << IOmarkers << std::endl
- << "OUTPUT INFORMATION AL MARKERS: " << EXmarkers << std::endl
- << "CHECK FOR COMMENT MARKERS: " << commentCHK << std::endl
- << "WRITE INPUT TO OUTPUT FILE: " << inToOut << std::endl
- << "MEMORY POINTER CAN LOOP: " << memloop << std::endl << std::endl;
- unsigned int strptr = 0, memptr = 0, Uptr, brPrePtr = 0;
- int br_count = 0; char cur_ins, bfmem[65536] = {0};
- string src = "", input; Uin = "";
- while (srcfile.good())
- {
- std::getline (srcfile, input);
- if (commentCHK)
- {
- if (input.find ("#", 0) != string::npos)
- {
- input.replace (input.find ("#", 0), input.length() - input.find ("#", 0), "");
- }
- }
- src += input;
- }
- if (EXmarkers) std::cout << "[DONE READING SOURCE]" << std::endl;
- while (strptr < src.length())
- {
- if (src.at(strptr) == '+' || src.at(strptr) == '-' || src.at(strptr) == '>' || src.at(strptr) == '<' ||
- src.at(strptr) == ',' || src.at(strptr) == '.' || src.at(strptr) == '[' || src.at(strptr) == ']')
- strptr++;
- else src.erase (strptr, 1);
- }
- strptr = 0;
- while (!(brPrePtr == src.length() || br_count < 0))
- {
- if (src.at (brPrePtr) == '[') br_count++;
- else if (src.at (brPrePtr) == ']') br_count--;
- brPrePtr++;
- }
- if (EXmarkers) std::cout << "[BRACKET CHECK DONE]" << std::endl;
- if (br_count < 0)
- {
- std::cerr << "[ERROR]: NO OPENING BRACKET" << std::endl;
- errfile << "[ERROR]: NO OPENING BRACKET" << std::endl;
- }
- else if (br_count > 0)
- {
- std::cerr << "[ERROR]: NO CLOSING BRACKET" << std::endl;
- errfile << "[ERROR]: NO CLOSING BRACKET" << std::endl;
- }
- else
- {
- input = "";
- br_count = 0;
- std::cout << "[PROGRAM STARTED]" << std::endl;
- if (IOmarkers) cout << "[OUT]\t";
- while (strptr < src.length())
- {
- cur_ins = src.at (strptr);
- if (cur_ins == '+')
- {
- if (bfmem[memptr] == 255) bfmem[memptr] = 0;
- else bfmem[memptr]++;
- }
- else if (cur_ins == '-')
- {
- if (bfmem[memptr] == 0) bfmem[memptr] = 255;
- else bfmem[memptr]--;
- }
- else if (cur_ins == '>')
- {
- if (memptr == 65535)
- {
- if (memloop) memptr = 0;
- else
- {
- std::cerr << "[ERROR]: OUT OF BOUNDS MEMORY (0xFFFF)" << std::endl;
- errfile << "[ERROR]: OUT OF BOUNDS MEMORY (0xFFFF)" << std::endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- }
- else memptr++;
- }
- else if (cur_ins == '<')
- {
- if (memptr == 0)
- {
- if (memloop) memptr = 65535;
- else
- {
- std::cerr << "[ERROR]: OUT OF BOUNDS MEMORY (-0x0001)" << std::endl;
- errfile << "[ERROR]: OUT OF BOUNDS MEMORY (-0x0001)" << std::endl;
- std::cout << "Press [ENTER] to continue" << std::endl;
- std::getline (cin, Uin);
- return 0;
- }
- }
- else memptr--;
- }
- else if (cur_ins == '.')
- {
- std::cout << bfmem[memptr];
- outfile << bfmem[memptr];
- if (bfmem[memptr] == '\n' && IOmarkers) cout << "[OUT]\t";
- if (charflush)
- {
- std::cout << std::flush;
- outfile << std::flush;
- }
- }
- else if (cur_ins == ',')
- {
- if (Uin.length() < 1)
- {
- if (IOmarkers) std::cout << "\n[IN]";
- if (fileStdIn)
- {
- if (infile.good())
- {
- std::getline (infile, Uin);
- cout << Uin << "\n";
- }
- else
- {
- Uin = " ";
- Uin.at(0) = 0;
- }
- }
- else std::getline (std::cin, Uin);
- if (inToOut) outfile << Uin << std::endl;
- if (IOmarkers) std::cout << "[OUT]\t";
- Uptr = 0;
- while (Uptr + 1 < Uin.length() && Uin.length() != 0)
- {
- if (Uin.at (Uptr) == '\\')
- {
- if (Uptr + 1 == Uin.length()) Uin.replace (Uptr, 1, "-");
- else if (Uin.at (Uptr + 1) == 'n') Uin.replace (Uptr, 2, "\n");
- else if (Uin.at (Uptr + 1) == 'a') Uin.replace (Uptr, 2, "\a");
- else if (Uin.at (Uptr + 1) == 'b') Uin.replace (Uptr, 2, "\b");
- else if (Uin.at (Uptr + 1) == '\\') Uin.replace (Uptr, 2, "\\");
- else Uin.replace (Uptr, 2, "-");
- }
- Uptr++;
- }
- if (appendLF || Uin.length() == 0) Uin += "\n";
- }
- bfmem[memptr] = Uin.at (0);
- Uin.erase (0, 1);
- }
- else if (cur_ins == '[')
- {
- if (!bfmem[memptr])
- {
- br_count = 1;
- while (br_count)
- {
- strptr++;
- cur_ins = src.at (strptr);
- if (cur_ins == '[') br_count++;
- else if (cur_ins == ']') br_count--;
- }
- }
- }
- else if (cur_ins == ']')
- {
- if (bfmem[memptr])
- {
- br_count = 1;
- while (br_count)
- {
- strptr--;
- cur_ins = src.at (strptr);
- if (cur_ins == ']') br_count++;
- else if (cur_ins == '[') br_count--;
- }
- }
- }
- if (strptr == src.length() - 1) break;
- strptr++;
- }
- std::cout << "\n[PROGRAM ENDED]" << std::endl;
- outfile.flush();
- outfile.close();
- errfile.flush();
- errfile.close();
- if (EXmarkers) std::cout << "[FILES FLUSHED]" << std::endl;
- }
- std::cout << "Press [ENTER] to continue" << std::flush;
- std::getline (std::cin, Uin);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement