コード例 #1
0
int main(int argc, char** argv) {

    Register aRegister;
    vector<int> parameters;
    vector<char> commands;
    
    extern char *optarg;
    extern int optind;
    
    int command = 0, err = 0;
    
    if (argc < 2) {
        cerr << "Incorrect Format: This program requires at minimum one argument" << endl;
        return -1;
    }
    
    bool PRINT_FLAG = false;
    
        
    while (( command = getopt( argc, argv, "i:s:r:l:v:p")) != -1) {
        switch(command) {
            case 'i':
                // Sets the initial register value
                for(int i=0;i<string(optarg).size();i++){
                    if(optarg[i]!='0' && optarg[i]!='1'){
                        cerr << "Invalid initial register value. Please enter only an combination of 0's and 1's" << endl;
                        return -1;
                    }
                }
                aRegister.setRegister(string(optarg));
                break;
            case 'p':
                // Sets print flag to indicate whether to print results at end
                PRINT_FLAG = true;
                break;
            case 'l':
                //
                if(optarg[0] != '0' && atoi(optarg) == 0) {
                    cerr << "Invalid parameter passed for -l : Must be a valid number" << endl;
                    return -1;
                } else if(atoi(optarg) < 0){
                    cerr << "Invalid parameter passed for -l : Must be a non-negative number" << endl;
                    return -1;
                }
                commands.push_back('l');
                parameters.push_back(atoi(optarg));
                break;
            case 'r':
                if(optarg[0] != '0' && atoi(optarg) == 0) {
                    cerr << "Invalid parameter passed for -r : Must be a valid number" << endl;
                    return -1;
                } else if(atoi(optarg) < 0){
                    cerr << "Invalid parameter passed for -r : Must be a non-negative number" << endl;
                    return -1;
                }
                commands.push_back('r');
                parameters.push_back(atoi(optarg));
                break;   
            case 's':
                // Specifies the number of bits in the shift register
                int shiftBits ;
                shiftBits = atoi(optarg);
                if (shiftBits <= 0) {
                    cerr << "Error: please specify a register size greater than 0" << endl;
                    return -1;
                }
                aRegister.shiftRegSize(shiftBits);
                break;
            case 'v':
                // Value to inject in vacated bit if other than default
                if((strlen(optarg) > 1) || (optarg[0] != '0' && optarg[0] != '1')){
                    cerr << "Invalid parameter: \"" << optarg << "\" given with -v argument" << endl << "Please enter 0 or 1 as parameter" << endl;
                    cerr << "Value must be 0 or 1" << endl;
                    return -1;
                }                           
                aRegister.setShiftValue(atoi(optarg));               
                break;        
            case '?':
                err = 1;
                break;
            default:
                err = 1;
                break;
        }
    }
    
    if (err) {
        cerr << "Unknown or incomplete command line argument(s) passed to program" << endl;
        return -1;
    }
    
    if(!excecuteOperations(commands, parameters, aRegister)) {
        cerr << "Error executing shift operations" << endl;
        return -1;
    }
    
    if(PRINT_FLAG){
        aRegister.printValue();
    }
    
    return 0;
}