All pastes #1417012 Raw Edit

Stuff

public text v1 · immutable
#1417012 ·published 2009-05-09 01:15 UTC
rendered paste body
#ifndef _VARLIST_
#define _VARLIST_

#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <stdlib.h>
#include <windows.h>

using namespace std;
string getbetween(string w, string s, string e,int ind );
string getbetween(string w, int s, string e);
#include "color.cpp"
string ctostr(int);
int ctoint(string);
#define JUMP_SIZE 500
#define SNPOS string::npos
bool strtobool(string);
string booltostr(bool);
void bugmsg(string w){
    //cout << w << endl;
}
struct varobj{
    string name;
    string txt;
};
struct varlistitem{
    string name;
    string txt;
    varlistitem * next;
    varlistitem * last;
    varlistitem * jump;
    varlistitem(){
        name="";
        txt="";
        next=NULL;
        last=NULL;
        jump=NULL;
    }
};
#define VLI varlistitem
#define VLIP varlistitem*
class varlist{
      private:
              VLIP start;
              VLIP end;
              VLIP last;
              VLIP ptrto(string name);
              VLIP bptrto(string name);
              VLIP lastjump;      
              int flag;
              int size;
              int mynum;    
      public:
             string myname;
             string report();
             varlist(string n);
             ~varlist();
             int set(string arg);// arg set, passed name=val
             int set(string name, string value);
          //   int set(string name,bool value){return set(name,booltostr(value));};
             int setb(string name,bool value){return set(name,booltostr(value));};
             int set(string name, int value){return set(name,ctostr(value));};
             int bset(string name, string value);
             int add(string n, string v){return set(n,v);};
             int globalset(string n, string v){return set(n,v);};
             string valueof(string name);
             int ivalueof(string name){return ctoint(valueof(name));};
             bool bvalueof(string name){return strtobool(valueof(name));};
             int load(string fname);
             int bload(string fname,bool a);
             int save(string fname,bool app);
             void output();
             void outnames();
             void outback();
             void appendfile(string fname);
             void clear();
             void tofile(string fname,bool app=false,string delim="\n"){save(fname,app);};
             void fillfromfile(string fname){load(fname);};
             string get(int where);
             bool get(int,string&,string&);
             bool getnext(VLIP&,string&,string&);
             void i_i(VLIP& t){t=start;};
             string get_name(int);
             int length(){return size;};
             string tostring(string,string);
             bool pop(string&,string&);
             bool isin(string);
             bool isset(string w){return isin(w);};
             bool remove(string);
             string tocsl(); 
             int addto(string w,int i);
             int increment(string w);
             string names_csl();
             void name(string n);
             
             
} variables("varliables");

string varlist::report(){
    string ret = "ahhh";
    if(end == NULL){
        ret += "end = null";
    }else{
        ret+= "end not null";
        if(end->next == NULL)
            ret+="end next = null";
        else
            ret+="end next not null";
        
    }
    ret+="\n";
    
    if(start == NULL){
        ret+="start = null";
    }else{
        ret+= "s not null";
        if(start->next == NULL)
            ret+="s next = null";
        else
            ret+="s next not null";

    }
    ret +="\nsize="+ctostr(size);
    VLIP t = start;
    int i = 0;
    while(t!=end){
        t=t->next;
        i++;
    }
    ret+="\nactual size="+ctostr(i);
    return ret;
}

bool varlist::getnext(VLIP& t,string& a, string& b){
    if(start==NULL)return false;
    if(t==NULL){
        t=start;
        return false;
    }
    a = t->name;
    b = t->txt;
    t=t->next;
    return true;
}
    
    
string varlist::names_csl(){
   // cout << "nc size: "<<size<<endl;
    VLIP temp = start;
    string csl="";
    //cout << "begin checking"<<endl;
    if(temp==NULL) return "";
    csl+=temp->name+",";
    //cout << "loop strat"<<endl;
    while((temp=temp->next)!=NULL){
       // cout << "loop again"<<endl;
        csl+=temp->name+",";
    }
    //cout << "csl: "<<csl<<endl;
    return csl;
}

int varlist::addto(string w,int i){
    VLIP temp = ptrto(w);
    if(flag==3){
        temp->txt = ctostr(ctoint(temp->txt)+i);
        return ctoint(temp->txt);
    }
    return -1;
}
int varlist::increment(string w){
    addto(w,1);
}
    
