/*	Copyright (C) 2004 Garrett A. Kajmowicz

	This file is part of the uClibc++ Library.

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#pragma once
#pragma GCC visibility push(default)
#include "ios"
namespace std
{
	int arduinoPrintFloat(double number, uint8_t digits, char *buffer, size_t buffer_size);

	/* We are making the following template class for serveral reasons.  Firstly,
	 * we want to keep the main ostream code neat and tidy.  Secondly, we want it
	 * to be easy to do partial specialization of the ostream code so that it can
	 * be expanded and put into the library.  This will allow us to make application
	 * code smaller at the expense of increased library size.  This is a fair
	 * trade-off when there are multiple applications being compiled.  Also, this
	 * feature will be used optionally via configuration options.  It will also
	 * allow us to keep the code bases in sync, dramatically simplifying the
	 * maintenance required.  We specialized for char because wchar and others
	 * require different scanf functions
	 */

	template <class traits, class charT, class dataType>
	class _UCXXEXPORT __ostream_printout
	{
	public:
		static void printout(basic_ostream<charT, traits> &stream, const dataType n);
	};

	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, char, signed long int>
	{
	public:
		static void printout(basic_ostream<char, traits> &stream, const signed long int n)
		{
			char buffer[20];
			const char *c_ld = "%ld";
			const char *c_lo = "%lo";
			const char *c_lX = "%lX";
			const char *c_lx = "%lx";
			const char *c_hashlo = "%#lo";
			const char *c_hashlX = "%#lX";
			const char *c_hashlx = "%#lx";

			const char *formatString = 0;

			if (stream.flags() & ios_base::dec)
			{
				formatString = c_ld;
			}
			else if (stream.flags() & ios_base::oct)
			{
				if (stream.flags() & ios_base::showbase)
				{
					formatString = c_hashlo;
				}
				else
				{
					formatString = c_lo;
				}
			}
			else if (stream.flags() & ios_base::hex)
			{
				if (stream.flags() & ios_base::showbase)
				{
					if (stream.flags() & ios_base::uppercase)
					{
						formatString = c_hashlX;
					}
					else
					{
						formatString = c_hashlx;
					}
				}
				else
				{
					if (stream.flags() & ios_base::uppercase)
					{
						formatString = c_lX;
					}
					else
					{
						formatString = c_lx;
					}
				}
			}

			stream.printout(buffer, snprintf(buffer, 20, formatString, n));

			if (stream.flags() & ios_base::unitbuf)
			{
				stream.flush();
			}
		}
	};

	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, char, unsigned long int>
	{
	public:
		static void printout(basic_ostream<char, traits> &stream, const unsigned long int n)
		{
			char buffer[20];
			const char *c_lo = "%lo";
			const char *c_lu = "%lu";
			const char *c_lX = "%lX";
			const char *c_lx = "%lx";
			const char *c_hashlo = "%#lo";
			const char *c_hashlX = "%#lX";
			const char *c_hashlx = "%#lx";
			const char *formatString = 0;

			if (stream.flags() & ios_base::dec)
			{
				formatString = c_lu;
			}
			else if (stream.flags() & ios_base::oct)
			{
				if (stream.flags() & ios_base::showbase)
				{
					formatString = c_hashlo;
				}
				else
				{
					formatString = c_lo;
				}
			}
			else if (stream.flags() & ios_base::hex)
			{
				if (stream.flags() & ios_base::showbase)
				{
					if (stream.flags() & ios_base::uppercase)
					{
						formatString = c_hashlX;
					}
					else
					{
						formatString = c_hashlx;
					}
				}
				else
				{
					if (stream.flags() & ios_base::uppercase)
					{
						formatString = c_lX;
					}
					else
					{
						formatString = c_lx;
					}
				}
			}

			stream.printout(buffer, snprintf(buffer, 20, formatString, n));
			if (stream.flags() & ios_base::unitbuf)
			{
				stream.flush();
			}
		}
	};

