Example #1
0
gzFile 
safe_gzopen(char const *file)
  {
  gzFile fp;
  if (STREQ(file, "-"))
    fp = gzdopen(fileno(stdin), "r");
  else
    fp = gzopen(file, "r");
  if ( isNull(fp) )
    errabort("%s : %s", file, strerror(errno));
  return fp;
  }
Example #2
0
File: paf.c Project: fxia22/HINGE
paf_file_t *paf_open(const char *fn)
{
	kstream_t *ks;
	gzFile fp;
	paf_file_t *pf;
	fp = fn && strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
	if (fp == 0) return 0;
	ks = ks_init(fp);
	pf = (paf_file_t*)calloc(1, sizeof(paf_file_t));
	pf->fp = ks;
	return pf;
}
Example #3
0
bool ZipFile::open(const String& filename, const String& mode) {
  assert(m_gzFile == nullptr);

  if (strchr(mode.c_str(), '+')) {
    raise_warning("cannot open a zlib stream for reading and writing "
                    "at the same time!");
    return false;
  }

  return m_innerFile->open(filename, mode) &&
    (m_gzFile = gzdopen(dup(m_innerFile->fd()), mode.data()));
}
Example #4
0
static int gz_file_open(struct output_file *out, int fd)
{
	struct output_file_gz *outgz = to_output_file_gz(out);

	outgz->gz_fd = gzdopen(fd, "wb9");
	if (!outgz->gz_fd) {
		error_errno("gzopen");
		return -errno;
	}

	return 0;
}
Example #5
0
bool emu_suspend(const char *file)
{
    gui_busy_raii gui_busy;

    FILE *fp = fopen_utf8(file, "wb");
    if(!fp)
        return false;

    int dupfd = dup(fileno(fp));
    fclose(fp);
    fp = nullptr;

    // gzdopen takes ownership of the fd
    gzFile gzf = gzdopen(dupfd, "wb");
    if(!gzf)
    {
        close(dupfd);
        return false;
    }

    size_t size = sizeof(emu_snapshot) + flash_suspend_flexsize();
    auto snapshot = (struct emu_snapshot *) malloc(size);
    if(!snapshot)
    {
        gzclose(gzf);
        return false;
    }

    snapshot->product = product;
    snapshot->asic_user_flags = asic_user_flags;
    // TODO: Max length
    strncpy(snapshot->path_boot1, path_boot1.c_str(), sizeof(snapshot->path_boot1) - 1);
    strncpy(snapshot->path_flash, path_flash.c_str(), sizeof(snapshot->path_flash) - 1);

    if(!flash_suspend(snapshot)
            || !cpu_suspend(snapshot)
            || !sched_suspend(snapshot)
            || !memory_suspend(snapshot))
    {
        free(snapshot);
        gzclose(gzf);
        return false;
    }

    snapshot->sig = SNAPSHOT_SIG;
    snapshot->version = SNAPSHOT_VER;

    bool success = (size_t) gzwrite(gzf, snapshot, size) == size;

    free(snapshot);
    gzclose(gzf);
    return success;
}
void Fast5Map::load_from_fasta(std::string fasta_filename)
{
    gzFile gz_fp;

    FILE* fp = fopen(fasta_filename.c_str(), "r");
    if(fp == NULL) {
        fprintf(stderr, "error: could not open %s for read\n", fasta_filename.c_str());
        exit(EXIT_FAILURE);
    }

    gz_fp = gzdopen(fileno(fp), "r");
    if(gz_fp == NULL) {
        fprintf(stderr, "error: could not open %s using gzdopen\n", fasta_filename.c_str());
        exit(EXIT_FAILURE);
    }

    kseq_t* seq = kseq_init(gz_fp);
    
    while(kseq_read(seq) >= 0) {
        if(seq->comment.l == 0) {
            fprintf(stderr, "error: no path associated with read %s\n", seq->name.s);
            exit(EXIT_FAILURE);
        }

        // This splitting code implicitly handles both the 2 and 3 field
        // fasta format that poretools will output. The FAST5 path
        // is always the last field.
        std::vector<std::string> fields = split(seq->comment.s, ' ');
        read_to_path_map[seq->name.s] = fields.back();
    }

    kseq_destroy(seq);
    gzclose(gz_fp);
    fclose(fp);
    
    // Sanity check that the first path actually points to a file
    if(read_to_path_map.size() > 0) {
        std::string first_read = read_to_path_map.begin()->first;
        std::string first_path = read_to_path_map.begin()->second;
        struct stat file_s;
        int ret = stat(first_path.c_str(), &file_s);
        if(ret != 0) {
            fprintf(stderr, "Error: could not find path to FAST5 for read %s\n", first_read.c_str());
            fprintf(stderr, "Please make sure that this path is accessible: %s\n", first_path.c_str());
            exit(EXIT_FAILURE);
        }
    }

    // Write the map as a fofn file so next time we don't have to parse
    // the entire fasta
    write_to_fofn(fasta_filename + FOFN_SUFFIX);
}
Example #7
0
File: parse.c Project: dcjones/cbgb
fastq_t* fastq_open(FILE* f)
{
    fastq_t* fqf = malloc_or_die(sizeof(fastq_t));
    or_die((int)((fqf->file = gzdopen(fileno(f), "rb")) != NULL),
           "Can not open gzip file.");
    
    fqf->state = STATE_ID1;
    fqf->buf = malloc_or_die(fastq_buf_size);
    fqf->buf[0] = '\0';
    fqf->c = fqf->buf;

    return fqf;
}
Example #8
0
int main(int argc, char *argv[])
{
    gzFile fp;
    if (argc == 1) {
        fprintf(stderr, "Version: %s\n", PACKAGE_VERSION);
        fprintf(stderr, "Usage: maq2sam <in.map> [<readGroup>]\n");
        return 1;
    }
    fp = strcmp(argv[1], "-")? gzopen(argv[1], "r") : gzdopen(fileno(stdin), "r");
    maq2tam_core(fp, argc > 2? argv[2] : 0);
    gzclose(fp);
    return 0;
}
Example #9
0
int main(void)
{
	gzFile fp;
	kseq_t *seq;
	int n = 0, slen = 0, qlen = 0;
	fp = gzdopen(fileno(stdin), "r");
	seq = kseq_init(fp);
	while (kseq_read(seq) >= 0)
		printf("%s\t%s\t%s\t%s\n", seq->name.s, seq->comment.s, seq->seq.s, seq->qual.s);
	kseq_destroy(seq);
	gzclose(fp);
	return 0;
}
Example #10
0
gzwRequest gzw_init(Request request) {
	char * ae=getHeader(request.headers,"Accept-Encoding");
	gzwRequest gzwr;
	gzwr.request=request;
	if (strstr(ae,ENCODING_GZIP)!=NULL) {
		gzwr.gzenabled=true;
		gzwr.gzfile= gzdopen(request.client,"w");
	} else {
		gzwr.gzenabled=false;
		gzwr.file= fdopen(request.client,"w");
	}
	return gzwr;
}
Example #11
0
int main(int argc, char *argv[])
{
	bwaidx_t *idx;
	gzFile fp;
	kseq_t *ks;
	mem_opt_t *opt;

	if (argc < 3) {
		fprintf(stderr, "Usage: bwamem-lite <idx.base> <reads.fq>\n");
		return 1;
	}

	idx = bwa_idx_load(argv[1], BWA_IDX_ALL); // load the BWA index
	if (NULL == idx) {
		fprintf(stderr, "Index load failed.\n");
		exit(EXIT_FAILURE);
	}
	fp = strcmp(argv[2], "-")? gzopen(argv[2], "r") : gzdopen(fileno(stdin), "r");
	if (NULL == fp) {
		fprintf(stderr, "Couldn't open %s : %s\n",
				strcmp(argv[2], "-") ? argv[2] : "stdin",
				errno ? strerror(errno) : "Out of memory");
		exit(EXIT_FAILURE);
	}
	ks = kseq_init(fp); // initialize the FASTA/Q parser
	opt = mem_opt_init(); // initialize the BWA-MEM parameters to the default values

	while (kseq_read(ks) >= 0) { // read one sequence
		mem_alnreg_v ar;
		int i, k;
		ar = mem_align1(opt, idx->bwt, idx->bns, idx->pac, ks->seq.l, ks->seq.s); // get all the hits
		for (i = 0; i < ar.n; ++i) { // traverse each hit
			mem_aln_t a;
			if (ar.a[i].secondary >= 0) continue; // skip secondary alignments
			a = mem_reg2aln(opt, idx->bns, idx->pac, ks->seq.l, ks->seq.s, &ar.a[i]); // get forward-strand position and CIGAR
			// print alignment
			err_printf("%s\t%c\t%s\t%ld\t%d\t", ks->name.s, "+-"[a.is_rev], idx->bns->anns[a.rid].name, (long)a.pos, a.mapq);
			for (k = 0; k < a.n_cigar; ++k) // print CIGAR
				err_printf("%d%c", a.cigar[k]>>4, "MIDSH"[a.cigar[k]&0xf]);
			err_printf("\t%d\n", a.NM); // print edit distance
			free(a.cigar); // don't forget to deallocate CIGAR
		}
		free(ar.a); // and deallocate the hit list
	}

	free(opt);
	kseq_destroy(ks);
	err_gzclose(fp);
	bwa_idx_destroy(idx);
	return 0;
}
Example #12
0
/* FIXME: This routine does not support data > 2GB (limited to int) */
ZP_DATASIZE_TYPE gunzip (char* inbuf, ZP_DATASIZE_TYPE inlen, char** outbuf, ZP_DATASIZE_TYPE *outlen, int max_growth)
{
		int filedes, filedes_unpack;
		FILE *file_pack, *file_unpack;
		char filenam[] = "/tmp/ziproxy_XXXXXX";
		char filenam_unpack[] = "/tmp/ziproxy_XXXXXX";
		gzFile gzfile = Z_NULL;
		char buff_unpack[GUNZIP_BUFF];
		int len_unpack_block;
		ZP_DATASIZE_TYPE max_outlen;

		max_outlen = (inlen * max_growth) / 100;
		*outlen = 0;
		
		if((filedes = mkstemp(filenam)) < 0) return 10;
		unlink(filenam);
		if((filedes_unpack = mkstemp(filenam_unpack)) < 0) return 10;
		unlink(filenam_unpack);

		if ((file_pack = fdopen(filedes, "w+")) == NULL) return 20;
		if ((file_unpack = fdopen(filedes_unpack, "w+")) == NULL) return 20;
		
		/* dump packed data into the file */
		fwrite(inbuf, inlen, 1, file_pack);
		fseek(file_pack, 0, SEEK_SET);

		/* proceed with unpacking data into another file */
		if((gzfile = gzdopen(dup(filedes), "rb")) == Z_NULL) return 20;
		while ((len_unpack_block = gzread(gzfile, buff_unpack, GUNZIP_BUFF)) > 0) {
			*outlen += len_unpack_block;
			if ((max_growth != 0) && (*outlen > max_outlen))
				return 100;
			fwrite(buff_unpack, len_unpack_block, 1, file_unpack);
		}

		/* a smaller decompressed file is not necessarily result of broken gzip data */
		//if (*outlen < inlen)
		//	return 120;

		gzclose(gzfile);
		fclose(file_pack);

		/* load unpacked data to the memory */
		if ((*outbuf=realloc(*outbuf, *outlen)) != NULL) {
			fseek(file_unpack, 0, SEEK_SET);
			fread(*outbuf, *outlen, 1, file_unpack);
		}
		fclose(file_unpack);
		
		return 0;
}
Example #13
0
/*
 * Try to get the object hash from a manifest file. Caller frees. Returns NULL
 * on failure.
 */