bool varlist::remove(string n){
    string a,b;
    if(start != NULL && n==start->name){
        pop(a,b);
        return true;
    }
    if(end != NULL && n==end->name){
        VLIP t=end;
        end = end->last;
        if(end!=NULL)end->next=NULL;
        size--;
        delete t;
        return true;
    }
    VLIP t = ptrto(n);
    if(flag==3){// found exact match
        VLIP tt = t->last;
        if(tt != NULL){
            tt->next = t->next;
        }
        if(t->next != NULL){
            t->next->last = tt;
        }
        size--;
        delete t;
        return true;
    }
    return false;
}
void varlist::outnames(){
    VLIP tm = start;
    while(tm != NULL){
        cout << tm->name<<endl;
        tm=tm->next;
    }
}
        
bool varlist::isin(string a){
    ptrto(a); 
    if(flag==3)return true;
    return false;
}
bool varlist::get(int x,string &n,string &v){
    if(x>size)return false;
    VLIP t = start;
    for(int i = 0; i < x && t!=NULL; i++){
        t = t->next;
    }
    if(t== NULL){
        n="";
        v="";
//        cout << "went past end"<<endl;
        return false;
    }
    string b = t->txt,xm;
    v = b;
    xm=t->name;
    n = xm;
//    cout <<"reached end of get with "<<n << "  " << v<<endl;
    return true;
}
        

bool varlist::pop(string& n, string &v){
    if(start == NULL){
        n="";
        v="";
        return false;
    }
    VLIP temp = start;
    if(end==start){
        if(size != 0)
        cout << "varlist pop issue #1"<<endl;
        delete temp;
        start=NULL;
        end=NULL;
        size=0;
        return true;
    }
    start = start->next;
    n = temp->name;
    v = temp->txt;
    if(start != NULL)start->last = NULL;
    if(start == NULL)end=NULL;
    size--;
    temp->name="";
    temp->txt="";
    delete temp;
    return true;
}


string varlist::tostring(string md = "=",string ed=";"){
    VLIP t = start;
    string ret = "";
    while(t != NULL){
        ret += t->name + md+t->txt+ed;
        t=t->next;
    }
    return ret;
}

string varlist::get(int where){
    VLIP t = start;
    while(t != NULL && where>0){
        t = t->next;
        where--;
    }
    if(t== NULL)return "";
    return t->name+"="+t->txt;
}
string varlist::get_name(int w){
    VLIP t = start;
    while(t != NULL && w>0){
        t = t->next;
        w--;
    }
    if(t== NULL)return "";
    return t->name;
}
#define cl() __LINE__ <<"   \r"
void varlist::clear(){
    int num=0;
    //cout << "size: "<<size<<endl;
    
    while(1){

        //num++;
        
        VLIP temp = start;
        //cout << cl();
        if(temp==NULL)break;
       // cout << cl();
        start->last=NULL;
        start = start->next;
       // cout << cl();
        temp->name="";
       // cout << cl();
        temp->txt="";
        //cout << cl();
        delete temp;
       // cout<<num<<"\r    ";
    }
    //cout << "cleared "<<num << "\n";
    start = NULL;
    end=NULL;
    size=0;
    
}
    
 //   string a,b;
   // while(pop(a,b));

    
    //how the hell did I think this was gunna work??:
    //VLIP temp = start;
//    VLIP tt = temp;
//    while(temp != NULL){
//        tt = temp;
//        temp = temp->next;
//        delete tt;
//    }
//}
void writefile(string f,string p){
    ofstream ofile(f.c_str());
    ofile << p;
    ofile.close();
}
//end result of slval: returns lval of arg if found, b becomes rval
string slval(string &w,string &b){
//    cout<< "slval:"<<w;
//    writefile("slval.txt",w);
    int x = w.find("=",0);
    if(x==SNPOS)return "";
    string t = w.substr(0,x);
    b = w.substr(x+1);
//    cout <<" "<<t<<" "<<b<<endl;
//    Sleep(1000);
    return t;
}

    
//    
//    
//    string t = "",r="";
//    int i =0;
//    while(i<w.length()){
//        if(w[i]=='=')break;
//        t+=w[i];
//        i++;
//    }
//    i++;
//    while(i < w.length()){
//        r+=w[i];
//        i++;
//    }
//    b=r;
//    return t;
//}
 