#if !defined(__STRICT_ANSI__) || (__cplusplus >= 201103L)
	extern "C"
	{
		size_t _CSL_u64_dec(uint64_t u64, char *OutChar);
		size_t _CSL_u64_oct(uint64_t u64, char *OutChar);
		size_t _CSL_u64_hex(uint64_t u64, char *OutChar, const char *Hex);
	}
	static constexpr char _CSL_Hex_Upper[] = "0123456789ABCDEF";
	static constexpr char _CSL_Hex_Lower[] = "0123456789abcdef";
	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, char, signed long long int>
	{
	public:
		static void printout(basic_ostream<char, traits> &stream, const signed long long int n)
		{
			char buffer[28];

			if (stream.flags() & ios_base::dec)
				if (n < 0)
				{
					buffer[0] = '-';
					stream.printout(buffer, _CSL_u64_dec(-n, buffer + 1) + 1);
				}
				else
					stream.printout(buffer, _CSL_u64_dec(n, buffer));
			else if (stream.flags() & ios_base::oct)
				if (stream.flags() & ios_base::showbase)
				{
					buffer[0] = '0';
					stream.printout(buffer, _CSL_u64_oct(n, buffer + 1) + 1);
				}
				else
					stream.printout(buffer, _CSL_u64_oct(n, buffer));
			else if (stream.flags() & ios_base::hex)
				if (stream.flags() & ios_base::showbase)
				{
					buffer[0] = '0';
					if (stream.flags() & ios_base::uppercase)
					{
						buffer[1] = 'X';
						stream.printout(buffer, _CSL_u64_hex(n, buffer + 2, _CSL_Hex_Upper) + 2);
					}
					else
					{
						buffer[1] = 'x';
						stream.printout(buffer, _CSL_u64_hex(n, buffer + 2, _CSL_Hex_Lower) + 2);
					}
				}
				else if (stream.flags() & ios_base::uppercase)
					stream.printout(buffer, _CSL_u64_hex(n, buffer, _CSL_Hex_Upper));
				else
					stream.printout(buffer, _CSL_u64_hex(n, buffer, _CSL_Hex_Lower));
			if (stream.flags() & ios_base::unitbuf)
				stream.flush();
		}
	};

	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, char, unsigned long long int>
	{
	public:
		static void printout(basic_ostream<char, traits> &stream, const unsigned long long int n)
		{
			char buffer[28];

			if (stream.flags() & ios_base::dec)
				stream.printout(buffer, _CSL_u64_dec(n, buffer));
			else if (stream.flags() & ios_base::oct)
				if (stream.flags() & ios_base::showbase)
				{
					buffer[0] = '0';
					stream.printout(buffer, _CSL_u64_oct(n, buffer + 1) + 1);
				}
				else
					stream.printout(buffer, _CSL_u64_oct(n, buffer));
			else if (stream.flags() & ios_base::hex)
				if (stream.flags() & ios_base::showbase)
				{
					buffer[0] = '0';
					if (stream.flags() & ios_base::uppercase)
					{
						buffer[1] = 'X';
						stream.printout(buffer, _CSL_u64_hex(n, buffer + 2, _CSL_Hex_Upper) + 2);
					}
					else
					{
						buffer[1] = 'x';
						stream.printout(buffer, _CSL_u64_hex(n, buffer + 2, _CSL_Hex_Lower) + 2);
					}
				}
				else if (stream.flags() & ios_base::uppercase)
					stream.printout(buffer, _CSL_u64_hex(n, buffer, _CSL_Hex_Upper));
				else
					stream.printout(buffer, _CSL_u64_hex(n, buffer, _CSL_Hex_Lower));

			if (stream.flags() & ios_base::unitbuf)
				stream.flush();
		}
	};

#endif // !defined(__STRICT_ANSI__) || (__cplusplus >= 201103L)

	// FIXME: Improve printing of doubles and floats.

	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, char, double>
	{
	public:
		static void printout(basic_ostream<char, traits> &stream, const double f)
		{
			char buffer[32];
			int length;
			length = arduinoPrintFloat(f, 2, &buffer[0], 32);

			/*
						if(stream.flags() & ios_base::scientific){
							if(stream.flags() & ios_base::uppercase){
								length = snprintf(buffer, 32, "%*.*E", static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
							}else{
								length = snprintf(buffer, 32, "%*.*e", static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
							}
						} else if(stream.flags() & ios_base::fixed){
							length = snprintf(buffer, 32, "%*.*f",static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
						} else {
							length = snprintf(buffer, 32, "%*.*g",static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
						}
			*/
			stream.printout(buffer, length);
			if (stream.flags() & ios_base::unitbuf)
			{
				stream.flush();
			}
		}
	};

	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, char, float>
	{
	public:
		static void printout(basic_ostream<char, traits> &stream, const float f)
		{
			char buffer[32];
			int length;
			length = arduinoPrintFloat(f, 2, &buffer[0], 32);

			/*
						if(stream.flags() & ios_base::scientific){
							if(stream.flags() & ios_base::uppercase){
								length = snprintf(buffer, 32, "%*.*LE", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
							}else{
								length = snprintf(buffer, 32, "%*.*Le", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
							}
						} else if(stream.flags() & ios_base::fixed){
							length = snprintf(buffer, 32, "%*.*Lf", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
						} else {
							length = snprintf(buffer, 32, "%*.*Lg", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
						}
			*/
			stream.printout(buffer, length);
			if (stream.flags() & ios_base::unitbuf)
			{
				stream.flush();
			}
		}
	};

