str::size_type extract_delimited_text(const str& in, const str& d1, const str& d2, str& out, size_t pos) { if(pos == str::npos) return pos; size_t end = pos; if((pos = in.find(d1, pos)) != str::npos) if((end = in.find(d2, (pos = pos + d1.size()))) != str::npos) { out = in.substr(pos, end - pos); return end + d2.size(); } return str::npos; }
bool RConImpl::rcon(const str& host, siz port, const str& cmd, str& reply) const { str_vec packets; if(!aocom(cmd, packets, host, port, TIMEOUT)) return false; const str header = "\xFF\xFF\xFF\xFFprint\x0A"; if(packets.empty()) { log("Empty response."); return false; } reply.clear(); for(const str& packet: packets) { if(packet.find(header) != 0) { log("Unrecognised response."); return false; } reply.append(packet.substr(header.size())); } return true; }
str strDecToStrBin(str dec){ str bin=""; int m=dec.size(); int i=0; while(dec.size()){ //printf("DEC %s\n", dec.c_str()); char l=dec.at(dec.size()-1); if(l=='0' || l=='2' || l=='4' || l=='6' || l=='8'){ //parzysta bin='0'+bin; }else{ bin='1'+bin; } dec=divideByTwo(dec); if(dec.at(0)=='0'){dec.erase(0,1);} } return bin; }
str operator+(str const &self, char const(&other)[N]) { std::string s; s.reserve(self.size() + N); s += self.get_data(); s += other; return {std::move(s)}; }
str operator+(char const(&self)[N], str const &other) { std::string s; s.reserve(other.size() + N); s += self; s += other.get_data(); return {std::move(s)}; }
uni ch(str in) { int a = 0, b = 0, c = 0, d = 0; int uk = 0; while (uk < in.size() && in[uk]!='.') a = a * 10 +(int)(in[uk] - '0'), uk++; uk++; while (uk < in.size() && in[uk]!='.') b = b * 10 +(int)(in[uk] - '0'), uk++; uk++; while (uk < in.size() && in[uk]!='.') c = c * 10 +(int)(in[uk] - '0'), uk++; uk++; while (uk < in.size() && in[uk]!='.') d = d * 10 +(int)(in[uk] - '0'), uk++; unsigned int rrr = d + c * 256 + b * 256*256 + a * 256*256*256; return rrr; }
void binaryNumberToCode(str bin){ vec<str> v; addCodeLine("ZERO"); //printf("ZMIANA\n"); while(bin!="0"){ //printf("BIN %s\n", bin.c_str()); char l=bin.at(bin.size()-1); if(l=='1'){ v.push_back("INC"); bin.at(bin.size()-1)='0'; }else if(l=='0'){ v.push_back("SHL"); bin.erase(bin.size()-1, 1); } } for(int i=v.size()-1; i>=0; i--){ addCodeLine(v.at(i)); } }
str get_filename_from_pathname(const str& pathname) { auto pos = pathname.find_last_of('/'); if(pos == pathname.size() - 1) throw std::runtime_error("bad pathname: " + pathname); else if(pos != str::npos) return pathname.substr(pos + 1); return pathname; }
uint fwrite(const str& s, const path& p, const bool append, const bool binary) { FILE* fp; uint size=s.size(); const char* buf=s.tocstr(); fp=fopen(p.tocstr(), append?(binary?"ab":"at"):(binary?"wb":"wt")); assert(fp);//File error: Could not open file for write size=fwrite(buf, 1, size, fp); fclose(fp); return size; }
str divideByTwo(str dec){ int rem=0; int m=dec.size(); for(int i=0; i<m; i++){ int a=dec.at(i)-48; //cyfra dec.at(i)=(rem+a)/2+48; if(a%2){rem=10;}else{rem=0;} } //printf("DZIEL %s\n", dec.c_str()); return dec; }
void network_socket::_addressBufferToString(str& s) const { size_type i; s.resize(20); inet_ntop(AF_INET,&reinterpret_cast<const sockaddr_in*>(_addrbuf)->sin_addr,&s[0],s.size()); i = 0; while (i < s.size()) { if (s[i] == 0) break; ++i; } s.resize(i); }
/************************************************* S.rfind(sub [,start [,end]]) -> int Search from right to left. Return the highest index in S where substring sub is found, such that sub is contained within s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. *************************************************/ int str::rfind(const str& sub, int start, int end) const { uint l=size(); if (start<0) start+=l; if (end<=0) end+=l; start=l-start; end=l-end; assert(start>0); assert(end>=0); criterator a=s.rbegin()+end, b=s.rbegin()+start; if (a>=b) return -1; criterator c=std::search(a, b, sub.rbegin(), sub.rend()); return (c==b)?-1:(b-c)-sub.size(); }
void MenuItem::SetText(const str& s) { if (s.size()==0) // we cannot have a string with no size _text = "Item"; else _text = s; // store text in our own buffer in anticipation that the item is going to be added to another item or bar if (_parentItem!=0) // (I can use either _parentItem or _parentBar since they both refer to the same memory address) {// the item has a parent, so we need to call an API routine to change the text HMENU hTopLevel; // not the absolute top level, but a level above this menu hTopLevel = (_parentState ? _parentItem->_hSubMenu : _parentBar->GetMenuHandle()); // modify text only ModifyMenu(hTopLevel,*this,MF_BYCOMMAND | MF_STRING,*this,&_text[0]); } // if the item doesn't have a parent, then this item's text will be added when the item is added to }
bool isNumeric(const str& token) { if(token == "") return false; for(uint i=0; i<token.length(); i++) { const char num = token.at(i); if(i == 0 && Util::toString(num) == kSubtract() && token.size() > 1) //token.size > 1 because "-" is not a number continue; if(isdigit(num) == false && Util::toString(num) != kDot()) return false; } return true; }
/** * IPv4 IPv6 agnostic OOB (out Of Band) comms * @param cmd * @param packets Returned packets * @param host Host to connect to * @param port Port to connect on * @param wait Timeout duration in milliseconds * @return false if failed to connect/send or receive else true */ bool aocom(const str& cmd, str_vec& packets, const str& host, int port , siz wait = TIMEOUT) { addrinfo hints; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 hints.ai_socktype = SOCK_DGRAM; addrinfo* res; if(int status = getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &res) != 0) { log(gai_strerror(status)); return false; } st_time_point timeout = st_clk::now() + std::chrono::milliseconds(wait); // try to connect to each int cs; addrinfo* p; for(p = res; p; p = p->ai_next) { if((cs = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) continue; if(!connect(cs, p->ai_addr, p->ai_addrlen)) break; ::close(cs); } freeaddrinfo(res); if(!p) { log("aocom: failed to connect: " << host << ":" << port); return false; } // cs good const str msg = "\xFF\xFF\xFF\xFF" + cmd; int n = 0; if((n = send(cs, msg.c_str(), msg.size(), 0)) < 0 || n < (int)msg.size()) { log("cs send: " << strerror(errno)); return false; } packets.clear(); char buf[2048]; n = sizeof(buf); while(n == sizeof(buf)) { while((n = recv(cs, buf, sizeof(buf), MSG_DONTWAIT)) == -1 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) { if(st_clk::now() > timeout) { log("socket timed out connecting to: " << host << ":" << port); return false; } // std::this_thread::yield(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); } if(n < 0) log("cs recv: " << strerror(errno)); if(n > 0) packets.push_back(str(buf, n)); } close(cs); return true; }
bool operator!=(char c, str const &s) { return s.size() != 1 or s[0] != c; }
bool operator!=(str const &s, char c) { return s.size() != 1 or s[0] != c; }
bool operator==(char c, str const &s) { return s.size() == 1 and s[0] == c; }
bool operator==(str const &s, char c) { return s.size() == 1 and s[0] == c; }
str pad(str const& s, size_t n = 12) { if(s.size() < n) return s + str(n - s.size(), ' '); return s; }
void property_value(const str& value) { current_.add_prop( propname_, value.substr(1, value.size() - 2) ); }
void disp(str v) { for (int i=0; i<v.size(); i++) cout << v[i] << endl; cout << endl; }
void ser_str(const str &v) { ser_u32(v.size()); crc32(v); for (const auto &ch : v) put8(ch); }
void init_time(Time& t, const str s) { t.s = get_time(); if (s.size() != 0) std::cout << s << std::endl; }
str format(int size, str s) { str S = s; str sp = ""; int n = s.size(); if (size>n) for (int h=0; h<size-n; h++) sp.push_back(' '); return sp+s; }
str erase_spaces(str s) { str ss = ""; for (int h=0; h<s.size(); h++) if (s[h]!=' ') ss.push_back(s[h]); return ss; }