string srval(string w){
    if(w=="")return "";
    string t ="";
    int i =0;
    while(i<w.length()){
        if(w[i]=='=')break;
        i++;
    }
    i++;
    while(i<w.length()){
        t+=w[i];
        i++;
    }
    return t;
}
int lines(string f){
    ifstream ifile(f.c_str());
    if(!ifile)return 0;
    string t;
    int i = 0;
    while(!ifile.eof()){
        getline(ifile,t);
        i++;
    }
    return i;
}
int varlist::bload(string fname,bool q=false){
    int l = 0;
    if(!q){
        cout << "load process starting \r";
        l = lines(fname);
        cout << "                      \rfile "<<fname<<" has "<<l<<" entries";
    }
    ifstream ifile(fname.c_str());
    if(!ifile){
        return 0;
    }
    string temp, tt,n;
    int all = 0, tot = 0;
    cout << endl;
    int i = 0;
    while(!ifile.eof()){
        getline(ifile,temp);
        tt = slval(temp,n);
        tot +=bset(tt,n);
        if(i % 100 == 0 && !q) percentturner(i,l);
        i++;
    }
    ifile.close();
    return tot;
}
int varlist::load(string fname){
//    cout << "load process starting \r";
    int l = lines(fname);
//    cout << "                      \rfile "<<fname<<" has "<<l<<" entries";
    ifstream ifile(fname.c_str());
    if(!ifile){
        return 0;
    }
    string temp, tt,n;
    int all = 0, tot = 0;
    cout << endl;
    int i = 0;
    while(!ifile.eof()){
        getline(ifile,temp);
        tt = slval(temp,n);
        tot += set(tt,n);
        if(i % 100 == 0) percentturner(i,l);
        i++;
    }
    ifile.close();
    return tot;
}
int varlist::save(string fname,bool app=false){
    ofstream ofile;
    if(app)ofile.open(fname.c_str(),ios::app);
    else   ofile.open(fname.c_str());
    
    VLIP temp = start;
    while(temp != NULL){
        
        ofile << temp->name<< "="<<temp->txt<< "\n";
        temp=temp->next;
    }
    ofile.close();
}
void varlist::appendfile(string fname){
    ofstream ofile(fname.c_str(),ios::app);
    VLIP temp = start;
    while(temp != NULL){
        ofile << temp->name;
        if(temp->txt != "") ofile << "="<<temp->txt;
        ofile << "\n";
        temp=temp->next;
    }
    ofile.close();
}
void setvlip(VLIP temp,string name, string value){
    temp->name = name;
    temp->txt = value;
    temp->next = NULL;
    temp->last = NULL;
    temp->jump = NULL;
}
void varlist::output(){
    VLIP temp = start;
    while(temp != NULL){
        cout << temp->name << "=" << temp->txt << endl;
        temp = temp->next;
    }
}
void varlist::outback(){
    VLIP temp = end;
    while(temp != NULL){
        cout << temp->name << "=" << temp->txt << endl;
        temp = temp->last;
    }
}    
string varlist::valueof(string name){
    VLIP temp = ptrto(name);
    if(flag != 3) return "";
    return temp->txt;
}
int varlist::set(string arg){
    string t,tt;
    t= slval(arg,tt);
    set(t,tt);
}
    
int varlist::set(string name, string value){
    VLIP temp = new VLI;
    setvlip(temp,name,value);
    VLIP ptr = ptrto(name);
    if(flag==3){ 
        if(ptr != NULL)ptr->txt=value;
        delete temp;
        return 0;
    }
    if(flag == 4){
        cout << "some problem occured, flag 4 returned\n";
        delete temp;
        return 0;
    }
    if(size+1 % JUMP_SIZE==0){
        
        lastjump->jump = end;
        lastjump = end;
    }
    size++;
    if(flag == 0){ // flag 0 = set as start
        temp->next = start;
        if(start != NULL)start->last = temp;
        else end = temp;
        start = temp;
        return 1;
    }
    if(flag == 2){// flag 2 = add before ptr
        temp->last = ptr->last;
        VLIP Tt = ptr->last;
        Tt->next = temp;
        ptr->last = temp;
        temp->next = ptr;
        return 1;
    }
    if(flag == 1){// flag 1 = add after end
        temp->last = end;
        end->next = temp;
        end = temp;
        return 1;
    }
    
    return 0;
}
int varlist::bset(string name, string value){
    VLIP temp = new VLI;
    setvlip(temp,name,value);
    VLIP ptr = ptrto(name);
    if(flag==3){ 
        if(ptr != NULL)ptr->txt=value;
        return 0;
    }
    if(flag == 4){
        cout << "some problem occured, flag 4 returned\n";
    }
    if(size+1 % JUMP_SIZE==0){
        
        lastjump->jump = end;
        lastjump = end;
    }
    size++;
    if(flag == 0){ // flag 0 = set as start
        temp->next = start;
        if(start != NULL)start->last = temp;
        else end = temp;
        start = temp;
        return 1;
    }
    if(flag == 2){// flag 2 = add before ptr
        temp->last = ptr->last;
        VLIP Tt = ptr->last;
        Tt->next = temp;
        ptr->last = temp;
        temp->next = ptr;
        return 1;
    }
    if(flag == 1){// flag 1 = add after end
        temp->last = end;
        end->next = temp;
        end = temp;
        return 1;
    }
    
    return 0;
}
varlist::~varlist(){
    clear();
}
varlist *lists[100];
int varlistnum=0;

