//
// Created by TheSkyWinds on 10/25/24.
//

// TODO: Add documentation on how it is used.

#ifndef ERRORSEAL_H
#define ERRORSEAL_H

#include <iostream>
#include <limits>

namespace error {
// Clears the buffer for std::cin.
    inline void ignoreLine() {

        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    }

/*
 * cinError() returns true if an error occurs.
 * cinError() also clears up the error and buffer, so that the program can continue to work.
 */
    inline bool cinError(){
        bool errorState{false};
        if (!std::cin) {

            if (std::cin.eof()) {
                std::exit(0); // Shut down the program now
            }

            std::cin.clear();
            ignoreLine();
            errorState = true;
        }
        return errorState;
    }

/*
 * Throws a generic, undefined error message, but does the same as the function above.
 * Primarily used for quick and easy debugging.
 */

    inline bool cinErrorVerbose() {
        bool errorState{false};
        if (!std::cin) {

            if (std::cin.eof()) {
                std::exit(0);
            }

            std::cin.clear();
            ignoreLine();

            std::cerr << "Whoops, you broke the program!\n";
            errorState = true;
        }
        return errorState;
    }
}


#endif //ERRORSEAL_H
