//---------------------------------------------------------------------- // MTDelete <-Index (size_t objId) | -Random (size_t numRandId)> [-Array] //---------------------------------------------------------------------- CmdExecStatus MTDeleteCmd::exec(const string& option) { // TODO // check option vector<string> options; if (!CmdExec::lexOptions(option, options)) return CMD_EXEC_ERROR; bool doIdx = false, doRand = false, doArray = false; vector<int> objIds(1); int nRand; size_t objOpt = 0, randOpt = 0; for (size_t i = 0, n = options.size(); i < n; ++i) { if (myStrNCmp("-Array", options[i], 2) == 0) { if (doArray) return CmdExec::errorOption(CMD_OPT_EXTRA,options[i]); doArray = true; } else if (myStrNCmp("-Index", options[i], 2) == 0) { if (doIdx || doRand) return CmdExec::errorOption(CMD_OPT_EXTRA,options[i]); if (++i == n) return CmdExec::errorOption(CMD_OPT_MISSING, options[i - 1]); if (!myStr2Int(options[i], objIds[0]) || (objIds[0] < 0)) return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[i]); objOpt = i; doIdx = true; } else if (myStrNCmp("-Random", options[i], 2) == 0) { if (doIdx || doRand) return CmdExec::errorOption(CMD_OPT_EXTRA,options[i]); randOpt = i; if (++i == n) return CmdExec::errorOption(CMD_OPT_MISSING, options[i - 1]); if (!myStr2Int(options[i], nRand) || (nRand <= 0)) return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[i]); doRand = true; } else return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[i]); } int s = (doArray)? mtest.getArrListSize(): mtest.getObjListSize(); if (doIdx) { if (objIds[0] >= s) { cerr << "Size of " << (doArray?"array":"object") << " list (" << s << ") is <= " << objIds[0] << "!!\n"; return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[objOpt]); } } else if (doRand) { objIds.clear(); if (s > 0) // has something to delete for (int i = 0; i < nRand; ++i) objIds.push_back(rnGen(s)); else { cerr << "Size of " << (doArray?"array":"object") << " list is 0!!" << endl; return CmdExec::errorOption(CMD_OPT_ILLEGAL, options[randOpt]); } } else return CmdExec::errorOption(CMD_OPT_MISSING, ""); if (doArray) for (size_t i = 0, n = objIds.size(); i < n; ++i) mtest.deleteArr(objIds[i]); else for (size_t i = 0, n = objIds.size(); i < n; ++i) mtest.deleteObj(objIds[i]); return CMD_EXEC_DONE; }
//---------------------------------------------------------------------- // MTDelete <-Index (size_t objId) | -Random (size_t numRandId)> [-Array] //---------------------------------------------------------------------- CmdExecStatus MTDeleteCmd::exec(const string& option) { // TODO vector<string> tokens; int objId, numRandId; if(option == ""){ return errorOption(CMD_OPT_MISSING, ""); } if(!CmdExec::lexOptions(option, tokens)){ return CMD_EXEC_ERROR; } if(tokens.size() == 2){ if(myStrNCmp("-Index", tokens[0], 2) == 0){ if(myStr2Int(tokens[1], objId)){ if(objId >= 0){ if(!objErr(objId, mtest.getObjListSize())){ mtest.deleteObj(objId); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else if(myStrNCmp("-Random", tokens[0], 2) == 0){ if(myStr2Int(tokens[1], numRandId)){ if(numRandId >= 1){ if(mtest.getObjListSize() != 0){ for(int i = 0; i < numRandId; i++){ mtest.deleteObj(rnGen(mtest.getObjListSize())); } } else{ cout << "Size of object list is 0 !!" << endl; return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[0]); } else if(tokens.size() == 3){ if(myStrNCmp("-Array", tokens[0], 2) == 0){ if(myStrNCmp("-Index", tokens[1], 2) == 0){ if(myStr2Int(tokens[2], objId)){ if(objId >= 0){ if(!arrErr(objId, mtest.getArrListSize())){ mtest.deleteArr(objId); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else if(myStrNCmp("-Random", tokens[1], 2) == 0){ if(myStr2Int(tokens[2], numRandId)){ if(numRandId >= 1){ if(mtest.getArrListSize() != 0){ for(int i = 0; i < numRandId; i++){ mtest.deleteArr(rnGen(mtest.getArrListSize())); } } else{ cout << "Size of array list is 0 !!" << endl; return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[2]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else if(myStrNCmp("-Array", tokens[2], 2) == 0){ if(myStrNCmp("-Index", tokens[0], 2) == 0){ if(myStr2Int(tokens[1], objId)){ if(objId >= 0){ if(!arrErr(objId, mtest.getArrListSize())){ mtest.deleteArr(objId); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else if(myStrNCmp("-Random", tokens[0], 2) == 0){ if(myStr2Int(tokens[1], numRandId)){ if(numRandId >= 1){ if(mtest.getArrListSize() != 0){ for(int i = 0; i < numRandId; i++){ mtest.deleteArr(rnGen(mtest.getArrListSize())); } } else{ cout << "Size of array list is 0 !!" << endl; return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[1]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[0]); } else return errorOption(CMD_OPT_ILLEGAL, tokens[0]); } else return errorOption(CMD_OPT_MISSING, ""); return CMD_EXEC_DONE; }