int read_xvg(char *fn,real ***y,int *ny) { FILE *fp; char *ptr; char *base=NULL; char *fmt=NULL; int k,line=0,nny,nx,maxx,rval; double lf; real **yy=NULL; *ny = 0; nny = 0; nx = 0; maxx = 0; fp = ffopen(fn,"r"); while ((ptr = fgets3(fp)) != NULL) { line++; trim(ptr); if ((ptr[0] != '@') && (ptr[0] != '#')) { if (nny == 0) { (*ny) = nny = wordcount(ptr); /* fprintf(stderr,"There are %d columns in your file\n",nny);*/ if (nny == 0) return 0; snew(yy,nny); snew(fmt,3*nny+1); snew(base,3*nny+1); } /* Allocate column space */ if (nx >= maxx) { maxx+=1024; for(k=0; (k<nny); k++) srenew(yy[k],maxx); } /* Initiate format string */ fmt[0] = '\0'; base[0] = '\0'; /* fprintf(stderr,"ptr='%s'\n",ptr);*/ for(k=0; (k<nny); k++) { strcpy(fmt,base); strcat(fmt,"%lf"); rval = sscanf(ptr,fmt,&lf); /* fprintf(stderr,"rval = %d\n",rval);*/ if ((rval == EOF) || (rval == 0)) break; yy[k][nx] = lf; srenew(fmt,3*(nny+1)+1); srenew(base,3*nny+1); strcat(base,"%*s"); } if (k != nny) { fprintf(stderr,"Only %d columns on line %d in file %s\n", k,line,fn); for( ; (k<nny); k++) yy[k][nx] = 0.0; } nx++; } } ffclose(fp); *y = yy; return nx; }
int read_xvg_legend(const char *fn, double ***y, int *ny, char **subtitle, char ***legend) { FILE *fp; char *ptr, *ptr0, *ptr1; char *base = NULL; char *fmt = NULL; int k, line = 0, nny, nx, maxx, rval, legend_nalloc, set, nchar; double lf; double **yy = NULL; char *tmpbuf; int len = STRLEN; *ny = 0; nny = 0; nx = 0; maxx = 0; fp = gmx_fio_fopen(fn, "r"); snew(tmpbuf, len); if (subtitle != NULL) { *subtitle = NULL; } legend_nalloc = 0; if (legend != NULL) { *legend = NULL; } while ((ptr = fgets3(fp, &tmpbuf, &len, 10*STRLEN)) != NULL && ptr[0] != '&') { line++; trim(ptr); if (ptr[0] == '@') { if (legend != NULL) { ptr++; trim(ptr); set = -1; if (strncmp(ptr, "subtitle", 8) == 0) { ptr += 8; if (subtitle != NULL) { *subtitle = read_xvgr_string(ptr); } } else if (strncmp(ptr, "legend string", 13) == 0) { ptr += 13; sscanf(ptr, "%d%n", &set, &nchar); ptr += nchar; } else if (ptr[0] == 's') { ptr++; sscanf(ptr, "%d%n", &set, &nchar); ptr += nchar; trim(ptr); if (strncmp(ptr, "legend", 6) == 0) { ptr += 6; } else { set = -1; } } if (set >= 0) { if (set >= legend_nalloc) { legend_nalloc = set + 1; srenew(*legend, legend_nalloc); (*legend)[set] = read_xvgr_string(ptr); } } } } else if (ptr[0] != '#') { if (nny == 0) { (*ny) = nny = wordcount(ptr); /* fprintf(stderr,"There are %d columns in your file\n",nny);*/ if (nny == 0) { return 0; } snew(yy, nny); snew(fmt, 3*nny+1); snew(base, 3*nny+1); } /* Allocate column space */ if (nx >= maxx) { maxx += 1024; for (k = 0; (k < nny); k++) { srenew(yy[k], maxx); } } /* Initiate format string */ fmt[0] = '\0'; base[0] = '\0'; /* fprintf(stderr,"ptr='%s'\n",ptr);*/ for (k = 0; (k < nny); k++) { strcpy(fmt, base); strcat(fmt, "%lf"); rval = sscanf(ptr, fmt, &lf); /* fprintf(stderr,"rval = %d\n",rval);*/ if ((rval == EOF) || (rval == 0)) { break; } yy[k][nx] = lf; srenew(fmt, 3*(nny+1)+1); srenew(base, 3*nny+1); strcat(base, "%*s"); } if (k != nny) { fprintf(stderr, "Only %d columns on line %d in file %s\n", k, line, fn); for (; (k < nny); k++) { yy[k][nx] = 0.0; } } nx++; } } gmx_fio_fclose(fp); *y = yy; sfree(tmpbuf); if (legend_nalloc > 0) { if (*ny - 1 > legend_nalloc) { srenew(*legend, *ny-1); for (set = legend_nalloc; set < *ny-1; set++) { (*legend)[set] = NULL; } } } return nx; }