#ifdef __UCLIBCXX_HAS_WCHAR__
	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, wchar_t, signed long int>
	{
	public:
		static void printout(basic_ostream<wchar_t, traits> &stream, const signed long int n)
		{
			wchar_t buffer[20];
			if (stream.flags() & ios_base::dec)
			{
				stream.printout(buffer, swprintf(buffer, 20, L"%ld", n));
			}
			else if (stream.flags() & ios_base::oct)
			{
				if (stream.flags() & ios_base::showbase)
				{
					stream.printout(buffer, swprintf(buffer, 20, L"%#lo", n));
				}
				else
				{
					stream.printout(buffer, swprintf(buffer, 20, L"%lo", n));
				}
			}
			else if (stream.flags() & ios_base::hex)
			{
				if (stream.flags() & ios_base::showbase)
				{
					if (stream.flags() & ios_base::uppercase)
					{
						stream.printout(buffer, swprintf(buffer, 20, L"%#lX", n));
					}
					else
					{
						stream.printout(buffer, swprintf(buffer, 20, L"%#lx", n));
					}
				}
				else
				{
					if (stream.flags() & ios_base::uppercase)
					{
						stream.printout(buffer, swprintf(buffer, 20, L"%lX", n));
					}
					else
					{
						stream.printout(buffer, swprintf(buffer, 20, L"%lx", n));
					}
				}
			}
			if (stream.flags() & ios_base::unitbuf)
			{
				stream.flush();
			}
		}
	};

	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, wchar_t, unsigned long int>
	{
	public:
		static void printout(basic_ostream<wchar_t, traits> &stream, const unsigned long int n)
		{
			wchar_t buffer[20];
			if (stream.flags() & ios_base::dec)
			{
				stream.printout(buffer, swprintf(buffer, 20, L"%lu", n));
			}
			else if (stream.flags() & ios_base::oct)
			{
				if (stream.flags() & ios_base::showbase)
				{
					stream.printout(buffer, swprintf(buffer, 20, L"%#lo", n));
				}
				else
				{
					stream.printout(buffer, swprintf(buffer, 20, L"%lo", n));
				}
			}
			else if (stream.flags() & ios_base::hex)
			{
				if (stream.flags() & ios_base::showbase)
				{
					if (stream.flags() & ios_base::uppercase)
					{
						stream.printout(buffer, swprintf(buffer, 20, L"%#lX", n));
					}
					else
					{
						stream.printout(buffer, swprintf(buffer, 20, L"%#lx", n));
					}
				}
				else
				{
					if (stream.flags() & ios_base::uppercase)
					{
						stream.printout(buffer, swprintf(buffer, 20, L"%lX", n));
					}
					else
					{
						stream.printout(buffer, swprintf(buffer, 20, L"%lx", n));
					}
				}
			}
			if (stream.flags() & ios_base::unitbuf)
			{
				stream.flush();
			}
		}
	};