int reg_vl(varlist* var){
 lists[varlistnum]=var;
 varlistnum++;
 return varlistnum-1;
}
void reg_vl_file(string fn){
    
    string ft;
    for(int i = 0; i < varlistnum; i++){
        ft = lists[i]->myname+fn;
        lists[i]->save(ft);
        cout << lists[i]->myname<<": "<<lists[i]->length()<<endl;
    }
}
void varlist::name(string name){
    myname=name;
}
varlist::varlist(string name=""){
    start = NULL;
    end = NULL;
    last = NULL;
    flag = -1;
    size=0;
    myname=name;
    mynum=reg_vl(this);
}
int ilowern(string o, string n){
    if(o > n)return 0;
    if(n > o)return 1;
    return 2;
}

VLIP varlist::ptrto(string name){
     if(start == NULL || ilowern(name,start->name)==1 ){ // before start
            flag = 0;
            return start;
      }
      
      if(end == NULL || ilowern(name,end->name)==0){ // after end
            flag = 1; 
            return end;
       }
      varlistitem* temp = start,*watch=NULL;
      int ilt=0,m=0;
      while(temp->jump != NULL){
            watch = temp;
            temp = temp->jump;
            if(ilowern(name,temp->name)==1){
                temp = watch;
                break;
            }
        }
      while(temp != NULL){ 
            ilt = ilowern(name, temp->name);
            if(ilt != 0){ // after or == temp
                if(ilt == 2) flag = 3;
                else flag = 2;
                return temp;
            }
            
            m++;
            temp = temp->next;
       }
       flag = 4;
       return NULL;
}

VLIP advance(VLIP t,int n){
    int i = 0;
    while(t!=NULL && i < n/2){
        t = t->next;
        i++;
    }
    VLIP ret = t;
    while(t!=NULL && i < n){
        t=t->next;
        i++;
    }
    return ret;
}
VLIP retreat(VLIP t,int n){
    int i = 0;
    while(t!=NULL && i < n/2){
        t = t->last;
        i++;
    }
    VLIP ret = t;
    while(t!=NULL && i < n){
        t=t->last;
        i++;
    }
    return ret;
}

VLIP varlist::bptrto(string name){
    
      if(start == NULL || ilowern(name,start->name)==1 ){ // before start
            flag = 0;
            return start;
      }
      
      if(end == NULL || ilowern(name,end->name)==0){ // after end
            flag = 1; 
            return end;
       }
       VLIP temp = start;
       int left = size/2,x=0,last=1;
       VLIP watch = advance(temp,left);
       while(left > 10){
            x=ilowern(name,temp->name);
            if(x==2){
                flag = 3;
                return temp;
            }
            left/=2;
            if(x==1){
                if(last == 0){
                    temp = watch;
                    last = 2;
                }
                else {
                    last = 1;
                    watch = advance(temp,left);
                }
            }
            else{
                if(last == 1){
                    temp = watch;
                    last = 2;
                }
                else{
                    last = 0;
                    watch = retreat(temp,left);
                }
            }
        }
        x = ilowern(name,temp->name);
        if(x == 1){
            for(int i = 0; i < 15; i++){
                if(temp!= NULL){
                    temp=temp->next;
                }else{
                    flag=4;
                    return NULL;
                }
                if(ilowern(name,temp->name)==0){
                    flag=2;
                    return temp;
                }
                if(name==temp->name){
                    flag=3;
                    return temp;
                }
            }
        }
        if(x == 0){
            for(int i = 0; i < 15; i++){
                if(temp!= NULL){
                    temp=temp->next;
                }else{
                    flag=4;
                    return NULL;
                }
                if(ilowern(name,temp->name)==1){
                    flag=2;
                    return temp->next;
                }
                if(name==temp->name){
                    flag=3;
                    return temp;
                }
            }
        }
        flag=4;
        return NULL;
    }
    
string varlist::tocsl(){
    VLIP t=start;
    string ret = "";
    while(t != NULL){
        ret+=t->name+"="+t->txt+",";
        t=t->next;
    }
    return ret;
}

int csltovlist(string csl,varlist &v){
    int ind = 0,ind2=0,ind3,i=0;
    while(1){
        ind3=ind;
        ind = csl.find("=",ind+1);
        if(ind==SNPOS)break;
        ind2 = csl.find(",",ind+1);
        if(ind2==SNPOS){
            v.set(csl.substr(ind3,ind-ind3),csl.substr(ind));
        }
        else
            v.set(csl.substr(ind3,ind-ind3),csl.substr(ind,ind2-ind));
        i++;
    }
    return i;
}

/* CSL methods:
    Comma Seperated List
    these methods allow you to store lists within strlist and varlist variables
    */
    

    

#endif