Ejemplo n.º 1
0
void break_text_lines(font_ptr font, xstring text, int total_width, str_vec& lines)
{
  lines.clear();
  int_vec breaks = calculate_text_breaks(text);
  unsigned break_pos = 0;
  xstring cand = "";
  while (text.length() > 0 && break_pos<breaks.size())
  {
    xstring sub = text.substr(0, breaks[break_pos]);
    Rect bounds = font->get_bounds(sub);
    if (bounds.get_width() > total_width)
    {
      if (cand.empty()) return; // Not enough space for single word
      lines.push_back(cand);
      text = text.substr(breaks[break_pos - 1]);
      text.trim();
      breaks = calculate_text_breaks(text);
      break_pos = 0;
    }
    else
    {
      cand = text.substr(0, breaks[break_pos]);
      break_pos++;
    }
  }
  if (text.length() > 0) lines.push_back(text.trim());
}
Ejemplo n.º 2
0
static void splitStr(std::string &str, char c, str_vec &split)
{
    split.clear();
    size_t p = 0, q = 0;
    while ( ( p = str.find(c, q) ) != std::string::npos ) {
        split.push_back( str.substr(q, p - q) );
        q = p + 1;
    }
    if ( q < str.length() ) {
        split.push_back( str.substr(q, str.length() - q) );
    }
}
Ejemplo n.º 3
0
/**
 * 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;
}