Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

msd_datatypes.cc

00001 //--------------------------------
00002 // msd_datatypes.cc
00003 //
00004 // Project: PDBe API Framework, EBI 
00005 // Module: Implementation of Data Layer's Data type Classes 
00006 // and utilities for PDBe API Framework 
00007 //
00008 // Data Layer
00009 // Last updated: 25 February 2004 10:17
00010 // (C) Siamak Sobhany
00011 //--------------------------------
00012 
00013 #ifndef __MSD_DATATYPES_H__
00014 #include "msd_datatypes.h"
00015 #endif
00016 
00017 
00018 msd_anyType::msd_anyType()
00019 {
00020 }
00021 msd_anyType::~msd_anyType()
00022 {
00023 }
00024 msd_anyType*& msd_anyType::operator[](int i)
00025 { static msd_anyType *p = this; 
00026   return p;
00027 }
00028 
00029 void msd_anyType::print(std::ostream &s)
00030 {}
00031 
00032 
00033 void msd_anyType::setst(msdbStream &s)
00034 {}
00035 
00036 
00037 void msd_anyType::getst(msdbStream &s)
00038 {}
00039 let msd_anyType::getval()
00040 {}
00041 
00042 
00043 msd_anySimpleType::msd_anySimpleType()
00044 {}
00045 msd_anySimpleType::~msd_anySimpleType()
00046 {}
00047 void msd_anySimpleType::print(std::ostream &s) 
00048 {}
00049 
00050 void msd_anySimpleType::setst(msdbStream &s)
00051 {}
00052 
00053 
00054 void msd_anySimpleType::getst(msdbStream &s)
00055 {}
00056 let msd_anySimpleType::getval()
00057 {}
00058 
00059 
00060 msd_datetime::msd_datetime()
00061 {}
00062 msd_datetime::~msd_datetime()
00063 {}
00064 msd_datetime::msd_datetime(otl_datetime &t)
00065 { msd_item = t; }
00066 
00067 void msd_datetime::print(std::ostream &s) 
00068 { s << msd_item.year;}
00069 void msd_datetime::setst(msdbStream &s) 
00070 { s << msd_item; }
00071 
00072 void msd_datetime::getst(msdbStream &s)
00073 { s >> msd_item;
00074 }
00075 let msd_datetime::getval()
00076 { 
00077 return  msdConst(msd_item.year);
00078 }
00079 
00080 msd_double::msd_double()
00081 { }
00082 msd_double::~msd_double()
00083 { }
00084 msd_double::msd_double(double d)
00085 { msd_item = d; }
00086 
00087 void msd_double::print(std::ostream &s) 
00088 { s << msd_item;}
00089 void msd_double::setst(msdbStream &s) 
00090 { s << msd_item; }
00091 
00092 void msd_double::getst(msdbStream &s) 
00093 { s >> msd_item;
00094 }
00095 let msd_double::getval() 
00096 { 
00097 return  msdConst(msd_item);
00098 }
00099 
00100 msd_float::msd_float()
00101 { }
00102 msd_float::~msd_float()
00103 { }
00104 msd_float::msd_float(float f)
00105 { msd_item = f; }
00106 
00107 void msd_float::print(std::ostream &s) 
00108 { s << msd_item;}
00109 void msd_float::setst(msdbStream &s) 
00110 { s << msd_item; }
00111 
00112 void msd_float::getst(msdbStream &s) 
00113 { s >> msd_item;
00114 }
00115 let msd_float::getval() 
00116 { 
00117 return  msdConst(msd_item);
00118 }
00119 
00120 
00121 msd_string::msd_string()
00122 {}
00123 msd_string::~msd_string()
00124 {if (msd_item)
00125  free(msd_item);
00126 }
00127 
00128 msd_string::msd_string(int size)
00129 { 
00130   msd_item=(char*)malloc(size);
00131  
00132 }
00133 
00134 msd_string::msd_string(char* st)
00135 { 
00136   msd_item=(char*)malloc(strlen(st)+1);
00137   sprintf(msd_item,"%s",st);
00138 }
00139 
00140 void msd_string::print(std::ostream &s) 
00141 { s << msd_item;}
00142 void msd_string::setst(msdbStream &s) 
00143 { s << msd_item; }
00144 
00145 void msd_string::getst(msdbStream &s) 
00146 { s >> msd_item;
00147 }
00148 let msd_string::getval() 
00149 {
00150 return  msdConst(msd_item);
00151 }
00152 
00153 
00154 msd_int::msd_int()
00155 { }
00156 msd_int::~msd_int()
00157 { }
00158 msd_int::msd_int(int i)
00159 { msd_item = i; }
00160 
00161 void msd_int::print(std::ostream &s) 
00162 { s << msd_item;}
00163 void msd_int::setst(msdbStream &s) 
00164 { s <<  msd_item; }
00165 
00166 void msd_int::getst(msdbStream &s) 
00167 { s >> msd_item;
00168 }
00169 let msd_int::getval() 
00170 {
00171 return  msdConst(msd_item);
00172 }
00173 
00174 msd_short::msd_short()
00175 { }
00176 msd_short::~msd_short()
00177 { }
00178 msd_short::msd_short(short int h)
00179 { msd_item = h; }
00180 
00181 void msd_short::print(std::ostream &s) 
00182 { s << msd_item;}
00183 void msd_short::setst(msdbStream &s) 
00184 { s <<  msd_item ; }
00185 
00186 void msd_short::getst(msdbStream &s)
00187 { s >> msd_item;
00188 }
00189 let msd_short::getval()
00190 {
00191 return  msdConst(msd_item);
00192 }
00193 
00194 msd_array::msd_array()
00195 { }
00196 msd_array::~msd_array()
00197 {if (msd_ptr)
00198 {for (int i=0;i < msd_size ; i++)
00199 { free(msd_ptr[i]);
00200 }
00201 } 
00202 }
00203 msd_array::msd_array(int n)
00204 { msd_size = n;
00205   msd_ptr = (msd_anyType**)malloc(n*sizeof(msd_anyType*));
00206 }
00207 msd_anyType*& msd_array::operator[](int i)
00208 { return msd_ptr[i];
00209 }
00210 void msd_array::print(std::ostream &s)
00211 { for (int i = 0; i < msd_size; i++)
00212     msd_ptr[i]->print(s);}
00213 
00214 void msd_array::setst(msdbStream &s)
00215 {
00216   for (int i = 0; i < msd_size; i++)
00217     msd_ptr[i]->setst(s);
00218 
00219 }
00220 void msd_array::getst(msdbStream &s) 
00221 {
00222   for (int i = 0; i < msd_size; i++)
00223     msd_ptr[i]->getst(s);
00224  
00225 }
00226 
00227 let msd_array::getval() 
00228 { stringstream arrliststr;
00229   arrliststr << "(";
00230   let arrlist = listEM( msdEnd);
00231   for (int i = 0; i < msd_size; i++){
00232         arrliststr << " "; 
00233     arrliststr << msd_ptr[i]->getval();
00234   }
00235  arrliststr << ")";
00236  arrlist = read(arrliststr);
00237  return arrlist;
00238 }
00239 
00241 
00242 
00243 
00244 
00245 
00246 void MSDItemInfo::display(void)
00247 {
00248  cout << "MSDItemInfo::display - missing subclass method\n";
00249 }
00250 
00251 void MSDItemList::add_item(MSDItemInfo * new_item)
00252 {
00253 MSDItem *temp;
00254 
00255    temp = new MSDItem(new_item);
00256    if (start == NULL)
00257       start = end_of_list = temp;
00258    else {
00259       end_of_list->next = temp;
00260       end_of_list = temp;
00261    }
00262 }
00263 
00264 
00265 
00266 
00267 void MSDItemList::get_iteminfo(char * item_name){
00268  MSDItem *temp;
00269  int found = 0; 
00270    temp = start;
00271    while ((temp != NULL) && !found) 
00272     {if (!strcmp(temp->item_data->name, item_name)) { 
00273                         found = 1;
00274                 } else { 
00275                         temp = temp->next; 
00276                 } 
00277    }
00278    if (temp != NULL) {
00279                   printf("OK");
00280                // printitem(temp);
00281         } else { 
00282                 printf("No item by that name exists in the database\n"); 
00283         } 
00284 
00285 }
00287 int ToUpperStrArray(char* in[]){
00288  int j=0;
00289  while (in[j]){
00290    for (int i=0;i<strlen(in[j]);i++){
00291       in[j][i]=toupper(in[j][i]);}
00292   j++;
00293  } 
00294  return (0);
00295 }
00297 void trimspace(char* buffer)
00298 {
00299         int len;
00300         len = strlen(buffer);
00301         char* ptemp;
00302         ptemp = new char[len+1];
00303         strcpy(ptemp,buffer);
00304 
00305         int index = 0;
00306         for(int z = 0; z < len; z++)
00307         {
00308                 if(ptemp[z] != ' ')
00309                 {
00310                         buffer[index] = ptemp[z];
00311                         index++;
00312                 }
00313         }
00314         buffer[index] = '\0';
00315 
00316         delete [] ptemp;
00317 }
00319 void removena(char* buffer)
00320 {
00321         int len;
00322         len = strlen(buffer);
00323         char* ptemp;
00324         ptemp = new char[len+1];
00325         strcpy(ptemp,buffer);
00326 
00327         int index = 0;
00328         for(int z = 0; z < len; z++)
00329         {
00330                 if(((ptemp[z] >= 'A')&& (ptemp[z] <= 'Z')) ||((ptemp[z] >= 'a')&& (ptemp[z] <= 'z')))
00331                 {
00332                         buffer[index] = ptemp[z];
00333                         index++;
00334                 }
00335         }
00336         buffer[index] = '\0';
00337 
00338         delete [] ptemp;
00339 }
00341 bool isalphanum(const char* str){
00342         bool res;
00343         for(int i = 0; i < strlen(str); i++)
00344         {
00345                 if(!isalpha(str[i]) && !isdigit(str[i]))
00346                 {
00347                         res = false;
00348                 }else
00349                         res = true;
00350         }
00351  return res;
00352 }
00353 bool isnum(const char* str){
00354         bool res;
00355         for(int i = 0; i < strlen(str); i++)
00356         {
00357                 if(!isdigit(str[i])) //(str[i] != '.') || !isdigit(str[i]))
00358                 {
00359                         res = false;
00360                 }else
00361                         res = true;
00362         }
00363  return res;
00364 }
00366 void trimcr(char* buffer)
00367 {
00368         int len;
00369         len = strlen(buffer);
00370         char* ptemp;
00371         ptemp = new char[len+1];
00372         strcpy(ptemp,buffer);
00373 
00374         int index = 0;
00375         for(int z = 0; z < len; z++)
00376         {
00377                 if(ptemp[z] != '\n')
00378                 {
00379                         buffer[index] = ptemp[z];
00380                         index++;
00381                 }
00382         }
00383         buffer[index] = '\0';
00384 
00385         delete [] ptemp;
00386 }
00387 
00388 
00390 // lower case
00391 void lcase(char* buffer)
00392 {
00393         int len = strlen(buffer);
00394 
00395         for(int i = 0; i < len; i++)
00396         {
00397                 buffer[i] = tolower(buffer[i]);
00398         }
00399 }
00400 // upercase
00401 void ucase(char* buffer)
00402 {
00403         int len = strlen(buffer);
00404 
00405         for(int i = 0; i < len; i++)
00406         {
00407                 buffer[i] = toupper(buffer[i]);
00408         }
00409 }
00410 
00412 // mid copy
00413 void midcopy(char* from,char* dest,int start_pos, int stop_pos)
00414 {
00415         int index = 0;
00416         for(int i = start_pos; i < stop_pos; i++)
00417         {
00418                 dest[index] = from[i];
00419                 index++;
00420         }
00421 
00422         dest[index] = '\0';
00423 }
00424 void leftcopy(char* from,char* dest,int pos)
00425 {
00426         int index = 0;
00427         for(int i = 0; i < pos; i++)
00428         {
00429                 dest[index] = from[i];
00430                 index++;
00431         }
00432         dest[index] = '\0';
00433 }
00434 // right copy
00435 void rightcopy(char* from,char* dest,int pos)
00436 {
00437         int index = 0;
00438         int len = strlen(from);
00439         for(int i = pos; i < len; i++)
00440         {
00441                 dest[index] = from[i];
00442                 index++;
00443         }
00444         dest[index] = '\0';
00445 
00446 }
00448 bool make_select_count (char* stm ,char* countstm){
00449         bool ret;
00450         int start,end,index=0;
00451         size_t len;
00452         //char * ustm=(char*)malloc(sizeof(stm)+1);
00453         char * ustm= new char[4096];
00454         sprintf(ustm,"%s",stm);
00455         ucase(ustm);
00456         char * tok = strstr (ustm,"SELECT");
00457         char * per = strstr (ustm,"FROM");
00458         if ((tok != NULL) && (per != NULL))
00459         {start=per-tok;
00460          end=strlen(stm);
00461          char* midbuff=new char[4096];//(char*)malloc(sizeof(ustm));
00462          midcopy(stm,midbuff,start,end);
00463          char* res=new char[4096]; //(char*)malloc(sizeof(midbuff)+17);
00464          sprintf(res,"select count(*) %s", midbuff);
00465          len = strlen(res);
00466          for(int i = 0; i < len; i++)
00467          {      countstm[index] = res[i];
00468                 index++;
00469          }
00470          countstm[index] = '\0';
00471          ret = true;
00472          delete midbuff;
00473          delete res;
00474         }else{
00475          len = strlen(ustm);
00476          for(int i = 0; i < len; i++)
00477          {      countstm[index] = ustm[i];
00478                 index++;
00479          }
00480          countstm[index] = '\0';
00481          ret = false;
00482         }
00483         delete ustm;
00484         return ret;
00485   }
00486 
00488 bool make_select_param (char* stm ,char* paramstm){
00489         bool ret;
00490         int index=0;
00491         size_t len;
00492         char * ustm=new char[4096]; //(char*)malloc(sizeof(stm)+1);
00493         sprintf(ustm,"%s",stm);
00494         char * tok = (char*) memchr (ustm,':',strlen(ustm));
00495         if (tok != NULL)
00496                 {
00497          len = strlen(ustm);
00498          for(int i = 0; i < len; i++)
00499          {
00500                 paramstm[index] = ustm[i];
00501                 index++;
00502          }
00503          paramstm[index] = '\0';
00504          ret = false;
00505         }else
00506         {char* res=new char[4096]; // (char*)malloc(sizeof(ustm)+20);
00507          sprintf(res," %s and :f<int> = 1 ", ustm);
00508          len = strlen(res);
00509          for(int i = 0; i < len; i++)
00510          {
00511                 paramstm[index] = res[i];
00512                 index++;
00513      }
00514          paramstm[index] = '\0';
00515          ret = true;
00516          delete res;
00517         }
00518         delete ustm;
00519         return ret;
00520   }
00522 int has_integer_name (char* name){
00523     int r = 0;
00524         char * tok;
00525         char * tok_list[4]={"_ID","SEQ","NUM","SERIAL"};
00526         char  ustm[64]; //=(char*)malloc(sizeof(name)+1);
00527         sprintf(ustm,"%s",name);
00528         ucase(ustm);
00529         for (int i=0;i<4 ;i++ )
00530         {tok = strstr (ustm,tok_list[i]);
00531          if (tok){
00532           r = 1;
00533           break;
00534          }
00535         }
00536         //free(ustm);
00537         return r;
00538 }
00540 
00541 
00542 
00543 
00544 int StrArrayToListStr (char* strarray[], char liststr[]){
00545  char tmp[5];
00546  int j=1;
00547  strcpy(tmp, "', '");
00548  sprintf(liststr,"('%s", strarray[0]);
00549   while (strarray[j]) //
00550  {   
00551    sprintf(liststr, "%s%s%s",liststr,tmp,strarray[j]); 
00552    j++;
00553  }
00554  sprintf(liststr, "%s')",liststr);
00555  return (0);
00556 }
00558  bool fileexists(char* fn){
00559    char filename[256];
00560    sprintf(filename,"%s", fn);
00561    bool result;
00562   fstream file;
00563   file.open(filename,ios::in);
00564   if(file.fail())
00565    result=false;
00566    else{
00567    file.close();
00568    result=true;}
00569    return result;
00570  }
00571 
00572  bool ssm_file_completed(char* fn, const char* endstr){
00573    char filename[256];
00574    char buffer[32];
00575    char * pchs;
00576    sprintf(filename,"%s", fn);
00577    bool result;
00578   fstream file;
00579   file.open(filename,ios::in);
00580   if(file.fail())
00581    result=false;
00582    else{
00583    file.seekg (32, ios::end);
00584    file.read (buffer,32);
00585    pchs = strstr (buffer,endstr); // "</SSMResults>"
00586    if (pchs != NULL)
00587     result=true;
00588    else
00589     result=false;
00590    file.close();
00591    }
00592    return result;
00593  }
00594 
00595 
00596  bool lsf_file_completed(char* fn){
00597   int lno=0;
00598   int got=0;
00599   fstream file;
00600   char* filename;
00601   filename = (char *)malloc(strlen(fn)+1);
00602   sprintf(filename,"%s", fn);
00603   bool result=false;
00604   char * pchs;
00605   char * linebuf = (char *)malloc(100);
00606   char * tempbuf = (char *)malloc(100);
00607   file.open(filename,ios::in);
00608   if(!file.fail())
00609   {do
00610     {file.getline(linebuf, 100);
00611          if(linebuf != NULL)
00612          {strcpy(tempbuf, linebuf);
00613           pchs = strstr (tempbuf, "Sender: LSF System");
00614           if (pchs != NULL)
00615           got =1;
00616          }
00617           lno++; 
00618    } while (!file.eof() && (got == 0) && lno < 3);
00619     if (got == 1){   
00620       result=true;
00621         }
00622   file.close();
00623   }
00624    free(filename);
00625    free(linebuf);
00626    free(tempbuf);
00627    return result;
00628  }
00629 
00630 
00631 
00632 void wait( int seconds )
00633 {clock_t endwait;
00634   endwait = clock() + seconds * CLOCKS_PER_SEC; // or CLK_TCK 
00635   while (clock() < endwait) {}
00636 }
00637 
00638 
00639 char* getDateTime(){
00640  time_t rawtime;
00641  struct tm * timeinfo;
00642  time ( &rawtime );
00643  timeinfo = localtime(&rawtime);
00644  return asctime (timeinfo);    
00645 }
00646 
00647 
00648 void msdPrompt(char* host){
00649  char prompt[64];
00650  char dt[32];
00651  sprintf(dt,"%s", getDateTime());
00652  trimcr(dt);
00653  sprintf(prompt,"\n[msdsoap@%s %s]:", host, dt);
00654  fprintf(stderr, prompt);
00655 }
00656 
00657 
00659 
00660 // MSDDataSet class methods.
00661 // Data Base constructor.
00662 MSDDataSet::MSDDataSet() {size = 0; contents = makeHashTable();}
00663 MSDDataSet::~MSDDataSet() {clrhash(contents); gc(contents);}
00664 void MSDDataSet::init() {size = 0; contents = makeHashTable();}
00665 void MSDDataSet::clean() {clrhash(contents); gc(contents);}
00666 // Add an entity to the Data Base.
00667 void MSDDataSet::addEntity(let entity) {
00668   if (!gethash(entity, contents)) {
00669     puthash(entity, contents, 0);
00670     size++;}}
00671 
00672 // Set the value of an attribute for given entity.
00673 void MSDDataSet::setValue(let entity, let attribute, let value) {
00674   let attributes = gethash(entity, contents);
00675   let old = assoc(attribute, attributes);
00676   if (old) rplacd(old, value);
00677   else {
00678     push(cons(attribute, value), attributes);
00679     puthash(entity, contents, attributes);}}
00680 
00681 // Return the value of an attribute for given entity.
00682 let MSDDataSet::getValue(let entity, let attribute) {
00683   return cdr(assoc(attribute, gethash(entity, contents)));}
00684 
00685 // Delete an entity to the Data Base.
00686 void MSDDataSet::delEntity(let entity) {
00687   if (!gethash(entity, contents)) {
00688     remhash(entity, contents);
00689     size--;}}
00690 
00692 msdDumpObjects::msdDumpObjects(){}
00693 
00694 msdDumpObjects::~msdDumpObjects(){}
00695 
00696 bool msdDumpObjects::msdAddObject(MSDDataSet& obj){
00697  bool res;
00698  DBBin <MSDDataSet> dbb("msdsoap.dmp");
00699  dbb.Open();
00700  MSDDataSet cobj= MSDDataSet();
00701  if (!dbb.FindRecord(obj.name, cobj))
00702  {fprintf (stderr, "\nAdding obj: %s.", obj.name);
00703   obj.id = dbb.GetNextRecordNo();
00704   res = dbb.AddRecord(obj);
00705  }else{
00706   cobj.init();
00707   fprintf (stderr,"Object: %s already exists.", obj.name);
00708   res = false;
00709  }
00710  dbb.Close();
00711  
00712  return res;
00713 }
00714 
00715 unsigned long msdDumpObjects::msdFindObject(const char* obj){
00716  unsigned long res;
00717  DBBin <MSDDataSet> dbb("msdsoap.dmp");
00718  dbb.Open();
00719   MSDDataSet cobj;
00720  if (!dbb.FindRecord(obj, cobj))
00721  {fprintf (stderr, "\nObject: %s does not exist.", obj);
00722   res = 2147483647;
00723  }else
00724  res = cobj.id;
00725  dbb.Close();
00726  return res;
00727 }
00728 
00729 
00730 MSDDataSet msdDumpObjects::msdGetObject(const char* obj){
00731 
00732  DBBin <MSDDataSet> dbb("msdsoap.dmp");
00733  dbb.Open();
00734  MSDDataSet cobj = MSDDataSet();
00735  if (!dbb.FindRecord(obj, cobj))
00736  {fprintf (stderr, "\nObject: %s does not exist.", obj);
00737   dbb.Close();
00738   cobj.init();
00739   return cobj;
00740  }else{
00741  fprintf (stderr, "\nObject: %s found.", obj);
00742  dbb.Close();
00743  cobj.init();
00744  return cobj;
00745  }
00746 }
00747 
00748 
00749 bool msdDumpObjects::msdEditObject(const char* obj){
00750 
00751  bool res;
00752  unsigned long idx = msdDumpObjects::msdFindObject(obj); 
00753  if ( idx != 2147483647)
00754  {DBBin <MSDDataSet> dbb("msdsoap.dmp");
00755   dbb.Open();
00756   
00757   MSDDataSet eobj;
00758   eobj.id=idx;
00759   res = dbb.ModifyRecord(eobj, idx-1);
00760   dbb.Close();
00761  }else{
00762   res =  false;
00763  }
00764   return res;
00765  }
00766 
00767 
00768 
00769 bool msdDumpObjects::msdDelObject(const char* obj){
00770  bool res;
00771  DBBin <MSDDataSet> dbb("msdsoap.dmp");
00772  dbb.Open();
00773   MSDDataSet cobj;
00774  if (!dbb.FindRecord(obj, cobj))
00775  {fprintf (stderr, "\nObject: %s does not exist.", obj);
00776   return false;
00777  }else{
00778   res =  dbb.DeleteRecord(cobj.id - 1);
00779  }
00780  dbb.Close();
00781  return res;
00782 }

Generated on Fri Apr 16 13:47:40 2004 for MSDAPI by doxygen 1.3.4-20031005