#if !defined(__STRICT_ANSI__) || (__cplusplus >= 201103L)

	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, wchar_t, signed long long int>
	{
	public:
		static void printout(basic_ostream<wchar_t, traits> &stream, const signed long long int n)
		{
			wchar_t buffer[28];
			if (stream.flags() & ios_base::dec)
			{
				stream.printout(buffer, swprintf(buffer, 27, L"%lld", n));
			}
			else if (stream.flags() & ios_base::oct)
			{
				if (stream.flags() & ios_base::showbase)
				{
					stream.printout(buffer, swprintf(buffer, 27, L"%#llo", n));
				}
				else
				{
					stream.printout(buffer, swprintf(buffer, 27, L"%llo", n));
				}
			}
			else if (stream.flags() & ios_base::hex)
			{
				if (stream.flags() & ios_base::showbase)
				{
					if (stream.flags() & ios_base::uppercase)
					{
						stream.printout(buffer, swprintf(buffer, 27, L"%#llX", n));
					}
					else
					{
						stream.printout(buffer, swprintf(buffer, 27, L"%#llx", n));
					}
				}
				else
				{
					if (stream.flags() & ios_base::uppercase)
					{
						stream.printout(buffer, swprintf(buffer, 27, L"%llX", n));
					}
					else
					{
						stream.printout(buffer, swprintf(buffer, 27, L"%llx", n));
					}
				}
			}
			if (stream.flags() & ios_base::unitbuf)
			{
				stream.flush();
			}
		}
	};

	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, wchar_t, unsigned long long int>
	{
	public:
		static void printout(basic_ostream<wchar_t, traits> &stream, const unsigned long long int n)
		{
			wchar_t buffer[28];
			if (stream.flags() & ios_base::dec)
			{
				stream.printout(buffer, swprintf(buffer, 27, L"%llu", n));
			}
			else if (stream.flags() & ios_base::oct)
			{
				if (stream.flags() & ios_base::showbase)
				{
					stream.printout(buffer, swprintf(buffer, 27, L"%#llo", n));
				}
				else
				{
					stream.printout(buffer, swprintf(buffer, 27, L"%llo", n));
				}
			}
			else if (stream.flags() & ios_base::hex)
			{
				if (stream.flags() & ios_base::showbase)
				{
					if (stream.flags() & ios_base::uppercase)
					{
						stream.printout(buffer, swprintf(buffer, 27, L"%#llX", n));
					}
					else
					{
						stream.printout(buffer, swprintf(buffer, 27, L"%#llx", n));
					}
				}
				else
				{
					if (stream.flags() & ios_base::uppercase)
					{
						stream.printout(buffer, swprintf(buffer, 27, L"%llX", n));
					}
					else
					{
						stream.printout(buffer, swprintf(buffer, 27, L"%llx", n));
					}
				}
			}
			if (stream.flags() & ios_base::unitbuf)
			{
				stream.flush();
			}
		}
	};

#endif // !defined(__STRICT_ANSI__) || (__cplusplus >= 201103L)

	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, wchar_t, double>
	{
	public:
		static void printout(basic_ostream<wchar_t, traits> &stream, const double f)
		{
			wchar_t buffer[32];
			wchar_t format_string[32];
			if (stream.flags() & ios_base::scientific)
			{
				if (stream.flags() & ios_base::uppercase)
				{
					swprintf(format_string, 32, L"%%%u.%uE", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
				}
				else
				{
					swprintf(format_string, 32, L"%%%u.%ue", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
				}
			}
			else if (stream.flags() & ios_base::fixed)
			{
				swprintf(format_string, 32, L"%%%u.%uf", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
			}
			else
			{
				swprintf(format_string, 32, L"%%%u.%ug", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
			}
			stream.printout(buffer, swprintf(buffer, 32, format_string, f));
			if (stream.flags() & ios_base::unitbuf)
			{
				stream.flush();
			}
		}
	};

	template <class traits>
	class _UCXXEXPORT __ostream_printout<traits, wchar_t, long double>
	{
	public:
		static void printout(basic_ostream<wchar_t, traits> &stream, const long double f)
		{
			wchar_t buffer[32];
			wchar_t format_string[32];
			if (stream.flags() & ios_base::scientific)
			{
				if (stream.flags() & ios_base::uppercase)
				{
					swprintf(format_string, 32, L"%%%u.%uLE", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
				}
				else
				{
					swprintf(format_string, 32, L"%%%u.%uLe", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
				}
			}
			else if (stream.flags() & ios_base::fixed)
			{
				swprintf(format_string, 32, L"%%%u.%uLf", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
			}
			else
			{
				swprintf(format_string, 32, L"%%%u.%uLg", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
			}
			stream.printout(buffer, swprintf(buffer, 32, format_string, f));
			if (stream.flags() & ios_base::unitbuf)
			{
				stream.flush();
			}
		}
	};

#endif //__UCLIBCXX_HAS_WCHAR__
}
#pragma GCC visibility pop