/*! \file pistring_std.h
 * \ingroup Text
 * \brief
 * \~english STD convertions for PIString
 * \~russian Преобразования в/из STD для строки
*/
/*
	PIP - Platform Independent Primitives
	STD for PIString
	Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

	This program 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 3 of the License, or
	(at your option) any later version.

	This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PISTRING_STD_H
#define PISTRING_STD_H


#include <string>
#ifdef QNX
  typedef std::basic_string<wchar_t> wstring;
#endif
#include "pistringlist.h"


inline std::string PIString2StdString(const PIString & v) {
	std::string s;
	ushort wc;
	uchar tc;
	if (v.size() > 0) {
		for (int i = 0; i < v.length(); ++i) {
			wc = v[i].unicode16Code();
			while (tc = wc & 0xFF, tc) {
				s.push_back(tc);
				wc >>= 8;
			}
		}
	}
	return s;
}


inline PIString StdString2PIString(const std::string & v) {
	return PIString(v.c_str(), v.length());
}


#ifdef HAS_LOCALE
inline std::wstring PIString2StdWString(const PIString & v) {
	std::wstring s;
	for (int i = 0; i < v.length(); ++i) {
		s.push_back(v[i].toWChar());
	}
	return s;
}


inline PIString StdWString2PIString(const std::wstring & v) {
	PIString s;
	uint l = v.size();
	for (uint i = 0; i < l; ++i) {
		s.push_back(PIChar(v[i]));
	}
	return s;
}
#endif



//! \relatesalso PIChar \brief Output operator to \c std::ostream
inline std::ostream & operator <<(std::ostream & s, const PIChar & v) {
	s << v.toCharPtr();
	return s;
}


//! \relatesalso PIString \brief Return concatenated string
inline PIString operator +(const PIString & f, const std::string & str) {
	PIString s(f);
	s += StdString2PIString(str);
	return s;
}


//! \relatesalso PIString \brief Return concatenated string
inline PIString operator +(const std::string & str, const PIString & f) {
	return StdString2PIString(str) + f;
}


//! \relatesalso PIString \brief Output operator to std::ostream (cout)
inline std::ostream & operator <<(std::ostream & s, const PIString & v) {
	for (int i = 0; i < v.length(); ++i) {
		s << v[i];
	}
	return s;
}


//! \relatesalso PIString \brief Input operator from std::istream (cin)
inline std::istream & operator >>(std::istream & s, PIString & v) {
	std::string ss;
	s >> ss;
	v = StdString2PIString(ss);
	return s;
}


//! \relatesalso PIStringList \brief Output operator to std::ostream (cout)
inline std::ostream & operator <<(std::ostream & s, const PIStringList & v) {
	s << PIChar("{");
	for (uint i = 0; i < v.size(); ++i) {
		s << PIChar("\"") << v[i] << PIChar("\"");
		if (i < v.size() - 1) s << PIStringAscii(", ");
	}
	s << PIChar("}");
	return s;
}


#endif // PISTRING_STD_H
