Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <list>
- #include <string>
- #include <fstream>
- /* alist.cpp */
- /* g++ filename -std=17 */
- using std::cin,std::cout,std::endl,
- std::cerr,std::list,std::getline,
- std::string,std::ios,std::fstream;
- /*
- for c++ 11 comment out the using line above
- and uncomment the line below and make
- further adjustments if needed maybe fstream
- or just compile it like it is and watch it work
- */
- // using namespace std;
- void usage();
- int parse_options( list<int> &l,
- string options,fstream &file);
- int search_list(list<int> &s,int v);
- void stars(int s);
- int data_storage(fstream &d);
- int write_to_file(list<int> &l,
- fstream &file);
- int print_file(fstream &file);
- void windex();
- /* work in progress */
- int
- main () {
- char run='y';
- list<int> the_list{};
- string options;
- /*
- let's save this crap
- data in a file, and
- recall it for further
- processing
- appending to file
- clearing file
- updating file
- */
- string crap="alist_crap_data_file";
- /* works just fine here
- but not in a function
- BS
- */
- fstream
- f(crap,ios::in|ios::out|ios::app);
- if(data_storage(f))
- cout<<"that worked!\n";
- else
- cout<<"ha crap \n";
- while(run=='y'){
- usage();
- cout<<"enter your slection"
- <<endl;
- getline(cin,options);
- if(!parse_options(the_list,options,f))
- run='n';
- } // end while
- return 0;
- } // end main
- int parse_options(list<int> &l,string options, fstream &file){
- int error=-1;
- char c='\0';
- int pos,value=0;;
- string crap,opt,val;
- /* find the command */
- if( (pos=options.find("-") !=
- string::npos)){
- crap=options.substr(pos);
- opt=crap.substr(0,pos);
- val=options.substr(pos + 1);
- if(opt.size() == 1)
- c = opt[0];
- try{
- if(val.size()>=1)
- value=std::stoi(val);
- }catch(std::invalid_argument& stoi){
- cout<<
- "terminating"
- " exception of type"
- " std::invalid_argument:"
- " stoi: no conversion"
- <<endl;
- /* just drop it into default
- switch and give feed back
- of orginal command given */
- c='?';
- } // end try catch
- }
- switch(c){
- case 'i':
- windex();
- stars(45);
- cout<<"insert\n";
- stars(45);
- if(value>=1 &&
- value<=pow(10,3)){
- l.push_back(value);
- error=1;
- }else{
- windex();
- stars(45);
- cout<<"Value "
- <<value
- <<" is not within limits "
- <<"1 - "
- <<pow(10,3)
- <<endl;
- stars(45);
- error=1;
- }
- break;
- case 'd':
- windex();
- stars(45);
- cout<<"delete\n";
- stars(45);
- break;
- case 'r':
- windex();
- stars(45);
- cout<<"refresh\n";
- stars(45);
- break;
- case 's':
- windex();
- stars(45);
- cout<<"searching "
- <<endl;
- stars(45);
- if(
- search_list(l,value)
- ){
- windex();
- stars(54);
- cout<<"found "
- <<value
- <<endl;
- stars(54);
- error=1;
- }else{
- windex();
- stars(45);
- cout<<"not found"
- <<endl;
- stars(45);
- error=1;
- }
- break;
- case 'x':
- windex();
- stars(45);
- cout<<"search and destroy[ete]"
- <<endl;
- stars(45);
- break;
- case 'q':
- windex();
- stars(54);
- cout<<"QUITING: ohhh nooooo!\n";
- stars(54);
- error=0;
- file.close();
- break;
- case 'w':
- windex();
- stars(45);
- cout<<"write to file\n";
- stars(45);
- write_to_file(l,file);
- break;
- case 'p':
- windex();
- stars(56);
- cout<<"print file\n";
- print_file(file);
- break;
- default:
- windex();
- stars(45);
- cerr
- <<"Invalid command "
- <<options
- <<endl;
- stars(45);
- error=1;
- break;
- } // end switch
- /* left overs
- for(auto it : the_list){
- cout<<"list "<<it<<endl;
- }
- */
- if(error)
- return 1;
- else
- return 0;
- } // end parse
- int search_list(list<int> &s,int v){
- auto it =
- find(s.begin(),s.end(),v);
- if(it != s.end())
- return 1;
- else
- return 0;
- }
- void usage(){
- cout<<"\nUSAGE:\n"
- <<" <option> [argument] \n"
- <<endl
- <<"insert < -i > [argumen]\n"
- <<"delete < -d > no argument\n"
- <<"refresh < -r > no argument\n"
- <<"search < -s > [ argument ]\n"
- <<"search and delete:\n"
- <<"\t"<<"< -x > [ argument ]\n"
- <<"write to file\n"
- <<"\t"<<"< -w > no argument\n"
- <<"print file\n"
- <<"\t"<<"< -p > no argument\n"
- <<"quit < -q >"
- <<endl;
- }
- void stars(int s){
- for(int i=0;i<s;i++)
- cout<<"*";
- cout<<endl;
- }
- int data_storage(fstream &d){
- // works in main just not in function???
- //fstream
- //d(s,ios::in|ios::out|ios::app);
- if(!d.is_open())
- return 0;
- else
- return 1;
- }
- int write_to_file(list<int> &l,fstream &file){
- if(file){
- for(auto const &it:l){
- file << it << endl;
- }
- return 1;
- }else{ return 0;}
- }
- int print_file(fstream &file){
- if(file){
- string st;
- file.seekp(0,ios::beg);
- stars(55);
- while(getline(file,st)){
- cout<< st <<endl;
- stars(55);
- return 1;
- }
- }else
- return 0;
- return -1;
- }
- void windex(){
- #ifdef __linux__
- system("clear");
- #elif _WIN32
- system("cls");
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement