void change_path(string command) { stringstream command_stream(command); string subcommand, cd_path; vector<string> subpath; vector<int> new_path; int tmp_path; command_stream >> subcommand; command_stream >> cd_path; if (cd_path == "..") { if (path.size() == 1) return; path.pop_back(); } else if (cd_path[0] == '/') { if (cd_path == "/") { new_path.push_back(0); path = new_path; return; } tmp_path = 0; subpath = split(cd_path, "/"); new_path.push_back(0); for (auto p : subpath) { if (tmp_path = check_folder(tmp_path, p)) { new_path.push_back(tmp_path); } else { cout << "no such directory" << endl; return; } } path = new_path; } else { tmp_path = path.back(); subpath = split(cd_path, "/"); new_path = path; for (auto p : subpath) { if (tmp_path = check_folder(tmp_path, p)) { new_path.push_back(tmp_path); } else { cout << "no such directory" << endl; return; } } path = new_path; } }
std::tuple<std::string, std::string> splitFirstWhiteSpace(const std::string line) { std::string head, tail; std::istringstream command_stream(line); command_stream >> head >> std::ws; std::getline(command_stream, tail); return std::make_tuple(head, tail); }
void create_folder(string command) { information new_information; address new_address; cataLog new_catalog; child_catalog new_child; stringstream command_stream(command); string subcommand, folder_name, share; command_stream >> subcommand; command_stream >> folder_name; command_stream >> share; if (folder_name == "") { cout << "mkdir: Please enter a directory name" << endl; return; } if (check_folder(path.back(), folder_name)) { cout << "mkdir: Cannot create folder \"" << folder_name << "\":directory exists" << endl; return; } new_information.name = folder_name; new_information.type = 1; new_information.ftype = "folder"; new_information.share = 1; new_information.readable = 1; new_information.writeable = 1; new_information.user = username_id; new_information.size = 0; new_information.block = -1; char date[255]; time_t t = time(0); strftime(date, 255, "%Y-%m-%d %H:%M:%S", localtime(&t)); new_information.create_time = date; new_information.last_edit_time = date; new_information.path = path; new_address.flag = 1; new_address.i_node = -1; if (catalog_free.size() == 0) { new_catalog.id = catalog.size(); new_catalog.addr = new_address; new_catalog.info = new_information; catalog.push_back(new_catalog); } else { new_catalog.id = catalog_free.back(); new_catalog.addr = new_address; new_catalog.info = new_information; catalog[catalog_free.back()] = new_catalog; catalog_free.pop_back(); } new_child.name = folder_name; new_child.id = new_catalog.id; catalog[path.back()].addr.c_catalog.push_back(new_child); }
int main() { User new_user; //初始化用户信息 new_user.username = "******"; new_user.password = "******"; user.push_back(new_user); string username; string password; string command; string subcommand; string temp_path; char t; init(); save(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN); cout << "welcome to system" << endl; cout << "please login" << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED); while (1) { username = ""; password = ""; cout << "username:"******"password:"******"username or password error" << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED); } } system("cls"); getline(cin, command); while (1) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE); cout << username; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED); cout << "@filesystem "; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE); if (path.size() == 1) cout << "/"; else { for (auto p : path) { if (p == 0) continue; cout << "/" << catalog[p].info.name; } } SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED); cout << endl << ">"; getline(cin, command); stringstream command_stream(command); command_stream >> subcommand; if (subcommand == "") { } else if (subcommand == "cd") { change_path(command); } else if (subcommand == "ls") { show_folder(command); } else if (subcommand == "create") { create(command); } else if (subcommand == "mkdir") { create_folder(command); } else if (subcommand == "delete") { delete_file(command); } else if (subcommand == "edit") { edit(command); } else if (subcommand == "search") { search_file(command); } else if (subcommand == "open") { open_file(command); } else if (subcommand == "close") { close_file(command); } else if (subcommand == "cp") { copy(command); } else if (subcommand == "mv") { move(command); } else if (subcommand == "bs"){ backstage(); } else if (subcommand == "l"){ l(); } else if (subcommand == "info"){ info(); } else if (subcommand == "format") { remove("disk.txt"); remove("diskdata.txt"); init(); } else { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED); cout << "command not found" << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED); } cout << endl; save(); command = ""; subcommand = ""; } }
int main() { string username; string password; string command; string subcommand; string temp_path; char t; load_user(); init(); save(); while (1) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN); cout << "welcome to system" << endl; cout << "please login" << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); while (1) { username = ""; password = ""; cout << "username:"******"password:"******"username or password error" << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); } } system("cls"); getline(cin, command); while (1) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); cout << username; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED); cout << "@filesystem "; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE); if (path.size() == 1) cout << "/"; else { for (auto p : path) { if (p == 0) continue; cout << "/" << catalog[p].info.name; } } SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); cout << endl << ">"; getline(cin, command); stringstream command_stream(command); command_stream >> subcommand; if (subcommand == "") { } else if (subcommand == "cd") { change_path(command); } else if (subcommand == "ls") { show_folder(command); } else if (subcommand == "create") { create(command); } else if (subcommand == "mkdir") { create_folder(command); } else if (subcommand == "delete") { delete_file(command); } else if (subcommand == "edit") { edit(command); } else if (subcommand == "search") { search_file(command); } else if (subcommand == "open") { open_file(command); } else if (subcommand == "close") { close_file(command); } else if (subcommand == "cp") { copy(command); } else if (subcommand == "mv") { move(command); } else if (subcommand == "bs"){ backstage(); } else if (subcommand == "l"){ l(); } else if (subcommand == "info"){ info(); } else if (subcommand == "format") { char c; cout << "Are you sure you want to format the disk?(y/N)"; c = _getch(); if (c == 'n' || c == 'N' || c == '\r') { cout << c << endl; continue; } else if (c == 'y' || c == 'Y') { remove("disk.txt"); remove("diskdata.txt"); init(); cout << c << "\nformat complete" << endl; } } else if (subcommand == "useradd") { add_user(command); } else if (subcommand == "userdelete") { delete_user(command); } else if (subcommand == "passwd") { change_password(command); } else if (subcommand == "clear") { system("cls"); } else if (subcommand == "exit") { system("cls"); break; } else { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); cout << "command not found" << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); } cout << endl; save(); command = ""; subcommand = ""; } } }
/** \brief the main program interacting with the user. The program is command line interactive. */ int main(int argc, char * const argv[]){ mapp::driver d; d.insert("hello",hello_execute); d.insert("synapse", nest::synapse_execute); d.insert("event",event_execute); // d.insert("spike",spike_execute); d.insert("kernel",coreneuron10_kernel_execute); d.insert("solver",coreneuron10_solver_execute); d.insert("cstep",coreneuron10_cstep_execute); d.insert("keyvalue",keyvalue_execute); d.insert("replib",replib_execute); //direct run if(argv[1] != NULL){ try { d.execute(argc,argv); } catch(mapp::driver_exception & e) { if(e.error_code != mapp::MAPP_USAGE) std::cerr << "caught exception: " << e.what() << "\n"; } catch(std::exception & e) { std::cerr << "caught exception: " << e.what() << "\n"; } return 0; } std::cout << "Welcome to NeuroMapp! Please enter " << "the name of the miniapp you wish to execute " << "followed by any arguments you wish to pass to it." << std::endl << "Please avoid preceding and trailing whitespace!" << std::endl << "To finish type quit" << std::endl << ">? "; // interactive run while(1) { #ifdef NEUROMAPP_CURSOR char* input = readline(""); add_history(input); std::string command(input); #else std::string command; std::getline(std::cin, command); #endif // I need to split the string into an arranesty of strings to pass it // in an argv style std::vector<std::string> command_v; command_v.push_back(argv[0]); std::istringstream command_stream(command); std::istream_iterator<std::string> wb(command_stream),we; std::copy(wb,we,std::back_inserter(command_v)); if( command_v[1].compare("quit") == 0 ) break; mapp::argv_data A(command_v.begin(),command_v.end()); try { d.execute(A.argc(),A.argv()); } catch(mapp::driver_exception & e) { if(e.error_code != mapp::MAPP_USAGE) std::cerr << "caught exception: " << e.what() << "\n"; } catch(std::exception & e) { std::cerr << "caught exception: " << e.what() << "\n"; } std::cout << std::endl << ">? "; } return 0; }
void add_user(string command) { stringstream command_stream(command); string subcommand, username, password, c_password; command_stream >> subcommand; command_stream >> username; int i = 0; if (username_id != 0) { cout << "You don't have permission to create a user" << endl; return; } if (user.size() == 8) { cout << "User full" << endl; return; } while (i < 3) { cout << "password:"******"retype password:"******"Sorry, passwords do not match." << endl; i++; } else break; } if (i == 3) { cout << "Have exhausted maximum number of retries for service." << endl; return; } User new_user; new_user.id = user.size(); new_user.username = username; char *passwd = new char[255]; for (i = 0; i <= password.length(); i++) passwd[i] = password[i]; passwd[i] = '\0'; new_user.password = MD5Encode(passwd); user.push_back(new_user); cout << "user " << username << " add successfully" << endl; save_user(); }
void change_password(string command) { stringstream command_stream(command); string subcommand, username, password, c_password; command_stream >> subcommand; command_stream >> username; int j = 0; int num; for (num = 0; num < user.size(); num++) { if (user[num].username == username) break; } if (num == user.size()) { cout << "Username no fount" << endl; return; } if (username_id != 0 && username_id != num) { cout << "You can't change other's password" << endl; return; } int i = 0; while (i < 3) { cout << "password:"******"retype password:"******"Sorry, passwords do not match." << endl; i++; } else break; } if (i == 3) { cout << "Have exhausted maximum number of retries for service." << endl; return; } char *passwd = new char[255]; for (i = 0; i <= password.length(); i++) passwd[i] = password[i]; passwd[i] = '\0'; user[num].password = MD5Encode(passwd); save_user(); cout << "password changed successfully" << endl; }
void delete_user(string command) { if (username_id != 0) { cout << "You don't have permission to delete user" << endl; return; } stringstream command_stream(command); string subcommand, username, password; command_stream >> subcommand; command_stream >> username; int j = 0; if (username == "admin") { cout << "You can't delete admin user" << endl; return; } int num; for (num = 0; num < user.size(); num++) { if (user[num].username == username) break; } if (num == user.size()) { cout << "Username no fount" << endl; return; } while (j < 3) { cout << "Please enter admin password to confirm:"; char t; while ((t = _getch()) != '\r') { if (t == '\b') { password = password.substr(0, password.length() - 1); } else password += t; } cout << endl; char *passwd = new char[255]; int i; for (i = 0; i <= password.length(); i++) passwd[i] = password[i]; passwd[i] = '\0'; if (user[username_id].password == MD5Encode(passwd)) break; cout << "Password error. "; j++; } if (j == 3) { cout << "Have exhausted maximum number of retries for service." << endl; return; } vector<User>::iterator it; it = user.begin() + num; user.erase(it); save_user(); cout << "User " << username << " delete successfully" << endl; }
void create(string command) { child_catalog child_file; information file_info; stringstream command_stream(command); string command1, name, size, share; command_stream >> command1; command_stream >> name; command_stream >> size; command_stream >> share; int zhengsize = 0; if (name == "") { cout << "Please input filename" << endl; return; } else { //判断重名 if (check_file(name, path.back())) { cout << "create: Cannot create file \"" << name << "\":file exists" << endl; return; } //写入文件信息,磁盘i节点信息 file_info.name = name; file_info.type = 0; char date[255]; time_t t = time(0); strftime(date, 255, "%Y-%m-%d %H:%M:%S", localtime(&t)); file_info.last_edit_time = file_info.create_time = date; //cout << file_info.create_time << endl; file_info.user = 0; if (size == "") { zhengsize = 512; } else if ((size[(size.size() - 1)] == 'k') || size[(size.size() - 1)] == 'K') { for (int i = size.size() - 2; i >= 0; i--) { zhengsize = zhengsize + (size[i] - '0') * (int)pow(10, size.size() - 2 - i); } } else if ((size[(size.size() - 1)] == 'm') || size[(size.size() - 1)] == 'M') { for (int i = size.size() - 2; i >= 0; i--) { zhengsize += (size[i] - '0') * (int)pow(10, size.size() - 2 - i); } zhengsize = zhengsize * 1000; } else { cout << "size is illegal" << endl; return; } file_info.size = zhengsize; disk_Index new_disk_index; //分配磁盘块 new_disk_index.block = allocata(file_info.size); if (new_disk_index.block.size() == 0) { return; } if (share == "n") { file_info.share = 0; file_info.readable = 0; file_info.writeable = 0; } else if (share == "swr" || share == "srw" || share == "") { file_info.share = 1; file_info.readable = 1; file_info.writeable = 1; } else if (share == "sr") { file_info.share = 1; file_info.readable = 1; file_info.writeable = 0; } else{ cout << "command is not exists" << endl; return; } free_i = ialloc(); if (free_i == -1) { return; } //判断文件类型 string ftype unsigned int i; for (i = name.size(); i > 0; i--) { if (name[i] == '.') { unsigned int j = i; while (j < name.size() - 1) { file_info.ftype += file_info.name[++j]; } break; } } if (i == 0) file_info.ftype = "file"; //cout << file_info.ftype << endl; file_info.path = path; if (disk_index_free.size() == 0) file_info.block = disk_index.size(); else file_info.block = disk_index_free.back(); index[free_i].info = file_info; INAMEindex new_inameindex; //建立文件名索引 new_inameindex.id = free_i; new_inameindex.name = index[free_i].info.name; inameindex.push_back(new_inameindex); IDATEindex new_idateindex; //建立日期索引 new_idateindex.id=free_i; new_idateindex.date=index[free_i].info.last_edit_time; idateindex.push_back(new_idateindex); ITYPEindex new_itypeindex; //建立类型索引 new_itypeindex.id = free_i; new_itypeindex.type = index[free_i].info.ftype; itypeindex.push_back(new_itypeindex); cataLog new_catalog; if (catalog_free.size() != 0) { new_catalog.id = catalog_free.size(); new_catalog.info = file_info; new_catalog.addr.flag = 0; new_catalog.addr.i_node = free_i; catalog[catalog_free.back()] = new_catalog; catalog_free.pop_back(); } else { new_catalog.id = catalog.size(); new_catalog.info = file_info; new_catalog.addr.flag = 0; new_catalog.addr.i_node = free_i; catalog.push_back(new_catalog); } child_file.id = new_catalog.id; child_file.name = name; catalog[path.back()].addr.c_catalog.push_back(child_file); if (disk_index_free.size() == 0) disk_index.push_back(new_disk_index); //磁盘索引内容增加 else { disk_index[disk_index_free.back()] = new_disk_index; disk_index_free.pop_back(); } } }