command_fn commands::at (const string& cmd) { // Note: value_type is pair<const key_type, mapped_type> // So: iterator->first is key_type (string) // So: iterator->second is mapped_type (command_fn) command_map::const_iterator result = map.find (cmd); if (result == map.end()) { throw yshell_exn (cmd + ": no such function"); } return result->second; }
//do not use a subscript operator on a map //e.g. m[k] and m is a map // ----> searches the map from the key k // ----> if does not find it, returns the default value // ----> will pollute map with a bunch of default values command_fn commands::at (const string& cmd) { // Note: value_type is pair<const key_type, mapped_type> // So: iterator->first is key_type (string) // So: iterator->second is mapped_type (command_fn) //can use "auto" instead of command_map::const_iterator command_map::const_iterator result = map.find (cmd); //loops through until it hits the end if (result == map.end()) { throw yshell_exn (cmd + ": no such function"); } return result->second; }
//ALL THE STUBS void fn_cat (inode_state& state, const wordvec& words){ if(words.size() > 1) { for (auto word = words.begin()+1; word != words.end(); word++){ bool r = ((*word)[0] == '/'); cout << state.readfile(split(*word,"/"),r) << endl; } } else { throw yshell_exn("cat:: no file given"); } DEBUGF ('c', state); DEBUGF ('c', words); }
void fn_cd (inode_state& state, const wordvec& words){ DEBUGF ('c', state); DEBUGF ('c', words); if(words.size() > 2) throw yshell_exn("Too many operands"); inode_ptr cwd = state.get_cwd(); state.set_cwd(cwd, words[1]); if(words[1] == "..") {state.directory_path_name.pop_back();} else if (state.directory_path_name.size() > 1) {state.directory_path_name.push_back("/"+words[1]);} else if (state.directory_path_name.size() == 1) {state.directory_path_name.push_back(words[1]);} }
void fn_cat (inode_state& state, const wordvec& words){ DEBUGF ('c', state); DEBUGF ('c', words); if (words.size() >= 2){ inode_ptr cwd = state.get_cwd(); for(unsigned int i = 1; i<words.size(); i++){ string w(words.at(i)); cwd->locate_plain_file(cwd, w); cout << '\n'; } } else if (words.size() == 1){ throw yshell_exn("cat: No filename specified"); } /* else if(words.size() > 2){ throw yshell_exn("cat: Too many operands"); } */ }