<commit_before>//--------------------------------------------------------------------------//
/// Copyright (c) 2019 by <NAME>. All Rights Reserved.                ///
/// License: http://www.opensource.org/licenses/BSD-2-Clause               ///
//--------------------------------------------------------------------------//

#ifndef RTM_RBASE_CONSOLE_H
#define RTM_RBASE_CONSOLE_H

#include <rbase/inc/platform.h>
#include <rbase/inc/stringfn.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>

#if RTM_PLATFORM_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif

#define RTM_WINDOWS_CONSOLE 0

#if RTM_PLATFORM_WINDOWS
#if (_WIN32_WINNT < 0x0603)
#undef	RTM_WINDOWS_CONSOLE
#define	RTM_WINDOWS_CONSOLE 1
#endif
#endif

#define RTM_CONSOLE_TEMP_BUFFER_SIZE	512

namespace rtm {

	class Console
	{
		static char* itoaf(char* _buffer, uint8_t _val)
		{
			int numDigits = 0;
			char digits[3];

			while (_val)
			{
				digits[numDigits++] = '0' + (_val % 10);
				_val /= 10;
			}

			if (numDigits)
			{
				for (int i = numDigits - 1; i >= 0; --i)
					*_buffer++ = digits[i];
			}
			else
				*_buffer++ = '0';
			*_buffer++ = ';';
			return _buffer;
		}

		static inline char* setColor(char* _buffer, uint32_t _buffSize, uint8_t _r, uint8_t _g, uint8_t _b)
		{
#if RTM_WINDOWS_CONSOLE
			RTM_UNUSED_1(_bufferSize);
			HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
			if (INVALID_HANDLE_VALUE != console)
			{
				DWORD att = 0;
				if (_r)	att |= FOREGROUND_RED;
				if (_g)	att |= FOREGROUND_GREEN;
				if (_b)	att |= FOREGROUND_BLUE;
				if (false
					|| (_r > 192)
					|| (_g > 192)
					|| (_b > 192))
					att |= FOREGROUND_INTENSITY;

				SetConsoleTextAttribute(console, (WORD)att);
			}
			return _buffer;
#else
			RTM_ASSERT(_buffSize >= 32, "");
			rtm::strlCat(_buffer, _buffSize, "\x1b[38;2;");
			char* b = itoaf(&_buffer[7], _r);
			b = itoaf(b, _g);
			b = itoaf(b, _b);
			b[-1] = 'm';
			b[0] = '\0';
			return b;
#endif
		}

		static inline void restoreColor(char* _buffer, uint32_t _buffSize)
		{
#if RTM_WINDOWS_CONSOLE
			setColor(255, 255, 255);
#else
			rtm::strlCat(_buffer, _buffSize, "\x1b[0m");
#endif
		}

		static inline void printf(const char* _str)
		{
#if RTM_WINDOWS_CONSOLE
			DWORD written = 0;
			HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
			if (INVALID_HANDLE_VALUE != console)
				WriteFile(console, _str, (DWORD)strlen(_str), &written, NULL);
#else
			::printf(_str);<commit_msg>Fix deprecated Windows code path for console<commit_after>//--------------------------------------------------------------------------//
/// Copyright (c) 2019 by <NAME>. All Rights Reserved.                ///
/// License: http://www.opensource.org/licenses/BSD-2-Clause               ///
//--------------------------------------------------------------------------//

#ifndef RTM_RBASE_CONSOLE_H
#define RTM_RBASE_CONSOLE_H

#include <rbase/inc/platform.h>
#include <rbase/inc/stringfn.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>

#if RTM_PLATFORM_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif

#define RTM_CONSOLE_TEMP_BUFFER_SIZE	512
#define RTM_WINDOWS_CONSOLE				0

#if RTM_PLATFORM_WINDOWS
#if (_WIN32_WINNT < 0x0603)
#undef	RTM_WINDOWS_CONSOLE
#define	RTM_WINDOWS_CONSOLE 1
#endif
#endif

namespace rtm {

	class Console
	{
		static char* itoaf(char* _buffer, uint8_t _val)
		{
			int numDigits = 0;
			char digits[3];

			while (_val)
			{
				digits[numDigits++] = '0' + (_val % 10);
				_val /= 10;
			}

			if (numDigits)
			{
				for (int i = numDigits - 1; i >= 0; --i)
					*_buffer++ = digits[i];
			}
			else
				*_buffer++ = '0';
			*_buffer++ = ';';
			return _buffer;
		}

		static inline char* setColor(char* _buffer, uint32_t _buffSize, uint8_t _r, uint8_t _g, uint8_t _b)
		{
#if RTM_WINDOWS_CONSOLE
			// can't make below thread safe in a cheap way, ignore
			RTM_UNUSED_4(_buffSize, _r, _g, _b);
			//HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
			//if (INVALID_HANDLE_VALUE != console)
			//{
			//	DWORD att = 0;
			//	if (_r)	att |= FOREGROUND_RED;
			//	if (_g)	att |= FOREGROUND_GREEN;
			//	if (_b)	att |= FOREGROUND_BLUE;
			//	if (false
			//		|| (_r > 192)
			//		|| (_g > 192)
			//		|| (_b > 192))
			//		att |= FOREGROUND_INTENSITY;

			//	SetConsoleTextAttribute(console, (WORD)att);
			//}
			return _buffer;
#else
			RTM_ASSERT(_buffSize >= 32, "");
			rtm::strlCat(_buffer, _buffSize, "\x1b[38;2;");
			char* b = itoaf(&_buffer[7], _r);
			b = itoaf(b, _g);
			b = itoaf(b, _b);
			b[-1] = 'm';
			b[0] = '\0';
			return b;
#endif
		}

		static inline void restoreColor(char* _buffer, uint32_t _buffSize)
		{
#if RTM_WINDOWS_CONSOLE
			RTM_UNUSED_2(_buffer, _buffSize);
			// can't make below thread safe in a cheap way, ignore
			//setColor(_buffer, _buffSize, 255, 255, 255);
#else
			rtm::strlCat(_buffer, _buffSize, "\x1b[0m");
#endif
		}

		static inline void printf(const char* _str)
		{
#if RTM_WINDOWS_CONSOLE
			DWORD written = 0;
			HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
			if (INVALID_HANDLE_VALUE != console)
				WriteFile(console, _str, (DWORD)strlen(_str), &written, NULL);
#else
			::printf(_str);
{'fraction_non_alphanumeric': 0.19655305964368708, 'fraction_numerical': 0.027304415182029435, 'mean_word_length': 2.5547143840330353, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '173788', 'n_tokens_mistral': 2358, 'n_tokens_neox': 1997, 'n_words': 444}