void utf8truncate(std::string& utf8str, size_t len) { try { size_t wlen = utf8::distance(utf8str.c_str(), utf8str.c_str() + utf8str.size()); if (wlen <= len) return; std::wstring wstr; wstr.resize(wlen); utf8::utf8to16(utf8str.c_str(), utf8str.c_str() + utf8str.size(), &wstr[0]); wstr.resize(len); char* oend = utf8::utf16to8(wstr.c_str(), wstr.c_str() + wstr.size(), &utf8str[0]); utf8str.resize(oend - (&utf8str[0])); // remove unused tail } catch (std::exception) { utf8str = ""; } }
void send_response(newsflash::native_socket_t sock, std::string resp, bool print = true) { resp.append("\r\n"); int sent = 0; do { const char* ptr = resp.data() + sent; const auto len = resp.size() - sent; const auto bytes = ::send(sock, ptr, len, 0); if (bytes < 0) throw std::runtime_error("socket send error"); sent += bytes; } while(sent != resp.size()); if (print) { resp.resize(resp.size() - 2); std::cout << "< " << resp << std::endl; } }
bool GetField(std::istream &is, std::string &name, std::string &value) { name.resize(0); // GCC workaround: 2.95.3 doesn't have clear() is >> name; #if defined(__COVERITY__) // The datafile being read is in /usr/share, and it protected by filesystem ACLs // __coverity_tainted_data_sanitize__(reinterpret_cast<void*>(&name)); #endif if (name.empty()) return false; if (name[name.size()-1] != ':') { char c; is >> skipws >> c; if (c != ':') SignalTestError(); }
/** remove_comments * * Removes all comments. */ void remove_comments(std::string& line, std::string& comment) { int width = 0; /// @todo meh make this code better. // Handling lines with only comments bool word = false; for (size_t i = 0; i < line.size(); i++) { if (line[i] == ';' || line[i] == '\n' || line[i] == '\r') break; if (isalpha(line[i])) word = true; width++; } size_t comment_begin = line.find(';'); if (comment_begin != std::string::npos) comment = line.substr(comment_begin); line.resize(word ? width : 0); }
float FoldingEngine<SEQ>:: decode_structure(float gamma, std::string& paren) const { float p=0.0; paren.resize(bp_.size()); std::fill(paren.begin(), paren.end(), '.'); if (!mea_) { if (max_bp_dist_==0) p = Centroid::execute(bp_, paren, gamma); else p = Centroid::execute(bp_, paren, max_bp_dist_, gamma); } else { if (max_bp_dist_==0) p = MEA::execute(bp_, paren, gamma); else p = MEA::execute(bp_, paren, max_bp_dist_, gamma); } return p; }
TextDirection GetTextDirection( std::string locale ) { TextDirection direction( LeftToRight ); if ( !locale.empty() && locale.size() > 2 ) { // We're only interested in the first two characters locale.resize(2); for ( const LocaleDirection* iter = &LOCALE_DIRECTION_LOOKUP_TABLE[0]; iter->locale; ++iter ) { if ( !locale.compare( iter->locale ) ) { direction = iter->direction; break; } } } return direction; }
/* check if a charset is known by Iconv */ bool servlist_check_encoding(std::string charset) { auto space = charset.find_first_of(' '); if (space != std::string::npos) charset.resize(space); if (!g_ascii_strcasecmp (charset.c_str(), "IRC")) /* special case */ { return true; } auto gic = g_iconv_open (charset.c_str(), "UTF-8"); if (gic != (GIConv)-1) { g_iconv_close (gic); return true; } return false; }
const std::string & Parser::_getNextToken(unsigned int & fromPosition) { fromPosition = _skipWhiteSpacesFromContent(fromPosition); unsigned int position = _fileContent.find_first_of(WHITESPACES, fromPosition); static std::string token; if(position > fromPosition) { unsigned int tokenSize = position - fromPosition; token.resize(tokenSize); memcpy(&token[0], &_fileContent[fromPosition], tokenSize); fromPosition = position; return token; } else { //TODO throw exception } token = ""; return token; }
///--------------------------------------------------------------------------------- /// ///--------------------------------------------------------------------------------- bool LoadTextFileToExistingString( const std::string& filePath, std::string& existingString ) { FILE* file; fopen_s( &file, filePath.c_str(), "rb" ); if (!file) { // freak out return false; } size_t neededStringSize = GetFileLength( file ); // Grow/shrink existingString.resize( neededStringSize ); fread( (void*)existingString.data(), sizeof( unsigned char ), neededStringSize, file ); fclose( file ); return true; }
// HINT: This information remains static throughout the object's lifetime inline VmbErrorType Interface::GetSerialNumber( std::string &rStrSerial ) const { VmbErrorType res; VmbUint32_t nLength; res = GetSerialNumber( NULL, nLength ); if ( VmbErrorSuccess == res ) { if ( 0 < nLength ) { rStrSerial.resize( nLength ); res = GetSerialNumber( &rStrSerial[0], nLength ); } else { rStrSerial.clear(); } } return res; }
void removeTrailingZeros(std::string& s) { if(s.find('.') != std::string::npos) { size_t newLength = s.size() - 1; while(s[newLength] == '0') { newLength--; } if(s[newLength] == '.') { newLength--; } if(newLength != s.size() - 1) { s.resize(newLength + 1); } } }
bool SpFileReader::ReadString(std::string &Str) { /* Read string length */ uint32 Len = Read<uint32>(); if (Len == 0) { Str = ""; return true; } /* Check if string is too long */ if (Len + GetPosition() >= Size_) return false; /* Read string characters */ Str.resize(Len); Read(&Str[0], Len); return true; }
// HINT: This information remains static throughout the object's lifetime inline VmbErrorType Interface::GetName( std::string &rStrName ) const { VmbErrorType res; VmbUint32_t nLength; res = GetName( NULL, nLength ); if ( VmbErrorSuccess == res ) { if ( 0 < nLength ) { rStrName.resize( nLength ); res = GetName( &rStrName[0], nLength ); } else { rStrName.clear(); } } return res; }
bool QIO::getFileContent(std::string fileName, std::string &content) { std::ifstream file(fileName.c_str()); if (!file) { std::cerr << " > ERROR: unable to open input file: \"" << fileName << "\"." << std::endl; return false; } file.seekg(0, std::ios::end); int length = file.tellg(); file.seekg(0, std::ios::beg); // content.reserve(length); // content.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()); content.resize(length); file.read((char*)content.data(), length); file.close(); return true; }
// ---------------------------------------------------------------------------------------------- // returns false if couldn't read a string containing at least one char bool EDITOR::readStringFromPatternsFile(EMUFILE *is, std::string& dest) { dest.resize(0); int charr; while (true) { charr = is->fgetc(); if (charr < 0) break; if (charr == 10 || charr == 13) // end of line { if (dest.size()) break; // already collected at least one char else continue; // skip the char and continue searching } else { dest.push_back(charr); } } return dest.size() != 0; }
/*! * \brief Receive message from a specific node rank via MPI * \param outMessage Message receive. * \param inTag Tag associated to the message to be received. * \param inRank Node rank of the sending node. */ void HPC::MPICommunication::receive(std::string& outMessage, const std::string& inTag, int inRank) const { Beagle_StackTraceBeginM(); MPI::Status lStatus; int lSize = 0; MPI::COMM_WORLD.Recv(&lSize, 1, MPI::INT, inRank, hashTag(inTag+"_size")); MPI::COMM_WORLD.Probe(inRank,hashTag(inTag+"_str"),lStatus); Beagle_AssertM(lStatus.Get_count(MPI::CHAR) == lSize); outMessage.resize(lSize); MPI::COMM_WORLD.Recv(&outMessage[0], lSize, MPI::CHAR, lStatus.Get_source(), hashTag(inTag+"_str")); #ifdef BEAGLE_HAVE_LIBZ if(mCompressionLevel->getWrappedValue() > 0){ std::string lString; decompressString(outMessage, lString); outMessage = lString; } #endif Beagle_HPC_StackTraceEndM("void HPC::MPICommunication::receive(std::string&, const std::string&, int) const"); }
void get_next(){ bool comment; do{ if(!std::getline(input, next_line)){ has_next = false; } else{ comment = false; next_line_idx++; // strip trailing carriage return, if any if(next_line.length() > 0 && *(next_line.rbegin()) == '\r') next_line.resize(next_line.size()-1); // convert to lowercase strlower(next_line); // Append a space, to catch blank comment lines (e.g. "c\n") that would otherwise not meet // the MCNP comment card spec ("a C anywhere in columns 1-5 followed by at least one blank.") // I have seen lines like "c\n" or " c\n" as complete comment cards in practice, so MCNP must // accept them. next_line.append(" "); // We want to find "c " within the first five // columns, but not if the c has anything other than a space before it. size_t idx = next_line.find("c "); if( idx < 5 ){ if( idx == 0 || next_line.at(idx-1) == ' '){ comment = true; } } } } while( has_next && comment ); // iterate until next_line is not a comment line. }
int ReadJobData(Feedback &fb, std::istream &s, const std::string &sChildOutputMode, std::string &sData) { std::stringstream ssMode(sChildOutputMode); std::string sMode1; ssMode >> sMode1; sData.clear(); if (sMode1 == "EOF") { std::string sLine, sEOF; if (!std::getline(s, sEOF)) return fb.Error(E_UTILS_READJOB) << ": Failed to read leading EOF mark. No more jobs?"; if (int nRet = ReadDelimData(fb, s, sData, "\n" + sEOF + "\n")) return nRet; } else if (sMode1 == "BYTES") { size_t nBytes; s.read(reinterpret_cast<char*>(&nBytes), sizeof(nBytes)); if (s.gcount() != sizeof(nBytes)) return fb.Error(E_UTILS_READJOB) << ": Failed to read byte count."; std::vector<char> v(nBytes); s.read(&(v[0]), nBytes); sData.resize(nBytes); std::copy(v.begin(), v.end(), sData.begin()); if (s.gcount() != nBytes) return fb.Error(E_UTILS_READJOB) << ": Failed to read " << nBytes << " bytes of job data."; } else if (sMode1 == "BIN-EOF") { int nTagLen; ssMode >> nTagLen; std::vector<char> tag(nTagLen); s.read(&tag[0], nTagLen); if (s.gcount() != nTagLen) return fb.Error(E_UTILS_READJOB) << ": Failed to read " << nTagLen << " bytes of leading binary eof tag."; if (int nRet = ReadDelimData(fb, s, sData, std::string(&tag[0]))) return nRet; }
void Georef::Forward(real lat, real lon, int prec, std::string& georef) { if (abs(lat) > 90) throw GeographicErr("Latitude " + Utility::str(lat) + "d not in [-90d, 90d]"); if (Math::isnan(lat) || Math::isnan(lon)) { georef = "INVALID"; return; } lon = Math::AngNormalize(lon); // lon in [-180,180) if (lat == 90) lat *= (1 - numeric_limits<real>::epsilon() / 2); prec = max(-1, min(int(maxprec_), prec)); if (prec == 1) ++prec; // Disallow prec = 1 // The C++ standard mandates 64 bits for long long. But // check, to make sure. GEOGRAPHICLIB_STATIC_ASSERT(numeric_limits<long long>::digits >= 45, "long long not wide enough to store 21600e9"); const long long m = 60000000000LL; long long x = (long long)(floor(lon * m)) - lonorig_ * m, y = (long long)(floor(lat * m)) - latorig_ * m; int ilon = int(x / m); int ilat = int(y / m); char georef1[maxlen_]; georef1[0] = lontile_[ilon / tile_]; georef1[1] = lattile_[ilat / tile_]; if (prec >= 0) { georef1[2] = degrees_[ilon % tile_]; georef1[3] = degrees_[ilat % tile_]; if (prec > 0) { x -= m * ilon; y -= m * ilat; long long d = (long long)pow(real(base_), maxprec_ - prec); x /= d; y /= d; for (int c = prec; c--;) { georef1[baselen_ + c ] = digits_[x % base_]; x /= base_; georef1[baselen_ + c + prec] = digits_[y % base_]; y /= base_; } } } georef.resize(baselen_ + 2 * prec); copy(georef1, georef1 + baselen_ + 2 * prec, georef.begin()); }
Bundle::TLineType Bundle::parseLine(const std::string &line, std::string &data) const { int startPos, endPos; TLineType type; data.resize(0); startPos = line.find_first_not_of("\t \r\n"); if ((startPos < 0) || (line.at(startPos) == '#')) return tCOMMENT; else if (line.at(startPos) == '"') { endPos = line.find_first_of('"', startPos+1); if (endPos < 0) endPos = line.length(); data = line.substr(startPos+1, endPos-startPos-1); return tAPPEND; } endPos = line.find_first_of("\t \r\n\""); if (endPos < 0) endPos = line.length(); std::string key = line.substr(startPos, endPos-startPos); if (key == "msgid") type = tMSGID; else if (key == "msgstr") type = tMSGSTR; else return tERROR; startPos = line.find_first_of('"', endPos + 1); if (startPos >= 0) { startPos++; endPos = line.find_first_of('"', startPos); if (endPos < 0) endPos = line.length(); data = line.substr( startPos, endPos-startPos); } return type; }
void CPinyinTrie::print(const TNode* pRoot, std::string& prefix, FILE *fp) const { static char buf[1024]; if (pRoot->m_nWordId > 0) { fprintf(fp, "%s", prefix.c_str()); if (pRoot->m_csLevel) fprintf(fp, "(GBK+)"); unsigned int sz = pRoot->m_nWordId; const TWordIdInfo *pwids = pRoot->getWordIdPtr(); for (unsigned int i = 0; i < sz; ++i) { unsigned int id = pwids[i].m_id; const TWCHAR *pw = operator[](id); int len = WCSLEN(pw); if (len != lengthAt(id)) { printf(" (lengthAt %d error) ", id); } WCSTOMBS(buf, pw, 1024); fprintf(fp, " %s", buf); if (pwids[i].m_bSeen == 0) fprintf(fp, "[x]"); else fprintf(fp, "[o]"); fprintf(fp, "(%d)", pwids[i].m_cost); } fprintf(fp, "\n"); } unsigned int sz = pRoot->m_nTransfer; const TTransUnit* ptrans = pRoot->getTrans(); for (unsigned int i = 0; i < sz; ++i) { unsigned s = ptrans[i].m_Syllable; const TNode *pch = transfer(pRoot, s); const char *str = CPinyinData::decodeSyllable(s); if (!str) break; prefix = prefix + str + '\''; print(pch, prefix, fp); prefix.resize(prefix.size() - strlen(str) - 1); } }
inline void append_printf_formatted_string(std::string& out, const char* format, TArgs&&... args) { // First try to write string with the max_size, if that doesn't // work snprintf will tell us how much space it needs. We // reserve that much space and try again. So this will always // work, even if the output is larger than the given max_size. // // Unfortunately this trick doesn't work on Windows, because // the _snprintf() function there only returns the length it // needs if max_size==0 and the buffer pointer is the null // pointer. So we have to take this into account. #ifndef _MSC_VER static const size_t max_size = 100; #else static const size_t max_size = 0; #endif size_t old_size = out.size(); int len = string_snprintf(out, old_size, max_size, format, std::forward<TArgs>(args)...); assert(len > 0); if (size_t(len) >= max_size) { int len2 = string_snprintf(out, old_size, size_t(len) + 1, format, std::forward<TArgs>(args)...); assert(len2 == len); } out.resize(old_size + size_t(len)); }
// Public service vehicle lanes and similar can introduce additional lanes into the lane string that // are not specifically marked for left/right turns. This function can be used from the profile to // trim the lane string appropriately // // left|throught| // in combination with lanes:psv:forward=1 // will be corrected to left|throught, since the final lane is not drivable. // This is in contrast to a situation with lanes:psv:forward=0 (or not set) where left|through| // represents left|through|through OSRM_ATTR_WARN_UNUSED inline std::string trimLaneString(std::string lane_string, std::int32_t count_left, std::int32_t count_right) { if (count_left) { bool sane = count_left < static_cast<std::int32_t>(lane_string.size()); for (std::int32_t i = 0; i < count_left; ++i) // this is adjusted for our fake pipe. The moment cucumber can handle multiple escaped // pipes, the '&' part can be removed if (lane_string[i] != '|') { sane = false; break; } if (sane) { lane_string.erase(lane_string.begin(), lane_string.begin() + count_left); } } if (count_right) { bool sane = count_right < static_cast<std::int32_t>(lane_string.size()); for (auto itr = lane_string.rbegin(); itr != lane_string.rend() && itr != lane_string.rbegin() + count_right; ++itr) { if (*itr != '|') { sane = false; break; } } if (sane) lane_string.resize(lane_string.size() - count_right); } return lane_string; }
// Reads the whole file to a string bool readFile( IrrlichtDevice* irr, const std::string& path, std::string& result) { intrusive_ptr<io::IReadFile> file(irr->getFileSystem()->createAndOpenFile(path.c_str()), false); if(!file.get()) return false; result.resize(file->getSize()); int bytesleft = file->getSize(); int bytesread = 0; while(bytesleft > 0) { int bs = file->read(&result[bytesread], bytesleft); bytesread += bs; bytesleft -= bs; } return true; }
size_t LoadFile(const std::string &filename, bool binary, std::string &buffer) { std::ifstream in(filename, binary ? std::ios::in | std::ios::binary : std::ios::in); if ( in.is_open() ) { in.seekg(0, std::ios::end); buffer.clear(); buffer.resize( in.tellg() ); in.seekg(0, std::ios::beg); in.read( &buffer[0], buffer.size() ); in.close(); utils::Log(CFormat(L"File '%%' (size: %% bytes):\n%%") << filename << buffer.size() << buffer, utils::ELogLevel::Debug); } else { throw std::runtime_error( utils::ws2s(CFormat(L"Can't open file '%%'") << filename) ); } return buffer.size(); }
LemminiLine(const std::string& line) : nr(0), x(0), y(0), flags(0) { size_t i = 0; // parse the name before the '='; while (i < line.size()) { const char c = line[i++]; if (c == '=') { while (! name.empty() && name[name.size() - 1] == ' ') name.resize(name.size() - 1); break; } else name += c; } // skip spaces after the '=' while (i < line.size() && line[i] == ' ') ++i; // parse str and the numbers at the same time int commas_seen = 0; bool minus = false; while (i < line.size()) { const char c = line[i++]; str += c; int* iptr = commas_seen == 0 ? &nr : commas_seen == 1 ? &x : commas_seen == 2 ? &y : &flags; if (c == ',') { if (minus) *iptr *= -1; minus = false; ++commas_seen; } else if (c == '-') minus = true; else if (c >= '0' && c <= '9') { *iptr *= 10; *iptr += c - '0'; } } }
static bool ReadStringFromFile (const char* pathName, std::string& output) { # ifdef _MSC_VER wchar_t widePath[MAX_PATH]; int res = ::MultiByteToWideChar (CP_UTF8, 0, pathName, -1, widePath, MAX_PATH); if (res == 0) widePath[0] = 0; FILE* file = _wfopen(widePath, L"rb"); # else // ifdef _MSC_VER FILE* file = fopen(pathName, "rb"); # endif // !ifdef _MSC_VER if (file == NULL) return false; fseek(file, 0, SEEK_END); long length = ftell(file); fseek(file, 0, SEEK_SET); if (length < 0) { fclose( file ); return false; } output.resize(length); size_t readLength = fread(&*output.begin(), 1, length, file); fclose(file); if (readLength != length) { output.clear(); return false; } replace_string(output, "\r\n", "\n", 0); return true; }
bool ParseV2::File::read_line(std::string& line, bool& subdirective) { line.clear(); if (ifs_) { static char temp[100]; ifs_.getline(temp, sizeof(temp)); if (ifs_) { ++line_number_; line = temp; size_t n = line.find('#'); if (n != string::npos) { line.resize(n); } if (line.size()) { subdirective = isspace(line[0]); } return true; } } return false; }
bool input_stream::read_line(std::string& str) { if(fd_ == -1) { return false; } const size_t block_size = 4096; char block[block_size]; const size_t nbytes = read(fd_,block,block_size); std::copy(block,block+nbytes,std::back_inserter(data_)); const std::deque<char>::iterator itor = std::find(data_.begin(),data_.end(),'\n'); if(itor != data_.end()) { str.resize(itor - data_.begin()); std::copy(data_.begin(),itor,str.begin()); data_.erase(data_.begin(),itor+1); return true; } else { return false; } }
// Remove whitespace from end of string void MDFN_rtrim(std::string &string) { size_t len = string.length(); if(len) { size_t x = len; size_t new_len = len; do { x--; if(!(string[x] == ' ' || string[x] == '\r' || string[x] == '\n' || string[x] == '\t' || string[x] == 0x0b)) break; new_len--; } while(x); string.resize(new_len); } }