// xlocmes internal header (from <locale>) for IBM
#ifndef _XLOCMES_
#define _XLOCMES_

#pragma info(none)
#ifndef __CHKHDR__
#  pragma report(level, E)
#endif
#pragma info(restore)

#if __IBMCPP__ < 400
#error This file to be used only with IBM VisualAge C++ v4 and later compilers.
#endif /* __IBMCPP__ */

#pragma namemangling(ansi)

   /********************************************************************/
   /*  <xlocmes> header file                                           */
   /*                                                                  */
   /*  VisualAge C++, Version 4.0                                      */
   /*    Licensed Material - Property of IBM                           */
   /*                                                                  */
   /*  5801-AAR and Other Materials                                    */
   /*                                                                  */
   /*  (c) Copyright IBM Corp 1991, 1998. All rights reserved.         */
   /*                                                                  */
   /*                                                                  */
   /*  Licensed Materials.                                             */
   /*                                                                  */
   /*  Dinkum C++ Library                                              */
   /*  Copyright (c) 1998.  Licensed to IBM Corp. and its suppliers.   */
   /*                                                                  */
   /********************************************************************/

#include <xiosbase>

 #if __TOS_OS2__ || __TOS_WIN__ || __TOS_AIX__
  #define _NLCAT	1
 #endif /* __TOS_OS2__ || __TOS_WIN__ || __TOS_AIX__ */

#ifdef _NLCAT
 #include <nl_types.h>
#endif /* _NLCAT */

_STD_BEGIN
		// STRUCT messages_base
struct messages_base : public locale::facet {
	typedef int catalog;
	explicit messages_base(size_t _R = 0)
		: locale::facet(_R) {}
	};
		// TEMPLATE CLASS messages
template<class _E>
	class messages : public messages_base {
public:
	typedef _E char_type;
	typedef basic_string<_E, char_traits<_E>,
		allocator<_E> > string_type;
	catalog open(const string& _X, const locale& _L) const
		{return (do_open(_X, _L)); }
	string_type get(catalog _C, int _S, int _M,
		const string_type& _D) const
		{return (do_get(_C, _S, _M, _D)); }
	void close(catalog _C) const
		{do_close(_C); }
	static locale::id id;
	explicit messages(size_t _R = 0)
		: messages_base(_R) {_Init(_Locinfo()); }
	messages(const _Locinfo& _Lobj, size_t _R = 0)
		: messages_base(_R) {_Init(_Lobj); }
	static size_t _Getcat(const locale::facet **_Ppf = 0)
		{if (_Ppf != 0 && *_Ppf == 0)
			*_Ppf = new messages<_E>;
		return (_X_MESSAGE); }
protected:
	void _Init(const _Locinfo& _Lobj)
		{}
	virtual catalog do_open(const string& _Str,
		const locale&) const
#ifdef _NLCAT
		{return (reinterpret_cast<catalog>(
			catopen(_Str.c_str(), 0))); }
#else /* _NLCAT */
		{return (0); }
#endif /* _NLCAT */
	virtual string_type do_get(catalog _Catd, int _Setnum,
		int _M, const string_type& _D) const
#ifdef _NLCAT
		{nl_catd _C = reinterpret_cast<nl_catd>(_Catd);
		string _S = catgets(_C, _Setnum, _M, "");
		if (_S.empty()) 
			return (_D);
		size_t _Gcs_len = _S.size();
		_E *_Gcs_str = new _E[_Gcs_len + 1];
		_E *_Gcs_iter = _Gcs_str;
		string::iterator _Sf = _S.begin(); 
		string::iterator _Sl = _S.last();

		// We must assume that there is a conversion from type
		// char to type _E.
		for (; _Sf != _Sl; ++_Sf, ++_Gcs_iter)
			*_Gcs_iter = reinterpret_cast<_E>(*_Sf);
		*_Gcs_iter = reinterpret_cast<_E>('\0');

		string_type _Ret = _Gcs_str;
		delete [] _Gcs_str;
		return (_Ret); }
#else /* _NLCAT */
		{return (_D); }
#endif /* _NLCAT */
	virtual void do_close(catalog _Catd) const
#ifdef _NLCAT
		{catclose(reinterpret_cast<nl_catd>(_Catd)); }
#else /* _NLCAT */
		{}
#endif /* _NLCAT */
	};
template<class _E>
	locale::id messages<_E>::id;

