<commit_before>/*
 * jsonParser.cpp
 *
 *  Created on: Jun 3, 2015
 *      Author: root
 */

#include "inc/jsonParser.h"
#include "inc/TaskCodeGenerator.h"

jsonParser::jsonParser(string json)
{
	bJsonValid = setJson(json);
	max_uint_value = to_string (std::numeric_limits<unsigned int>::max());
}

bool jsonParser::sizeEqualSizeOfUnitTests(Json::Value object, string name)
{
	int object_size = object.size();
	if ( unit_test_num != object_size )
	{
		last_error = "ERROR: json format is not correct. " + name + " array size = " +
				to_string(object_size) + " , but unit_test_num = " + to_string(unit_test_num);
		return false;
	}
	return true;
}

bool jsonParser::mustHaveSizeMoreZeroAndBeNotTwoDimensionalArray(Json::Value object, string name , string ps, string ps2)
{
	if (!mustHaveSizeMoreZero(object ,name , ps))
		return false;
	if (object[0].isArray())
		if (!mustBeNotArray(object[0][0] , string(name + "[0][0]") , ps2))
			return false;
	return true;
}

bool jsonParser::isStringInt(string value, bool enable_zero_len)
{
	/*regex regStr("^[-0-9][0-9]{0,}");
	return  std::regex_match( value, regStr );*/
	if (value.size() == 0)
	{
		if (enable_zero_len)
			return true;
		else
			return false;
	}
	char cymbl = value[0];
	if (cymbl == '-' && value.size() == 1)
		return false;
	else
		if (cymbl != '-' && (cymbl < '0' || cymbl > '9'))
			return false;

	for (int i = 1; i < value.size(); i++)
	{
		cymbl = value[i];
		if (cymbl < '0' || cymbl > '9')
			return false;
	}
	return true;
}

bool jsonParser::isStringUnsignedInt(string value, bool enable_zero_len)
{
	regex regStr("^[0-9][0-9]{0,}");
	return  std::regex_match( value, regStr );
}

bool jsonParser::isStringBool(string value, bool enable_zero_len)
{
	if (value.size() == 0)
	{
		if (enable_zero_len)
			return true;
		else
			return false;
	}
	std::transform(value.begin(), value.end(), value.begin(), ::tolower);
	return (value == "false" || value == "true" || value == "1" || value == "0" );
}

bool jsonParser::isStringFloat(string value, bool enable_zero_len)
{
	/*string range_no_quotes = value.substr(1, value.size() - 3);
	regex regStr("^[-0-9][0-9]{0,}.?[0-9]{1,}");
	//regex regStr("[0-9]{0,}");
	return  std::regex_match( value, regStr );*/
	bool no_point = true;

	if (value.size() == 0)
	{
		if (enable_zero_len)
			return true;
		else
			return false;
	}
	char cymbl = value[0];
	if (cymbl != '-' && (cymbl < '0' || cymbl > '9'))
		return false;

	for (int i = 1; i < value.size(); i++)
	{
		cymbl = value[i];
		if (cymbl == '.')
		{
			if (no_point)
				no_point = false;
			else
				return false;
		}
		else
			if (cymbl < '0' || cymbl > '9')
				return false;
	}
	return true;
}

bool jsonParser::rangeValidation(bool &range_size_inited, int &range_size, string range, string field_name)
{
	string range_no_quotes = range.substr(1, range.size() - 3);
	regex regStr("^[-0-9][0-9]{0,}..[-0-9][0-9]{0,}");
	if (!std::regex_match( range_no_quotes, regStr ))
	{
		last_error = "ERROR: json format is not correct. " + field_name + " range format invalid "; //
		last_error += range;
		return false;
	}
	is_results_range = true;
	sscanf(range_no_quotes.c_str(),"%d..%d", &range_first, &range_last);

	if (range_first > range_last)
	{
		int temp = range_first;
		range_first = range_last;
		range_last = temp;
	}
	int range_i_size = abs(range_first - range_last);
	if ( !range_size_inited )
	{
		range_size = range_i_size;
		range_size_inited = true;
	}
	else
		if (range_i_size != range_size)
		{
			last_error = "ERROR: json format is not correct. " + field_name +
					" range size (" + to_string(range_i_size) +
					") != results[0] range size ("  +
					to_string(range_size) + ")";
			return false;
		}
	return true;
}

bool jsonParser::mustBeArrayFloat(Json::Value object, string name , int size, bool enable_zero_len , string ps, string ps2 )
{
	if (!mustBeArray(object,name,size,ps))
		return false;
	int obj_size = object.size();
	for (int i = 0; i < obj_size ; i++)
		if (!mustBeFloat(object[i], name + stringInScobcah(i), ps2))
			return false;
	return true;
}

bool jsonParser::mustBeArrayInt(Json::Value object, string name , int size, bool enable_zero_len , string ps, string ps2 )
{
	if (!mustBeArray(object,name ,size ,ps))
		return false;
	for (int i = 0; i < object.size(); i++)
		if (!mustBeInt(object[i], name + stringInScobcah(i), ps2))
			return false;
	return true;
}

