在c++支持unicode转换和unicode文件保存

来源:互联网 发布:大赢家炒股软件 编辑:程序博客网 时间:2024/06/05 19:06

/********************************************************************
  created:  2008/01/11
  created:  11:1:2008   4:30
  filename:   f:/备份/开发资源/解决方案/others1/prj2/prj2.cpp
  file path:  f:/备份/开发资源/解决方案/others1/prj2
  file base:  prj2
  file ext:  cpp
  author:    Loserwang
  
  purpose:  display the unicode
*********************************************************************/

#include <cstring>
#include <clocale>

#include <iostream>
#include <xlocale>
#include <string>
#include <fstream>
#include <algorithm>
#include <functional>
#include <iomanip>

#ifdef UNICODE

  #define unicode_header true
  #define _t(x) L##x
  #define _convert mbstowcs
  #define _tcout wcout
  #define _tsetlocale _wsetlocale

  typedef wchar_t _tchar;
  typedef _tchar _convert_dst;
  typedef char _convert_src;
  typedef size_t _convert_sz;

#else

  #define unicode_header false
  #define _t(x) x
  #define _convert wcstombs
  #define _tcout cout
  #define _tsetlocale setlocale

  typedef char _tchar;
  typedef _tchar _convert_dst;
  typedef wchar_t _convert_src;
  typedef size_t _convert_sz;

#endif

  namespace std{
    typedef basic_string<_tchar> _tstring;
  };

template<typename DisTy, typename CharTy = _tchar>class disp
:public std::unary_function<void, DisTy>
{
  typedef std::basic_ostream<CharTy> ostype;
  const _tchar* delim;
  ostype& os;
public:
  disp(ostype& os_ = std::_tcout, const _tchar* delim_ = _t("/t"))
    :os(os_), delim(delim_)
  {}
  template<typename Ty1> void operator()(const Ty1& t)
  {
    if(delim)
      os << DisTy(t) << delim;
    else
      os << DisTy(t) ;
  }
};

std::_tstring make_tstring(std::string& s, const _tchar* name)
{
  if(typeid(_tchar) == typeid(char))
    return *reinterpret_cast<std::_tstring*>(&s);

  std::auto_ptr<_convert_dst> dst(new _convert_dst[s.size() + 1]);

  const _tchar* oname = _tsetlocale(LC_ALL, name);
  _convert(dst.get(), (_convert_src*)s.c_str(), s.size() + 1);
  _tsetlocale(LC_ALL, oname);

  return std::basic_string<_tchar>(dst.get());
}

int main()
{
  std::_tcout.imbue(std::locale(".936"));
  std::string locale_name = std::_tcout.getloc().name();

  std::_tstring s = make_tstring(locale_name, _t(".936"));
  std::_tcout << s << std::endl;
  locale_name = "一生梦与醒缘份纠缠不清/r/n"
    "三世红尘独享尽爱与恨/r/n"
    "缘终一个字你我心手相牵/r/n"
    "渺渺茫茫只求一生缘/r/n";
  s = make_tstring(locale_name, _t(".936"));
  std::_tcout << s << std::endl;

  char* bytes = (char*)(s.c_str());
  //std::for_each(bytes, bytes + (s.size() + 1) * sizeof(_tchar), disp<int>());
  std::copy(bytes, bytes + (s.size() + 1) * sizeof(_tchar), std::ostream_iterator<int, _tchar>(std::_tcout, _t("/t")));

  std::ofstream f(".//..//outfiles//prj2.txt", std::ios_base::binary);
  unicode_header ? f << char(0xFF) << char(0xFE) : void();
  //for_each(bytes, bytes + (s.size() + 1) * sizeof(_tchar), disp<char, char>(f, 0));
  std::copy(bytes, bytes + (s.size() + 1) * sizeof(_tchar), std::ostream_iterator<char>(f));

  f.close();
  return 0;
}

 
原创粉丝点击