<commit_before><?php

// (c) 2016 <NAME> <<EMAIL>>

// This script should be included and not called directly.
// However, it is no security issue if it is called directly, because it only
// contains functions (thus, no code is executed).

/**
 * Opens a file, decodes the JSON contained in it and returns the decoded
 * object.
 *
 * @params $filename The JSON file to decode.
 * @returns The object stored in $filename in case of success.
 * @returns false In case of failure.
 */
function load_json_file ($filename) {
	$length = filesize ($filename);
	if ($length === false) {
		return false;
	}

	$fh = fopen($filename, 'r');
	if ($fh === false) {
		return false;
	}

	$contents = fread($fh, $length);
	if ($contents === false) {
		return false;
	}

	fclose ($fh);
	// fclose errors are ignored

	$object = json_decode($contents);
	if (is_null($object)) {
		return false;
	}

	return $object;
}

/**
 * Encodes an object as JSON and writes it to a file.
 *
 * @param $object The object to encode.
 * @param $filename The JSON file to write to.
 * @returns True in case of success, false in case of failure.
 */
function save_json_file ($object, $filename) {
	$json = json_encode($object);

	if ($json === false) {
		return false;
	}

	$fh = fopen ($filename, 'w');
	if ($fh === false) {
		return false;
	}

	if (fwrite ($fh, $json) === false) {
		return false;
	}

	fclose($fh);
	// fclose errors are ignored

	return true;
}

?><commit_msg>json helper functions now return HelperResult objects rather than boolean<commit_after><?php

// (c) 2016 <NAME> <<EMAIL>>

// This script should be included and not called directly.
// However, it is no security issue if it is called directly, because it only
// contains functions (thus, no code is executed).

require_once 'type_definitions.php';
require_once 'helper_result.php';

/**
 * Opens a file, decodes the JSON contained in it and returns the decoded
 * object.
 *
 * @params $filename The JSON file to decode.
 * @returns A HelperResult object.
 */
function load_json_file ($filename) {
	$length = filesize ($filename);
	if ($length === false) {
		return helperFailure (
			'LOAD_JSON_FAILED',
			'Loading a JSON file failed'
		);
	}

	$fh = fopen($filename, 'r');
	if ($fh === false) {
		return helperFailure (
			'LOAD_JSON_FAILED',
			'Loading a JSON file failed'
		);
	}

	$contents = fread($fh, $length);
	if ($contents === false) {
		return helperFailure (
			'LOAD_JSON_FAILED',
			'Loading a JSON file failed'
		);
	}

	fclose ($fh);
	// fclose errors are ignored

	$object = json_decode($contents);
	if (is_null($object)) {
		return helperFailure (
			'PARSE_JSON_FAILED',
			'Loading a JSON file failed'
		);
	}

	return helperSuccess($object);
}

/**
 * Encodes an object as JSON and writes it to a file.
 *
 * @param $object The object to encode.
 * @param $filename The JSON file to write to.
 * @returns A HelperResult object.
 */
function save_json_file ($object, $filename) {
	$json = json_encode($object);

	if ($json === false) {
		return helperFailure (
			'SAVE_JSON_FAILED',
			'Saving a JSON file failed'
		);
	}

	$fh = fopen ($filename, 'w');
	if ($fh === false) {
		return helperFailure (
			'SAVE_JSON_FAILED',
			'Saving a JSON file failed'
		);
	}

	if (fwrite ($fh, $json) === false) {
		return helperFailure (
			'SAVE_JSON_FAILED',
			'Saving a JSON file failed'
		);
	}

	fclose($fh);
	// fclose errors are ignored

	return helperSuccess(null);
}

?>
{'fraction_non_alphanumeric': 0.13716942749200814, 'fraction_numerical': 0.0023249055507120024, 'mean_word_length': 3.511140235910878, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '7633813', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1121, 'n_words': 402}