struct file_hash *
manifest_get(struct conf *conf, const char *manifest_path)
{
	int fd;
	gzFile f = NULL;
	struct manifest *mf = NULL;
	struct hashtable *hashed_files = NULL; /* path --> struct file_hash */
	uint32_t i;
	struct file_hash *fh = NULL;

	fd = open(manifest_path, O_RDONLY | O_BINARY);
	if (fd == -1) {
		/* Cache miss. */
		cc_log("No such manifest file");
		goto out;
	}
	f = gzdopen(fd, "rb");
	if (!f) {
		close(fd);
		cc_log("Failed to gzdopen manifest file");
		goto out;
	}
	mf = read_manifest(f);
	if (!mf) {
		cc_log("Error reading manifest file");
		goto out;
	}

	hashed_files = create_hashtable(1000, hash_from_string, strings_equal);

	/* Check newest object first since it's a bit more likely to match. */
	for (i = mf->n_objects; i > 0; i--) {
		if (verify_object(conf, mf, &mf->objects[i - 1], hashed_files)) {
			fh = x_malloc(sizeof(*fh));
			*fh = mf->objects[i - 1].hash;
			goto out;
		}
	}

out:
	if (hashed_files) {
		hashtable_destroy(hashed_files, 1);
	}
	if (f) {
		gzclose(f);
	}
	if (mf) {
		free_manifest(mf);
	}
	return fh;
}
Example #14
0
size_t fastq_paired_two_files_for_each_parallel(string& file1, string& file2, function<void(Alignment&, Alignment&)> lambda) {
    gzFile fp1 = (file1 != "-") ? gzopen(file1.c_str(), "r") : gzdopen(fileno(stdin), "r");
    gzFile fp2 = (file2 != "-") ? gzopen(file2.c_str(), "r") : gzdopen(fileno(stdin), "r");
    size_t len = 2 << 18; // 256k
    size_t nLines = 0;
    int thread_count = get_thread_count();
    //vector<Alignment> alns; alns.resize(thread_count);
    vector<char*> bufs; bufs.resize(thread_count);
    for (auto& buf : bufs) {
        buf = new char[len];
    }
    bool more_data = true;
#pragma omp parallel shared(fp1, fp2, more_data, bufs)
    {
        int tid = omp_get_thread_num();
        while (more_data) {
            Alignment mate1, mate2;
            char* buf = bufs[tid];
            bool got_anything = false;
#pragma omp critical (fastq_input)
            {
                if (more_data) {
                    got_anything = more_data = get_next_alignment_pair_from_fastqs(fp1, fp2, buf, len, mate1, mate2);
                    nLines++;
                }
            }
            if (got_anything) {
                lambda(mate1, mate2);
            }
        }
    }
    for (auto& buf : bufs) {
        delete buf;
    }
    gzclose(fp1);
    gzclose(fp2);
    return nLines;
}
Example #15
0
int
main(int argc, char * argv[])
{
  int i;
  gzFile f;
  int seen_stdin = 0;

  if (argc < 2) {
    f = gzdopen(fileno(stdin), "r");
    process_file(f);
    gzclose(f);
  } else {
    for (i = 1; i < argc; i++) {
      if (strncmp(argv[i], "-", 2) == 0) {
	if (seen_stdin) {
	  // ignore it
	  //fprintf(stderr, "cannot cat stdin twice\n");
	  //exit(1);
	} else {
	  seen_stdin = 1;
	  f = gzdopen(fileno(stdin), "r");
	  process_file(f);
	  gzclose(f);
	}
      } else {
	f = gzopen(argv[i], "r");
	if (f == NULL) {
	  fprintf(stderr, "error opening file [%s]: %s\n", argv[i], strerror(errno));
	}
	process_file(f);
	gzclose(f);
      }
    }
  }

  fclose(stdout);
  return 0;
}
Example #16
0
gzFile GZdopen(int fd,const char *mode){
	gzFile gz;
	int handle;

	thread_yield();

	inGzip++; FL_F_Gzip = "Gzdopen"; FL_L_Gzip = __LINE__;
    if( isWindows() && !withDG_Zlib() ){
	handle = 0x80000000 | fd2handle(fd);
	gz = gzdopen(handle,mode);
	if( gz == 0 ){
		syslog_ERROR("-- failed gzdopen(0x%X)\n",handle);
		gz = gzdopen(fd,mode);
	}
    }else{
	gz = gzdopen(fd,mode);
    }
	inGzip--;
	if( gz == 0 ){
		syslog_ERROR("-- failed gzdopen(%d)\n",fd);
	}
	return gz;
}
Example #17
0
gzFile err_xzopen_core(const char *func, const char *fn, const char *mode)
{
	gzFile fp;
	if (strcmp(fn, "-") == 0) {
		fp = gzdopen(fileno((strstr(mode, "r"))? stdin : stdout), mode);
		/* According to zlib.h, this is the only reason gzdopen can fail */
		if (!fp) err_fatal(func, "Out of memory");
		return fp;
	}
	if ((fp = gzopen(fn, mode)) == 0) {
		err_fatal(func, "fail to open file '%s' : %s", fn, errno ? strerror(errno) : "Out of memory");
	}
	return fp;
}
Example #18
0
static void md5_one(const char *fn)
{
    hts_md5_context *md5_one, *md5_all;
    int l, i, k;
    gzFile fp;
    kseq_t *seq;
    unsigned char unordered[16], digest[16];
    char hex[33];

    for (l = 0; l < 16; ++l) unordered[l] = 0;
    fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
    if (fp == 0) {
        fprintf(stderr, "md5fa: %s: No such file or directory\n", fn);
        exit(1);
    }

    if (!(md5_all = hts_md5_init()))
        exit(1);

    if (!(md5_one = hts_md5_init())) {
        hts_md5_destroy(md5_all);
        exit(1);
    }

    seq = kseq_init(fp);
    while ((l = kseq_read(seq)) >= 0) {
        for (i = k = 0; i < seq->seq.l; ++i) {
            if (islower(seq->seq.s[i])) seq->seq.s[k++] = toupper(seq->seq.s[i]);
            else if (isupper(seq->seq.s[i])) seq->seq.s[k++] = seq->seq.s[i];
        }
        hts_md5_reset(md5_one);
        hts_md5_update(md5_one, (unsigned char*)seq->seq.s, k);
        hts_md5_final(digest, md5_one);
        hts_md5_hex(hex, digest);
        for (l = 0; l < 16; ++l)
            unordered[l] ^= digest[l];
        printf("%s  %s  %s\n", hex, fn, seq->name.s);
        hts_md5_update(md5_all, (unsigned char*)seq->seq.s, k);
    }
    hts_md5_final(digest, md5_all);
    kseq_destroy(seq);

    hts_md5_hex(hex, digest);
    printf("%s  %s  >ordered\n", hex, fn);
    hts_md5_hex(hex, unordered);
    printf("%s  %s  >unordered\n", hex, fn);

    hts_md5_destroy(md5_all);
    hts_md5_destroy(md5_one);
}
Example #19
0
size_t fastq_paired_interleaved_for_each(string& filename, function<void(Alignment&, Alignment&)> lambda) {
    gzFile fp = (filename != "-") ? gzopen(filename.c_str(), "r") : gzdopen(fileno(stdin), "r");
    size_t len = 2 << 18; // 256k
    size_t nLines = 0;
    char *buffer = new char[len];
    Alignment mate1, mate2;
    while(get_next_interleaved_alignment_pair_from_fastq(fp, buffer, len, mate1, mate2)) {
        lambda(mate1, mate2);
        nLines++;
    }
    gzclose(fp);
    delete buffer;
    return nLines;
}
Example #20
0
static int load_zlib(struct kmod_file *file)
{
	int err = 0;
	off_t did = 0, total = 0;
	_cleanup_free_ unsigned char *p = NULL;

	errno = 0;
	file->gzf = gzdopen(file->fd, "rb");
	if (file->gzf == NULL)
		return -errno;
	file->fd = -1; /* now owned by gzf due gzdopen() */

	for (;;) {
		int r;

		if (did == total) {
			void *tmp = realloc(p, total + READ_STEP);
			if (tmp == NULL) {
				err = -errno;
				goto error;
			}
			total += READ_STEP;
			p = tmp;
		}

		r = gzread(file->gzf, p + did, total - did);
		if (r == 0)
			break;
		else if (r < 0) {
			int gzerr;
			const char *gz_errmsg = gzerror(file->gzf, &gzerr);

			ERR(file->ctx, "gzip: %s\n", gz_errmsg);

			/* gzip might not set errno here */
			err = gzerr == Z_ERRNO ? -errno : -EINVAL;
			goto error;
		}
		did += r;
	}

	file->memory = p;
	file->size = did;
	p = NULL;
	return 0;

error:
	gzclose(file->gzf);
	return err;
}
Example #21
0
void GzipInputFile::open(const std::string& fileName)
{
  assert(m_fd == -1);
  m_fd = ::open(fileName.c_str(), O_RDONLY);
  TRY_SYS_CALL(m_fd != -1, "open(" + fileName + ", O_RDONLY)");
  m_gzfile = gzdopen(m_fd, "r");
  if (m_gzfile == NULL)
    {
      ::close(m_fd);
      m_fd = -1;
      GZIP_STOP("gzdfile(" + fileName + ") cannot create valid gzip file descriptor");
    }
  m_fileName = fileName;
}
Example #22
0
/**
 * Compress data from fd_source into fd_dest.
 *
 * @param fd_source	Source file descriptor
 * @param fd_dest	Destination file descriptor
 * @return 		Number of bytes readed from fd_source or -1 on error
 */
