char vcf_file::peek() { if (!compressed) return vcf_in.peek(); else { char c = gzgetc(gzvcf_in); gzungetc(c, gzvcf_in); return c; } }
/* * Peeks at and returns the next character without consuming it from the * input. (Ie a combination of getc and ungetc). */ int zfpeek(zfp *zf) { int c; if (zf->fp) { c = getc(zf->fp); if (c != EOF) ungetc(c, zf->fp); } else { c = gzgetc(zf->gz); if (c != EOF) gzungetc(c, zf->gz); } return c; }
std::pair<int,int> operator()( gzFile gzfile, const policy & p ) const { char ch; int rv = 0; int gzrv; while( (gzrv = gzread(gzfile,&ch,sizeof(char))) != 0 ) { rv += gzrv; if(p(ch)) { gzungetc(ch,gzfile); return std::make_pair(rv,gzrv); } } return std::make_pair(rv,gzrv); }
int ZOLTAN_FILE_ungetc(int c, ZOLTAN_FILE* file) { if (file->type == STANDARD) return (ungetc(c, file->strm.fileunc)); file->pos --; if (file->pos < 0) { file->size = 0; #ifdef ZOLTAN_GZIP if (file->type == GZIP) return (gzungetc(c, file->strm.filegz)); #endif } file->buffer[file->pos] = (char)c; return (c); }
bool auto_detect_fastq(gzFile fp, char * reads_filename) { // autodetect fasta/fastq format bool fastq=false; char c; c = gzgetc(fp); while (c == '#' || c == ';') { // discard this line while (c != -1 && c != '\n') c = gzgetc(fp); if (gzeof(fp)) break; if (c == -1) { fprintf(stderr, "did not find the end of a comment line in the input file [%s]. try disabling input autodetection\n", reads_filename); exit(1); } c = gzgetc(fp); } if (!gzeof(fp)) { if (c == -1) { fprintf(stderr, "did not find a non-comment line in the input file [%s]. try disabling input autodetection\n", reads_filename); exit(1); } if (c == '@') { fprintf(stderr,"detected fastq format in input file [%s]\n", reads_filename); fastq = true; } else if (c == '>') { fprintf(stderr, "detected fasta format in input file [%s]\n", reads_filename); fastq = false; } else { fprintf(stderr, "unrecognized character [%c] in input file [%s]. try disabling input autodetection\n", (char)c, reads_filename); exit(1); } gzungetc(c, fp); } return fastq; }
TInt CTestlibz::libzgzio() { __UHEAP_MARK; #ifdef NO_GZCOMPRESS INFO_PRINT1(_L("NO_GZCOMPRESS -- gz* functions cannot compress")); return KErrNone; #else const char Buffer[] = "Symbian Libz!"; Byte uncompr[15]; char fname[] = "C:\\Libz\\Test1\\bye.gz"; int err = 0; int len; gzFile file; z_off_t pos; err = iRfile.Create(iRfs, KGZFILE, EFileShareAny); if( err != KErrNone && err != KErrAlreadyExists ) { INFO_PRINTF1(_L("File create successfully\n")); return KErrGeneral; } len = (int)strlen(Buffer)+1; //-------------gzopen()------------------ INFO_PRINTF1(_L("gzopen()\n")); file = gzopen(fname, "wb"); if (file == NULL) { INFO_PRINTF1(_L("gzopen error")); return KErrGeneral; } //-------------gzputc() ------------ INFO_PRINTF1(_L("gputc()\n")); gzputc(file, 'S'); if (gzputs(file, "ymbian") != 6) { INFO_PRINTF2(_L("gzputs err: %s\n"), gzerror(file, &err)); return KErrGeneral; } //-------------gzprintf() ------------ INFO_PRINTF1(_L("gzprintf()\n")); if (gzprintf(file, " %s!", "Libz") != 6) { INFO_PRINTF2(_L("gzprintf err: %s\n"), gzerror(file, &err)); return KErrGeneral; } //-------------gzseek() ------------ INFO_PRINTF1(_L("gzseek()\n")); if(gzseek(file, 1L, SEEK_CUR) != 14){ /* add one zero byte */ INFO_PRINTF2(_L("gzseek err: %s\n"), gzerror(file, &err)); return KErrGeneral; } //-------------gzclose() ------------ INFO_PRINTF1(_L("gzclose()\n")); if(gzclose(file) == Z_ERRNO){ INFO_PRINTF2(_L("gzclose err: %s\n"), gzerror(file, &err)); return KErrGeneral; } //-------------gzopen()------------------ INFO_PRINTF1(_L("gzopen()\n")); file = gzopen(fname, "rb"); if (file == NULL) { INFO_PRINTF1(_L("gzopen error\n")); return KErrGeneral; } strcpy((char*)uncompr, "garbage"); //-------------gzread()------------------ INFO_PRINTF1(_L("gzread()\n")); if (gzread(file, uncompr, sizeof(uncompr)) != len) { INFO_PRINTF2(_L("gzread err: %s\n"), gzerror(file, &err)); return KErrGeneral; } if (strcmp((char*)uncompr, Buffer)) { INFO_PRINTF2(_L("bad gzread: %s\n"), (char*)uncompr); return KErrGeneral; } //-------------gzseek() & gztell()----------------- INFO_PRINTF1(_L("gzseek & gztell()\n")); pos = gzseek(file, -7L, SEEK_CUR); if (gztell(file) != pos || pos != 7) { INFO_PRINTF3(_L("gzseek error, pos=%ld, gztell=%ld\n"), (long)pos, (long)gztell(file)); return KErrGeneral; } //-------------gzgetc()------------------ INFO_PRINTF1(_L("gzgetc()\n")); if (gzgetc(file) != ' ') { INFO_PRINTF1(_L("gzgetc error")); return KErrGeneral; } //-------------gzungetc()------------------ INFO_PRINTF1(_L("gzungetc\n")); if (gzungetc(' ', file) != ' ') { INFO_PRINTF1(_L("gzungetc error\n")); return KErrGeneral; } //-------------gzgets()------------------ INFO_PRINTF1(_L("gzgets()\n")); gzgets(file, (char*)uncompr, sizeof(uncompr)); if (strlen((char*)uncompr) != 6) { /* " Libz!" */ INFO_PRINTF2(_L("gzgets err after gzseek: %s\n"), gzerror(file, &err)); return KErrGeneral; } if (strcmp((char*)uncompr, Buffer + 7)) { INFO_PRINTF1(_L("bad gzgets after gzseek\n")); return KErrGeneral; } //-------------gzclose() ------------ if(gzclose(file) == Z_ERRNO) { INFO_PRINTF2(_L("gzclose err: %s\n"), gzerror(file, &err)); return KErrGeneral; } #endif __UHEAP_MARKEND; return KErrNone; }
/* =========================================================================== * Test read/write of .gz files */ void test_gzio( const char *fname, Byte *uncompr, uLong uncomprLen) { #ifdef NO_GZCOMPRESS fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n"); #else int err; int len = (int)strlen(hello)+1; gzFile file; z_off_t pos; file = gzopen(fname, "wb"); if (file == NULL) { fprintf(stderr, "gzopen error\n"); exit(1); } gzputc(file, 'h'); if (gzputs(file, "ello") != 4) { fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); exit(1); } if (gzprintf(file, ", %s!", "hello") != 8) { fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); exit(1); } gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ gzclose(file); file = gzopen(fname, "rb"); if (file == NULL) { fprintf(stderr, "gzopen error\n"); exit(1); } strcpy((char*)uncompr, "garbage"); if (gzread(file, uncompr, (unsigned)uncomprLen) != len) { fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); exit(1); } if (strcmp((char*)uncompr, hello)) { fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); exit(1); } else { printf("gzread(): %s\n", (char*)uncompr); } pos = gzseek(file, -8L, SEEK_CUR); if (pos != 6 || gztell(file) != pos) { fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", (long)pos, (long)gztell(file)); exit(1); } if (gzgetc(file) != ' ') { fprintf(stderr, "gzgetc error\n"); exit(1); } if (gzungetc(' ', file) != ' ') { fprintf(stderr, "gzungetc error\n"); exit(1); } gzgets(file, (char*)uncompr, (int)uncomprLen); if (strlen((char*)uncompr) != 7) { /* " hello!" */ fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); exit(1); } if (strcmp((char*)uncompr, hello + 6)) { fprintf(stderr, "bad gzgets after gzseek\n"); exit(1); } else { printf("gzgets() after gzseek: %s\n", (char*)uncompr); } gzclose(file); #endif }
/* Read the next ICP frame from the file. */ int icp_readdetector(gzFile infile, int rows, int columns, Counts frame[], int *linenum) { z_off_t restore_pos; int c = 0, r = 0, number = 0, innumber = 0, inframe = 0; int ch; /* Don't do anything if there are no data frames */ if (rows*columns == 0) return ICP_GOOD; /* Store zeros in all the values, just in case the frame doesn't read properly */ // memset(frame, 0, rows*columns*sizeof(*frame)); /* First character in a line in the table is a space and the second is a digit. Note that sometimes ICP drops frames, so we need to check if the next line is a motor line. Usually these start with two spaces so we can easily check for them, but sometimes they start with a space and a dash if the motor values is negative. Even worse, for NG7 slit scans only!, they can start with a single character and need to use a different algorithm: if the first line is not a singleton integer or a comma separated sequence, then it is not a frame. */ ch = gzgetc(infile); /* Format column character */ if (ch < 0) return (gzeof(infile)?ICP_GOOD:ICP_READ_ERROR); if (ch != ' ') return ICP_FORMAT_COLUMN_ERROR; /* Remember where we are in case we need the NG7 slit restore */ restore_pos = gztell(infile); /* Try for a quick reject based on space-space or space-dash */ ch = gzgetc(infile); /* Peek at next character... */ if (ch < 0) return (gzeof(infile)?ICP_GOOD:ICP_READ_ERROR); gzungetc(ch,infile); /* ...and put it back */ if (ch == ' ' || ch == '-') return ICP_GOOD; /* Empty frame */ for (;;) { ch = gzgetc(infile); // printf("next char is %c\n",ch); switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': /* Append a digit to the current number */ number = number * 10 + ch - '0'; innumber = 1; break; case ',': /* Put a comma between every column */ if (++c >= columns) { return ICP_COLUMN_ERROR; } else { *frame++ = number; } // printf("Next number is %d\n",number); number=0; innumber = 0; inframe = 1; break; case ';': /* Put a semicolon between every row */ if (++c != columns) { return ICP_COLUMN_ERROR; } else if (++r == rows) { return ICP_ROW_ERROR; } else { *frame++ = number; } // printf("Last number is %d\n",number); c = number = 0; innumber = 0; inframe = 1; break; case ' ': /* Shouldn't have a space between values unless the last character was punctuation, or * if we are not in a frame */ if (innumber) { if (!inframe) { gzseek(infile, restore_pos, SEEK_SET); return ICP_GOOD; } else { return ICP_UNEXPECTED_CHARACTER; } } /* Skip spaces at the end of the line; these will be after a final punctuation mark. Move past the format column of the next line. */ do { ch = gzgetc(infile); } while (ch == ' '); if (ch == '\r') ch = gzgetc(infile); if (ch != '\n') return ICP_UNEXPECTED_CHARACTER; (*linenum)++; ch = gzgetc(infile); if (ch != ' ') return ICP_FORMAT_COLUMN_ERROR; /* Continue with the next character. */ break; case '\r': break; case '\n': if (innumber) { // printf("ending the line inside the number %d\n",number); /* If line ends without punctuation, then we are at the end of the frame. */ if (++c != columns) { return ICP_COLUMN_ERROR; } else if (++r != rows) { return ICP_ROW_ERROR; } else { *frame++ = number; } (*linenum)++; return ICP_GOOD; } else { /* If line ends with punctuation, then we are still in the table and the next character should be a blank in the format column. */ (*linenum)++; ch = gzgetc(infile); if (ch < 0) return ICP_READ_ERROR; if (ch >= 0 && ch != ' ') return ICP_UNEXPECTED_CHARACTER; } break; case -1: /* Check for error */ if (!gzeof(infile)) return ICP_READ_ERROR; /* EOF should be end of frame */ if (innumber) { if (++c != columns) { return ICP_COLUMN_ERROR; } else if (++r != rows) { return ICP_ROW_ERROR; } else { *frame++ = number; } } return ICP_GOOD; break; default: /* if it isn't a digit, space, comma, semicolon or newline indicates * we are not in a frame. */ if (inframe) { return ICP_UNEXPECTED_CHARACTER; } else { gzseek(infile, restore_pos, SEEK_SET); return ICP_GOOD; } } } }