bool jsonParser::mustBeArrayString(Json::Value object, string name, int size, bool enable_zero_len  , string ps, string ps2 )
{
	if (!mustBeArray(object,name,size ,ps))
		return false;
	for (int i = 0; i < object.size(); i++)
		if (!mustBeString(object[i], name + stringInScobcah(i), ps2))
			return false;
	return true;<commit_msg>fix mustBeArrayOfInt - enable zero length<commit_after>/*
 * jsonParser.cpp
 *
 *  Created on: Jun 3, 2015
 *      Author: root
 */

#include "inc/jsonParser.h"
#include "inc/TaskCodeGenerator.h"

jsonParser::jsonParser(string json)
{
	bJsonValid = setJson(json);
	max_uint_value = to_string (std::numeric_limits<unsigned int>::max());
}

bool jsonParser::sizeEqualSizeOfUnitTests(Json::Value object, string name)
{
	int object_size = object.size();
	if ( unit_test_num != object_size )
	{
		last_error = "ERROR: json format is not correct. " + name + " array size = " +
				to_string(object_size) + " , but unit_test_num = " + to_string(unit_test_num);
		return false;
	}
	return true;
}

bool jsonParser::mustHaveSizeMoreZeroAndBeNotTwoDimensionalArray(Json::Value object, string name , string ps, string ps2)
{
	if (!mustHaveSizeMoreZero(object ,name , ps))
		return false;
	if (object[0].isArray())
		if (!mustBeNotArray(object[0][0] , string(name + "[0][0]") , ps2))
			return false;
	return true;
}

bool jsonParser::isStringInt(string value, bool enable_zero_len)
{
	/*regex regStr("^[-0-9][0-9]{0,}");
	return  std::regex_match( value, regStr );*/
	if (value.size() == 0)
	{
		if (enable_zero_len)
			return true;
		else
			return false;
	}
	char cymbl = value[0];
	if (cymbl == '-' && value.size() == 1)
		return false;
	else
		if (cymbl != '-' && (cymbl < '0' || cymbl > '9'))
			return false;

	for (int i = 1; i < value.size(); i++)
	{
		cymbl = value[i];
		if (cymbl < '0' || cymbl > '9')
			return false;
	}
	return true;
}

bool jsonParser::isStringUnsignedInt(string value, bool enable_zero_len)
{
	regex regStr("^[0-9][0-9]{0,}");
	return  std::regex_match( value, regStr );
}

bool jsonParser::isStringBool(string value, bool enable_zero_len)
{
	if (value.size() == 0)
	{
		if (enable_zero_len)
			return true;
		else
			return false;
	}
	std::transform(value.begin(), value.end(), value.begin(), ::tolower);
	return (value == "false" || value == "true" || value == "1" || value == "0" );
}

bool jsonParser::isStringFloat(string value, bool enable_zero_len)
{
	/*string range_no_quotes = value.substr(1, value.size() - 3);
	regex regStr("^[-0-9][0-9]{0,}.?[0-9]{1,}");
	//regex regStr("[0-9]{0,}");
	return  std::regex_match( value, regStr );*/
	bool no_point = true;

	if (value.size() == 0)
	{
		if (enable_zero_len)
			return true;
		else
			return false;
	}
	char cymbl = value[0];
	if (cymbl != '-' && (cymbl < '0' || cymbl > '9'))
		return false;

	for (int i = 1; i < value.size(); i++)
	{
		cymbl = value[i];
		if (cymbl == '.')
		{
			if (no_point)
				no_point = false;
			else
				return false;
		}
		else
			if (cymbl < '0' || cymbl > '9')
				return false;
	}
	return true;
}

bool jsonParser::rangeValidation(bool &range_size_inited, int &range_size, string range, string field_name)
{
	string range_no_quotes = range.substr(1, range.size() - 3);
	regex regStr("^[-0-9][0-9]{0,}..[-0-9][0-9]{0,}");
	if (!std::regex_match( range_no_quotes, regStr ))
	{
		last_error = "ERROR: json format is not correct. " + field_name + " range format invalid "; //
		last_error += range;
		return false;
	}
	is_results_range = true;
	sscanf(range_no_quotes.c_str(),"%d..%d", &range_first, &range_last);

	if (range_first > range_last)
	{
		int temp = range_first;
		range_first = range_last;
		range_last = temp;
	}
	int range_i_size = abs(range_first - range_last);
	if ( !range_size_inited )
	{
		range_size = range_i_size;
		range_size_inited = true;
	}
	else
		if (range_i_size != range_size)
		{
			last_error = "ERROR: json format is not correct. " + field_name +
					" range size (" + to_string(range_i_size) +
					") != results[0] range size ("  +
					to_string(range_size) + ")";
			return false;
		}
	return true;
}

bool jsonParser::mustBeArrayFloat(Json::Value object, string name , int size, bool enable_zero_len , string ps, string ps2 )
{
	if (!mustBeArray(object,name,size,ps))
		return false;
	int obj_size = object.size();
	for (int i = 0; i < obj_size ; i++)
		if (!mustBeFloat(object[i], name + stringInScobcah(i), ps2))
			return false;
	return true;
}

bool jsonParser::mustBeArrayInt(Json::Value object, string name , int size, bool enable_zero_len , string ps, string ps2 )
{
	if (!mustBeArray(object,name ,size ,ps))
		return false;
	for (int i = 0; i < object.size(); i++)
		if (!mustBeInt(object[i], name + stringInScobcah(i), ps2,enable_zero_len))
			return false;
	return true;
}

bool jsonParser::mustBeArrayString(Json::Value object, string name, int size, bool enable_zero_len  , string ps, string ps2 )
{
	if (!mustBeArray(object,name,size ,ps))
		return false;
	for (int i = 0; i < object.size(); i++)
		if (!mustBeString(object[i], name + stringInScobcah(i), ps2))
			return false;
	return true;
{'fraction_non_alphanumeric': 0.16726580672446606, 'fraction_numerical': 0.01585958976527807, 'mean_word_length': 4.066416711301553, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 15, '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': '6688435', 'n_tokens_mistral': 3864, 'n_tokens_neox': 3624, 'n_words': 863}