static off_t gzCompress(void *cancel_cookie, int fd_source, int fd_dest)
{
	char buf[BUF_SIZE];
	int wr;
	int rd;
	gzFile fd_gz = NULL;
	off_t size = 0;
	int dup_fd;

	dup_fd = dup(fd_dest);
	if (dup_fd == -1)
	{
		return (off_t) FAIL;
	}

	fd_gz = gzdopen(dup_fd, COMPRESSLEVEL_BACKGROUND);
	if (fd_gz == NULL)
	{
		file_close(&dup_fd);
		return (off_t) FAIL;
	}

	while ((rd = read(fd_source, buf, sizeof(buf))) > 0)
	{
		size += rd;

		wr = gzwrite(fd_gz, buf, rd);
		if(wr == -1)
		{
			gzClose(fd_gz);
			return (off_t) FAIL;
		}

		if (compress_testcancel(cancel_cookie))
		{
			break;
		}
	}

	if (rd < 0)
	{
		gzClose(fd_gz);
		return (off_t) FAIL;
	}
	
	gzClose(fd_gz);
	
	return size;
}
Example #23
0
/*!
  @see futil_open()
 */
gzFile futil_gzopen(const char *path, const char *mode)
{
  ctx_assert(strcmp(path, "-") != 0 || strcmp(mode,"w") == 0);
  gzFile gzout = strcmp(path, "-") == 0 ? gzdopen(fileno(stdout), mode)
                                        : gzopen(path, mode);
  if(gzout == NULL)
    die("Cannot open gzfile: %s [%s]", futil_outpath_str(path), strerror(errno));

  // Set buffer size
  #if ZLIB_VERNUM >= 0x1240
    gzbuffer(gzout, DEFAULT_IO_BUFSIZE);
  #endif

  return gzout;
}
NS_IMETHODIMP
nsGZFileWriter::InitANSIFileDesc(FILE* aFile)
{
    mGZFile = gzdopen(dup(fileno(aFile)), "wb");
    fclose(aFile);

    // gzdopen returns nullptr on error.
    if (NS_WARN_IF(!mGZFile)) {
        return NS_ERROR_FAILURE;
    }

    mInitialized = true;

    return NS_OK;
}
Example #25
0
File: bin.c Project: bitursa/maos
/*
  Open a bin file from a fd that may be a socket.
*/
static file_t* zfdopen(int sock, const char *mod) {
    file_t* fp=calloc(1, sizeof(file_t));
    fp->isgzip=0;
    fp->fd=sock;
    if(fp->isgzip) {
        if(!(fp->p=gzdopen(fp->fd,mod))) {
            error("Error gzdopen for %d\n",sock);
        }
    } else {
        if(!(fp->p=fdopen(fp->fd,mod))) {
            error("Error fdopen for %d\n",sock);
        }
    }
    return fp;
}
Example #26
0
File: arg.c Project: lh3/fastARG
int main_regtest(int argc, char *argv[])
{
	arg_t *a;
	gzFile fp;
	if (argc == 1) {
		fprintf(stderr, "Usage: fastARG regtest <in.arg>\n");
		return 1;
	}
	fp = strcmp(argv[1], "-") == 0? gzdopen(fileno(stdin), "r") : gzopen(argv[1], "r");
	a = arg_load(fp);
	gzclose(fp);
	arg_print(a);
	arg_destroy(a);
	return 0;
}
Example #27
0
VISIBLE QFile *
Qdopen (int fd, const char *mode)
{
	QFile      *file;
	char       *m, *p;
#ifdef HAVE_ZLIB
	int         zip = 0;
#endif
	int         len = strlen (mode);

	m = alloca (len + 1);
#ifdef _WIN32
	setmode (fd, O_BINARY);
#endif
	for (p = m; *mode && p - m < len; mode++) {
		if (*mode == 'z') {
#ifdef HAVE_ZLIB
			zip = 1;
#endif
			continue;
		}
		*p++ = *mode;
	}

	*p = 0;

	file = calloc (sizeof (*file), 1);
	if (!file)
		return 0;
#ifdef HAVE_ZLIB
	if (zip) {
		file->gzfile = gzdopen (fd, m);
		if (!file->gzfile) {
			free (file);
			return 0;
		}
	} else
#endif
	{
		file->file = fdopen (fd, m);
		if (!file->file) {
			free (file);
			return 0;
		}
	}
	file->c = -1;
	return file;
}
Example #28
0
reghash_t *stk_reg_read(const char *fn)
{
	reghash_t *h = kh_init(reg);
	gzFile fp;
	kstream_t *ks;
	int dret;
	kstring_t *str;
	// read the list
	str = calloc(1, sizeof(kstring_t));
	fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
	ks = ks_init(fp);
	while (ks_getuntil(ks, 0, str, &dret) >= 0) {
		int beg = -1, end = -1;
		reglist_t *p;
		khint_t k = kh_get(reg, h, str->s);
		if (k == kh_end(h)) {
			int ret;
			char *s = strdup(str->s);
			k = kh_put(reg, h, s, &ret);
			memset(&kh_val(h, k), 0, sizeof(reglist_t));
		}
		p = &kh_val(h, k);
		if (dret != '\n') {
			if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0])) {
				beg = atoi(str->s);
				if (dret != '\n') {
					if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0])) {
						end = atoi(str->s);
						if (end < 0) end = -1;
					}
				}
			}
		}
		// skip the rest of the line
		if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n');
		if (end < 0 && beg > 0) end = beg, beg = beg - 1; // if there is only one column
		if (beg < 0) beg = 0, end = INT_MAX;
		if (p->n == p->m) {
			p->m = p->m? p->m<<1 : 4;
			p->a = realloc(p->a, p->m * 8);
		}
		p->a[p->n++] = (uint64_t)beg<<32 | end;
	}
	ks_destroy(ks);
	gzclose(fp);
	free(str->s); free(str);
	return h;
}
Example #29
0
static int libtar_gzopen(void* call_data, const char *pathname,
  int oflags, mode_t mode)
{
  char *gzoflags;
  int fd;
  struct gzStruct* gzf = (struct gzStruct*)call_data;

  switch (oflags & O_ACCMODE)
  {
  case O_WRONLY:
    gzoflags = "wb";
    break;
  case O_RDONLY:
    gzoflags = "rb";
    break;
  default:
  case O_RDWR:
    errno = EINVAL;
    return -1;
  }

  fd = open(pathname, oflags, mode);
  if (fd == -1)
    {
    return -1;
    }

#if defined(__BEOS__) && !defined(__ZETA__)  /* no fchmod on BeOS...do pathname instead. */
  if ((oflags & O_CREAT) && chmod(pathname, mode & 07777))
    {
    return -1;
    }
#elif !defined(_WIN32) || defined(__CYGWIN__)
  if ((oflags & O_CREAT) && fchmod(fd, mode & 07777))
    {
    return -1;
    }
#endif

  gzf->GZFile = gzdopen(fd, gzoflags);
  if (!gzf->GZFile)
  {
    errno = ENOMEM;
    return -1;
  }

  return fd;
}
Example #30
0
// Open gzFile
// rb - read
// wb6 - write with compresion level 6
// wb9 - write with compresion level 9
// wbT -  write without compression
gzFile open_gzfile(const char* name, const char* mode, uint64_t buf_size){
  gzFile fh = NULL;

  if( strcmp(name, "-") == 0 )
    fh = gzdopen(fileno(stdin), mode);
  else
    fh = gzopen(name, mode);

  if(fh == NULL)
    return NULL;

  if(gzbuffer(fh, buf_size) < 0)
    return NULL;

  return fh;
}