------------------------------------------------------------------------------
MC logo
Replacement Main
[^] CSc 422 Assignment 4
------------------------------------------------------------------------------
[CSc 422 Assignment 1] [CSc 422 Assignment 2] [CSc 422 Assignment 3] [CSc 422 Assignment 4]
[Replacement Main] [replacement_h] [replacement_cpp] [FIFO Class] [LRU Class] [LRU Implementation] [NRU Class] [NRU Implementation] [Reader Test Class]
vmx.cpp
#include <ctype.h>
#include <stdlib.h>

#include <iostream>
#include <stdexcept>
#include <string>

#include "replacement.h"
#include "readtest.h"
#include "lru.h"
#include "fifo.h"
#include "nru.h"
//#include "clock.h"
//#include "aging.h"

using std::string;

class Args {
public:
        Args(int argc, char **argv): m_argc(argc), m_argv(argv),
                                     m_lastexists("argument") {
                m_name = *m_argv++;
                --m_argc;
        }

        char *program_name() { return m_name; }

        // If there are no more arguments, print a message using msg, and
        // exit.  Otherwise, return *this.
        Args & exists(string msg = "argument")
        {
                if(m_argc <= 0) {
                        std::cout << msg << " missing" << std::endl;
                        exit(1);
                }
                m_lastexists = msg;
                return *this;
        }

        // Get the next argument, with given default value if absent.
        char *next(char *dflt = NULL) {
                if(m_argc <= 0) return dflt;
                char *ret = *m_argv++;
                --m_argc;
                return ret;
        }

        // Get the next argument as an integer value with the indicated
        // default, and the indicated msg.  If the message is blank, use 
        // the previous one from exists(), if any.
        int nexti(int dflt = 0, string msg = "") {
                if(m_argc <= 0) return dflt;
                char *ret = next();
                if(!isdigit(*ret)) {
                        if(msg == "") msg = m_lastexists;
                        if(msg == "") msg = "numeric argument";
                        std::cout << msg << " is not numeric: " << ret
                                  << std::endl;
                        exit(1);
                }
                m_lastexists = "";
                return atoi(ret);
        }
        int nexti(string msg) {
                return nexti(0, msg);
        }

private:
        char *m_name;           // Program name.
        int m_argc;             // Number of remaining args.
        char **m_argv;          // Remaining args.
        string m_lastexists;    // Keep the string from exists() as default
                                // message for required arguments.
};

int main(int argc, char **argv)
{
        Args args(argc, argv);

        // Get the basic stuff from the command line: 
        char *tracefile = args.exists("file name").next();
        string algname = args.exists("algorithm").next();
        int nframe = args.exists("frame count").nexti();
        int pagebits = args.nexti(12);

        // Create the algorithm object.
        ReplacementAlg *alg;
        if(algname == "readtest") 
                alg = new ReadTest;
        else if(algname == "lru") 
                alg = new LRUReplacementAlg(nframe, pagebits);
        else if(algname == "nru") 
                alg = new NRUReplacementAlg(nframe, pagebits,
                                            args.nexti(100));
        else if(algname == "fifo") 
                alg = new FIFOReplacementAlg(nframe, pagebits);
//      else if(algname == "clock") 
//              alg = new ClockReplacementAlg(nframe, pagebits);
//      else if(algname == "aging") 
//              alg = new AgingReplacementAlg(nframe, pagebits,
//                                            args.nexti(500));
        else {
                std::cerr << "Unknown or unimplemented replacement algorithm "
                          << algname << std::endl;
                exit(2);
        }

        try {
                alg->read(tracefile);
        } catch(std::exception &e) {
                std::cerr << "Failed: " << e.what() << std::endl;
        }

        alg->print_stats(std::cout);
}