Allink  v0.1
VarDataString.cpp
1 /***********************************************************************
2 VarData: This Program reads and writes a specific file format
3 storing all the information relative to a set of equal structure
4 polymers to the CHAIN, PART and GENERAL structures. It provides
5 two different ways to backfold the coordinates, a fanction that
6 creates an initial system with different option and some function
7 for the data analisys. The first calculate the distribution of the
8 monomer in the box, the second the distribution of the bonds.
9 Copyright (C) 2008 by Giovanni Marelli <sabeiro@virgilio.it>
10 
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 ***********************************************************************/
26 #include "../include/VarData.h"
27 #include <stdarg.h>
28 
29 bool VarData::ReadString(const char *String,char *cLine,double *Value){
30  if(String == NULL) return 1;
31  int iLen = (int) (strlen(cLine));
32  char cVar[STRSIZE];
33  char cVal[STRSIZE];
34  int Paren[2];
35  for(int i=0;i<iLen;i++){
36  if(cLine[i]=='#')//comment
37  break;
38  if(cLine[i]==String[0]){
39  Paren[0] = i;
40  for(int j=i;j<iLen;j++){
41  if(cLine[j]==' '){
42  Paren[1] = j;
43  memset(cVar,0,STRSIZE);
44  strncpy(cVar,cLine+Paren[0],Paren[1]-Paren[0]);
45  //printf("%s %s\n",cVar,String);
46  for(int q =0;q<Paren[1]-Paren[0];q++) cVar[q] = cLine[q+Paren[0]];
47  if( (strcmp(cVar,String))==0 ){
48  for(int l=j;l<iLen;l++){
49  if(cLine[l]!=' '){
50  Paren[0] = l;
51  for(int h=l;h<iLen;h++){
52  if(cLine[h]==' ' || cLine[h]=='\n'){
53  Paren[1] = h;
54  //sprintf(cVal,"");
55  strncpy(cVal,cLine+Paren[0],Paren[1]-Paren[0]);
56  //printf("\t%d %d %s\n",Paren[0],Paren[1],cVal);
57  sprintf(cVal+Paren[1]-Paren[0]," ");
58  //strncpy(cVal+Paren[1]-Paren[0],cSpa,3);
59  sscanf(cVal,"%lf",Value);
60  //printf("%s Vale %f\n",cVar,*Value);
61  return 1;
62  }
63  }
64  break;
65  }
66  }
67  break;
68  }
69  else break;
70  }
71  }
72  //break;
73  }
74  }
75  return 0;
76 }
77 bool VarData::ReadString(const char *String,double *Value,char *Line){
78  //printf("--%s\n",Line);
79  char *pLine = Line;
80  char cVar[STRSIZE];
81  char cVal[STRSIZE];
82  int Paren[2];
83  if(pLine!=NULL){
84  int iLen = (int) (strlen(Line));
85  //printf("%s\n",Line);
86  printf("%s->%s",String,Line);
87  for(int i=0;i<iLen;i++){
88  if(pLine[i]=='#')//comment
89  break;
90  if(pLine[i]==String[0]){
91  Paren[0] = i;
92  for(int j=i;j<iLen;j++){
93  if(pLine[j]==' '){
94  Paren[1] = j;
95  memset(cVar,STRSIZE,sizeof(char));
96  strncpy(cVar,Line+Paren[0],Paren[1]-Paren[0]);
97  //for(int q =0;q<Paren[1]-Paren[0];q++) cVar[q] = Line[q+Paren[0]];
98  printf(" %d %d %s %d\n",Paren[0],Paren[1],cVar,strlen(cVar));
99  if( (strcmp(cVar,String))==0 ){
100  // printf("%s\n",Line);
101  //printf(" %d %d %s\n",Paren[0],Paren[1],cVar);
102  for(int l=j;l<iLen;l++){
103  if(pLine[l]!=' '){
104  Paren[0] = l;
105  for(int h=l;h<iLen;h++){
106  if(pLine[h]==' ' || pLine[h]=='\n'){
107  Paren[1] = h;
108  //sprintf(cVal,"");
109  strncpy(cVal,Line+Paren[0],Paren[1]-Paren[0]);
110  //printf("\t%d %d %s\n",Paren[0],Paren[1],cVal);
111  sprintf(cVal+Paren[1]-Paren[0]," ");
112  sscanf(cVal,"%lf",Value);
113  printf("%s Vale %f\n",cVar,*Value);
114  return 1;
115  }
116  }
117  break;
118  }
119  }
120  break;
121  }
122  else break;
123  }
124  }
125  //break;
126  }
127  }
128  }
129  return 0;
130 }
131 int VarData::ReadVal(char *pLine,double *Value){
132  if(pLine==NULL) return 0;
133  char cVar[STRSIZE];
134  char cVal[20];
135  int Paren[2];
136  int Incr = 0;
137  *Value = 0.;
138  int iLen = (int) (strlen(pLine));
139  memset(cVar,0,STRSIZE);
140  for(int i=0;i<iLen;i++){
141  if(pLine[i]=='#')//comment
142  return 0;
143  if(pLine[i]!=' '){
144  Paren[0] = i;
145  for(int j=i;j<iLen;j++){
146  if(pLine[j]==' ' || pLine[j]=='\n'){
147  Incr = Paren[1] = j;
148  strncpy(cVar,pLine+Paren[0],Paren[1]-Paren[0]);
149  sprintf(cVal+Paren[1]-Paren[0]," ");
150  sscanf(cVar,"%lf",Value);
151  //printf(" %d %s %lf\n",Incr,cVar,*Value);
152  pLine += Incr;
153  return Incr;
154  }
155  }
156  }
157  }
158  return 0;
159 }
160 int VarData::Fetch(char *str,char *mask,int NArg,double *Val){
161  int sLen = strlen(str);
162  int sPos = 0;
163  int IfContinue = 0;
164  // if(!strncmp(str,mask,strlen(mask))) return 0;
165  // char *pOpen = strpbrk(str,"([");
166  // char *pClose = strpbrk(str,")]");
167  // if(pOpen == NULL || pClose == NULL) return 0;
168  // sPos = pOpen - str +1;
169  // sLen = pClose - str;
170  for(int s=0;s<sLen;s++){
171  if(!strncmp(str+s,mask,strlen(mask)) ){
172  //if(str[s] == mask[0]){
173  sPos = s;
174  for(int ss=s;ss<sLen;ss++){
175  if(str[ss] == '(' || str[ss] == '['){
176  sPos = ss+1;
177  for(int sss=ss;sss<sLen;sss++){
178  if(str[sss] == ')' || str[sss] == ']'){
179  sLen = sss;
180  IfContinue = 1;
181  break;
182  }
183  }
184  break;
185  }
186  }
187  break;
188  }
189  }
190  if(!IfContinue) return 0;
191  //char *sNumber = (char*)calloc(160,sizeof(char));
192  //strncpy(sNumber,str+sPos,sLen-sPos);
193  char *sNumber = str+sPos;
194  int sNum = 0;
195  for(int a=0;a<NArg;a++){
196  sscanf(sNumber+sNum,"%lf",Val+a);
197  //FIXME: two blank spaces/one letter
198  for(int s=sNum+1;s<sLen-sPos;s++){
199  if(sNumber[s] == ' '){
200  for(int ss=s;ss<sLen-sPos;ss++){
201  if(sNumber[ss] != ' '){
202  sNum = ss;
203  break;
204  }
205  }
206  break;
207  }
208  }
209  //printf("%s-%d) %s vale %lf in %d\n",mask,a,sNumber+sNum,*(Val+a),sPos);
210  }
211  //free(sNumber);
212  return sPos;
213 }
214 int VarData::BraketPos(char *str,char *mask,int *sPos,int *sLen){
215  if(!strncmp(str,mask,strlen(mask))) return 1;
216  char *pInit = strpbrk(str,mask);
217  if(pInit == NULL) return 1;
218  int InitPos = pInit - str + 1;
219  char *pOpen = strpbrk(str+InitPos,"([{");
220  char *pClose = strpbrk(str+InitPos,"}])");
221  //printf("%s %d %d %d %s\n",mask,pInit,*sPos,*sLen,str+InitPos);
222  if(pOpen == NULL || pClose == NULL) return 1;
223  *sPos = pOpen - str +1;
224  *sLen = pClose - str;
225  //printf("%s %d %d %d %s\n",mask,pInit,*sPos,*sLen,str+InitPos);
226  return 0;
227 }
228 int VarData::Fetch(char *str,char *mask,char *fmt, ... ){
229  int sLen = 0;
230  int sPos = 0;
231  int IfContinue = BraketPos(str,mask,&sPos,&sLen);
232  if(IfContinue) return 0;
233  char Field[120];
234  memset(Field,' ',120*sizeof(char));
235  //char *Field = (char *)calloc(sLen-sPos+1,sizeof(char));
236  strncpy(Field,str+sPos,sLen-sPos);
237  va_list args;
238  va_start(args,fmt);
239  vsscanf(Field,fmt,args);
240  // printf("Sotto %s trovato %s come %s\n",mask,Field,fmt);
241  va_end(args);
242  //free(Field);
243  return sPos;
244 }
245 // //FIXME: fa seg fault
246 // int VarData::Fetch(char *str,char *mask,char *fmt, ... ){
247 // int sLen = 0;
248 // int sPos = 0;
249 // int IfContinue = BraketPos(str,mask,&sPos,&sLen);
250 // if(IfContinue) return 0;
251 // char *Field = (char *)calloc(sLen-sPos+1,sizeof(char));
252 // if(Field == NULL){
253 // printf("Fetch non allocated in %s %s %s\n",mask,str,fmt);
254 // return 0;
255 // }
256 // // char Field[60];
257 // // memset(Field,' ',60*sizeof(char));
258 // strncpy(Field,str+sPos,sLen-sPos);
259 // int ret = 0;
260 // va_list args;
261 // va_start(args,fmt);
262 // ret = vsscanf(Field,fmt,args);
263 // //printf("Sotto %s trovato %s come %s\n",mask,Field,fmt);
264 // va_end(args);
265 // free(Field);
266 // return sPos;
267 // }
int Fetch(char *str, char *mask, char *fmt,...)
Retrive from a string the information concerning the mask.
int BraketPos(char *str, char *mask, int *sPos, int *sLen)
Retrive from a string the position of the brakets.
bool ReadString(const char *String, char *cLine, double *Value)
Copy the value in the.
int ReadVal(char *pLine, double *Value)
Copy the value in the String to the Value referring to the position of pLine.