 |
|
 |
The namespace below would allow you to have a special namespace "tstd". This way you keep your hands of the std namespace.
#include "tstdlibs.h"
using namespace tstd;
int main()
{
tcout << _T("Hello") << tendl;
}
Below is a dump of "tstdlibs.h"
#ifndef _TSTDLIBS_H
#define _TSTDLIBS_H
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#ifdef _UNICODE
typedef wchar_t tchar;
#ifndef _T
#define _T(s) L##s
#endif
#ifndef _TSTR
#define _TSTR(s) L##s
#endif
#else
typedef char tchar;
#ifndef _T
#define _T(s) s
#endif
#ifndef _TSTR
#define _TSTR(s) s
#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;
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;
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
Let me know what you guys think. I was reading some of the other comments about Ansi \ Unicode conversions. It's true when it comes to saving files in unicode the stream is converted to ansi. This requires you to manipulate your locale settings so overcome this issue.
Regards,
Lu
|
|
|
|
 |
|
 |
You want to put them into the std namespace, as ADL[^] on stream operators then works more smoothly.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
 |
|
 |
std::string (VC++6, STL version) causes memory corruption on multi-processors PC.
MSDN site has article giving given few work-arounds. Has any one experienced to which one is more suitable option for VC++ 6 apps running for variety of Windows (98/ME/2000/XP/2003) (without upgrading to .NET) ?
Thanks for sharing your views.
Ana
|
|
|
|
 |
|
 |
But i think every programmer had already made such file for himself.
dude
|
|
|
|
 |
|
 |
cozy dude wrote: i think every programmer had already made such file for himself
You may be right*, but I did not find any such file already posted on CP. So now there is one for anyone else who comes here looking to solve the same problem I had.
*cue Billy Joel:
You may be right,
I may be crazy,
But it just might be a lunitic you're looking for...
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04
"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05
Within you lies the power for good - Use it!
|
|
|
|
 |
|
 |
I've done this in the past, but never in a comprehensive manner. Each time, I'd just conditionally declare macros for just those functions/objects I am interested in (using at that moment)
PJ's done everyone a big help by posting this. Now we'd just have to #include the PJ header!
Regards,
Nish
|
|
|
|
 |
|
 |
Well I haven't, and in fact I've never troubled to look into using STL strings until now. This header is very useful and will probably get me experimenting more with STL. Good job!
|
|
|
|
 |
|
|
 |
|
 |
Thanks for the link Nemanja.
I am not a big fan of the fstream classes, I much rather prefer to use MFC's CFile and CStdioFile. What I am after though is the stringstream classes. I have read somewhere (can not find the link right now) that the wfstream classes were designed to read and write MBCS files and DBCS memory in order to minimize file sizes, so if one keeps that in mind one would only use wfstreams for text files, and fstreams for binary. So in my case use tfstreams for text, and fstreams for binary data. Do that and the problems descibed in the article should not be a problem. Or am I, in my naivety, mistaken?
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04
"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05
Within you lies the power for good - Use it!
|
|
|
|
 |
|
 |
For the record - it wasn't me who voted this post down.
Anyway, I almost agree - C++ Standard Library IO is not exactly my favorite code, mostly because it is slow. Most benchmarks aiming to "prove" that Java is faster than C++ are actually (ab)using fstream.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
 |
|
 |
Hey PJ
How come you mixed and used both in your generic header?
Regards,
Nish
|
|
|
|
 |
|
 |
wcerr, wcout, wcin etc are objects so he has to use #define: he cannot use typedef.
Member in good standing - the great cult of Firefox at CP
|
|
|
|
 |
|
 |
Rama Krishna Vavilala wrote: wcerr, wcout, wcin etc are objects so he has to use #define: he cannot use typedef.
Yes, but why did he choose not to use #define for everything? The tchar.h macros use #define, so I was wondering why PJ went for a deviation there.
Regards,
Nish
|
|
|
|
 |
|
 |
tchar.h uses #define to rename the c string functions, but uses typedef to declare the TCHAR types. Use typedef to rename data types and class types, use #define to rename non-types such as objects, functions and constants.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04
"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05
Within you lies the power for good - Use it!
|
|
|
|
 |
|
|
 |
|
 |
I don't know about PJ's reasons, but I normally do typedef because I hate #define's. Also, intellisense is lost (not checked in VS2005) if you do #defines instead of using typedef's.
Member in good standing - the great cult of Firefox at CP
|
|
|
|
 |
|
 |
Why not use references?#if defined UNICODE || defined _UNICODE
wostream& tcout = wcout;
wostream& tcerr = wcerr;
wostream& tclog = wclog;
wistream& tcin = wcin;
#else
ostream& tcout = cout;
ostream& tcerr = cerr;
ostream& tclog = clog;
istream& tcin = cin;
#endif
// Dalle
|
|
|
|
 |
|
|
 |
|
 |
Heh, I think you need a profiler to back that up...
|
|
|
|
 |
|
 |
why ?
using references implies that there is a declaration, an affectation, and then type checking (which slows down), while using #defines is a preprocessor treatment... (so none of the cases quoted above).
TOXCCT >>> GEII power [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]
|
|
|
|
 |
|
 |
Like I said in my reply to Roland below, I am here to learn. If you can show me code that works the way you propose, that does not involve cpp, lib, or dll files that have to be added to the project (I want a simple header file that I can place in my global include directory) and where I can use the syntax std::tcout etc. then I am all eyes. I have found that using #define fits my requirements exactly.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04
"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05
Within you lies the power for good - Use it!
|
|
|
|
 |
|
 |
Hi, PJ Arends, thanks, you did a good job.
Nishant Sivakumar wrote: Hey PJ
How come you mixed and used both in your generic header?
Regards,
Nish
I think only those who familiar with MFC/Windows programming style can fully understand PJ's solution. Really, I don't want to offense Nishant Sivakumar.
|
|
|
|
 |
|
 |
You should not put something into namespace std. This namespace is not available for mere mortals. In general, put your names only in your own namespace(s). In this case it may even be better to use no namespace at all.
namespace pja
{
#if defined UNICODE || defined _UNICODE
typedef std::wstring tstring; }
Also, include the necessary iostream header files (maybe the lightweight <iosfwd> is sufficient).
Otherwise your approach seems to be practical. I've used a similar solution in http://www.codeproject.com/string/lstring.asp[^]
|
|
|
|
 |
|
 |
I actually did not "add" anything to the std namespace, I just renamed a few things.
The reason I did it this way is because when I use the STL classes I like to preface the class name with std::, ie I prefer to see std::string in my code rather than simply string. By declaring the typedefs this way I can preseve that.
Roland Pibinger wrote: Also, include the necessary iostream header files (maybe the lightweight is sufficient).
I did include <string> which indirectly includes <iosfwd>, among others. But you are probably right that a direct inclusion is probably better.
Roland Pibinger wrote: I've used a similar solution in http://www.codeproject.com/string/lstring.asp[^]
And I looked at your class before I embarked down this road. Your class works well, but it only wraps the string class. What about all the stream classes? I figured by the time I wrapped all the stream classes the same way you wrapped the string class i would end up with a header file a couple hundred KB in size. There had to be a better way, and I think this is it.
I posted this class to both share what I learned, and to hopefully improve by having others critique it.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04
"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05
Within you lies the power for good - Use it!
|
|
|
|
 |