int read_qdppp_scidac_binary_data(char *buff_out, long int buff_size, char *fin) { char buf[MAXBUF]; /* auxilary buffer for using a buffer copy. Bytes first copied to buf then from buf to buff_out*/ LimeReader *reader; int status; n_uint64_t nbytes, bytes_left, bytes_to_copy, read_bytes; int rec_seek,msg_seek; int rec, msg; char *lime_type; size_t bytes_pad; int MB_flag, ME_flag; int i; /* Open file */ FILE *fp = fopen(fin, "r"); if(fp == NULL) { fprintf(stderr,"Unable to open file %s for reading\n", fin); return EXIT_FAILURE; } /* Open LIME reader */ reader = limeCreateReader(fp); if( reader == (LimeReader *)NULL ) { fprintf(stderr, "Unable to open LimeReader\n"); return EXIT_FAILURE; } /* Loop over records */ msg=0; rec=0; while( (status = limeReaderNextRecord(reader)) != LIME_EOF ){ if( status != LIME_SUCCESS ) { fprintf(stderr, "limeReaderNextRecord returned status = %d\n", status); return EXIT_FAILURE; } nbytes = limeReaderBytes(reader); lime_type = limeReaderType(reader); bytes_pad = limeReaderPadBytes(reader); MB_flag = limeReaderMBFlag(reader); ME_flag = limeReaderMEFlag(reader); /* Update message and record numbers */ if(MB_flag == 1){ msg++; rec = 0; } rec++; #if 1 printf("\n\n"); printf("Type: %s\n", lime_type); printf("Data Length: %ld\n", nbytes); printf("Padding Length: %zd\n", bytes_pad); printf("MB flag: %d\n", MB_flag); printf("ME flag: %d\n", ME_flag); #endif /* Skip to next record until target record is reached */ /*if (msg != msg_seek || rec != rec_seek) continue;*/ if (strcmp(lime_type,"scidac-binary-data") != 0) continue; /*check that the data record has the expected number of byutes*/ if(nbytes != buff_size) { fprintf(stderr,"Error, expecting %ld bytes but data has %ld bytes\n",buff_size,nbytes); return EXIT_FAILURE; } /* Buffered copy */ bytes_left = nbytes; int bcopied=0; while(bytes_left > (n_uint64_t)0){ bytes_to_copy = mino((n_uint64_t)MAXBUF,bytes_left); read_bytes = bytes_to_copy; status = limeReaderReadData((void *)buf, &read_bytes, reader); if( status < 0 && status != LIME_EOR ) { fprintf(stderr, "LIME read error occurred: status= %d\n", status); exit(EXIT_FAILURE); } if (read_bytes != bytes_to_copy) { fprintf(stderr, "Read error %lld bytes wanted,%lld read\n", (unsigned long long)nbytes, (unsigned long long)read_bytes); return EXIT_FAILURE; } /* copy to the output buffer */ for(i=0; i<read_bytes; i++) buff_out[bcopied+i]=buf[i]; bcopied += read_bytes; bytes_left -= bytes_to_copy; } /* Quit at this record */ break; } limeDestroyReader(reader); DCAP(fclose)(fp); return EXIT_SUCCESS; }
void meson_ASCII_IO_one(char *filename, int rec_seek, int msg_seek, int chan_seek, int mom_seek) { int j,k=0,l=0; char buf[MAXBUF]; LimeReader *r; int status; twopt_mom *corr; n_uint64_t nbytes, bytes_left, bytes_to_copy, read_bytes; int rec, msg; char *lime_type; size_t bytes_pad; int MB_flag, ME_flag; FILE *fin=NULL; corr=malloc(NMOM_MAX*sizeof(twopt_mom)); fin=fopen(filename,"r"); error_root(fin == (FILE *)NULL,0,"meson_ASCII_IO [IOarchive.c]", "IOarchive: Unable to open file %s for reading\n", filename); /* Open LIME reader */ r = limeCreateReader(fin); error_root(r == (LimeReader *)NULL,0,"meson_ASCII_IO [IOarchive.c]", "Unable to open LimeReader\n"); /* Loop over records */ rec = 0; msg = 0; while( (status = limeReaderNextRecord(r)) != LIME_EOF ){ error_root(status != LIME_SUCCESS,0,"meson_ASCII_IO [IOarchive.c]", "IOarchive: limeReaderNextRecord returned status = %d\n",status); nbytes = limeReaderBytes(r); lime_type = limeReaderType(r); bytes_pad = limeReaderPadBytes(r); MB_flag = limeReaderMBFlag(r); ME_flag = limeReaderMEFlag(r); /* Update message and record numbers */ if(MB_flag == 1){ msg++; rec = 0; } rec++; /* Skip to next record until target record is reached */ if (msg != msg_seek || rec != rec_seek) continue; /* Buffered copy */ bytes_left = nbytes; l=0; while(bytes_left > (n_uint64_t)0){ bytes_to_copy=mino((n_uint64_t)(NMOM_MAX*sizeof(twopt_mom)),bytes_left); read_bytes = bytes_to_copy; error_root((int)read_bytes>MAXBUF,0,"meson_ASCII_IO [IOarchive.c]", "read_bytes exceeds MAXBUF"); status = limeReaderReadData((void *)buf, &read_bytes, r); error_root(status<0 && status !=LIME_EOR,1,"meson_ASCII_IO [IOarchive.c]", "LIME read error occurred: status= %d", status); error_root(read_bytes != bytes_to_copy,1,"meson_ASCII_IO [IOarchive.c]", "Read error %lld bytes wanted,%lld read\n", (unsigned long long)nbytes, (unsigned long long)read_bytes); /* Print to stdout */ if (l==chan_seek){ corr=(twopt_mom*)buf; for(j=0;j<TT;j++) { printf("%d %s %d %d %d %d %d %d %.16e %.16e\n", k,lime_type, (*(corr+mom_seek)).musrc, (*(corr+mom_seek)).musnk, (*(corr+mom_seek)).p[0], (*(corr+mom_seek)).p[1], (*(corr+mom_seek)).p[2], j, (*(corr+mom_seek)).corr[j].re, (*(corr+mom_seek)).corr[j].im); } } bytes_left -= bytes_to_copy; l=l+1; } /* Quit at this record */ break; } limeDestroyReader(r); fclose(fin); }
int main(int argc, char *argv[]) { char* data_buf; LimeReader *reader; FILE *fp; int status; n_uint64_t nbytes, read_bytes; int msg,rec,first; char *lime_type; size_t bytes_pad; int MB_flag, ME_flag; if( argc != 2 ) { fprintf(stderr, "Usage: %s <lime_file>\n", argv[0]); return EXIT_FAILURE; } fp = DCAPL(fopen)(argv[1], "r"); if(fp == (FILE *)NULL) { fprintf(stderr,"Unable to open file %s for reading\n", argv[1]); return EXIT_FAILURE; } reader = limeCreateReader(fp); if( reader == (LimeReader *)NULL ) { fprintf(stderr, "Unable to open LimeReader\n"); return EXIT_FAILURE; } msg = 0; first = 1; rec = 0; while( (status = limeReaderNextRecord(reader)) != LIME_EOF ){ if( status != LIME_SUCCESS ) { fprintf(stderr, "limeReaderNextRecord returned status = %d\n", status); return EXIT_FAILURE; } nbytes = limeReaderBytes(reader); lime_type = limeReaderType(reader); bytes_pad = limeReaderPadBytes(reader); MB_flag = limeReaderMBFlag(reader); ME_flag = limeReaderMEFlag(reader); if (MB_flag == 1 || first) { first = 0; rec = 0; msg++; } rec++; printf("\n\n"); printf("Message: %d\n", msg); printf("Record: %d\n", rec); printf("Type: %s\n", lime_type); printf("Data Length: %llu\n", (unsigned long long)nbytes); printf("Padding Length: %lu\n", (unsigned long)bytes_pad); printf("MB flag: %d\n", MB_flag); printf("ME flag: %d\n", ME_flag); /* TO DO: Buffer the input */ if(nbytes < MAX_BYTES){ data_buf = (char *)malloc((size_t)nbytes+1); if( data_buf == (char *)NULL) { fprintf(stderr, "Couldn't malloc data buf\n"); return EXIT_FAILURE; } read_bytes = nbytes; status = limeReaderReadData((void *)data_buf, &read_bytes, reader); if( status < 0 ) { if( status != LIME_EOR ) { fprintf(stderr, "LIME read error occurred: status= %d %llu bytes wanted, %llu read\n", status, (unsigned long long)nbytes, (unsigned long long)read_bytes); return EXIT_FAILURE; } } data_buf[nbytes]='\0'; if(!all_ascii(data_buf, nbytes)) printf("Data: [Binary data]\n"); else printf("Data: \"%s\" \n", data_buf); free(data_buf); } else{ printf("Data: [Long record skipped]\n"); } } limeDestroyReader(reader); DCAP(fclose)(fp); return EXIT_SUCCESS; }