#ifdef _NLCAT
		// CLASS messages<char>
template<>
	class messages<char> : public messages_base {
public:
	typedef char _E;
	typedef _E char_type;
	typedef basic_string<_E, char_traits<_E>,
		allocator<_E> > string_type;
	catalog open(const string& _X, const locale& _L) const
		{return (do_open(_X, _L)); }
	string_type get(catalog _C, int _S, int _M,
		const string_type& _D) const
		{return (do_get(_C, _S, _M, _D)); }
	void close(catalog _C) const
		{do_close(_C); }
	static locale::id id;
	explicit messages(size_t _R = 0)
		: messages_base(_R) {_Init(_Locinfo()); }
	messages(const _Locinfo& _Lobj, size_t _R = 0)
		: messages_base(_R) {_Init(_Lobj); }
	static size_t _Getcat(const locale::facet **_Ppf = 0)
		{return (_X_MESSAGE); }
protected:
	void _Init(const _Locinfo& _Lobj)
		{}
	virtual catalog do_open(const string& _Str,
		const locale&) const
		{return (reinterpret_cast<catalog>(
			catopen(_Str.c_str(), 0))); }
	virtual string_type do_get(catalog _Catd, int _Setnum,
		int _M, const string_type& _D) const
		{nl_catd _C = reinterpret_cast<nl_catd>(_Catd);
		string_type _Ret = catgets(_C, _Setnum, _M, "");
		return (_Ret.empty() ? _D : _Ret); }
	virtual void do_close(catalog _Catd) const
		{catclose(reinterpret_cast<nl_catd>(_Catd)); }
	};

		// CLASS messages<wchar_t>
template<>
	class messages<wchar_t> : public messages_base {
public:
	typedef wchar_t _E;
	typedef _E char_type;
	typedef basic_string<_E, char_traits<_E>,
		allocator<_E> > string_type;
	catalog open(const string& _X, const locale& _L) const
		{return (do_open(_X, _L)); }
	string_type get(catalog _C, int _S, int _M,
		const string_type& _D) const
		{return (do_get(_C, _S, _M, _D)); }
	void close(catalog _C) const
		{do_close(_C); }
	static locale::id id;
	explicit messages(size_t _R = 0)
		: messages_base(_R) {_Init(_Locinfo()); }
	messages(const _Locinfo& _Lobj, size_t _R = 0)
		: messages_base(_R) {_Init(_Lobj); }
	static size_t _Getcat(const locale::facet **_Ppf = 0)
		{return (_X_MESSAGE); }
protected:
	void _Init(const _Locinfo& _Lobj)
		{}
	virtual catalog do_open(const string& _Str,
		const locale&) const
		{return (reinterpret_cast<catalog>(
			catopen(_Str.c_str(), 0))); }
	virtual string_type do_get(catalog _Catd, int _Setnum,
		int _M, const string_type& _D) const
		{nl_catd _C = reinterpret_cast<nl_catd>(_Catd);
		string _S = catgets(_C, _Setnum, _M, "");
		if (_S.empty())
			return (_D);
		size_t _Wcs_len = mbstowcs(L"", _S.c_str(), 0);
		wchar_t *_Wcs_str = new wchar_t[_Wcs_len + 1];
		size_t _Sz = mbstowcs(_Wcs_str, _S.c_str(),
			_Wcs_len + 1);
		if (0 < _Sz)
			{string_type _Ret = _Wcs_str;
			delete [] _Wcs_str;
			return (_Ret); }
        else {delete [] _Wcs_str;
			return (_D); }}
	virtual void do_close(catalog _Catd) const
		{catclose(reinterpret_cast<nl_catd>(_Catd)); }
	};
#endif /* _NLCAT */

		// TEMPLATE CLASS messages_byname
template<class _E>
	class messages_byname : public messages<_E> {
public:
	explicit messages_byname(const char *_S, size_t _R = 0)
		: messages<_E>(_Locinfo(_S), _R) {}
_PROTECTED:
	virtual ~messages_byname()
		{}
	};
_STD_END

#pragma namemangling()

#pragma info(none)
#ifndef __CHKHDR__
#  pragma info(pop)
#endif
#pragma info(restore)

#endif /* _XLOCMES_ */

/*
 * Copyright (c) 1995 by P.J. Plauger.  ALL RIGHTS RESERVED. 
 * Consult your license regarding permissions and restrictions.
V2.2:0081 */
