tstdlibs.h 3.34 KB
Newer Older
Nicolas Metts's avatar
Nicolas Metts committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#ifndef _TSTDLIBS_H
#define _TSTDLIBS_H

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <stdlib.h>

#ifdef _WIN32
	#include <tchar.h> // For _T() macro!
#else // Non Windows Platform!
	#ifdef _UNICODE
		typedef wchar_t TCHAR;
		#ifndef _T
			#define _T(s) L##s
		#endif
		#ifndef _TSTR
			#define _TSTR(s) L##s
	    #endif
	#else
	    typedef wchar_t TCHAR;
		#ifndef _T
			#define _T(s) s
		#endif
		#ifndef _TSTR
	        #define _TSTR(s) s
		#endif
	#endif
#endif

namespace tstd
{
#ifdef _UNICODE
	typedef std::wstring			tstring;
	typedef std::wostream			tostream;
	typedef std::wistream			tistream;
	typedef std::wiostream			tiostream;
	typedef std::wistringstream		tistringstream;
	typedef std::wostringstream		tostringstream;
	typedef std::wstringstream		tstringstream;
	typedef std::wifstream			tifstream;
	typedef std::wofstream			tofstream;
	typedef std::wfstream			tfstream;
	typedef std::wfilebuf			tfilebuf;
	typedef std::wios			    tios;
	typedef std::wstreambuf			tstreambuf;
	typedef std::wstreampos			tstreampos;
	typedef std::wstringbuf			tstringbuf;

// declare an unnamed namespace as a superior alternative to the use of global static variable declarations.
namespace
{
    tostream& tcout = std::wcout;
	tostream& tcerr = std::wcerr;
	tostream& tclog = std::wclog;
	tistream& tcin	= std::wcin;

    std::wostream& tendl( std::wostream& output )
    {
        output << std::endl;
        return output;
    }

	tstring wstr_to_tstr(const std::wstring& arg)
	{
		return arg;
	}

	tstring str_to_tstr(const std::string& arg)
	{
		tstring res(arg.length(), L'\0');
		mbstowcs(const_cast<wchar_t*>(res.data()), arg.c_str(), arg.length());
		return res;
	}

	std::wstring tstr_to_wstr(const tstring& arg)
	{
		return arg;
	}

	std::string tstr_to_str(const tstring& arg)
	{
		std::string res(arg.length(), '\0');
		wcstombs(const_cast<char*>(res.data()), arg.c_str(), arg.length());
		return res;
	}
}

#else

	typedef std::string			    tstring;
	typedef std::ostream			tostream;
	typedef std::istream			tistream;
	typedef std::iostream			tiostream;
	typedef std::istringstream		tistringstream;
	typedef std::ostringstream		tostringstream;
	typedef std::stringstream		tstringstream;
	typedef std::ifstream			tifstream;
	typedef std::ofstream			tofstream;
	typedef std::fstream			tfstream;
	typedef std::filebuf			tfilebuf;
	typedef std::ios				tios;
	typedef std::streambuf			tstreambuf;
	typedef std::streampos			tstreampos;
	typedef std::stringbuf			tstringbuf;

// declare an unnamed namespace as a superior alternative to the use of global static variable declarations.
namespace
{
    tostream& tcout = std::cout;
	tostream& tcerr = std::cerr;
	tostream& tclog = std::clog;
	tistream& tcin	= std::cin;

    std::ostream& tendl( std::ostream& output )
    {
        output << std::endl;
        return output;
    }

	tstring wstr_to_tstr(const std::wstring& arg)
	{
		tstring res( arg.length(), '\0' );
		wcstombs( const_cast<char*>(res.data()) , arg.c_str(), arg.length());
           return res;
	}

	tstring str_to_tstr(const std::string& arg)
	{
		return arg;
	}

	std::wstring tstr_to_wstr(const tstring& arg)
	{
		std::wstring res(arg.length(), L'\0');
        mbstowcs(const_cast<wchar_t*>(res.data()), arg.c_str(), arg.length());
        return res;
	}

    std::string tstr_to_str(const tstring& arg)
    {
        return arg;
    }
}

#endif
}

#endif