<commit_before>#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#endif
#include "lz4mt_io_cstdio.h"
#include "lz4mt.h"

namespace {
FILE* fopen_(const char* filename, const char* mode) {
#if defined(_MSC_VER)
	FILE* fp = nullptr;
	::fopen_s(&fp, filename, mode);
	return fp;
#else
	return ::fopen(filename, mode);
#endif
}

void fclose_(FILE* fp) {
	if(fp) {
		if(fp != stdin && fp != stdout) {
			::fclose(fp);
		}
	}
}

FILE* getStdin() {
#ifdef _WIN32
	(void) _setmode(_fileno(stdin), _O_BINARY);
#endif
	return stdin;
}

FILE* getStdout() {
#ifdef _WIN32
	(void) _setmode(_fileno(stdout), _O_BINARY);
#endif
	return stdout;
}
}

namespace Lz4Mt { namespace Cstdio {
bool fileExist(const std::string& filename) {
	if("stdin" == filename || "stdout" == filename) {
		return false;
	} else {
		FILE* fp = fopen_(filename.c_str(), "rb");
		fclose_(fp);
		return nullptr != fp;
	}
}

FILE* readCtx(const Lz4MtContext* ctx) {
	return reinterpret_cast<FILE*>(ctx->readCtx);
}

FILE* writeCtx(const Lz4MtContext* ctx) {
	return reinterpret_cast<FILE*>(ctx->writeCtx);
}

bool openIstream(Lz4MtContext* ctx, const std::string& filename) {
	FILE* fp = nullptr;
	if("stdin" == filename) {
		fp = getStdin();
	} else {
		fp = fopen_(filename.c_str(), "rb");
	}
	ctx->readCtx = reinterpret_cast<void*>(fp);
	return nullptr != fp;
}

bool openOstream(Lz4MtContext* ctx, const std::string& filename) {
	FILE* fp = nullptr;
	if("stdout" == filename) {
		fp = getStdout();
	} else {
		fp = fopen_(filename.c_str(), "wb");
	}
	ctx->writeCtx = reinterpret_cast<void*>(fp);
	return nullptr != fp;
}

void closeIstream(Lz4MtContext* ctx) {
	fclose_(readCtx(ctx));
	ctx->readCtx = nullptr;
}

void closeOstream(Lz4MtContext* ctx) {
	fclose_(writeCtx(ctx));
	ctx->writeCtx = nullptr;
}

int read(Lz4MtContext* ctx, void* dst, int dstSize) {
	if(auto* fp = readCtx(ctx)) {
		return static_cast<int>(::fread(dst, 1, dstSize, fp));
	} else {
		return 0;
	}
}

int readSkippable(const Lz4MtContext* ctx<commit_msg>Remove unnecessary cast.<commit_after>#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#endif
#include "lz4mt_io_cstdio.h"
#include "lz4mt.h"

namespace {
FILE* fopen_(const char* filename, const char* mode) {
#if defined(_MSC_VER)
	FILE* fp = nullptr;
	::fopen_s(&fp, filename, mode);
	return fp;
#else
	return ::fopen(filename, mode);
#endif
}

void fclose_(FILE* fp) {
	if(fp) {
		if(fp != stdin && fp != stdout) {
			::fclose(fp);
		}
	}
}

FILE* getStdin() {
#ifdef _WIN32
	(void) _setmode(_fileno(stdin), _O_BINARY);
#endif
	return stdin;
}

FILE* getStdout() {
#ifdef _WIN32
	(void) _setmode(_fileno(stdout), _O_BINARY);
#endif
	return stdout;
}
}

namespace Lz4Mt { namespace Cstdio {
bool fileExist(const std::string& filename) {
	if("stdin" == filename || "stdout" == filename) {
		return false;
	} else {
		FILE* fp = fopen_(filename.c_str(), "rb");
		fclose_(fp);
		return nullptr != fp;
	}
}

FILE* readCtx(const Lz4MtContext* ctx) {
	return reinterpret_cast<FILE*>(ctx->readCtx);
}

FILE* writeCtx(const Lz4MtContext* ctx) {
	return reinterpret_cast<FILE*>(ctx->writeCtx);
}

bool openIstream(Lz4MtContext* ctx, const std::string& filename) {
	FILE* fp = nullptr;
	if("stdin" == filename) {
		fp = getStdin();
	} else {
		fp = fopen_(filename.c_str(), "rb");
	}
	ctx->readCtx = fp;
	return nullptr != fp;
}

bool openOstream(Lz4MtContext* ctx, const std::string& filename) {
	FILE* fp = nullptr;
	if("stdout" == filename) {
		fp = getStdout();
	} else {
		fp = fopen_(filename.c_str(), "wb");
	}
	ctx->writeCtx = fp;
	return nullptr != fp;
}

void closeIstream(Lz4MtContext* ctx) {
	fclose_(readCtx(ctx));
	ctx->readCtx = nullptr;
}

void closeOstream(Lz4MtContext* ctx) {
	fclose_(writeCtx(ctx));
	ctx->writeCtx = nullptr;
}

int read(Lz4MtContext* ctx, void* dst, int dstSize) {
	if(auto* fp = readCtx(ctx)) {
		return static_cast<int>(::fread(dst, 1, dstSize, fp));
	} else {
		return 0;
	}
}

int readSkippable(const Lz4MtContext* ctx
{'fraction_non_alphanumeric': 0.1703446565831887, 'fraction_numerical': 0.009422266303000248, 'mean_word_length': 4.863372093023256, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, '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': '5770245', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1666, 'n_words': 361}