AutoChar.h 658 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifndef NULLSOFT_AUTOCHARH
  2. #define NULLSOFT_AUTOCHARH
  3. class AutoChar
  4. {
  5. public:
  6. AutoChar(const wchar_t *convert) : allocated(false), narrow(0)
  7. {
  8. // review maybe CP_UTF8?
  9. int size = WideCharToMultiByte(CP_ACP, 0, convert, -1, 0, 0, NULL, NULL);
  10. if (!size)
  11. return;
  12. narrow = new char[size];
  13. allocated=true;
  14. if (!WideCharToMultiByte(CP_ACP, 0, convert, -1, narrow, size, NULL, NULL))
  15. {
  16. delete [] narrow;
  17. narrow=0;
  18. allocated=false;
  19. }
  20. }
  21. ~AutoChar()
  22. {
  23. if (allocated)
  24. {
  25. delete [] narrow;
  26. narrow=0;
  27. allocated=false;
  28. }
  29. }
  30. operator char *()
  31. {
  32. return narrow;
  33. }
  34. private:
  35. bool allocated;
  36. char *narrow;
  37. };
  38. #endif