const std::wstring& getAlphabet() { const wchar_t kMinCharCode = 32, kMaxCharCode = 126; static std::wstring alphabet; if (alphabet.empty()) { alphabet.reserve(kMaxCharCode - kMinCharCode); for (wchar_t c = kMinCharCode; c <= kMaxCharCode; ++c) { alphabet.push_back(c); } } return alphabet; }
// Reads string from between double quotes. void readQuote(std::wistream& stream, std::wstring& str_out) { wchar_t c; str_out = L""; // Parse to first " while (1) { stream.get(c); enforce(stream.good(), "Expected quoted string, got end of stream"); if (c == '"') break; enforce(isspace(c), std::wstring(L"Expected quoted string, got ") + c); } // Parse quoted text bool escaping = false; while (1) { stream.get(c); enforce(stream.good(), "Unexpected end of stream while reading quoted string"); if (escaping) { str_out.push_back(c); escaping = false; } else { if (c == '\\') escaping = true; else if (c == '"') break; else str_out.push_back(c); } } }
static void clean_name_win32(std::wstring &fn) { if (fn.size()<2) return; if (fn.size()==2 && fn[1]==_TEXT(':')){ fn.push_back(_TEXT(DIR_SEPARATOR)); return; } if (fn.size()==3 && fn[1]==_TEXT(':')){ return; } if (fn[fn.size()-1] == _TEXT(DIR_SEPARATOR)){ fn.erase(fn.size()-1); } }
bool string2Bevel(const wchar_t* _str , xuiBevel& bevel) { static std::wstring str; str=L""; for(int i = 0 ; i < (int)wcslen(_str) ; i ++) { if(_str[i] != ' ' && _str[i] != '\t') { str.push_back(_str[i]); } } swscanf(str.c_str(),L"%f:%f:%f:%f",&bevel.tl,&bevel.tr,&bevel.br,&bevel.bl); return true; }
fBool fcyXml::tryReadCDATA(fcyLexicalReader& tReader, std::wstring& tOut) { tOut = L""; if(tReader.TryMatch(L"<![CDATA[", false, true)) { while(!tReader.TryMatch(L"]]>", false, true)) { tOut.push_back(tReader.ReadChar()); } return true; } return false; }
bool VT::delete_file_folder(std::wstring path) { SHFILEOPSTRUCTW fileOp = {0}; // Double null-terminated string path.push_back(L'\0'); path.push_back(L'\0'); fileOp.fFlags = FOF_NO_UI; fileOp.pFrom = path.c_str(); fileOp.wFunc = FO_DELETE; fileOp.hwnd = NULL; return (SHFileOperationW(&fileOp) == 0); }
void convert_aux(const char* from, const char* from_end, wchar_t* to, wchar_t* to_end, std::wstring& target, const codecvt_type& cvt, conv_method how) { std::mbstate_t state = std::mbstate_t(); const char* from_next; wchar_t* to_next; ::SetLastError(ERROR_SUCCESS); if (cvt.in(state, from, from_end, from_next, to, to_end, to_next) == std::codecvt_base::ok) { target.append(to, to_next); return ; } if (how.type() == conv_method::stop) { throw windows_exception("character conversion failed"); } from_next = from; while (from_next != from_end) { wchar_t to_buf[4]; int len = cvt.length(state, from_next, from_end, 1); if (len <= 0) return; const char* from_mid; if (cvt.in(state, from_next, from_next + len, from_mid, to_buf, to_buf + _countof(to_buf), to_next) == std::codecvt_base::ok) { assert(from_next + len == from_mid); target.append(to_buf, to_next); from_next += len; } else { if (how.type() == conv_method::replace) { target.push_back(static_cast<wchar_t>(how.default_char())); from_next += 1; } else { from_next += len; } } } }
//Read String(Wide Char Version) void ReadStringFromFileW(std::wstring& outString) { do { char tmpChar = 0; BINARY_READ(tmpChar); if (tmpChar) { outString.push_back(tmpChar); } else { break; } } while (true); }
/** * Read UTF-16 format code units using the specified byte order into wstring up to but not including \U0000. */ void sbuf_t::getUTF16(size_t i, byte_order_t bo, std::wstring &utf16_string) const { // clear any residual value utf16_string = std::wstring(); // read the code units size_t off; for (off=i; off<bufsize-1; off += 2) { uint16_t code_unit = get16u(off, bo); //cout << "sbuf.cpp getUTF16 i: " << i << " code unit: " << code_unit << "\n"; // stop before \U0000 if (code_unit == 0) { // at \U0000 break; } // accept the code unit utf16_string.push_back(code_unit); } }
/** * Read the requested number of UTF-16 format code units using the specified byte order into wstring including any \U0000. */ void sbuf_t::getUTF16(size_t i, size_t num_code_units_requested, byte_order_t bo, std::wstring &utf16_string) const { // clear any residual value utf16_string = std::wstring(); if(i>=bufsize) { // past EOF return; } if(i+num_code_units_requested*2+1>bufsize) { // clip at EOF num_code_units_requested = ((bufsize-1)-i)/2; } // NOTE: we can't use wstring constructor because we require 16 bits, // not whatever sizeof(wchar_t) is. // utf16_string = std::wstring((const char *)buf+i,num_code_units_requested); // get code units individually for (size_t j = 0; j < num_code_units_requested; j++) { utf16_string.push_back(get16u(i + j, bo)); } }
void csu_utf8_to_wstr(std::string &src, std::wstring &dest) { // http://www.linuxquestions.org/questions/programming-9/wstring-utf8-conversion-in-pure-c-701084/ dest.clear(); wchar_t w = 0; int bytes = 0; wchar_t err = L'�'; for (size_t i = 0; i < src.size(); i++){ unsigned char c = (unsigned char)src[i]; if (c <= 0x7f){//first byte if (bytes){ dest.push_back(err); bytes = 0; } dest.push_back((wchar_t)c); } else if (c <= 0xbf){//second/third/etc byte if (bytes){ w = ((w << 6)|(c & 0x3f)); bytes--; if (bytes == 0) dest.push_back(w); } else dest.push_back(err); } else if (c <= 0xdf){//2byte sequence start bytes = 1; w = c & 0x1f; } else if (c <= 0xef){//3byte sequence start bytes = 2; w = c & 0x0f; } else if (c <= 0xf7){//3byte sequence start bytes = 3; w = c & 0x07; } else{ dest.push_back(err); bytes = 0; } } if (bytes) dest.push_back(err); }
bool string2Color(const wchar_t* _str , xColor_4f& color) { static std::wstring str; str=L""; for(int i = 0 ; i < (int)wcslen(_str) ; i ++) { if(_str[i] != ' ' && _str[i] != '\t') { str.push_back(_str[i]); } } assert(str.length() > 0); if(str[0] == '[') { swscanf(str.c_str(),L"[%f,%f,%f,%f]",&color.r,&color.g,&color.b,&color.a); } else { xColor_4ub _cl; swscanf(str.c_str() ,L"%x", &_cl.color); color = _cl; } return true; }
//----------------------------------------------------------------------------- int main(int argc, char **argv) { char ch, iname[256]=""; mgl_suppress_warn(true); while(1) { ch = getopt(argc, argv, "1:2:3:4:5:6:7:8:9:hL:s:"); if(ch>='1' && ch<='9') p.AddParam(ch-'0', optarg); else if(ch=='s') { setlocale(LC_CTYPE, ""); FILE *fp = fopen(optarg,"r"); if(fp) { wchar_t ch; while((ch=fgetwc(fp))!=WEOF) opt.push_back(ch); fclose(fp); } } else if(ch=='L') setlocale(LC_CTYPE, optarg); else if(ch=='h' || (ch==-1 && optind>=argc)) { printf("mglview show plot from MGL script or MGLD file.\nCurrent version is 2.%g\n",MGL_VER2); printf("Usage:\tmglview [parameter(s)] scriptfile\n"); printf( "\t-1 str set str as argument $1 for script\n" "\t... ...\n" "\t-9 str set str as argument $9 for script\n" "\t-s opt set MGL script for setting up the plot\n" "\t-L loc set locale to loc\n" "\t- get script from standard input\n" "\t-h print this message\n" ); ch = 'h'; break; } else if(ch==-1 && optind<argc) { strncpy(iname, argv[optind][0]=='-'?"":argv[optind],256); break; } } if(ch=='h') return 0; bool mgld=(*iname && iname[strlen(iname)-1]=='d'); if(!mgld) { str = opt + L"\n"; setlocale(LC_CTYPE, ""); FILE *fp = *iname?fopen(iname,"r"):stdin; if(fp) { wchar_t ch; while((ch=fgetwc(fp))!=WEOF) str.push_back(ch); fclose(fp); } else { printf("No file for MGL script\n"); return 0; } } mgl_ask_func = mgl_ask_gets; mgl_ask_func = mgl_ask_qt; mglQT gr(mgld?NULL:show, *iname?iname:"mglview"); if(mgld) { gr.Setup(false); gr.NewFrame(); setlocale(LC_NUMERIC, "C"); if(!opt.empty()) { p.Execute(&gr,opt.c_str()); printf("Setup script: %s\n",gr.Message()); gr.ImportMGLD(iname,true); } else gr.ImportMGLD(iname); setlocale(LC_NUMERIC, ""); gr.EndFrame(); gr.Update(); } if(!mglGlobalMess.empty()) printf("%s",mglGlobalMess.c_str()); return gr.Run(); }
void Reader::getToken(std::wstring &token) { CASE_BEGIN token.clear(); while (pos<str.size() && iswspace(str[pos])) { ++pos; } if (pos>=str.size()) { throw(UnexpectedEndException()); } wchar_t ch = str[pos]; switch (ch) { case L'(': case L')': case L',': case L'↑': case L'¬': case L'∧': case L'∨': case L'⇒': case L'⇔': case L'⊤': case L'⊥': case L'∀': case L'∃': case L'=': case L'.': token.push_back(ch); ++pos; return; break; } if (iswalpha(ch)==false) { throw(AlphaExpectedException()); } ++pos; if (pos<str.size() && str[pos]==L'_') { ++pos; if (pos>=str.size() || iswalpha(str[pos])==false) { throw(AlphaExpectedException()); } while (pos<str.size() && iswalnum(str[pos])) { token += str[pos++]; } Symbol s = dictionary(token); switch (ch) { case L'v': if (s.type==NONE_SYMBOL) { Variable v; dictionary.insert(token, v); dictionaryExtender.merge(); } else if (s.type!=VARIABLE) { throw(VariableExpectedException()); } return; break; case L'c': if (s.type==NONE_SYMBOL) { ConstantSymbol c; dictionary.insert(token, c); dictionaryExtender.merge(); } else if (s.type!=CONSTANT) { throw(ConstantExpectedException()); } return; case L'f': if (s.type==NONE_SYMBOL) { token = std::wstring(L"f_")+token; } else if (s.type!=OPERATION) { throw(OperationExpectedException()); } return; case L'R': if (s.type==NONE_SYMBOL) { token = std::wstring(L"R_")+token; } else if (s.type!=RELATION) { throw(RelationExpectedException()); } return; break; default: throw(OneOfExpectedException(L"vcfR")); break; } } --pos; while (pos<str.size() && iswalnum(str[pos])) { token += str[pos++]; } return; CASE_END throw(UnexpectedEndException()); }
void TextDecoder::Append(std::wstring& str, const uint8_t* bytes, size_t length, CharacterSet charset) { switch (charset) { case CharacterSet::Unknown: case CharacterSet::ISO8859_1: case CharacterSet::ASCII: { str.append(bytes, bytes + length); break; } case CharacterSet::ISO8859_2: case CharacterSet::ISO8859_3: case CharacterSet::ISO8859_4: case CharacterSet::ISO8859_5: case CharacterSet::ISO8859_6: case CharacterSet::ISO8859_7: case CharacterSet::ISO8859_8: case CharacterSet::ISO8859_9: case CharacterSet::ISO8859_10: case CharacterSet::ISO8859_11: case CharacterSet::ISO8859_13: case CharacterSet::ISO8859_14: case CharacterSet::ISO8859_15: case CharacterSet::ISO8859_16: case CharacterSet::Cp437: case CharacterSet::Cp1250: case CharacterSet::Cp1251: case CharacterSet::Cp1252: case CharacterSet::Cp1256: { str.reserve(str.length() + length); for (size_t i = 0; i < length; ++i) { uint8_t c = bytes[i]; if (c < 128) str.push_back(c); else str.push_back(Codecs::SINGLE_BYTE_CODEPAGES[((int)charset - (int)CharacterSet::ISO8859_2) * 128 + c - 128]); } break; } case CharacterSet::Shift_JIS: { std::vector<uint16_t> buf; JPTextDecoder::AppendShiftJIS(buf, bytes, length); TextUtfEncoding::AppendUtf16(str, buf.data(), buf.size()); break; } case CharacterSet::Big5: { std::vector<uint16_t> buf; Big5TextDecoder::AppendBig5(buf, bytes, length); TextUtfEncoding::AppendUtf16(str, buf.data(), buf.size()); break; } case CharacterSet::GB2312: { std::vector<uint16_t> buf; GBTextDecoder::AppendGB2312(buf, bytes, length); TextUtfEncoding::AppendUtf16(str, buf.data(), buf.size()); break; } case CharacterSet::GB18030: { std::vector<uint16_t> buf; GBTextDecoder::AppendGB18030(buf, bytes, length); TextUtfEncoding::AppendUtf16(str, buf.data(), buf.size()); break; } case CharacterSet::EUC_JP: { std::vector<uint16_t> buf; JPTextDecoder::AppendEUCJP(buf, bytes, length); TextUtfEncoding::AppendUtf16(str, buf.data(), buf.size()); break; } case CharacterSet::EUC_KR: { std::vector<uint16_t> buf; KRTextDecoder::AppendEucKr(buf, bytes, length); TextUtfEncoding::AppendUtf16(str, buf.data(), buf.size()); break; break; } case CharacterSet::UnicodeBig: { str.reserve(str.length() + length / 2); for (size_t i = 0; i + 1 < length; i += 2) { str.push_back((static_cast<wchar_t>(bytes[i]) << 8) + bytes[i + 1]); } break; } case CharacterSet::UTF8: { TextUtfEncoding::AppendUtf8(str, bytes, length); break; } default: break; } }