static void append(string_type& str, const char* f, const char* l) { str.append(f, l); }
void set_string(const string_type & str) { m_str.resize(str.size() + m_reserve_prepend + m_reserve_append); std::copy( str.begin(), str.end(), m_str.begin() + static_cast<typename string_type::difference_type>(m_reserve_prepend)); m_full_msg_computed = false; }
inline void write(const string_type & src, string_type & dest) { dest.insert( dest.begin(), src.begin(), src.end() ); }
date_type parse_date(std::istreambuf_iterator<charT>& sitr, std::istreambuf_iterator<charT>& stream_end, string_type format) const { bool use_current_char = false; charT current_char = *sitr; unsigned short year(0), month(0), day(0), day_of_year(0); const_itr itr(format.begin()); while (itr != format.end() && (sitr != stream_end)) { if (*itr == '%') { itr++; if (*itr != '%') { //ignore '%%' unsigned short i = 0; switch(*itr) { case 'a': { //this value is just throw away. It could be used for //error checking potentially, but it isn't helpful in //actually constructing the date - we just need to get it //out of the stream match_results mr = m_weekday_short_names.match(sitr, stream_end); unsigned int wkday = mr.current_match; if (mr.has_remaining()) { current_char = mr.last_char(); use_current_char = true; } break; } case 'A': { //this value is just throw away. It could be used for //error checking potentially, but it isn't helpful in //actually constructing the date - we just need to get it //out of the stream match_results mr = m_weekday_long_names.match(sitr, stream_end); unsigned int wkday = mr.current_match; if (mr.has_remaining()) { current_char = mr.last_char(); use_current_char = true; } break; } case 'b': { match_results mr = m_month_short_names.match(sitr, stream_end); month = mr.current_match; if (mr.has_remaining()) { current_char = mr.last_char(); use_current_char = true; } break; } case 'B': { match_results mr = m_month_long_names.match(sitr, stream_end); month = mr.current_match; if (mr.has_remaining()) { current_char = mr.last_char(); use_current_char = true; } break; } case 'd': { day = var_string_to_int<unsigned short, charT>(sitr, 2); break; } case 'j': { day_of_year = fixed_string_to_int<unsigned short, charT>(sitr, 3); break; } case 'm': { month = var_string_to_int<unsigned short, charT>(sitr, 2); break; } case 'Y': { year = fixed_string_to_int<unsigned short, charT>(sitr, 4); break; } case 'y': { year = fixed_string_to_int<unsigned short, charT>(sitr, 2); year += 2000; //make 2 digit years in this century break; } default: {} //ignore those we don't understand }//switch } itr++; //advance past format specifier } else { //skip past chars in format and in buffer itr++; if (use_current_char) { use_current_char = false; current_char = *sitr; } else { sitr++; } } } if (day_of_year != 0) { date_type d(year-1,12,31); //end of prior year return d + duration_type(day_of_year); } return date_type(year, month, day); }
response const put (request request_, string_type const & content_type, string_type const & body_) { request_ << ::boost::network::body(body_) << header("Content-Type", content_type) << header("Content-Length", boost::lexical_cast<string_type>(body_.size())); return put(request_); }
void set_string(const string_type & str) { m_str.resize( str.size() + m_reserve_prepend + m_reserve_append); std::copy( str.begin(), str.end(), m_str.begin() + m_reserve_prepend); m_full_msg_computed = false; }
// Write to a wstring void write(string_type& s, const char_type* from, const char_type* to) { assert(from <= to); s.append(from, to); }
//Based on the example at https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx. Process::id_type Process::open(const string_type &command, const string_type &path) { if(open_stdin) stdin_fd=std::unique_ptr<fd_type>(new fd_type(NULL)); if(read_stdout) stdout_fd=std::unique_ptr<fd_type>(new fd_type(NULL)); if(read_stderr) stderr_fd=std::unique_ptr<fd_type>(new fd_type(NULL)); Handle stdin_rd_p; Handle stdin_wr_p; Handle stdout_rd_p; Handle stdout_wr_p; Handle stderr_rd_p; Handle stderr_wr_p; SECURITY_ATTRIBUTES security_attributes; security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); security_attributes.bInheritHandle = TRUE; security_attributes.lpSecurityDescriptor = nullptr; std::lock_guard<std::mutex> lock(create_process_mutex); if(stdin_fd) { if (!CreatePipe(&stdin_rd_p, &stdin_wr_p, &security_attributes, 0) || !SetHandleInformation(stdin_wr_p, HANDLE_FLAG_INHERIT, 0)) return 0; } if(stdout_fd) { if (!CreatePipe(&stdout_rd_p, &stdout_wr_p, &security_attributes, 0) || !SetHandleInformation(stdout_rd_p, HANDLE_FLAG_INHERIT, 0)) { return 0; } } if(stderr_fd) { if (!CreatePipe(&stderr_rd_p, &stderr_wr_p, &security_attributes, 0) || !SetHandleInformation(stderr_rd_p, HANDLE_FLAG_INHERIT, 0)) { return 0; } } PROCESS_INFORMATION process_info; STARTUPINFO startup_info; ZeroMemory(&process_info, sizeof(PROCESS_INFORMATION)); ZeroMemory(&startup_info, sizeof(STARTUPINFO)); startup_info.cb = sizeof(STARTUPINFO); startup_info.hStdInput = stdin_rd_p; startup_info.hStdOutput = stdout_wr_p; startup_info.hStdError = stderr_wr_p; if(stdin_fd || stdout_fd || stderr_fd) startup_info.dwFlags |= STARTF_USESTDHANDLES; string_type process_command=command; #ifdef MSYS_PROCESS_USE_SH size_t pos=0; while((pos=process_command.find('\\', pos))!=string_type::npos) { process_command.replace(pos, 1, "\\\\\\\\"); pos+=4; } pos=0; while((pos=process_command.find('\"', pos))!=string_type::npos) { process_command.replace(pos, 1, "\\\""); pos+=2; } process_command.insert(0, "sh -c \""); process_command+="\""; #endif BOOL bSuccess = CreateProcess(nullptr, process_command.empty()?nullptr:&process_command[0], nullptr, nullptr, TRUE, 0, nullptr, path.empty()?nullptr:path.c_str(), &startup_info, &process_info); if(!bSuccess) { CloseHandle(process_info.hProcess); CloseHandle(process_info.hThread); return 0; } else { CloseHandle(process_info.hThread); } if(stdin_fd) *stdin_fd=stdin_wr_p.detach(); if(stdout_fd) *stdout_fd=stdout_rd_p.detach(); if(stderr_fd) *stderr_fd=stderr_rd_p.detach(); closed=false; data.id=process_info.dwProcessId; data.handle=process_info.hProcess; return process_info.dwProcessId; }
/*! Take a line from the csv, turn it into a time_zone_type, * and add it to the map. Zone_specs in csv file are expected to * have eleven fields that describe the time zone. Returns true if * zone_spec successfully added to database */ bool parse_string(string_type& s) { std::vector<string_type> result; typedef boost::token_iterator_generator<boost::escaped_list_separator<charT>, string_type::const_iterator, string_type >::type token_iter_type; token_iter_type i = boost::make_token_iterator<string_type>(s.begin(), s.end(),boost::escaped_list_separator<charT>()); token_iter_type end; while (i != end) { result.push_back(*i); i++; } enum db_fields { ID, STDABBR, STDNAME, DSTABBR, DSTNAME, GMTOFFSET, DSTADJUST, START_DATE_RULE, START_TIME, END_DATE_RULE, END_TIME, FIELD_COUNT }; //take a shot at fixing gcc 4.x error const unsigned int expected_fields = static_cast<unsigned int>(FIELD_COUNT); if (result.size() != expected_fields) { std::stringstream msg; msg << "Expecting " << FIELD_COUNT << " fields, got " << result.size() << " fields in line: " << s; throw bad_field_count(msg.str()); } // initializations bool has_dst = true; if(result[DSTABBR] == std::string()) { has_dst = false; } // start building components of a time_zone time_zone_names names(result[STDNAME], result[STDABBR], result[DSTNAME], result[DSTABBR]); time_duration_type utc_offset = posix_time::duration_from_string(result[GMTOFFSET]); dst_adjustment_offsets adjust(time_duration_type(0,0,0), time_duration_type(0,0,0), time_duration_type(0,0,0)); boost::shared_ptr<rule_type> rules; if(has_dst) { adjust = dst_adjustment_offsets( posix_time::duration_from_string(result[DSTADJUST]), posix_time::duration_from_string(result[START_TIME]), posix_time::duration_from_string(result[END_TIME]) ); rules = boost::shared_ptr<rule_type>(parse_rules(result[START_DATE_RULE], result[END_DATE_RULE])); } string_type id(result[ID]); boost::shared_ptr<time_zone_base_type> zone(new time_zone_type(names, utc_offset, adjust, rules)); return (add_record(id, zone)); }
lazy_substring(const string_type& str) : m_string(&str) , m_start(0) , m_end(str.size()) { }
//--------------------------------------------------------------------------- void Calc() { // ParserX parser(pckALL_NON_COMPLEX); ParserX parser(pckALL_COMPLEX); // Create an array variable Value arr1(3, 0); arr1.At(0) = (float_type)1.0; arr1.At(1) = (float_type)2.0; arr1.At(2) = (float_type)3.0; Value arr2(3, 0); arr2.At(0) = (float_type)4.0; arr2.At(1) = (float_type)3.0; arr2.At(2) = (float_type)2.0; Value arr3(4, 0); arr3.At(0) = (float_type)1.0; arr3.At(1) = (float_type)2.0; arr3.At(2) = (float_type)3.0; arr3.At(3) = (float_type)4.0; Value arr4(3, 0); arr4.At(0) = (float_type)4.0; arr4.At(1) = false; arr4.At(2) = _T("hallo"); // Create a 3x3 matrix with zero elements Value m1(3, 3, 0); m1.At(0, 0) = 1; m1.At(1, 1) = 1; m1.At(2, 2) = 1; Value m2(3, 3, 0); m2.At(0, 0) = 1; m2.At(0, 1) = 2; m2.At(0, 2) = 3; m2.At(1, 0) = 4; m2.At(1, 1) = 5; m2.At(1, 2) = 6; m2.At(2, 0) = 7; m2.At(2, 1) = 8; m2.At(2, 2) = 9; Value val[5]; val[0] = (float_type)1.1; val[1] = 1; val[2] = false; val[3] = _T("Hello"); val[4] = _T("World"); Value fVal[3]; fVal[0] = (float_type)1.11; fVal[1] = (float_type)2.22; fVal[2] = (float_type)3.33; Value iVal[3]; iVal[0] = 1; iVal[1] = 2; iVal[2] = 3; Value sVal[3]; sVal[0] = _T("hello"); sVal[1] = _T("world"); sVal[2] = _T("test"); Value cVal[3]; cVal[0] = mup::cmplx_type(1, 1); cVal[1] = mup::cmplx_type(2, 2); cVal[2] = mup::cmplx_type(3, 3); Value ans; parser.DefineVar(_T("ans"), Variable(&ans)); // some tests for vectors parser.DefineVar(_T("va"), Variable(&arr1)); parser.DefineVar(_T("vb"), Variable(&arr2)); parser.DefineVar(_T("vc"), Variable(&arr3)); parser.DefineVar(_T("vd"), Variable(&arr4)); parser.DefineVar(_T("m1"), Variable(&m1)); parser.DefineVar(_T("m2"), Variable(&m2)); parser.DefineVar(_T("a"), Variable(&fVal[0])); parser.DefineVar(_T("b"), Variable(&fVal[1])); parser.DefineVar(_T("c"), Variable(&fVal[2])); parser.DefineVar(_T("ia"), Variable(&iVal[0])); parser.DefineVar(_T("ib"), Variable(&iVal[1])); parser.DefineVar(_T("ic"), Variable(&iVal[2])); parser.DefineVar(_T("ca"), Variable(&cVal[0])); parser.DefineVar(_T("cb"), Variable(&cVal[1])); parser.DefineVar(_T("cc"), Variable(&cVal[2])); parser.DefineVar(_T("sa"), Variable(&sVal[0])); parser.DefineVar(_T("sb"), Variable(&sVal[1])); // Add functions for inspecting the parser properties parser.DefineFun(new FunListVar); parser.DefineFun(new FunListFunctions); parser.DefineFun(new FunListConst); parser.DefineFun(new FunBenchmark); parser.DefineFun(new FunEnableOptimizer); parser.DefineFun(new FunSelfTest); parser.DefineFun(new FunEnableDebugDump); parser.DefineFun(new FunTest0); parser.DefineFun(new FunPrint); parser.EnableAutoCreateVar(true); //#ifdef _DEBUG // ParserXBase::EnableDebugDump(1, 0); //#endif for(;;) { try { console() << sPrompt; string_type sLine; std::getline(mup::console_in(), sLine); if (sLine.length()==0) continue; if (sLine==_T("dbg")) { sLine = _T("sum(3)/sum(3,4,5)"); mup::console() << sLine << endl; } switch(CheckKeywords(sLine.c_str(), parser)) { case 0: break; case 1: continue; case -1: return; } parser.SetExpr(sLine); // The returned result is of type Value, value is a Variant like // type that can be either a boolean an integer or a floating point value ans = parser.Eval(); // ans = parser.Eval(); // Value supports C++ streaming like this: //console() << _T("Result (type: '" << ans.GetType() << "'):\n"); console() << _T("ans = ") << ans << _T("\n"); // Or if you need the specific type use this: //switch (result.GetType()) //{ //case 's': cout << result.GetString() << " (string)" << "\n"; break; //case 'i': cout << result.GetInt() << " (int)" << "\n"; break; //case 'f': cout << result.GetFloat() << " (float)" << "\n"; break; //case 'c': cout << result.GetFloat() << "+" << result.GetImag() << "i (complex)" << "\n"; break; //case 'b': break; //} } catch(ParserError &e) { if (e.GetPos()!=-1) { string_type sMarker; sMarker.insert(0, sPrompt.size() + e.GetPos(), ' '); sMarker += _T("^\n"); console() << sMarker; } console() << e.GetMsg() << _T(" (Errc: ") << e.GetCode() << _T(")") << _T("\n\n"); //if (e.GetContext().Ident.length()) // console() << _T("Ident.: ") << e.GetContext().Ident << _T("\n"); //if (e.GetToken().length()) // console() << _T("Token: \"") << e.GetToken() << _T("\"\n"); } // try / catch } // for (;;) }
void ThinConsole::SetWindowTitle(string_type const& title) { ExceptionCheck(SetConsoleTitle(title.c_str()), __FUNCTION__, __LINE__); }
inline std::string get_host(std::string default_host = "127.0.0.1") const { if (!host.empty()) return utf8::cvt<std::string>(host); return default_host; }
stringProxy::stringProxy(const string_type& s, const size_type start, const size_type end) : m_buffer(s), m_start(start), m_end(end == std::numeric_limits <size_type>::max() ? s.length() : end) { }
const char_type* c_str() const { return value.c_str(); }
//! Cleanup method void clear() { m_Stream.clear(); m_StreamBuf.clear(); m_Buffer.clear(); }
operator const char_type* () const { return value.c_str(); }
void expand_escapes(string_type& s) { for(unsigned int i = 0; i < s.size(); ++i) { if(s[i] == BOOST_RE_STR('\\')) { switch(s[i+1]) { case BOOST_RE_STR('a'): s.erase(s.begin() + i); s[i] = BOOST_RE_STR('\a'); break; case BOOST_RE_STR('b'): s.erase(s.begin() + i); s[i] = BOOST_RE_STR('\b'); break; case BOOST_RE_STR('f'): s.erase(s.begin() + i); s[i] = BOOST_RE_STR('\f'); break; case BOOST_RE_STR('n'): s.erase(s.begin() + i); s[i] = BOOST_RE_STR('\n'); break; case BOOST_RE_STR('r'): s.erase(s.begin() + i); s[i] = BOOST_RE_STR('\r'); break; case BOOST_RE_STR('t'): s.erase(s.begin() + i); s[i] = BOOST_RE_STR('\t'); break; case BOOST_RE_STR('v'): s.erase(s.begin() + i); s[i] = BOOST_RE_STR('\v'); break; default: if( (s[i + 1] >= BOOST_RE_STR('0')) && (s[i + 1] <= BOOST_RE_STR('9')) ) { int val = 0; unsigned int pos = i; ++i; while((i < s.size()) && (s[i] >= BOOST_RE_STR('0')) && (s[i] <= BOOST_RE_STR('9'))) { val *= 10; val += s[i] - BOOST_RE_STR('0'); ++i; } s.erase(s.begin() + pos, s.begin() + i); if(0 == val) { s.insert(s.begin()+pos, ' '); s[pos] = 0; } else s.insert(s.begin() + pos, (string_type::value_type)val); i = pos; } else { s.erase(s.begin() + i); } } } } }
//! Converts escape sequences to the corresponding characters void char_constants< wchar_t >::translate_escape_sequences(string_type& str) { using namespace std; // to make sure we can use C functions unqualified string_type::iterator it = str.begin(); while (it != str.end()) { it = std::find(it, str.end(), L'\\'); if (std::distance(it, str.end()) >= 2) { it = str.erase(it); switch (*it) { case L'n': *it = L'\n'; break; case L'r': *it = L'\r'; break; case L'a': *it = L'\a'; break; case L'\\': ++it; break; case L't': *it = L'\t'; break; case L'b': *it = L'\b'; break; case L'x': { string_type::iterator b = it; if (std::distance(++b, str.end()) >= 2) { char_type c1 = *b++, c2 = *b++; if (iswxdigit(c1) && iswxdigit(c2)) { *it++ = char_type((to_number(c1) << 4) | to_number(c2)); it = str.erase(it, b); } } break; } case L'u': { string_type::iterator b = it; if (std::distance(++b, str.end()) >= 4) { char_type c1 = *b++, c2 = *b++, c3 = *b++, c4 = *b++; if (iswxdigit(c1) && iswxdigit(c2) && iswxdigit(c3) && iswxdigit(c4)) { *it++ = char_type( (to_number(c1) << 12) | (to_number(c2) << 8) | (to_number(c3) << 4) | to_number(c4)); it = str.erase(it, b); } } break; } case L'U': { string_type::iterator b = it; if (std::distance(++b, str.end()) >= 8) { char_type c1 = *b++, c2 = *b++, c3 = *b++, c4 = *b++; char_type c5 = *b++, c6 = *b++, c7 = *b++, c8 = *b++; if (iswxdigit(c1) && iswxdigit(c2) && iswxdigit(c3) && iswxdigit(c4) && iswxdigit(c5) && iswxdigit(c6) && iswxdigit(c7) && iswxdigit(c8)) { *it++ = char_type( (to_number(c1) << 28) | (to_number(c2) << 24) | (to_number(c3) << 20) | (to_number(c4) << 16) | (to_number(c5) << 12) | (to_number(c6) << 8) | (to_number(c7) << 4) | to_number(c8)); it = str.erase(it, b); } } break; } default: { if (*it >= L'0' && *it <= L'7') { string_type::iterator b = it; int c = (*b++) - L'0'; if (*b >= L'0' && *b <= L'7') c = c * 8 + (*b++) - L'0'; if (*b >= L'0' && *b <= L'7') c = c * 8 + (*b++) - L'0'; *it++ = char_type(c); it = str.erase(it, b); } break; } } } } }
bool is_escape(Char e) { char_eq f(e); return std::find_if(escape_.begin(),escape_.end(),f)!=escape_.end(); }
PrintfState<string_type&, char_type> SPrintf(string_type& s, const string_type& format) { return PrintfState<string_type&, char_type>(s, format.c_str()); }
bool is_c(Char e) { char_eq f(e); return std::find_if(c_.begin(),c_.end(),f)!=c_.end(); }
//------------------------------------------------------------------ void index_storage::extract_element_content(const element& e, string_type& ret) const { ret.assign(e.content(), e.content_len()); clean_and_trim_string(ret); }
bool is_quote(Char e) { char_eq f(e); return std::find_if(quote_.begin(),quote_.end(),f)!=quote_.end(); }
/*! * The method finds the attribute value by name. * * \param key Attribute name. * \return Iterator to the found element or \c end() if the attribute with such name is not found. */ const_iterator find(string_type const& key) const { return find_impl(key.data(), key.size()); }
explicit matches_predicate(string_type const& operand) : m_operand(regex_type::compile(operand.c_str(), operand.size(), regex_type::ECMAScript | regex_type::optimize)) { }
explicit predicate(RelationT const& rel, string_type const& operand) : RelationT(rel), m_operand(regex_type::compile(operand.c_str(), operand.size(), regex_type::ECMAScript | regex_type::optimize)) { }
/// TODO: Check that the uploaded file isn't empty too. bool empty() const { return value.empty(); }
inline void write(char_array src, string_type & dest ) { const char_type * end = src; for ( ; *end; ++end) {} dest.insert( dest.begin(), src, end); }
size_type get(OtherString const& str) const { return boost::lexical_cast<size_type>(str.c_str() + std::wcslen(prefix_.c_str())); }