<commit_before>/*
 * Copyright (c) 2013 <NAME> <<EMAIL>>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <zlib.h>

#include <file.h>
#include <xmem.h>
#include <str.h>

#define READ_BUF_SIZE	4096
#define INVALID_FILE_ARGUMENT() \
	do { \
		fprintf(stderr, "%s(): invalid file argument", __func__); \
		abort(); \
	} while(0)
#define DBG(fmt, ...)

static char	*fgz_gets(gzFile f);
static char	*fp_gets(FILE *fp);

f_file_t
f_open(const char *path, const char *mode)
{
	f_file_t	 f = NULL;
	const char	*p;

	f = xmalloc(sizeof(*f));
	p = strrchr(path, '.');
	if (p != NULL && !strcasecmp(p + 1, "gz")) {
		f->type = FILE_TYPE_GZ;
		f->f.fgz = gzopen(path, mode);
		if (f->f.fgz == NULL)
			goto fail;
	} else {
		f->type = FILE_TYPE_NORMAL;
		f->f.fp = fopen(path, mode);
		if (f->f.fp == NULL)
			goto fail;
	}

	return f;

fail:
	xfree(f);
	return NULL;
}

ssize_t
f_read(f_file_t f, char *buf, ssize_t len)
{
	if (f->type == FILE_TYPE_NORMAL)
		return fread(buf, sizeof(*buf), len, f->f.fp);
	else if (f->type == FILE_TYPE_GZ)
		return gzread(f->f.fgz, buf, len);
	INVALID_FILE_ARGUMENT();
}

ssize_t
f_write(f_file_t f, const char *buf, ssize_t len)
{
	if (f->type == FILE_TYPE_NORMAL)
		return fwrite(buf, len, sizeof(*buf), f->f.fp);
	else if (f->type == FILE_TYPE_GZ)
		return gzwrite(f->f.fgz, buf, len);
	INVALID_FILE_ARGUMENT();
}

char *
f_gets(f_file_t f)
{
	if (f->type == FILE_TYPE_NORMAL)
		return fp_gets(f->f.fp);
	else if (f->type == FILE_TYPE_GZ)
		return fgz_gets(f->f.fgz);
	INVALID_FILE_ARGUMENT();
}

void
f_close(f_file_t f)
{
	if (f->type == FILE_TYPE_NORMAL)
		fclose(f->f.fp);
	else if (f->type == FILE_TYPE_GZ)
		gzclose(f->f.fgz);
	INVALID_FILE_ARGUMENT();
}

static char *
fgz_gets(gzFile f)
{
	static char		 buf[READ_BUF_SIZE];
	const size_t		 bufsize = sizeof buf;
	static char		*nl = NULL;
	char			*next_nl;
	int			 nread;
	char			*line = NULL;

	if (nl != NULL) {
		next_nl = strchr(nl + 1, '\n');
		if (next_nl != NULL)
			*next_nl = '\0';<commit_msg>Bugfix: code which shouldn't be reached is in fact reached.<commit_after>/*
 * Copyright (c) 2013 <NAME> <<EMAIL>>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <zlib.h>

#include <file.h>
#include <xmem.h>
#include <str.h>

#define READ_BUF_SIZE	4096
#define INVALID_FILE_ARGUMENT() \
	do { \
		fprintf(stderr, "%s(): invalid file argument", __func__); \
		abort(); \
	} while(0)
#define DBG(fmt, ...)

static char	*fgz_gets(gzFile f);
static char	*fp_gets(FILE *fp);

f_file_t
f_open(const char *path, const char *mode)
{
	f_file_t	 f = NULL;
	const char	*p;

	f = xmalloc(sizeof(*f));
	p = strrchr(path, '.');
	if (p != NULL && !strcasecmp(p + 1, "gz")) {
		f->type = FILE_TYPE_GZ;
		f->f.fgz = gzopen(path, mode);
		if (f->f.fgz == NULL)
			goto fail;
	} else {
		f->type = FILE_TYPE_NORMAL;
		f->f.fp = fopen(path, mode);
		if (f->f.fp == NULL)
			goto fail;
	}

	return f;

fail:
	xfree(f);
	return NULL;
}

ssize_t
f_read(f_file_t f, char *buf, ssize_t len)
{
	if (f->type == FILE_TYPE_NORMAL)
		return fread(buf, sizeof(*buf), len, f->f.fp);
	else if (f->type == FILE_TYPE_GZ)
		return gzread(f->f.fgz, buf, len);
	INVALID_FILE_ARGUMENT();
}

ssize_t
f_write(f_file_t f, const char *buf, ssize_t len)
{
	if (f->type == FILE_TYPE_NORMAL)
		return fwrite(buf, len, sizeof(*buf), f->f.fp);
	else if (f->type == FILE_TYPE_GZ)
		return gzwrite(f->f.fgz, buf, len);
	INVALID_FILE_ARGUMENT();
}

char *
f_gets(f_file_t f)
{
	if (f->type == FILE_TYPE_NORMAL)
		return fp_gets(f->f.fp);
	else if (f->type == FILE_TYPE_GZ)
		return fgz_gets(f->f.fgz);
	INVALID_FILE_ARGUMENT();
}

void
f_close(f_file_t f)
{
	if (f->type == FILE_TYPE_NORMAL)
		fclose(f->f.fp);
	else if (f->type == FILE_TYPE_GZ)
		gzclose(f->f.fgz);
        else {
            INVALID_FILE_ARGUMENT();
        }
}

static char *
fgz_gets(gzFile f)
{
	static char		 buf[READ_BUF_SIZE];
	const size_t		 bufsize = sizeof buf;
	static char		*nl = NULL;
	char			*next_nl;
	int			 nread;
	char			*line = NULL;

	if (nl != NULL) {
		next_nl = strchr(nl + 1, '\n');
		if (next_nl != NULL)
			*next_nl = '\0';
{'fraction_non_alphanumeric': 0.14323467230443976, 'fraction_numerical': 0.004228329809725159, 'mean_word_length': 3.8397271952259167, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2705478', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2223, 'n_words': 655}