Allink  v0.1
VarDataString.cpp
00001 /***********************************************************************
00002 VarData: This  Program reads and writes a specific file format 
00003 storing all the information relative to a set of equal structure
00004 polymers to the CHAIN, PART and GENERAL structures. It provides 
00005 two different ways to backfold the coordinates, a fanction that 
00006 creates an initial system with different option and some function
00007 for the data analisys. The first calculate the distribution of the
00008 monomer in the box, the second the distribution of the bonds.
00009 Copyright (C) 2008 by Giovanni Marelli <sabeiro@virgilio.it>
00010 
00011 
00012 This program is free software; you can redistribute it and/or modify
00013 it under the terms of the GNU General Public License as published by
00014 the Free Software Foundation; either version 2 of the License, or
00015 (at your option) any later version.
00016 
00017 This program is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 GNU General Public License for more details.
00021 
00022 You should have received a copy of the GNU General Public License
00023 along with this program; if not, write to the Free Software
00024 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 ***********************************************************************/
00026 #include "../include/VarData.h"
00027 #include <stdarg.h>
00028 
00029 bool VarData::ReadString(const char *String,char *cLine,double *Value){
00030   if(String == NULL) return 1;
00031   int iLen = (int) (strlen(cLine));
00032   char cVar[STRSIZE];
00033   char cVal[STRSIZE];
00034   int Paren[2];
00035   for(int i=0;i<iLen;i++){
00036     if(cLine[i]=='#')//comment
00037       break;
00038     if(cLine[i]==String[0]){
00039       Paren[0] = i;
00040       for(int j=i;j<iLen;j++){
00041    if(cLine[j]==' '){
00042      Paren[1] = j;
00043      memset(cVar,0,STRSIZE);
00044      strncpy(cVar,cLine+Paren[0],Paren[1]-Paren[0]);
00045      //printf("%s %s\n",cVar,String);
00046      for(int q =0;q<Paren[1]-Paren[0];q++) cVar[q] = cLine[q+Paren[0]];
00047      if( (strcmp(cVar,String))==0 ){
00048        for(int l=j;l<iLen;l++){
00049          if(cLine[l]!=' '){
00050       Paren[0] = l;
00051       for(int h=l;h<iLen;h++){
00052         if(cLine[h]==' ' || cLine[h]=='\n'){
00053           Paren[1] = h;
00054           //sprintf(cVal,"");
00055           strncpy(cVal,cLine+Paren[0],Paren[1]-Paren[0]);
00056           //printf("\t%d %d %s\n",Paren[0],Paren[1],cVal); 
00057           sprintf(cVal+Paren[1]-Paren[0],"   ");
00058           //strncpy(cVal+Paren[1]-Paren[0],cSpa,3);
00059           sscanf(cVal,"%lf",Value);
00060           //printf("%s Vale %f\n",cVar,*Value);
00061           return 1;
00062         }
00063       }
00064       break;
00065          }
00066        }
00067        break;
00068      }
00069      else break;
00070    }
00071       }
00072       //break;
00073     }
00074   }
00075   return 0;
00076 }
00077 bool VarData::ReadString(const char *String,double *Value,char *Line){
00078   //printf("--%s\n",Line);
00079   char *pLine = Line;
00080   char cVar[STRSIZE];
00081   char cVal[STRSIZE];
00082   int Paren[2];
00083   if(pLine!=NULL){
00084     int iLen = (int) (strlen(Line));
00085     //printf("%s\n",Line);
00086     printf("%s->%s",String,Line);
00087     for(int i=0;i<iLen;i++){
00088       if(pLine[i]=='#')//comment
00089    break;
00090        if(pLine[i]==String[0]){
00091    Paren[0] = i;
00092    for(int j=i;j<iLen;j++){
00093      if(pLine[j]==' '){
00094        Paren[1] = j;
00095        memset(cVar,STRSIZE,sizeof(char));
00096        strncpy(cVar,Line+Paren[0],Paren[1]-Paren[0]);
00097        //for(int q =0;q<Paren[1]-Paren[0];q++) cVar[q] = Line[q+Paren[0]];
00098        printf("  %d %d %s   %d\n",Paren[0],Paren[1],cVar,strlen(cVar)); 
00099        if( (strcmp(cVar,String))==0 ){
00100          //       printf("%s\n",Line);
00101          //printf("  %d %d %s\n",Paren[0],Paren[1],cVar); 
00102          for(int l=j;l<iLen;l++){
00103       if(pLine[l]!=' '){
00104         Paren[0] = l;
00105         for(int h=l;h<iLen;h++){
00106           if(pLine[h]==' ' || pLine[h]=='\n'){
00107             Paren[1] = h;
00108             //sprintf(cVal,"");
00109             strncpy(cVal,Line+Paren[0],Paren[1]-Paren[0]);
00110             //printf("\t%d %d %s\n",Paren[0],Paren[1],cVal); 
00111             sprintf(cVal+Paren[1]-Paren[0],"   ");
00112             sscanf(cVal,"%lf",Value);
00113             printf("%s Vale %f\n",cVar,*Value);
00114             return 1;
00115           }
00116         }
00117         break;
00118       }
00119          }
00120          break;
00121        }
00122        else break;
00123      }
00124    }
00125    //break;
00126       }
00127     }
00128   }
00129   return 0;
00130 }
00131 int VarData::ReadVal(char *pLine,double *Value){
00132   if(pLine==NULL) return 0;
00133   char cVar[STRSIZE];
00134   char cVal[20];
00135   int Paren[2];
00136   int Incr = 0;
00137   *Value = 0.;
00138   int iLen = (int) (strlen(pLine));
00139   memset(cVar,0,STRSIZE);
00140   for(int i=0;i<iLen;i++){
00141     if(pLine[i]=='#')//comment
00142       return 0;
00143     if(pLine[i]!=' '){
00144       Paren[0] = i;
00145       for(int j=i;j<iLen;j++){
00146    if(pLine[j]==' ' || pLine[j]=='\n'){
00147      Incr = Paren[1] = j;
00148      strncpy(cVar,pLine+Paren[0],Paren[1]-Paren[0]);
00149      sprintf(cVal+Paren[1]-Paren[0],"   ");
00150      sscanf(cVar,"%lf",Value);
00151      //printf("  %d %s %lf\n",Incr,cVar,*Value);
00152      pLine += Incr;
00153      return Incr;
00154    }
00155       }
00156     }
00157   }
00158   return 0;
00159 }
00160 int VarData::Fetch(char *str,char *mask,int NArg,double *Val){
00161   int sLen = strlen(str);
00162   int sPos = 0;
00163   int IfContinue = 0;
00164   // if(!strncmp(str,mask,strlen(mask))) return 0;
00165   // char *pOpen = strpbrk(str,"([");
00166   // char *pClose = strpbrk(str,")]");
00167   // if(pOpen == NULL || pClose == NULL) return 0;
00168   // sPos = pOpen - str +1;
00169   // sLen = pClose - str;
00170   for(int s=0;s<sLen;s++){
00171     if(!strncmp(str+s,mask,strlen(mask)) ){
00172       //if(str[s] == mask[0]){
00173       sPos = s;
00174       for(int ss=s;ss<sLen;ss++){
00175    if(str[ss] == '(' || str[ss] == '['){
00176      sPos = ss+1;
00177      for(int sss=ss;sss<sLen;sss++){
00178        if(str[sss] == ')' || str[sss] == ']'){
00179          sLen = sss;
00180          IfContinue = 1;
00181          break;
00182        }
00183      }
00184      break;
00185    }
00186       }
00187       break;
00188     }
00189   }
00190   if(!IfContinue) return 0;
00191   //char *sNumber = (char*)calloc(160,sizeof(char));
00192   //strncpy(sNumber,str+sPos,sLen-sPos);
00193   char *sNumber = str+sPos;
00194   int sNum = 0;
00195   for(int a=0;a<NArg;a++){
00196     sscanf(sNumber+sNum,"%lf",Val+a);
00197     //FIXME: two blank spaces/one letter
00198     for(int s=sNum+1;s<sLen-sPos;s++){
00199       if(sNumber[s] == ' '){
00200    for(int ss=s;ss<sLen-sPos;ss++){
00201      if(sNumber[ss] != ' '){
00202        sNum = ss;
00203        break;
00204      }
00205    }
00206    break;
00207       }
00208     }
00209     //printf("%s-%d) %s vale %lf in %d\n",mask,a,sNumber+sNum,*(Val+a),sPos);
00210   }
00211   //free(sNumber);
00212   return sPos;
00213 }
00214 int VarData::BraketPos(char *str,char *mask,int *sPos,int *sLen){
00215   if(!strncmp(str,mask,strlen(mask))) return 1;
00216   char *pInit = strpbrk(str,mask);
00217   if(pInit == NULL) return 1;
00218   int InitPos = pInit - str + 1;
00219   char *pOpen = strpbrk(str+InitPos,"([{");
00220   char *pClose = strpbrk(str+InitPos,"}])");
00221   //printf("%s %d %d %d %s\n",mask,pInit,*sPos,*sLen,str+InitPos);
00222   if(pOpen == NULL || pClose == NULL) return 1;
00223   *sPos = pOpen - str +1;
00224   *sLen = pClose - str;
00225   //printf("%s %d %d %d %s\n",mask,pInit,*sPos,*sLen,str+InitPos);
00226   return 0;
00227 }
00228 int VarData::Fetch(char *str,char *mask,char *fmt, ... ){
00229   int sLen = 0;
00230   int sPos = 0;
00231   int IfContinue = BraketPos(str,mask,&sPos,&sLen);
00232   if(IfContinue) return 0;
00233   char Field[120];
00234   memset(Field,' ',120*sizeof(char));
00235   //char *Field = (char *)calloc(sLen-sPos+1,sizeof(char));
00236   strncpy(Field,str+sPos,sLen-sPos);
00237   va_list args;
00238   va_start(args,fmt);
00239   vsscanf(Field,fmt,args);
00240   //  printf("Sotto %s trovato %s come %s\n",mask,Field,fmt);
00241   va_end(args);
00242   //free(Field);
00243   return sPos;
00244 }
00245 // //FIXME: fa seg fault
00246 // int VarData::Fetch(char *str,char *mask,char *fmt, ... ){
00247 //   int sLen = 0;
00248 //   int sPos = 0;
00249 //   int IfContinue = BraketPos(str,mask,&sPos,&sLen);
00250 //   if(IfContinue) return 0;
00251 //   char *Field = (char *)calloc(sLen-sPos+1,sizeof(char));
00252 //   if(Field == NULL){
00253 //     printf("Fetch non allocated in %s %s %s\n",mask,str,fmt);
00254 //     return 0;
00255 //   }
00256 //   // char Field[60];
00257 //   // memset(Field,' ',60*sizeof(char));
00258 //   strncpy(Field,str+sPos,sLen-sPos);
00259 //   int ret = 0;
00260 //   va_list args;
00261 //   va_start(args,fmt);
00262 //   ret = vsscanf(Field,fmt,args);
00263 //   //printf("Sotto %s trovato %s come %s\n",mask,Field,fmt);
00264 //   va_end(args);
00265 //   free(Field);
00266 //   return sPos;
00267 // }