<本文PDF文檔下載>C++的流和本地化策略集
BS在設(shè)計(jì)C++流的時(shí)候希望其具備智能化,并且是可擴(kuò)展的智能化,也就是說,C++的流可以“讀懂”一些內(nèi)容。比如:
std::cout << 123 << "ok" << std::endl;
這句代碼中,std::cout是能判斷出123是int而"ok"是const char[3]。利用流的智能,甚至可以做一些基礎(chǔ)類型的轉(zhuǎn)換,比如從int到string,string到int:
std::string str("123");
std::stringstream sstr(str);
int i;
sstr >> i;
int i = 123;
std::stringstream sstr;
sstr << i;
std::string str = sstr.str();
盡管如此,C++并不滿足,C++甚至希望流能“明白”時(shí)間,貨幣的表示法。而時(shí)間和貨幣的表示方法在世界范圍內(nèi)是不同的,所以,每一個(gè)流都有自己的locale在影響其行為,C++中叫做激活(imbue,也有翻譯成浸染)。而我們知道,每一個(gè)locale都有多個(gè)facet,這些facet并非總是被use_facet使用的。決定使用哪些facet的,是流的緩存basic_streambuf及其派生類basic_stringbuf和basic_filebuf。我們要用到的facet是codecvt,這個(gè)facet只被basic_filebuf使用——這就是為什么只能用fstream來(lái)實(shí)現(xiàn)寬窄轉(zhuǎn)換,而無(wú)法使用sstream來(lái)實(shí)現(xiàn)的原因。
頭文件:
//filename string_wstring_fstream.hpp
#ifndef STRING_WSTRING_FSTREAM_HPP
#define STRING_WSTRING_FSTREAM_HPP
#include <string>
const std::wstring s2ws(const std::string& s);
const std::string ws2s(const std::wstring& s);
#endif
實(shí)現(xiàn):
#include <string>
#include <fstream>
#include "string_wstring_fstream.hpp"
const std::wstring s2ws(const std::string& s)
{
std::locale sys_loc("");
std::ofstream ofs("cvt_buf");
ofs << s;
ofs.close();
std::wifstream wifs("cvt_buf");
wifs.imbue(sys_loc);
std::wstring wstr;
wifs >> wstr;
wifs.close();
return wstr;
}
const std::string ws2s(const std::wstring& s)
{
std::locale sys_loc("");
std::wofstream wofs("cvt_buf");
wofs.imbue(sys_loc);
wofs << s;
wofs.close();
std::ifstream ifs("cvt_buf");
std::string str;
ifs >> str;
ifs.close();
return str;
}
在窄到寬的轉(zhuǎn)化中,我們先使用默認(rèn)的本地化策略集(locale)將s通過窄文件流ofs傳入文件,這是char到char的傳遞,沒有任何轉(zhuǎn)換;然后我們打開寬文件流wifs,并用系統(tǒng)的本地化策略集(locale)去激活(imbue)之,流在讀回寬串wstr的時(shí)候,就是char到wchar_t的轉(zhuǎn)換,并且因?yàn)榧せ盍藄ys_loc,所以實(shí)現(xiàn)標(biāo)準(zhǔn)窄到寬的轉(zhuǎn)換。
在寬到窄的轉(zhuǎn)化中,我們先打開的是寬文件流wofs,并且用系統(tǒng)的本地化策略集sys_loc激活(imbue)之,這時(shí)候,因?yàn)橐獙懙奈募vt_buf是一個(gè)外部編碼,所以執(zhí)行了從wchar_t到char的標(biāo)準(zhǔn)轉(zhuǎn)換。讀回來(lái)的文件流從char到char,不做任何轉(zhuǎn)換。