void StringAppendVT(const CharType *format, va_list ap, std::basic_string<CharType> &output) { CharType stack_buffer[1024]; /* first, we try to finish the task using a fixed-size buffer in the stack */ va_list ap_copy; GG_VA_COPY(ap_copy, ap); int result = vsnprintfT(stack_buffer, COUNT_OF(stack_buffer), format, ap_copy); va_end(ap_copy); if (result >= 0 && result < static_cast<int>(COUNT_OF(stack_buffer))) { /* It fits */ output.append(stack_buffer, result); return; } /* then, we have to repeatedly increase buffer size until it fits. */ int buffer_size = COUNT_OF(stack_buffer); std::basic_string<CharType> heap_buffer; for (;;) { if (result != -1) { assert(0); return; /* not expected, result should be -1 here */ } buffer_size <<= 1; /* try doubling the buffer size */ if (buffer_size > 32 * 1024 * 1024) { assert(0); return; /* too long */ } /* resize */ heap_buffer.resize(buffer_size); /* * NOTE: You can only use a va_list once. Since we're in a while loop, we * need to make a new copy each time so we don't use up the original. */ GG_VA_COPY(ap_copy, ap); result = vsnprintfT(&heap_buffer[0], buffer_size, format, ap_copy); va_end(ap_copy); if ((result >= 0) && (result < buffer_size)) { /* It fits */ output.append(&heap_buffer[0], result); return; } } }
void AppendCharIfAbsent( std::basic_string<_Elem, _Traits, _Ax>& str, _Elem c) { typedef std::basic_string<_Elem, _Traits, _Ax> string_type; string_type::reverse_iterator r_iter = str.rbegin(); if(*r_iter != c) str.append(1, c); }
void TokenizeBody (SBody *p_soBody, std::basic_string<unsigned char> &p_strBin, SDoc &p_soDoc) { SBody *psoBody; STokenInd soTokenInd; std::map<STokenInd, std::map<u_int8, u_int8>>::iterator iterTokenizer; std::map<u_int8, u_int8>::iterator iterToken; psoBody = p_soBody; while (psoBody) { TokenizeElement (psoBody, p_strBin, p_soDoc); if (psoBody->m_psoAttrList) { TokinizeAttribute (psoBody->m_psoAttrList, p_strBin, p_soDoc); p_strBin.append ((unsigned char*)"\x01", 1); } if (psoBody->m_psoChildList) { TokenizeElement (psoBody->m_psoChildList, p_strBin, p_soDoc); p_strBin.append ((unsigned char*)"\x01", 1); } psoBody = psoBody->m_psoNext; } }
template<typename T, typename O> typename Fsm<T, O>::State Fsm<T, O>::getNextLinearSequence(State startState, std::basic_string<Char>& input, BitSequence<Token>& output) const { State state = startState; for (; state < getNumberOfStates() && transition_table[state].size() == 1; ++state) { input.append(1, transition_table[state].begin()->first); if (!transition_table[state].begin()->second.output.empty() || transition_table[state].begin()->second.nextState != state + 1) { output = transition_table[state].begin()->second.output; state = transition_table[state].begin()->second.nextState; break; } } return state; }
/******************************************************************************* ** ** Function: nativeNfcTag_doTransceiveStatus ** ** Description: Receive the completion status of transceive operation. ** status: operation status. ** buf: Contains tag's response. ** bufLen: Length of buffer. ** ** Returns: None ** *******************************************************************************/ void nativeNfcTag_doTransceiveStatus (tNFA_STATUS status, uint8_t* buf, uint32_t bufLen) { SyncEventGuard g (sTransceiveEvent); ALOGD ("%s: data len=%d", __FUNCTION__, bufLen); if (!sWaitingForTransceive) { ALOGE ("%s: drop data", __FUNCTION__); return; } sRxDataStatus = status; if (sRxDataStatus == NFA_STATUS_OK || sRxDataStatus == NFA_STATUS_CONTINUE) sRxDataBuffer.append (buf, bufLen); if (sRxDataStatus == NFA_STATUS_OK) sTransceiveEvent.notifyOne (); }
inline void serializeString(SF::Archive & ar, std::basic_string<C,T,A> & s) { if (ar.isRead()) { boost::uint32_t count = 0; ar & count; SF::IStream &is = *ar.getIstream(); s.resize(0); std::size_t minSerializedLength = sizeof(C); if (ar.verifyAgainstArchiveSize(count*minSerializedLength)) { if (count > s.capacity()) { s.reserve(count); } } boost::uint32_t charsRemaining = count; const boost::uint32_t BufferSize = 512; C buffer[BufferSize]; while (charsRemaining) { boost::uint32_t charsToRead = RCF_MIN(BufferSize, charsRemaining); boost::uint32_t bytesToRead = charsToRead*sizeof(C); RCF_VERIFY( is.read( (char *) buffer, bytesToRead) == bytesToRead, RCF::Exception(RCF::_SfError_ReadFailure())) (bytesToRead)(BufferSize)(count); s.append(buffer, charsToRead); charsRemaining -= charsToRead; } } else if (ar.isWrite()) { boost::uint32_t count = static_cast<boost::uint32_t >(s.length()); ar & count; ar.getOstream()->writeRaw( (char *) s.c_str(), count*sizeof(C)); } }
void FormatMessageGLE(std::basic_string<wchar_t, Traits, Alloc>& out, int code) { wchar_t* lpMsgBuf(0); FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, 0, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lpMsgBuf, 0, NULL); if(lpMsgBuf) { out = lpMsgBuf; LocalFree(lpMsgBuf); } else { out = L"Unknown error: "; wchar_t temp[50]; _itow(code, temp, 50); out.append(temp); } }
inline void code_convert(const CharT* str1, std::size_t len, std::basic_string< CharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& = std::locale()) { str2.append(str1, len); }
inline void code_convert(std::basic_string< CharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< CharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& = std::locale()) { str2.append(str1.c_str(), str1.size()); }
void TokinizeAttribute (SBody *p_psoBody, std::basic_string<unsigned char> &p_strBin, SDoc &p_soDoc) { static u_int8 ui8CodePage = 0; SBody *psoBody; STokenInd soTokenInd; std::map<STokenInd, std::map<u_int8, u_int8>>::iterator iterTokenizer; std::map<u_int8, u_int8>::iterator iterToken; SWBXMLToken soToken = { 0 }; psoBody = p_psoBody; while (psoBody) { /* инициализируем структуру для поиска токена */ soTokenInd.m_strType = psoBody->m_strType; soTokenInd.m_strName = psoBody->m_strName; soTokenInd.m_strValue = psoBody->m_strValue; /* запрашиваем нобходимый токен */ iterTokenizer = g_mapTokenizer.find (soTokenInd); /* если токен не найден */ if (iterTokenizer == g_mapTokenizer.end () && soTokenInd.m_strValue.length ()) { soTokenInd.m_strValue = ""; /* ищем токен повторно для пустого значения */ iterTokenizer = g_mapTokenizer.find (soTokenInd); } /* если токен так и не найден */ if (iterTokenizer == g_mapTokenizer.end ()) { /* LITERAL */ soToken.m_uiToken = 0x04; p_strBin.append (&soToken.m_ui8Value, sizeof (soToken)); /* определяем индекс литерала */ std::basic_string<mb_u_int32> strInd; CreateLiteral (p_psoBody, p_soDoc, strInd); for (unsigned int i = 0; i < strInd.length (); i++) p_strBin.append (&(strInd[i].m_uiValue), 1); } else { /* формируем токен */ iterToken = iterTokenizer->second.find (ui8CodePage); /* если значение, соответствующее текущей кодовой странице, найдено */ if (iterToken == iterTokenizer->second.end ()) { iterToken = iterTokenizer->second.begin (); ui8CodePage = iterToken->first; p_strBin.append ((unsigned char*)"\x00", 1); p_strBin.append ((unsigned char*)&(iterToken->first), 1); } soToken.m_ui8Value = iterToken->second; /* формируем токен */ p_strBin.append (&soToken.m_ui8Value, sizeof (soToken)); } /* если значение не токенизировано */ if (psoBody->m_strValue.length () && 0 == soTokenInd.m_strValue.length ()) { /* ищем токенизированное значение */ soTokenInd.m_strType = "value"; soTokenInd.m_strName = psoBody->m_strValue; soTokenInd.m_strValue = ""; iterTokenizer = g_mapTokenizer.find (soTokenInd); /* если подходящий токен не найден */ if (iterTokenizer == g_mapTokenizer.end ()) { p_strBin.append ((unsigned char*)"\x03", 1); p_strBin.append ((unsigned char*)psoBody->m_strValue.c_str (), psoBody->m_strValue.length () + 1); } else { iterToken = iterTokenizer->second.find (ui8CodePage); if (iterToken == iterTokenizer->second.end ()) { iterToken = iterTokenizer->second.begin (); ui8CodePage = iterToken->first; p_strBin.append ((unsigned char*)"\x00", 1); p_strBin.append ((unsigned char*)&(iterToken->first), 1); } soToken.m_ui8Value = iterToken->second; p_strBin.append (&soToken.m_ui8Value, 1); } } psoBody = psoBody->m_psoNext; } }
bool join_sub_string ( const basic_cstring_view<CharT, Traits>& in, typename basic_cstring_view<CharT, Traits>::size_type& pos, std::basic_string<CharT, Traits, Allocator>& out, InputIterator valuesBegin, InputIterator valuesEnd, bool optional ) { using string_type = std::basic_string<CharT, Traits, Allocator>; using size_type = typename string_type::size_type; bool escapeChar = false; bool replacedAllValues = true; const auto numValues = static_cast<size_type>(std::distance(valuesBegin, valuesEnd)); if (numValues < 0) throw std::invalid_argument("reversed range iterators in join_string"); for (auto num = in.size(); pos < num;) { /* Get next character */ auto c = in[pos++]; if (escapeChar) { /* Add character without transformation to output string */ out.push_back(c); escapeChar = false; } else { if (c == CharT('\\')) { /* Next character will be added without transformation */ escapeChar = true; } else if (c == CharT('{')) { /* Parse index N in '{N}' */ string_type idxStr; while (pos < num) { /* Get next character */ c = in[pos++]; if (c == CharT('}')) break; else idxStr.push_back(c); } /* Get value by index from array */ const auto idx = static_cast<size_type>(std::stoul(idxStr)); if (idx < numValues) { /* Append value to output string */ const auto& val = *(valuesBegin + idx); if (string_empty(val)) replacedAllValues = false; else string_append(out, val); } else if (optional) { /* This sub string will not be added to the final output string */ replacedAllValues = false; } else { /* If this value replacement was not optional -> error */ throw std::out_of_range( "index (" + std::to_string(idx) + ") out of range [0, " + std::to_string(numValues) + ") in join_string" ); } } else if (c == CharT('[')) { /* Parse optional part with recursive call */ string_type outOpt; if (join_sub_string(in, pos, outOpt, valuesBegin, valuesEnd, true)) out.append(outOpt); } else if (c == CharT(']')) { /* Close optional part and return from recursive call */ break; } else { /* Add current character to output string */ out.push_back(c); } } } if (escapeChar) throw std::invalid_argument("incomplete escape character in report string"); return replacedAllValues; }