<commit_before>/* mopub_exchange_connector.cc
   Jeremy Barnes, 15 March 2013

   Implementation of the MoPub exchange connector.
*/
#include <algorithm>
#include <boost/tokenizer.hpp>

#include "mopub_exchange_connector.h"
#include "rtbkit/plugins/bid_request/openrtb_bid_request.h"
#include "rtbkit/plugins/exchange/http_auction_handler.h"
#include "rtbkit/core/agent_configuration/agent_config.h"
#include "rtbkit/openrtb/openrtb_parsing.h"
#include "soa/types/json_printing.h"
#include <boost/any.hpp>
#include <boost/lexical_cast.hpp>
#include "jml/utils/file_functions.h"

#include "crypto++/blowfish.h"
#include "crypto++/modes.h"
#include "crypto++/filters.h"

using namespace std;
using namespace Datacratic;

namespace RTBKIT {

/*****************************************************************************/
/* MOPUB EXCHANGE CONNECTOR                                                */
/*****************************************************************************/

MoPubExchangeConnector::
MoPubExchangeConnector(ServiceBase & owner, const std::string & name)
    : OpenRTBExchangeConnector(owner, name) {
    this->auctionResource = "/auctions";
    this->auctionVerb = "POST";
}

MoPubExchangeConnector::
MoPubExchangeConnector(const std::string & name,
                       std::shared_ptr<ServiceProxies> proxies)
    : OpenRTBExchangeConnector(name, proxies) {
    this->auctionResource = "/auctions";
    this->auctionVerb = "POST";
}

ExchangeConnector::ExchangeCompatibility
MoPubExchangeConnector::
getCampaignCompatibility(const AgentConfig & config,
                         bool includeReasons) const {
    ExchangeCompatibility result;
    result.setCompatible();

    auto cpinfo = std::make_shared<CampaignInfo>();

    const Json::Value & pconf = config.providerConfig["mopub"];

    try {
        cpinfo->seat = Id(pconf["seat"].asString());
        if (!cpinfo->seat)
            result.setIncompatible("providerConfig.mopub.seat is null",
                                   includeReasons);
    } catch (const std::exception & exc) {
        result.setIncompatible
        (string("providerConfig.mopub.seat parsing error: ")
         + exc.what(), includeReasons);
        return result;
    }

    try {
        cpinfo->iurl = pconf["iurl"].asString();
        if (!cpinfo->iurl.size())
            result.setIncompatible("providerConfig.mopub.iurl is null",
                                   includeReasons);
    } catch (const std::exception & exc) {
        result.setIncompatible
        (string("providerConfig.mopub.iurl parsing error: ")
         + exc.what(), includeReasons);
        return result;
    }

    result.info = cpinfo;

    return result;
}

namespace {

using Datacratic::jsonDecode;

/** Given a configuration field, convert it to the appropriate JSON */
template<typename T>
void getAttr(ExchangeConnector::ExchangeCompatibility & result,
             const Json::Value & config,
             const char * fieldName,
             T & field,
             bool includeReasons) {
    try {
        if (!config.isMember(fieldName)) {
            result.setIncompatible
            ("creative[].providerConfig.mopub." + string(fieldName)
             + " must be specified", includeReasons);
            return;
        }

        const Json::Value & val = config[fieldName];

        jsonDecode(val, field);
    } catch (const std::exception & exc) {
        result.setIncompatible("creative[].providerConfig.mopub."
                               + string(fieldName) + ": error parsing field: "
                               + exc.what(), includeReasons);
        return;
    }
}

} // file scope

ExchangeConnector::ExchangeCompatibility
MoPubExchangeConnector::
getCreativeCompatibility(const Creative & creative,
                         bool includeReasons) const {
    ExchangeCompatibility result;
    result.setCompatible();

    auto crinfo = std::make_shared<CreativeInfo>();

    const Json::Value & pconf = creative.providerConfig["mopub"];
    std::string tmp;

    boost::char_separator<char> sep(" ,");

    // 1.  Must have mopub.attr containing creative attributes.  These
    //     turn into MoPubCreativeAttribute filters.
    getAttr(result, pconf, "attr", tmp, includeReasons);
    if (!tmp.empty())  {
        boost::tokenizer<boost::char_separator<char>> tok(tmp, sep);
        auto& ints = crinfo->attr;
        std::transform(tok.begin(), tok.end(),
        std::inserter(ints, ints.begin()),[&](const std::string& s) {
            return atoi(s.data());
        });
    }
    tmp.clear();

    // 2. Must have mopub.type containing attribute type.
    getAttr(result, pconf, "type", tmp, includeReasons);
    if (!tmp.empty())  {
        boost::tokenizer<boost::char_separator<char>> tok(tmp, sep);
        auto& ints = crinfo->type;
        std::transform(tok.begin(), tok.end(),
        std::inserter(ints, ints.begin()),[&](const std::string& s) {
            return atoi(s.data());
        });
    }
    tmp.clear();

    // 3. Must have mopub.cat containing attribute type.
    getAttr(result, pconf, "cat", tmp, includeReasons);
    if (!tmp.empty())  {
        boost::tokenizer<boost::char_separator<char>> tok(tmp, sep);
        auto& strs = crinfo->cat;
        copy(tok.begin(),tok.end(),inserter(strs,strs.begin()));
    }
    tmp.clear();

    // 4.  Must have mopub.adm that includes MoPub's macro
    getAttr(result, pconf, "adm", crinfo->adm, includeReasons);
    if (crinfo->adm.find("${AUCTION_PRICE:BF}") == string::npos)
        result.setIncompatible
        ("creative[].providerConfig.mopub.adm ad markup must contain "
         "encrypted win price macro ${AUCTION_PRICE:BF}",
         includeReasons);

    // 5.  Must have creative ID in mopub.crid
    getAttr(result, pconf, "crid", crinfo->crid, includeReasons);
    if (!crinfo->crid)
        result.setIncompatible
        ("creative[].providerConfig.mopub.crid is null",
         includeReasons);

    // 6.  Must have advertiser names array in mopub.adomain
    getAttr(result, pconf, "adomain", crinfo->adomain,  includeReasons);
    if (crinfo->adomain.empty())
        result.setIncompatible
        ("creative[].providerConfig.mopub.adomain is empty",
         includeReasons);

    // Cache the information
    result.info = crinfo;

    return result;
}
std::shared_ptr<BidRequest>
MoPubExchangeConnector::
parseBidRequest(HttpAuctionHandler & connection,
                const HttpHeader & header,
                const std::string & payload) {
    std::shared_ptr<BidRequest> res;

    // Check for JSON content-type
    if (header.contentType != "application/json") {
        connection.sendErrorResponse("non-JSON request");
        return res;
    }

    // Parse the bid request
    ML::Parse_Context context("Bid Request", payload.c_str(), payload.size());
    res.reset(OpenRtbBidRequestParser::parseBidRequest(context, exchangeName(), exchangeName()));

    // get restrictions enforced by MoPub.
    //1) blocked category
    std::vector<std::string> strv;
    for (const auto& cat: res->blockedCategories)
        strv.push_back(cat.val);
    res->restrictions.addStrings("blockedCategories", strv);

    //2) per slot: blocked type and attribute;
    std::vector<int> intv;
    for (auto& spot: res->imp) {
        for (const auto& t: spot.banner->btype) {
            intv.push_back (t.val);
        }
        spot.restrictions.addInts("blockedTypes", intv);
        intv.clear();
        for (const auto& a: spot.banner->battr) {
            intv.push_back (a.val);
        }
        spot.restrictions.addInts("blockedAttrs", intv);
    }
    return res;
}

void
MoPubExchangeConnector::
setSeatBid(Auction const & auction,
           int spotNum,
           OpenRTB::BidResponse & response) const {

    const Auction::Data * current = auction.getCurrentData();

    // Get the winning bid
    auto & resp = current->winningResponse(spotNum);

    // Find how the agent is configured.  We need to copy some of the
    // fields into the bid.
    const AgentConfig * config =
        std::static_pointer_cast<const AgentConfig>(resp.agentConfig).get();

    std::string en = exchangeName();

    // Get the exchange specific data for this campaign
    auto cpinfo = config->getProviderData<CampaignInfo>(en);

    // Put in the fixed parts from the creative
    int creativeIndex = resp.agentCreativeIndex;

    auto & creative = config->creatives.at(creativeIndex);

    // Get the exchange specific data for this creative
    auto crinfo = creative.getProviderData<CreativeInfo>(en);

    // Find the index in the seats array
    int seatIndex = 0;
    while(response.seatbid.size() != seatIndex) {
        if(response.seatbid[seatIndex].seat == cpinfo->seat) break;
        ++seatIndex;
    }

    // Create if required
    if(seatIndex == response.seatbid.size()) {
        response.seatbid.emplace_back();
        response.seatbid.back().seat = cpinfo->seat;
    }

    // Get the seatBid object
    OpenRTB::SeatBid & seatBid = response.seatbid.at(seatIndex);

    // Add a new bid to the array
    seatBid.bid.emplace_back();
    auto & b = seatBid.bid.back();

    // Put in the variable parts
    b.cid = Id(resp.agent);
    b.id = Id(auction.id, auction.request->imp[0].id);
    b.impid = auction.request->imp[spotNum].id;
    b.price.val = getAmountIn<CPM>(resp.price.maxPrice);
    b.adm = crinfo->adm;
    b.adomain = crinfo->adomain;
    b.crid = crinfo->crid;
    b.iurl = cpinfo->iurl;
}

template <typename T>
bool disjoint (const set<T>& s1, const set<T>& s2) {
    auto i = s1.begin(), j = s2.begin();
    while (i != s1.end() && j != s2.end()) {
        if (*i == *j)
            return false;
        else if (*i < *j)
            ++i;
        else
            ++j;
    }
    return true;
}
bool
MoPubExchangeConnector::
bidRequestCreativeFilter(const BidRequest & request,
                         const AgentConfig & config,
                         const void * info) const {
    const auto crinfo = reinterpret_cast<const CreativeInfo*>(info);

    // 1) we first check for blocked content categories
    const auto& blocked_categories = request.restrictions.get("blockedCategories");
    for (const auto& cat: crinfo->cat)
        if (blocked_categories.contains(cat)) {
            this->recordHit ("blockedCategory");
            return false;
        }

    // 2) now go throught the spots.
    for (const auto& spot: request.imp) {
        const auto& blocked_types = spot.restrictions.get("blockedTypes");
        for (const auto& t: crinfo->type)
            if (blocked_types.contains(t)) {
                this->recordHit ("blockedType");
                return false;
            }
        const auto& blocked_attr = spot.restrictions.get("blockedAttrs");
        for (const auto& a: crinfo->attr)
            if (blocked_attr.contains(a)) {
                this->recordHit ("blockedAttr");
                return false;
            }
    }

    return true;
}

} // namespace RTBKIT

namespace {
using namespace RTBKIT;

struct Init {
    Init() {
        ExchangeConnector::registerFactory<MoPubExchangeConnector>();
    }
} init;
}

<commit_msg>Fix AUCTION_PRICE macro<commit_after>/* mopub_exchange_connector.cc
   Jeremy Barnes, 15 March 2013

   Implementation of the MoPub exchange connector.
*/
#include <algorithm>
#include <boost/tokenizer.hpp>

#include "mopub_exchange_connector.h"
#include "rtbkit/plugins/bid_request/openrtb_bid_request.h"
#include "rtbkit/plugins/exchange/http_auction_handler.h"
#include "rtbkit/core/agent_configuration/agent_config.h"
#include "rtbkit/openrtb/openrtb_parsing.h"
#include "soa/types/json_printing.h"
#include <boost/any.hpp>
#include <boost/lexical_cast.hpp>
#include "jml/utils/file_functions.h"

#include "crypto++/blowfish.h"
#include "crypto++/modes.h"
#include "crypto++/filters.h"

using namespace std;
using namespace Datacratic;

namespace RTBKIT {

/*****************************************************************************/
/* MOPUB EXCHANGE CONNECTOR                                                */
/*****************************************************************************/

MoPubExchangeConnector::
MoPubExchangeConnector(ServiceBase & owner, const std::string & name)
    : OpenRTBExchangeConnector(owner, name) {
    this->auctionResource = "/auctions";
    this->auctionVerb = "POST";
}

MoPubExchangeConnector::
MoPubExchangeConnector(const std::string & name,
                       std::shared_ptr<ServiceProxies> proxies)
    : OpenRTBExchangeConnector(name, proxies) {
    this->auctionResource = "/auctions";
    this->auctionVerb = "POST";
}

ExchangeConnector::ExchangeCompatibility
MoPubExchangeConnector::
getCampaignCompatibility(const AgentConfig & config,
                         bool includeReasons) const {
    ExchangeCompatibility result;
    result.setCompatible();

    auto cpinfo = std::make_shared<CampaignInfo>();

    const Json::Value & pconf = config.providerConfig["mopub"];

    try {
        cpinfo->seat = Id(pconf["seat"].asString());
        if (!cpinfo->seat)
            result.setIncompatible("providerConfig.mopub.seat is null",
                                   includeReasons);
    } catch (const std::exception & exc) {
        result.setIncompatible
        (string("providerConfig.mopub.seat parsing error: ")
         + exc.what(), includeReasons);
        return result;
    }

    try {
        cpinfo->iurl = pconf["iurl"].asString();
        if (!cpinfo->iurl.size())
            result.setIncompatible("providerConfig.mopub.iurl is null",
                                   includeReasons);
    } catch (const std::exception & exc) {
        result.setIncompatible
        (string("providerConfig.mopub.iurl parsing error: ")
         + exc.what(), includeReasons);
        return result;
    }

    result.info = cpinfo;

    return result;
}

namespace {

using Datacratic::jsonDecode;

/** Given a configuration field, convert it to the appropriate JSON */
template<typename T>
void getAttr(ExchangeConnector::ExchangeCompatibility & result,
             const Json::Value & config,
             const char * fieldName,
             T & field,
             bool includeReasons) {
    try {
        if (!config.isMember(fieldName)) {
            result.setIncompatible
            ("creative[].providerConfig.mopub." + string(fieldName)
             + " must be specified", includeReasons);
            return;
        }

        const Json::Value & val = config[fieldName];

        jsonDecode(val, field);
    } catch (const std::exception & exc) {
        result.setIncompatible("creative[].providerConfig.mopub."
                               + string(fieldName) + ": error parsing field: "
                               + exc.what(), includeReasons);
        return;
    }
}

} // file scope

ExchangeConnector::ExchangeCompatibility
MoPubExchangeConnector::
getCreativeCompatibility(const Creative & creative,
                         bool includeReasons) const {
    ExchangeCompatibility result;
    result.setCompatible();

    auto crinfo = std::make_shared<CreativeInfo>();

    const Json::Value & pconf = creative.providerConfig["mopub"];
    std::string tmp;

    boost::char_separator<char> sep(" ,");

    // 1.  Must have mopub.attr containing creative attributes.  These
    //     turn into MoPubCreativeAttribute filters.
    getAttr(result, pconf, "attr", tmp, includeReasons);
    if (!tmp.empty())  {
        boost::tokenizer<boost::char_separator<char>> tok(tmp, sep);
        auto& ints = crinfo->attr;
        std::transform(tok.begin(), tok.end(),
        std::inserter(ints, ints.begin()),[&](const std::string& s) {
            return atoi(s.data());
        });
    }
    tmp.clear();

    // 2. Must have mopub.type containing attribute type.
    getAttr(result, pconf, "type", tmp, includeReasons);
    if (!tmp.empty())  {
        boost::tokenizer<boost::char_separator<char>> tok(tmp, sep);
        auto& ints = crinfo->type;
        std::transform(tok.begin(), tok.end(),
        std::inserter(ints, ints.begin()),[&](const std::string& s) {
            return atoi(s.data());
        });
    }
    tmp.clear();

    // 3. Must have mopub.cat containing attribute type.
    getAttr(result, pconf, "cat", tmp, includeReasons);
    if (!tmp.empty())  {
        boost::tokenizer<boost::char_separator<char>> tok(tmp, sep);
        auto& strs = crinfo->cat;
        copy(tok.begin(),tok.end(),inserter(strs,strs.begin()));
    }
    tmp.clear();

    // 4.  Must have mopub.adm that includes MoPub's macro
    getAttr(result, pconf, "adm", crinfo->adm, includeReasons);
    if (crinfo->adm.find("${AUCTION_PRICE}") == string::npos)
        result.setIncompatible
        ("creative[].providerConfig.mopub.adm ad markup must contain "
         "encrypted win price macro ${AUCTION_PRICE}",
         includeReasons);

    // 5.  Must have creative ID in mopub.crid
    getAttr(result, pconf, "crid", crinfo->crid, includeReasons);
    if (!crinfo->crid)
        result.setIncompatible
        ("creative[].providerConfig.mopub.crid is null",
         includeReasons);

    // 6.  Must have advertiser names array in mopub.adomain
    getAttr(result, pconf, "adomain", crinfo->adomain,  includeReasons);
    if (crinfo->adomain.empty())
        result.setIncompatible
        ("creative[].providerConfig.mopub.adomain is empty",
         includeReasons);

    // Cache the information
    result.info = crinfo;

    return result;
}
std::shared_ptr<BidRequest>
MoPubExchangeConnector::
parseBidRequest(HttpAuctionHandler & connection,
                const HttpHeader & header,
                const std::string & payload) {
    std::shared_ptr<BidRequest> res;

    // Check for JSON content-type
    if (header.contentType != "application/json") {
        connection.sendErrorResponse("non-JSON request");
        return res;
    }

    // Parse the bid request
    ML::Parse_Context context("Bid Request", payload.c_str(), payload.size());
    res.reset(OpenRtbBidRequestParser::parseBidRequest(context, exchangeName(), exchangeName()));

    // get restrictions enforced by MoPub.
    //1) blocked category
    std::vector<std::string> strv;
    for (const auto& cat: res->blockedCategories)
        strv.push_back(cat.val);
    res->restrictions.addStrings("blockedCategories", strv);

    //2) per slot: blocked type and attribute;
    std::vector<int> intv;
    for (auto& spot: res->imp) {
        for (const auto& t: spot.banner->btype) {
            intv.push_back (t.val);
        }
        spot.restrictions.addInts("blockedTypes", intv);
        intv.clear();
        for (const auto& a: spot.banner->battr) {
            intv.push_back (a.val);
        }
        spot.restrictions.addInts("blockedAttrs", intv);
    }
    return res;
}

void
MoPubExchangeConnector::
setSeatBid(Auction const & auction,
           int spotNum,
           OpenRTB::BidResponse & response) const {

    const Auction::Data * current = auction.getCurrentData();

    // Get the winning bid
    auto & resp = current->winningResponse(spotNum);

    // Find how the agent is configured.  We need to copy some of the
    // fields into the bid.
    const AgentConfig * config =
        std::static_pointer_cast<const AgentConfig>(resp.agentConfig).get();

    std::string en = exchangeName();

    // Get the exchange specific data for this campaign
    auto cpinfo = config->getProviderData<CampaignInfo>(en);

    // Put in the fixed parts from the creative
    int creativeIndex = resp.agentCreativeIndex;

    auto & creative = config->creatives.at(creativeIndex);

    // Get the exchange specific data for this creative
    auto crinfo = creative.getProviderData<CreativeInfo>(en);

    // Find the index in the seats array
    int seatIndex = 0;
    while(response.seatbid.size() != seatIndex) {
        if(response.seatbid[seatIndex].seat == cpinfo->seat) break;
        ++seatIndex;
    }

    // Create if required
    if(seatIndex == response.seatbid.size()) {
        response.seatbid.emplace_back();
        response.seatbid.back().seat = cpinfo->seat;
    }

    // Get the seatBid object
    OpenRTB::SeatBid & seatBid = response.seatbid.at(seatIndex);

    // Add a new bid to the array
    seatBid.bid.emplace_back();
    auto & b = seatBid.bid.back();

    // Put in the variable parts
    b.cid = Id(resp.agent);
    b.id = Id(auction.id, auction.request->imp[0].id);
    b.impid = auction.request->imp[spotNum].id;
    b.price.val = getAmountIn<CPM>(resp.price.maxPrice);
    b.adm = crinfo->adm;
    b.adomain = crinfo->adomain;
    b.crid = crinfo->crid;
    b.iurl = cpinfo->iurl;
}

template <typename T>
bool disjoint (const set<T>& s1, const set<T>& s2) {
    auto i = s1.begin(), j = s2.begin();
    while (i != s1.end() && j != s2.end()) {
        if (*i == *j)
            return false;
        else if (*i < *j)
            ++i;
        else
            ++j;
    }
    return true;
}
bool
MoPubExchangeConnector::
bidRequestCreativeFilter(const BidRequest & request,
                         const AgentConfig & config,
                         const void * info) const {
    const auto crinfo = reinterpret_cast<const CreativeInfo*>(info);

    // 1) we first check for blocked content categories
    const auto& blocked_categories = request.restrictions.get("blockedCategories");
    for (const auto& cat: crinfo->cat)
        if (blocked_categories.contains(cat)) {
            this->recordHit ("blockedCategory");
            return false;
        }

    // 2) now go throught the spots.
    for (const auto& spot: request.imp) {
        const auto& blocked_types = spot.restrictions.get("blockedTypes");
        for (const auto& t: crinfo->type)
            if (blocked_types.contains(t)) {
                this->recordHit ("blockedType");
                return false;
            }
        const auto& blocked_attr = spot.restrictions.get("blockedAttrs");
        for (const auto& a: crinfo->attr)
            if (blocked_attr.contains(a)) {
                this->recordHit ("blockedAttr");
                return false;
            }
    }

    return true;
}

} // namespace RTBKIT

namespace {
using namespace RTBKIT;

struct Init {
    Init() {
        ExchangeConnector::registerFactory<MoPubExchangeConnector>();
    }
} init;
}

<|endoftext|>


<commit_before>#include <string>
#include <sstream>
#include <iostream>
#include <time.h>
#include <stdio.h>

#include "aircotec.h"
#include "igc.h"

using namespace std;

#define BAUDRATE 57600

#define FLAG_FIX   0x1
#define FLAG_FIX3D 0x2

/* Maximum timeout in seconds for readline, during download */
#define DOWN_TIMEOUT 2

/* Read line from input & check the checksum */
/* Timeout specified in seconds, 0 means no timeout */
string AircotecGps::ac_readline(dt_callback cb, void *arg,
				int pktcount, int expcount,
				int timeout)
{
    time_t starttime = time(NULL);
  restart:
    if (timeout && time(NULL) - starttime > timeout)
	throw TimeoutException();

    string result;
    char ch;

    while (1) {
	if (cb) {
	    bool rv = cb(arg, pktcount, expcount);
	    if (!rv)
		throw Exception("Download cancelled");
	}
	try {
	    ch = dev->read();
	} catch (TimeoutException e) {
	    // TODO: Handle timeouts in some reasonable way
	    goto restart;
	}

	if (ch == '\n')
	    break;
	if (ch == '\r')
	    continue;
	result += ch;
    }
    if (result.size() < 2)
	goto restart;

    string cksum_s = result.substr(result.size()-2);
    int cksum_wr;
    if (sscanf(cksum_s.c_str(), "%2X", &cksum_wr) != 1)
	goto restart;
    int cksum = 0;
    for (unsigned int i=0; i < result.size()-2; i++)
	cksum ^= result[i];

    if (cksum != cksum_wr)
	goto restart;

    return result;
}

void AircotecGps::get_header(dt_callback cb, void *arg, int &pointcount, time_t &basetime, enum aircotec_height &height_type)
{
    char devtype[5];
    int vermaj, vermin, verpatch;
    int flnum;
    struct tm tmv;
    int tmp1, interval, cksum;
    char cheight_type;

    /* Wait for the initial line */
    while(1) {
	string line = ac_readline(cb, arg, 0, 0, 0);
	/* parse */
	int np = sscanf(line.c_str(), "@%2s%1d%1d%2d%4X%2d%2d%2d%2d%2d%2d%2d%2d%4X%2X%c**%2X",
			devtype, &vermaj, &vermin, &verpatch, &gpsunitid, &flnum,
			&tmv.tm_year, &tmv.tm_mon, &tmv.tm_mday, &tmv.tm_hour, &tmv.tm_min, &tmv.tm_sec,
			&tmp1, &pointcount, &interval, &cheight_type, &cksum);
	if (np != 17)
	    continue;
	break;
    }
    /* Make GPS name */
    stringstream sgps;
    sgps << "Aircotec ";
    if (string("xc") == devtype) {
	sgps << "XC-Trainer ";
	sgps << "v" << vermaj << '.' << vermin << '-' << verpatch;
    } else if (string("tn") == devtype) {
	sgps << "Top-navigator ";
	sgps << "v" << vermaj << '.' << vermin << hex << verpatch << dec;
    } else {
	sgps << "Unknown(" << devtype << ") ";
	sgps << "v" << vermaj << '.' << vermin << '-' << verpatch;
    }
    gpsname = sgps.str();

    /* Make starting time */
    tmv.tm_year += 100;
    tmv.tm_mon -= 1;
    basetime = make_gmtime(&tmv);

    if (cheight_type == '1')
	height_type = AIRCOTEC_GPS;
    else if (cheight_type == '2')
	height_type = AIRCOTEC_GPS_BARO;
    else
	height_type = AIRCOTEC_BARO;
}

PointArr AircotecGps::download_tracklog(dt_callback cb, void *arg)
{
    time_t basetime;
    int pointcount;
    enum aircotec_height height_type;

    /* Set the device to baudrate */
    dev->set_speed(BAUDRATE);

    try {
	get_header(cb, arg, pointcount, basetime, height_type);
    } catch (TimeoutException) {
	throw NoData("No data received.");
    }

    int32_t dlat, dlon;
    int dpos, dtime, fix, cksum;
    int16_t alt, alt2;
    PointArr result;
    /* Read the rest */
    for (int i = 0; i < pointcount; i++) {
	string line = ac_readline(cb, arg, i, pointcount, DOWN_TIMEOUT);
	int np;
	if (height_type == AIRCOTEC_GPS_BARO) {
	    np = sscanf(line.c_str(), "%4X%4X%2X%6X%6X%4hX%4hX%2X",
			    &dpos, &dtime, &fix, &dlat, &dlon, &alt, &alt2, &cksum);
	    if (np != 8) // Syntax error
		throw Exception("Incorrect data received.");
	} else {
	    np = sscanf(line.c_str(), "%4X%4X%2X%6X%6X%4hX%2X",
			    &dpos, &dtime, &fix, &dlat, &dlon, &alt, &cksum);
	    if (np != 7) // Syntax error
		throw Exception("Incorrect data received.");
	}

	if (dpos != i)
	    throw Exception("Incorrect position data received");

	// Skip points without fix. Maxpunkte saves it and
	// sets fix3d to false..
	if (! (fix & FLAG_FIX))
	    continue;

	Trackpoint newpoint;
	if (height_type == AIRCOTEC_GPS) {
	    newpoint.gpsalt = alt;
	} else if (height_type == AIRCOTEC_BARO) {
	    newpoint.baroalt = alt;
	} else {
	    newpoint.gpsalt = alt;
	    newpoint.baroalt = alt2;
	}
	newpoint.fix3d = true;
	newpoint.time = basetime + dtime;
	// Modify dlat & dlon to be proper negative numbers, if it is negative
	if (dlat & 0x800000) dlat |= 0xff000000;
	if (dlon & 0x800000) dlon |= 0xff000000;

	newpoint.lat = dlat / 24000.0;
	newpoint.lon = dlon / 24000.0;

	result.push_back(newpoint);
    }

    return result;
}
<commit_msg>Fix wrong flight number decoding in aircotec protocol.<commit_after>#include <string>
#include <sstream>
#include <iostream>
#include <time.h>
#include <stdio.h>

#include "aircotec.h"
#include "igc.h"

using namespace std;

#define BAUDRATE 57600

#define FLAG_FIX   0x1
#define FLAG_FIX3D 0x2

/* Maximum timeout in seconds for readline, during download */
#define DOWN_TIMEOUT 2

/* Read line from input & check the checksum */
/* Timeout specified in seconds, 0 means no timeout */
string AircotecGps::ac_readline(dt_callback cb, void *arg,
				int pktcount, int expcount,
				int timeout)
{
    time_t starttime = time(NULL);
  restart:
    if (timeout && time(NULL) - starttime > timeout)
	throw TimeoutException();

    string result;
    char ch;

    while (1) {
	if (cb) {
	    bool rv = cb(arg, pktcount, expcount);
	    if (!rv)
		throw Exception("Download cancelled");
	}
	try {
	    ch = dev->read();
	} catch (TimeoutException e) {
	    // TODO: Handle timeouts in some reasonable way
	    goto restart;
	}

	if (ch == '\n')
	    break;
	if (ch == '\r')
	    continue;
	result += ch;
    }
    if (result.size() < 2)
	goto restart;

    string cksum_s = result.substr(result.size()-2);
    int cksum_wr;
    if (sscanf(cksum_s.c_str(), "%2X", &cksum_wr) != 1)
	goto restart;
    int cksum = 0;
    for (unsigned int i=0; i < result.size()-2; i++)
	cksum ^= result[i];

    if (cksum != cksum_wr)
	goto restart;

    return result;
}

void AircotecGps::get_header(dt_callback cb, void *arg, int &pointcount, time_t &basetime, enum aircotec_height &height_type)
{
    char devtype[5];
    int vermaj, vermin, verpatch;
    int flnum;
    struct tm tmv;
    int tmp1, interval, cksum;
    char cheight_type;

    /* Wait for the initial line */
    while(1) {
	string line = ac_readline(cb, arg, 0, 0, 0);
	/* parse */
	int np = sscanf(line.c_str(), "@%2s%1d%1d%2d%4X%2X%2d%2d%2d%2d%2d%2d%2d%4X%2X%c**%2X",
			devtype, &vermaj, &vermin, &verpatch, &gpsunitid, &flnum,
			&tmv.tm_year, &tmv.tm_mon, &tmv.tm_mday, &tmv.tm_hour, &tmv.tm_min, &tmv.tm_sec,
			&tmp1, &pointcount, &interval, &cheight_type, &cksum);
	if (np != 17)
	    continue;
	break;
    }
    /* Make GPS name */
    stringstream sgps;
    sgps << "Aircotec ";
    if (string("xc") == devtype) {
	sgps << "XC-Trainer ";
	sgps << "v" << vermaj << '.' << vermin << '-' << verpatch;
    } else if (string("tn") == devtype) {
	sgps << "Top-navigator ";
	sgps << "v" << vermaj << '.' << vermin << hex << verpatch << dec;
    } else {
	sgps << "Unknown(" << devtype << ") ";
	sgps << "v" << vermaj << '.' << vermin << '-' << verpatch;
    }
    gpsname = sgps.str();

    /* Make starting time */
    tmv.tm_year += 100;
    tmv.tm_mon -= 1;
    basetime = make_gmtime(&tmv);

    if (cheight_type == '1')
	height_type = AIRCOTEC_GPS;
    else if (cheight_type == '2')
	height_type = AIRCOTEC_GPS_BARO;
    else
	height_type = AIRCOTEC_BARO;
}

PointArr AircotecGps::download_tracklog(dt_callback cb, void *arg)
{
    time_t basetime;
    int pointcount;
    enum aircotec_height height_type;

    /* Set the device to baudrate */
    dev->set_speed(BAUDRATE);

    try {
	get_header(cb, arg, pointcount, basetime, height_type);
    } catch (TimeoutException) {
	throw NoData("No data received.");
    }

    int32_t dlat, dlon;
    int dpos, dtime, fix, cksum;
    int16_t alt, alt2;
    PointArr result;
    /* Read the rest */
    for (int i = 0; i < pointcount; i++) {
	string line = ac_readline(cb, arg, i, pointcount, DOWN_TIMEOUT);
	int np;
	if (height_type == AIRCOTEC_GPS_BARO) {
	    np = sscanf(line.c_str(), "%4X%4X%2X%6X%6X%4hX%4hX%2X",
			    &dpos, &dtime, &fix, &dlat, &dlon, &alt, &alt2, &cksum);
	    if (np != 8) // Syntax error
		throw Exception("Incorrect data received.");
	} else {
	    np = sscanf(line.c_str(), "%4X%4X%2X%6X%6X%4hX%2X",
			    &dpos, &dtime, &fix, &dlat, &dlon, &alt, &cksum);
	    if (np != 7) // Syntax error
		throw Exception("Incorrect data received.");
	}

	if (dpos != i)
	    throw Exception("Incorrect position data received");

	// Skip points without fix. Maxpunkte saves it and
	// sets fix3d to false..
	if (! (fix & FLAG_FIX))
	    continue;

	Trackpoint newpoint;
	if (height_type == AIRCOTEC_GPS) {
	    newpoint.gpsalt = alt;
	} else if (height_type == AIRCOTEC_BARO) {
	    newpoint.baroalt = alt;
	} else {
	    newpoint.gpsalt = alt;
	    newpoint.baroalt = alt2;
	}
	newpoint.fix3d = true;
	newpoint.time = basetime + dtime;
	// Modify dlat & dlon to be proper negative numbers, if it is negative
	if (dlat & 0x800000) dlat |= 0xff000000;
	if (dlon & 0x800000) dlon |= 0xff000000;

	newpoint.lat = dlat / 24000.0;
	newpoint.lon = dlon / 24000.0;

	result.push_back(newpoint);
    }

    return result;
}
<|endoftext|>


<commit_before>#include "common/config/resources.h"
#include "common/protobuf/utility.h"

#include "test/integration/http_integration.h"
#include "test/integration/utility.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/utility.h"

#include "api/cds.pb.h"
#include "api/discovery.pb.h"
#include "api/eds.pb.h"
#include "api/lds.pb.h"
#include "api/rds.pb.h"
#include "gtest/gtest.h"

using testing::AssertionFailure;
using testing::AssertionResult;
using testing::AssertionSuccess;

namespace Envoy {
namespace {

class AdsIntegrationTest : public HttpIntegrationTest,
                           public testing::TestWithParam<Network::Address::IpVersion> {
public:
  AdsIntegrationTest() : HttpIntegrationTest(Http::CodecClient::Type::HTTP2, GetParam()) {}

  void SetUp() override {
    fake_upstreams_.emplace_back(new FakeUpstream(0, FakeHttpConnection::Type::HTTP2, version_));
    registerPort("endpoint", fake_upstreams_.back()->localAddress()->ip()->port());
    fake_upstreams_.emplace_back(new FakeUpstream(0, FakeHttpConnection::Type::HTTP2, version_));
    registerPort("ads_upstream", fake_upstreams_.back()->localAddress()->ip()->port());
    createTestServer("test/config/integration/server_ads.yaml", {"http"});
  }

  void TearDown() override {
    test_server_.reset();
    fake_upstreams_.clear();
  }

  AssertionResult compareDiscoveryRequest(const std::string& expected_type_url,
                                          const std::string& expected_version,
                                          const std::vector<std::string>& expected_resource_names) {
    envoy::api::v2::DiscoveryRequest discovery_request;
    ads_stream_->waitForGrpcMessage(*dispatcher_, discovery_request);
    if (expected_type_url != discovery_request.type_url()) {
      return AssertionFailure() << fmt::format("type_url {} does not match expected {}",
                                               discovery_request.type_url(), expected_type_url);
    }
    const std::vector<std::string> resource_names(discovery_request.resource_names().cbegin(),
                                                  discovery_request.resource_names().cend());
    if (expected_resource_names != resource_names) {
      return AssertionFailure() << fmt::format(
                 "resources {} do not match expected {} in {}",
                 fmt::join(resource_names.begin(), resource_names.end(), ","),
                 fmt::join(expected_resource_names.begin(), expected_resource_names.end(), ","),
                 discovery_request.DebugString());
    }
    if (expected_version != discovery_request.version_info()) {
      return AssertionFailure() << fmt::format("version {} does not match expected {} in {}",
                                               discovery_request.version_info(), expected_version,
                                               discovery_request.DebugString());
    }
    return AssertionSuccess();
  }

  void sendDiscoveryResponse(const std::string& type_url, const Protobuf::Message& message,
                             const std::string& version) {
    envoy::api::v2::DiscoveryResponse discovery_response;
    discovery_response.set_version_info(version);
    discovery_response.set_type_url(type_url);
    discovery_response.add_resources()->PackFrom(message);
    ads_stream_->sendGrpcMessage(discovery_response);
  }

  envoy::api::v2::Cluster buildCluster(const std::string& name) {
    return TestUtility::parseYaml<envoy::api::v2::Cluster>(fmt::format(R"EOF(
      name: {}
      connect_timeout: 5s
      type: EDS
      eds_cluster_config: {{ eds_config: {{ ads: {{}} }} }}
      lb_policy: ROUND_ROBIN
      http2_protocol_options: {{}}
    )EOF",
                                                                       name));
  }

  envoy::api::v2::ClusterLoadAssignment buildClusterLoadAssignment(const std::string& name) {
    return TestUtility::parseYaml<envoy::api::v2::ClusterLoadAssignment>(
        fmt::format(R"EOF(
      cluster_name: {}
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: {}
                port_value: {}
    )EOF",
                    name, Network::Test::getLoopbackAddressString(GetParam()),
                    fake_upstreams_[0]->localAddress()->ip()->port()));
  }

  envoy::api::v2::Listener buildListener(const std::string& name, const std::string& route_config) {
    return TestUtility::parseYaml<envoy::api::v2::Listener>(
        fmt::format(R"EOF(
      name: {}
      address:
        socket_address:
          address: {}
          port_value: 0
      filter_chains:
        filters:
        - name: envoy.http_connection_manager
          config:
            codec_type: HTTP2
            rds:
              route_config_name: {}
              config_source: {{ ads: {{}} }}
            http_filters: [{{ name: envoy.router, config: {{ deprecated_v1: true }}}}]
    )EOF",
                    name, Network::Test::getLoopbackAddressString(GetParam()), route_config));
  }

  envoy::api::v2::RouteConfiguration buildRouteConfig(const std::string& name,
                                                      const std::string& cluster) {
    return TestUtility::parseYaml<envoy::api::v2::RouteConfiguration>(fmt::format(R"EOF(
      name: {}
      virtual_hosts:
      - name: integration
        domains: ["*"]
        routes:
        - match: {{ prefix: "/" }}
          route: {{ cluster: {} }}
    )EOF",
                                                                                name, cluster));
  }

  void makeSingleRequest() {
    registerTestServerPorts({"http"});
    auto client_conn = makeClientConnection(lookupPort("http"));
    testRouterHeaderOnlyRequestAndResponse(std::move(client_conn), true);
    cleanupUpstreamAndDownstream();
    fake_upstream_connection_ = nullptr;
  }

  void initialize() {
    ads_connection_ = fake_upstreams_[1]->waitForHttpConnection(*dispatcher_);
    ads_stream_ = ads_connection_->waitForNewStream();
    ads_stream_->startGrpcStream();
  }

  FakeHttpConnectionPtr ads_connection_;
  FakeStreamPtr ads_stream_;
};

INSTANTIATE_TEST_CASE_P(IpVersions, AdsIntegrationTest,
                        testing::ValuesIn(TestEnvironment::getIpVersionsForTest()));

// Validate basic config delivery and upgrade.
TEST_P(AdsIntegrationTest, Basic) {
  initialize();

  // Send initial configuration, validate we can process a request.
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}));
  sendDiscoveryResponse(Config::TypeUrl::get().Cluster, buildCluster("cluster_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "", {"cluster_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().ClusterLoadAssignment,
                        buildClusterLoadAssignment("cluster_0"), "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "1", {}));
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "", {}));
  sendDiscoveryResponse(Config::TypeUrl::get().Listener,
                        buildListener("listener_0", "route_config_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "1", {"cluster_0"}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "", {"route_config_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().RouteConfiguration,
                        buildRouteConfig("route_config_0", "cluster_0"), "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "1", {}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "1", {"route_config_0"}));

  test_server_->waitForCounterGe("listener_manager.listener_create_success", 1);
  makeSingleRequest();

  // Upgrade RDS/CDS/EDS to a newer config, validate we can process a request.
  sendDiscoveryResponse(Config::TypeUrl::get().Cluster, buildCluster("cluster_1"), "2");
  sendDiscoveryResponse(Config::TypeUrl::get().ClusterLoadAssignment,
                        buildClusterLoadAssignment("cluster_1"), "2");
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "1",
                                      {"cluster_1", "cluster_0"}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "1", {"cluster_1"}));
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "2", {}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "2", {"cluster_1"}));
  sendDiscoveryResponse(Config::TypeUrl::get().RouteConfiguration,
                        buildRouteConfig("route_config_0", "cluster_1"), "2");
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "2", {"route_config_0"}));

  makeSingleRequest();

  // Upgrade LDS/RDS, validate we can process a request.
  sendDiscoveryResponse(Config::TypeUrl::get().Listener,
                        buildListener("listener_1", "route_config_1"), "2");
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "2",
                                      {"route_config_1", "route_config_0"}));
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "2", {}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "2", {"route_config_1"}));
  sendDiscoveryResponse(Config::TypeUrl::get().RouteConfiguration,
                        buildRouteConfig("route_config_1", "cluster_1"), "3");
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "3", {"route_config_1"}));

  test_server_->waitForCounterGe("listener_manager.listener_create_success", 2);
  makeSingleRequest();
}

// Validate that we can recover from failures.
TEST_P(AdsIntegrationTest, Failure) {
  initialize();

  // Send initial configuration, failing each xDS once (via a type mismatch), validate we can
  // process a request.
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}));
  sendDiscoveryResponse(Config::TypeUrl::get().Cluster, buildClusterLoadAssignment("cluster_0"),
                        "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "", {}));

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}));
  sendDiscoveryResponse(Config::TypeUrl::get().Cluster, buildCluster("cluster_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "", {"cluster_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().ClusterLoadAssignment, buildCluster("cluster_0"),
                        "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "1", {}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "", {"cluster_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().ClusterLoadAssignment,
                        buildClusterLoadAssignment("cluster_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "1", {"cluster_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().Listener,
                        buildRouteConfig("listener_0", "route_config_0"), "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "", {}));
  sendDiscoveryResponse(Config::TypeUrl::get().Listener,
                        buildListener("listener_0", "route_config_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "", {"route_config_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().RouteConfiguration,
                        buildListener("route_config_0", "cluster_0"), "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "1", {}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "", {"route_config_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().RouteConfiguration,
                        buildRouteConfig("route_config_0", "cluster_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "1", {"route_config_0"}));

  test_server_->waitForCounterGe("listener_manager.listener_create_success", 1);
  makeSingleRequest();
}

} // namespace
} // namespace Envoy
<commit_msg>test: fix merge race in ads_integration_test. (#1706)<commit_after>#include "common/config/resources.h"
#include "common/protobuf/utility.h"

#include "test/integration/http_integration.h"
#include "test/integration/utility.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/utility.h"

#include "api/cds.pb.h"
#include "api/discovery.pb.h"
#include "api/eds.pb.h"
#include "api/lds.pb.h"
#include "api/rds.pb.h"
#include "gtest/gtest.h"

using testing::AssertionFailure;
using testing::AssertionResult;
using testing::AssertionSuccess;

namespace Envoy {
namespace {

class AdsIntegrationTest : public HttpIntegrationTest,
                           public testing::TestWithParam<Network::Address::IpVersion> {
public:
  AdsIntegrationTest() : HttpIntegrationTest(Http::CodecClient::Type::HTTP2, GetParam()) {}

  void SetUp() override {
    fake_upstreams_.emplace_back(new FakeUpstream(0, FakeHttpConnection::Type::HTTP2, version_));
    registerPort("endpoint", fake_upstreams_.back()->localAddress()->ip()->port());
    fake_upstreams_.emplace_back(new FakeUpstream(0, FakeHttpConnection::Type::HTTP2, version_));
    registerPort("ads_upstream", fake_upstreams_.back()->localAddress()->ip()->port());
    createTestServer("test/config/integration/server_ads.yaml", {"http"});
  }

  void TearDown() override {
    test_server_.reset();
    fake_upstreams_.clear();
  }

  AssertionResult compareDiscoveryRequest(const std::string& expected_type_url,
                                          const std::string& expected_version,
                                          const std::vector<std::string>& expected_resource_names) {
    envoy::api::v2::DiscoveryRequest discovery_request;
    ads_stream_->waitForGrpcMessage(*dispatcher_, discovery_request);
    if (expected_type_url != discovery_request.type_url()) {
      return AssertionFailure() << fmt::format("type_url {} does not match expected {}",
                                               discovery_request.type_url(), expected_type_url);
    }
    const std::vector<std::string> resource_names(discovery_request.resource_names().cbegin(),
                                                  discovery_request.resource_names().cend());
    if (expected_resource_names != resource_names) {
      return AssertionFailure() << fmt::format(
                 "resources {} do not match expected {} in {}",
                 fmt::join(resource_names.begin(), resource_names.end(), ","),
                 fmt::join(expected_resource_names.begin(), expected_resource_names.end(), ","),
                 discovery_request.DebugString());
    }
    if (expected_version != discovery_request.version_info()) {
      return AssertionFailure() << fmt::format("version {} does not match expected {} in {}",
                                               discovery_request.version_info(), expected_version,
                                               discovery_request.DebugString());
    }
    return AssertionSuccess();
  }

  void sendDiscoveryResponse(const std::string& type_url, const Protobuf::Message& message,
                             const std::string& version) {
    envoy::api::v2::DiscoveryResponse discovery_response;
    discovery_response.set_version_info(version);
    discovery_response.set_type_url(type_url);
    discovery_response.add_resources()->PackFrom(message);
    ads_stream_->sendGrpcMessage(discovery_response);
  }

  envoy::api::v2::Cluster buildCluster(const std::string& name) {
    return TestUtility::parseYaml<envoy::api::v2::Cluster>(fmt::format(R"EOF(
      name: {}
      connect_timeout: 5s
      type: EDS
      eds_cluster_config: {{ eds_config: {{ ads: {{}} }} }}
      lb_policy: ROUND_ROBIN
      http2_protocol_options: {{}}
    )EOF",
                                                                       name));
  }

  envoy::api::v2::ClusterLoadAssignment buildClusterLoadAssignment(const std::string& name) {
    return TestUtility::parseYaml<envoy::api::v2::ClusterLoadAssignment>(
        fmt::format(R"EOF(
      cluster_name: {}
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: {}
                port_value: {}
    )EOF",
                    name, Network::Test::getLoopbackAddressString(GetParam()),
                    fake_upstreams_[0]->localAddress()->ip()->port()));
  }

  envoy::api::v2::Listener buildListener(const std::string& name, const std::string& route_config) {
    return TestUtility::parseYaml<envoy::api::v2::Listener>(
        fmt::format(R"EOF(
      name: {}
      address:
        socket_address:
          address: {}
          port_value: 0
      filter_chains:
        filters:
        - name: envoy.http_connection_manager
          config:
            codec_type: HTTP2
            rds:
              route_config_name: {}
              config_source: {{ ads: {{}} }}
            http_filters: [{{ name: envoy.router, config: {{ deprecated_v1: true }}}}]
    )EOF",
                    name, Network::Test::getLoopbackAddressString(GetParam()), route_config));
  }

  envoy::api::v2::RouteConfiguration buildRouteConfig(const std::string& name,
                                                      const std::string& cluster) {
    return TestUtility::parseYaml<envoy::api::v2::RouteConfiguration>(fmt::format(R"EOF(
      name: {}
      virtual_hosts:
      - name: integration
        domains: ["*"]
        routes:
        - match: {{ prefix: "/" }}
          route: {{ cluster: {} }}
    )EOF",
                                                                                name, cluster));
  }

  void makeSingleRequest() {
    registerTestServerPorts({"http"});
    testRouterHeaderOnlyRequestAndResponse(true);
    cleanupUpstreamAndDownstream();
    fake_upstream_connection_ = nullptr;
  }

  void initialize() override {
    BaseIntegrationTest::initialize();
    ads_connection_ = fake_upstreams_[1]->waitForHttpConnection(*dispatcher_);
    ads_stream_ = ads_connection_->waitForNewStream();
    ads_stream_->startGrpcStream();
  }

  FakeHttpConnectionPtr ads_connection_;
  FakeStreamPtr ads_stream_;
};

INSTANTIATE_TEST_CASE_P(IpVersions, AdsIntegrationTest,
                        testing::ValuesIn(TestEnvironment::getIpVersionsForTest()));

// Validate basic config delivery and upgrade.
TEST_P(AdsIntegrationTest, Basic) {
  initialize();

  // Send initial configuration, validate we can process a request.
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}));
  sendDiscoveryResponse(Config::TypeUrl::get().Cluster, buildCluster("cluster_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "", {"cluster_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().ClusterLoadAssignment,
                        buildClusterLoadAssignment("cluster_0"), "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "1", {}));
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "", {}));
  sendDiscoveryResponse(Config::TypeUrl::get().Listener,
                        buildListener("listener_0", "route_config_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "1", {"cluster_0"}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "", {"route_config_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().RouteConfiguration,
                        buildRouteConfig("route_config_0", "cluster_0"), "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "1", {}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "1", {"route_config_0"}));

  test_server_->waitForCounterGe("listener_manager.listener_create_success", 1);
  makeSingleRequest();

  // Upgrade RDS/CDS/EDS to a newer config, validate we can process a request.
  sendDiscoveryResponse(Config::TypeUrl::get().Cluster, buildCluster("cluster_1"), "2");
  sendDiscoveryResponse(Config::TypeUrl::get().ClusterLoadAssignment,
                        buildClusterLoadAssignment("cluster_1"), "2");
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "1",
                                      {"cluster_1", "cluster_0"}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "1", {"cluster_1"}));
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "2", {}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "2", {"cluster_1"}));
  sendDiscoveryResponse(Config::TypeUrl::get().RouteConfiguration,
                        buildRouteConfig("route_config_0", "cluster_1"), "2");
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "2", {"route_config_0"}));

  makeSingleRequest();

  // Upgrade LDS/RDS, validate we can process a request.
  sendDiscoveryResponse(Config::TypeUrl::get().Listener,
                        buildListener("listener_1", "route_config_1"), "2");
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "2",
                                      {"route_config_1", "route_config_0"}));
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "2", {}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "2", {"route_config_1"}));
  sendDiscoveryResponse(Config::TypeUrl::get().RouteConfiguration,
                        buildRouteConfig("route_config_1", "cluster_1"), "3");
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "3", {"route_config_1"}));

  test_server_->waitForCounterGe("listener_manager.listener_create_success", 2);
  makeSingleRequest();
}

// Validate that we can recover from failures.
TEST_P(AdsIntegrationTest, Failure) {
  initialize();

  // Send initial configuration, failing each xDS once (via a type mismatch), validate we can
  // process a request.
  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}));
  sendDiscoveryResponse(Config::TypeUrl::get().Cluster, buildClusterLoadAssignment("cluster_0"),
                        "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "", {}));

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}));
  sendDiscoveryResponse(Config::TypeUrl::get().Cluster, buildCluster("cluster_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "", {"cluster_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().ClusterLoadAssignment, buildCluster("cluster_0"),
                        "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "1", {}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "", {"cluster_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().ClusterLoadAssignment,
                        buildClusterLoadAssignment("cluster_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, "1", {"cluster_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().Listener,
                        buildRouteConfig("listener_0", "route_config_0"), "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "", {}));
  sendDiscoveryResponse(Config::TypeUrl::get().Listener,
                        buildListener("listener_0", "route_config_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "", {"route_config_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().RouteConfiguration,
                        buildListener("route_config_0", "cluster_0"), "1");

  EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Listener, "1", {}));
  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "", {"route_config_0"}));
  sendDiscoveryResponse(Config::TypeUrl::get().RouteConfiguration,
                        buildRouteConfig("route_config_0", "cluster_0"), "1");

  EXPECT_TRUE(
      compareDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "1", {"route_config_0"}));

  test_server_->waitForCounterGe("listener_manager.listener_create_success", 1);
  makeSingleRequest();
}

} // namespace
} // namespace Envoy
<|endoftext|>


<commit_before>// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.

#include "stream.h"
#include "sync.h"       // For frame_archive
#include "image.h"      // For image alignment, rectification, and deprojection routines
#include <algorithm>    // For sort
#include <tuple>        // For make_tuple

using namespace rsimpl;

rs_extrinsics stream_interface::get_extrinsics_to(const rs_stream_interface & other) const
{
    auto& r = dynamic_cast<const stream_interface&>(other);
    auto from = get_pose(), to = r.get_pose();
    if(from == to) return {{1,0,0,0,1,0,0,0,1},{0,0,0}};
    auto transform = inverse(from) * to;
    rs_extrinsics extrin;
    (float3x3 &)extrin.rotation = transform.orientation;
    (float3 &)extrin.translation = transform.position;
    return extrin;
}

native_stream::native_stream(device_config & config, rs_stream stream) : config(config), stream(stream)
{
    for(auto & subdevice_mode : config.info.subdevice_modes)
    {
        for(auto pad_crop : subdevice_mode.pad_crop_options)
        {
            for(auto & unpacker : subdevice_mode.pf.unpackers)
            {
                auto selection = subdevice_mode_selection(subdevice_mode, pad_crop, (int)(&unpacker - subdevice_mode.pf.unpackers.data()));
                if(selection.provides_stream(stream)) modes.push_back(selection);
            }
        }
    }

    auto get_tuple = [stream](const subdevice_mode_selection & selection)
    {
        return std::make_tuple(-selection.get_width(), -selection.get_height(), -selection.get_framerate(), selection.get_format(stream));
    };

    std::sort(begin(modes), end(modes), [get_tuple](const subdevice_mode_selection & a, const subdevice_mode_selection & b) { return get_tuple(a) < get_tuple(b); });
    auto it = std::unique(begin(modes), end(modes), [get_tuple](const subdevice_mode_selection & a, const subdevice_mode_selection & b) { return get_tuple(a) == get_tuple(b); });
    if(it != end(modes)) modes.erase(it, end(modes));
}

void native_stream::get_mode(int mode, int * w, int * h, rs_format * f, int * fps) const
{
    auto & selection = modes[mode];
    if(w) *w = selection.get_width();
    if(h) *h = selection.get_height();
    if(f) *f = selection.get_format(stream);
    if(fps) *fps = selection.get_framerate();
}

bool native_stream::is_enabled() const
{
    return (archive && archive->is_stream_enabled(stream)) || config.requests[stream].enabled;
}

subdevice_mode_selection native_stream::get_mode() const
{
    if(archive && archive->is_stream_enabled(stream)) return archive->get_mode(stream);
    if(config.requests[stream].enabled)
    {
        for(auto subdevice_mode : config.select_modes())
        {
            if(subdevice_mode.provides_stream(stream)) return subdevice_mode;
        }
        throw std::logic_error("no mode found"); // Should never happen, select_modes should throw if no mode can be found
    }
    throw std::runtime_error(to_string() << "stream not enabled: " << stream);
}

rs_intrinsics native_stream::get_intrinsics() const
{
    const auto m = get_mode();
    return pad_crop_intrinsics(m.mode.native_intrinsics, m.pad_crop);
}

rs_intrinsics native_stream::get_rectified_intrinsics() const
{
    const auto m = get_mode();
    if(m.mode.rect_modes.empty()) return get_intrinsics();
    return pad_crop_intrinsics(m.mode.rect_modes[0], m.pad_crop);
}

int native_stream::get_frame_number() const
{
    if(!is_enabled()) throw std::runtime_error(to_string() << "stream not enabled: " << stream);
    return archive->get_frame_number(stream);
}

double native_stream::get_frame_timestamp() const
{
    if (!is_enabled()) throw std::runtime_error(to_string() << "stream not enabled: " << stream);
    return archive->get_frame_timestamp(stream);
}

long long native_stream::get_frame_system_time() const
{
    return archive->get_frame_system_time(stream);
}

const uint8_t * native_stream::get_frame_data() const
{
    if(!is_enabled()) throw std::runtime_error(to_string() << "stream not enabled: " << stream);
    return (const uint8_t *) archive->get_frame_data(stream);
}

const uint8_t * point_stream::get_frame_data() const
{
    if(image.empty() || number != get_frame_number())
    {
        image.resize(get_image_size(get_intrinsics().width, get_intrinsics().height, get_format()));

        if(source.get_format() == RS_FORMAT_Z16)
        {
            deproject_z(reinterpret_cast<float *>(image.data()), get_intrinsics(), reinterpret_cast<const uint16_t *>(source.get_frame_data()), get_depth_scale());
        }
        else if(source.get_format() == RS_FORMAT_DISPARITY16)
        {
            deproject_disparity(reinterpret_cast<float *>(image.data()), get_intrinsics(), reinterpret_cast<const uint16_t *>(source.get_frame_data()), get_depth_scale());
        }
        else assert(false && "Cannot deproject image from a non-depth format");

        number = get_frame_number();
    }
    return image.data();
}

const uint8_t * rectified_stream::get_frame_data() const
{
    // If source image is already rectified, just return it without doing any work
    if(get_pose() == source.get_pose() && get_intrinsics() == source.get_intrinsics()) return source.get_frame_data();

    if(image.empty() || number != get_frame_number())
    {
        if(table.empty()) table = compute_rectification_table(get_intrinsics(), get_extrinsics_to(source), source.get_intrinsics());
        image.resize(get_image_size(get_intrinsics().width, get_intrinsics().height, get_format()));
        rectify_image(image.data(), table, source.get_frame_data(), get_format());
        number = get_frame_number();
    }
    return image.data();
}

const uint8_t * aligned_stream::get_frame_data() const
{
    if(image.empty() || number != get_frame_number())
    {
        image.resize(get_image_size(get_intrinsics().width, get_intrinsics().height, get_format()));
        memset(image.data(), from.get_format() == RS_FORMAT_DISPARITY16 ? 0xFF : 0x00, image.size());
        if(from.get_format() == RS_FORMAT_Z16)
        {
            align_z_to_other(image.data(), (const uint16_t *)from.get_frame_data(), from.get_depth_scale(), from.get_intrinsics(), from.get_extrinsics_to(to), to.get_intrinsics());
        }
        else if(from.get_format() == RS_FORMAT_DISPARITY16)
        {
            align_disparity_to_other(image.data(), (const uint16_t *)from.get_frame_data(), from.get_depth_scale(), from.get_intrinsics(), from.get_extrinsics_to(to), to.get_intrinsics());
        }
        else if(to.get_format() == RS_FORMAT_Z16)
        {
            align_other_to_z(image.data(), (const uint16_t *)to.get_frame_data(), to.get_depth_scale(), to.get_intrinsics(), to.get_extrinsics_to(from), from.get_intrinsics(), from.get_frame_data(), from.get_format());
        }
        else if(to.get_format() == RS_FORMAT_DISPARITY16)
        {
            align_other_to_disparity(image.data(), (const uint16_t *)to.get_frame_data(), to.get_depth_scale(), to.get_intrinsics(), to.get_extrinsics_to(from), from.get_intrinsics(), from.get_frame_data(), from.get_format());
        }
        else assert(false && "Cannot align two images if neither have depth data");
        number = get_frame_number();
    }
    return image.data();
}
<commit_msg>fixing access violation in case start was not called before get_frame_timestamp<commit_after>// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.

#include "stream.h"
#include "sync.h"       // For frame_archive
#include "image.h"      // For image alignment, rectification, and deprojection routines
#include <algorithm>    // For sort
#include <tuple>        // For make_tuple

using namespace rsimpl;

rs_extrinsics stream_interface::get_extrinsics_to(const rs_stream_interface & other) const
{
    auto& r = dynamic_cast<const stream_interface&>(other);
    auto from = get_pose(), to = r.get_pose();
    if(from == to) return {{1,0,0,0,1,0,0,0,1},{0,0,0}};
    auto transform = inverse(from) * to;
    rs_extrinsics extrin;
    (float3x3 &)extrin.rotation = transform.orientation;
    (float3 &)extrin.translation = transform.position;
    return extrin;
}

native_stream::native_stream(device_config & config, rs_stream stream) : config(config), stream(stream)
{
    for(auto & subdevice_mode : config.info.subdevice_modes)
    {
        for(auto pad_crop : subdevice_mode.pad_crop_options)
        {
            for(auto & unpacker : subdevice_mode.pf.unpackers)
            {
                auto selection = subdevice_mode_selection(subdevice_mode, pad_crop, (int)(&unpacker - subdevice_mode.pf.unpackers.data()));
                if(selection.provides_stream(stream)) modes.push_back(selection);
            }
        }
    }

    auto get_tuple = [stream](const subdevice_mode_selection & selection)
    {
        return std::make_tuple(-selection.get_width(), -selection.get_height(), -selection.get_framerate(), selection.get_format(stream));
    };

    std::sort(begin(modes), end(modes), [get_tuple](const subdevice_mode_selection & a, const subdevice_mode_selection & b) { return get_tuple(a) < get_tuple(b); });
    auto it = std::unique(begin(modes), end(modes), [get_tuple](const subdevice_mode_selection & a, const subdevice_mode_selection & b) { return get_tuple(a) == get_tuple(b); });
    if(it != end(modes)) modes.erase(it, end(modes));
}

void native_stream::get_mode(int mode, int * w, int * h, rs_format * f, int * fps) const
{
    auto & selection = modes[mode];
    if(w) *w = selection.get_width();
    if(h) *h = selection.get_height();
    if(f) *f = selection.get_format(stream);
    if(fps) *fps = selection.get_framerate();
}

bool native_stream::is_enabled() const
{
    return (archive && archive->is_stream_enabled(stream)) || config.requests[stream].enabled;
}

subdevice_mode_selection native_stream::get_mode() const
{
    if(archive && archive->is_stream_enabled(stream)) return archive->get_mode(stream);
    if(config.requests[stream].enabled)
    {
        for(auto subdevice_mode : config.select_modes())
        {
            if(subdevice_mode.provides_stream(stream)) return subdevice_mode;
        }
        throw std::logic_error("no mode found"); // Should never happen, select_modes should throw if no mode can be found
    }
    throw std::runtime_error(to_string() << "stream not enabled: " << stream);
}

rs_intrinsics native_stream::get_intrinsics() const
{
    const auto m = get_mode();
    return pad_crop_intrinsics(m.mode.native_intrinsics, m.pad_crop);
}

rs_intrinsics native_stream::get_rectified_intrinsics() const
{
    const auto m = get_mode();
    if(m.mode.rect_modes.empty()) return get_intrinsics();
    return pad_crop_intrinsics(m.mode.rect_modes[0], m.pad_crop);
}

int native_stream::get_frame_number() const
{
    if (!is_enabled()) throw std::runtime_error(to_string() << "stream not enabled: " << stream);
    if (!archive) throw  std::runtime_error(to_string() << "streaming not started!");
    return archive->get_frame_number(stream);
}

double native_stream::get_frame_timestamp() const
{
    if (!is_enabled()) throw std::runtime_error(to_string() << "stream not enabled: " << stream);
    if (!archive) throw  std::runtime_error(to_string() << "streaming not started!");
    return archive->get_frame_timestamp(stream);
}

long long native_stream::get_frame_system_time() const
{
    if (!is_enabled()) throw std::runtime_error(to_string() << "stream not enabled: " << stream);
    if (!archive) throw  std::runtime_error(to_string() << "streaming not started!");
    return archive->get_frame_system_time(stream);
}

const uint8_t * native_stream::get_frame_data() const
{
    if(!is_enabled()) throw std::runtime_error(to_string() << "stream not enabled: " << stream);
    if (!archive) throw  std::runtime_error(to_string() << "streaming not started!");
    return (const uint8_t *) archive->get_frame_data(stream);
}

const uint8_t * point_stream::get_frame_data() const
{
    if(image.empty() || number != get_frame_number())
    {
        image.resize(get_image_size(get_intrinsics().width, get_intrinsics().height, get_format()));

        if(source.get_format() == RS_FORMAT_Z16)
        {
            deproject_z(reinterpret_cast<float *>(image.data()), get_intrinsics(), reinterpret_cast<const uint16_t *>(source.get_frame_data()), get_depth_scale());
        }
        else if(source.get_format() == RS_FORMAT_DISPARITY16)
        {
            deproject_disparity(reinterpret_cast<float *>(image.data()), get_intrinsics(), reinterpret_cast<const uint16_t *>(source.get_frame_data()), get_depth_scale());
        }
        else assert(false && "Cannot deproject image from a non-depth format");

        number = get_frame_number();
    }
    return image.data();
}

const uint8_t * rectified_stream::get_frame_data() const
{
    // If source image is already rectified, just return it without doing any work
    if(get_pose() == source.get_pose() && get_intrinsics() == source.get_intrinsics()) return source.get_frame_data();

    if(image.empty() || number != get_frame_number())
    {
        if(table.empty()) table = compute_rectification_table(get_intrinsics(), get_extrinsics_to(source), source.get_intrinsics());
        image.resize(get_image_size(get_intrinsics().width, get_intrinsics().height, get_format()));
        rectify_image(image.data(), table, source.get_frame_data(), get_format());
        number = get_frame_number();
    }
    return image.data();
}

const uint8_t * aligned_stream::get_frame_data() const
{
    if(image.empty() || number != get_frame_number())
    {
        image.resize(get_image_size(get_intrinsics().width, get_intrinsics().height, get_format()));
        memset(image.data(), from.get_format() == RS_FORMAT_DISPARITY16 ? 0xFF : 0x00, image.size());
        if(from.get_format() == RS_FORMAT_Z16)
        {
            align_z_to_other(image.data(), (const uint16_t *)from.get_frame_data(), from.get_depth_scale(), from.get_intrinsics(), from.get_extrinsics_to(to), to.get_intrinsics());
        }
        else if(from.get_format() == RS_FORMAT_DISPARITY16)
        {
            align_disparity_to_other(image.data(), (const uint16_t *)from.get_frame_data(), from.get_depth_scale(), from.get_intrinsics(), from.get_extrinsics_to(to), to.get_intrinsics());
        }
        else if(to.get_format() == RS_FORMAT_Z16)
        {
            align_other_to_z(image.data(), (const uint16_t *)to.get_frame_data(), to.get_depth_scale(), to.get_intrinsics(), to.get_extrinsics_to(from), from.get_intrinsics(), from.get_frame_data(), from.get_format());
        }
        else if(to.get_format() == RS_FORMAT_DISPARITY16)
        {
            align_other_to_disparity(image.data(), (const uint16_t *)to.get_frame_data(), to.get_depth_scale(), to.get_intrinsics(), to.get_extrinsics_to(from), from.get_intrinsics(), from.get_frame_data(), from.get_format());
        }
        else assert(false && "Cannot align two images if neither have depth data");
        number = get_frame_number();
    }
    return image.data();
}
<|endoftext|>


<commit_before>// Copyright 2015 Google Inc. All rights reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build ignore

#include "strutil.h"

#include <ctype.h>
#include <limits.h>
#include <unistd.h>

#include <algorithm>
#include <functional>
#include <stack>
#include <utility>

#include "log.h"

static bool isSpace(char c) {
  return (9 <= c && c <= 13) || c == 32;
}

WordScanner::Iterator& WordScanner::Iterator::operator++() {
  int len = static_cast<int>(in->size());
  for (s = i + 1; s < len; s++) {
    if (!isSpace((*in)[s]))
      break;
  }
  if (s >= len) {
    in = NULL;
    s = 0;
    i = 0;
    return *this;
  }

  // skip until the next whitespace character
  i = s + strcspn(in->data() + s, "\x09\x0a\x0b\x0c\x0d ");
  return *this;
}

StringPiece WordScanner::Iterator::operator*() const {
  return in->substr(s, i - s);
}

WordScanner::WordScanner(StringPiece in) : in_(in) {}

WordScanner::Iterator WordScanner::begin() const {
  Iterator iter;
  iter.in = &in_;
  iter.s = 0;
  iter.i = -1;
  ++iter;
  return iter;
}

WordScanner::Iterator WordScanner::end() const {
  Iterator iter;
  iter.in = NULL;
  iter.s = 0;
  iter.i = 0;
  return iter;
}

void WordScanner::Split(vector<StringPiece>* o) {
  for (StringPiece t : *this)
    o->push_back(t);
}

WordWriter::WordWriter(string* o) : out_(o), needs_space_(false) {}

void WordWriter::MaybeAddWhitespace() {
  if (needs_space_) {
    out_->push_back(' ');
  } else {
    needs_space_ = true;
  }
}

void WordWriter::Write(StringPiece s) {
  MaybeAddWhitespace();
  AppendString(s, out_);
}

ScopedTerminator::ScopedTerminator(StringPiece s) : s_(s), c_(s[s.size()]) {
  const_cast<char*>(s_.data())[s_.size()] = '\0';
}

ScopedTerminator::~ScopedTerminator() {
  const_cast<char*>(s_.data())[s_.size()] = c_;
}

void AppendString(StringPiece str, string* out) {
  out->append(str.begin(), str.end());
}

bool HasPrefix(StringPiece str, StringPiece prefix) {
  ssize_t size_diff = str.size() - prefix.size();
  return size_diff >= 0 && str.substr(0, prefix.size()) == prefix;
}

bool HasSuffix(StringPiece str, StringPiece suffix) {
  ssize_t size_diff = str.size() - suffix.size();
  return size_diff >= 0 && str.substr(size_diff) == suffix;
}

bool HasWord(StringPiece str, StringPiece w) {
  size_t found = str.find(w);
  if (found == string::npos)
    return false;
  if (found != 0 && !isSpace(str[found - 1]))
    return false;
  size_t end = found + w.size();
  if (end != str.size() && !isSpace(str[end]))
    return false;
  return true;
}

StringPiece TrimPrefix(StringPiece str, StringPiece prefix) {
  ssize_t size_diff = str.size() - prefix.size();
  if (size_diff < 0 || str.substr(0, prefix.size()) != prefix)
    return str;
  return str.substr(prefix.size());
}

StringPiece TrimSuffix(StringPiece str, StringPiece suffix) {
  ssize_t size_diff = str.size() - suffix.size();
  if (size_diff < 0 || str.substr(size_diff) != suffix)
    return str;
  return str.substr(0, size_diff);
}

Pattern::Pattern(StringPiece pat) : pat_(pat), percent_index_(pat.find('%')) {}

bool Pattern::Match(StringPiece str) const {
  if (percent_index_ == string::npos)
    return str == pat_;
  return MatchImpl(str);
}

bool Pattern::MatchImpl(StringPiece str) const {
  return (HasPrefix(str, pat_.substr(0, percent_index_)) &&
          HasSuffix(str, pat_.substr(percent_index_ + 1)));
}

StringPiece Pattern::Stem(StringPiece str) const {
  if (!Match(str))
    return "";
  return str.substr(percent_index_,
                    str.size() - (pat_.size() - percent_index_ - 1));
}

void Pattern::AppendSubst(StringPiece str,
                          StringPiece subst,
                          string* out) const {
  if (percent_index_ == string::npos) {
    if (str == pat_) {
      AppendString(subst, out);
      return;
    } else {
      AppendString(str, out);
      return;
    }
  }

  if (MatchImpl(str)) {
    size_t subst_percent_index = subst.find('%');
    if (subst_percent_index == string::npos) {
      AppendString(subst, out);
      return;
    } else {
      AppendString(subst.substr(0, subst_percent_index), out);
      AppendString(str.substr(percent_index_, str.size() - pat_.size() + 1),
                   out);
      AppendString(subst.substr(subst_percent_index + 1), out);
      return;
    }
  }
  AppendString(str, out);
}

void Pattern::AppendSubstRef(StringPiece str,
                             StringPiece subst,
                             string* out) const {
  if (percent_index_ != string::npos && subst.find('%') != string::npos) {
    AppendSubst(str, subst, out);
    return;
  }
  StringPiece s = TrimSuffix(str, pat_);
  out->append(s.begin(), s.end());
  out->append(subst.begin(), subst.end());
}

string NoLineBreak(const string& s) {
  size_t index = s.find('\n');
  if (index == string::npos)
    return s;
  string r = s;
  while (index != string::npos) {
    r = r.substr(0, index) + "\\n" + r.substr(index + 1);
    index = r.find('\n', index + 2);
  }
  return r;
}

StringPiece TrimLeftSpace(StringPiece s) {
  size_t i = 0;
  for (; i < s.size(); i++) {
    if (isSpace(s[i]))
      continue;
    char n = s.get(i + 1);
    if (s[i] == '\\' && (n == '\r' || n == '\n')) {
      i++;
      continue;
    }
    break;
  }
  return s.substr(i, s.size() - i);
}

StringPiece TrimRightSpace(StringPiece s) {
  size_t i = 0;
  for (; i < s.size(); i++) {
    char c = s[s.size() - 1 - i];
    if (isSpace(c)) {
      if ((c == '\r' || c == '\n') && s.get(s.size() - 2 - i) == '\\')
        i++;
      continue;
    }
    break;
  }
  return s.substr(0, s.size() - i);
}

StringPiece TrimSpace(StringPiece s) {
  return TrimRightSpace(TrimLeftSpace(s));
}

StringPiece Dirname(StringPiece s) {
  size_t found = s.rfind('/');
  if (found == string::npos)
    return StringPiece(".");
  if (found == 0)
    return StringPiece("");
  return s.substr(0, found);
}

StringPiece Basename(StringPiece s) {
  size_t found = s.rfind('/');
  if (found == string::npos || found == 0)
    return s;
  return s.substr(found + 1);
}

StringPiece GetExt(StringPiece s) {
  size_t found = s.rfind('.');
  if (found == string::npos)
    return StringPiece("");
  return s.substr(found);
}

StringPiece StripExt(StringPiece s) {
  size_t slash_index = s.rfind('/');
  size_t found = s.rfind('.');
  if (found == string::npos ||
      (slash_index != string::npos && found < slash_index))
    return s;
  return s.substr(0, found);
}

void NormalizePath(string* o) {
  if (o->empty())
    return;
  size_t start_index = 0;
  if ((*o)[0] == '/')
    start_index++;
  size_t j = start_index;
  size_t prev_start = start_index;
  for (size_t i = start_index; i <= o->size(); i++) {
    char c = (*o)[i];
    if (c != '/' && c != 0) {
      (*o)[j] = c;
      j++;
      continue;
    }

    StringPiece prev_dir = StringPiece(o->data() + prev_start, j - prev_start);
    if (prev_dir == ".") {
      j--;
    } else if (prev_dir == ".." && j != 2 /* .. */) {
      if (j == 3) {
        // /..
        j = start_index;
      } else {
        size_t orig_j = j;
        j -= 4;
        j = o->rfind('/', j);
        if (j == string::npos) {
          j = start_index;
        } else {
          j++;
        }
        if (StringPiece(o->data() + j, 3) == "../") {
          j = orig_j;
          (*o)[j] = c;
          j++;
        }
      }
    } else if (!prev_dir.empty()) {
      if (c) {
        (*o)[j] = c;
        j++;
      }
    }
    prev_start = j;
  }
  if (j > 1 && (*o)[j - 1] == '/')
    j--;
  o->resize(j);
}

void AbsPath(StringPiece s, string* o) {
  if (s.get(0) == '/') {
    o->clear();
  } else {
    char buf[PATH_MAX];
    if (!getcwd(buf, PATH_MAX)) {
      fprintf(stderr, "getcwd failed\n");
      CHECK(false);
    }

    CHECK(buf[0] == '/');
    *o = buf;
    *o += '/';
  }
  AppendString(s, o);
  NormalizePath(o);
}

template <typename Cond>
size_t FindOutsideParenImpl(StringPiece s, Cond cond) {
  bool prev_backslash = false;
  stack<char> paren_stack;
  for (size_t i = 0; i < s.size(); i++) {
    char c = s[i];
    if (cond(c) && paren_stack.empty() && !prev_backslash) {
      return i;
    }
    switch (c) {
      case '(':
        paren_stack.push(')');
        break;
      case '{':
        paren_stack.push('}');
        break;

      case ')':
      case '}':
        if (!paren_stack.empty() && c == paren_stack.top()) {
          paren_stack.pop();
        }
        break;
    }
    prev_backslash = c == '\\' && !prev_backslash;
  }
  return string::npos;
}

size_t FindOutsideParen(StringPiece s, char c) {
  return FindOutsideParenImpl(s, [&c](char d) { return c == d; });
}

size_t FindTwoOutsideParen(StringPiece s, char c1, char c2) {
  return FindOutsideParenImpl(
      s, [&c1, &c2](char d) { return d == c1 || d == c2; });
}

size_t FindThreeOutsideParen(StringPiece s, char c1, char c2, char c3) {
  return FindOutsideParenImpl(
      s, [&c1, &c2, &c3](char d) { return d == c1 || d == c2 || d == c3; });
}

size_t FindEndOfLine(StringPiece s, size_t e, size_t* lf_cnt) {
  while (e < s.size()) {
    e += strcspn(s.data() + e, "\n\\");  // skip to line end
    if (e >= s.size()) {
      CHECK(s.size() == e);
      break;
    }
    char c = s[e];
    if (c == '\0')
      break;
    if (c == '\\') {
      if (s[e + 1] == '\n') {
        e += 2;
        ++*lf_cnt;
      } else if (s[e + 1] == '\r' && s[e + 2] == '\n') {
        e += 3;
        ++*lf_cnt;
      } else if (s[e + 1] == '\\') {
        e += 2;
      } else {
        e++;
      }
    } else if (c == '\n') {
      ++*lf_cnt;
      return e;
    }
  }
  return e;
}

StringPiece TrimLeadingCurdir(StringPiece s) {
  while (s.substr(0, 2) == "./")
    s = s.substr(2);
  return s;
}

void FormatForCommandSubstitution(string* s) {
  while ((*s)[s->size() - 1] == '\n')
    s->pop_back();
  for (size_t i = 0; i < s->size(); i++) {
    if ((*s)[i] == '\n')
      (*s)[i] = ' ';
  }
}

string SortWordsInString(StringPiece s) {
  vector<string> toks;
  for (StringPiece tok : WordScanner(s)) {
    toks.push_back(tok.as_string());
  }
  sort(toks.begin(), toks.end());
  return JoinStrings(toks, " ");
}

string ConcatDir(StringPiece b, StringPiece n) {
  string r;
  if (!b.empty() && (n.empty() || n[0] != '/')) {
    b.AppendToString(&r);
    r += '/';
  }
  n.AppendToString(&r);
  NormalizePath(&r);
  return r;
}

string EchoEscape(const string& str) {
  const char* in = str.c_str();
  string buf;
  for (; *in; in++) {
    switch (*in) {
      case '\\':
        buf += "\\\\\\\\";
        break;
      case '\n':
        buf += "\\n";
        break;
      case '"':
        buf += "\\\"";
        break;
      default:
        buf += *in;
    }
  }
  return buf;
}

void EscapeShell(string* s) {
  static const char delimiters[] = "\"$\\`";
  size_t prev = 0;
  size_t i = strcspn(s->c_str(), delimiters);
  if (i == s->size())
    return;

  string r;
  for (; i < s->size();) {
    StringPiece(*s).substr(prev, i - prev).AppendToString(&r);
    char c = (*s)[i];
    r += '\\';
    if (c == '$') {
      if ((*s)[i + 1] == '$') {
        r += '$';
        i++;
      }
    }
    r += c;
    i++;
    prev = i;
    i += strcspn(s->c_str() + i, delimiters);
  }
  StringPiece(*s).substr(prev).AppendToString(&r);
  s->swap(r);
}

bool IsInteger(StringPiece s) {
  if (s.size() == 0) {
    return false;
  }
  for (auto c : s) {
    if (c < '0' || c > '9') {
      return false;
    }
  }
  return true;
}
<commit_msg>strutil: SkipUntil: restore function and limit outcome to len<commit_after>// Copyright 2015 Google Inc. All rights reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build ignore

#include "strutil.h"

#include <ctype.h>
#include <limits.h>
#include <unistd.h>

#include <algorithm>
#include <functional>
#include <stack>
#include <utility>

#include "log.h"

static bool isSpace(char c) {
  return (9 <= c && c <= 13) || c == 32;
}

static int SkipUntil(const char* s, size_t len, const char* delimiters) {
  return std::min(len, strcspn(s, delimiters));
}

WordScanner::Iterator& WordScanner::Iterator::operator++() {
  int len = static_cast<int>(in->size());
  for (s = i + 1; s < len; s++) {
    if (!isSpace((*in)[s]))
      break;
  }
  if (s >= len) {
    in = NULL;
    s = 0;
    i = 0;
    return *this;
  }

  // skip until the next whitespace character
  i = s + SkipUntil(in->data() + s, len - s, "\x09\x0a\x0b\x0c\x0d ");
  return *this;
}

StringPiece WordScanner::Iterator::operator*() const {
  return in->substr(s, i - s);
}

WordScanner::WordScanner(StringPiece in) : in_(in) {}

WordScanner::Iterator WordScanner::begin() const {
  Iterator iter;
  iter.in = &in_;
  iter.s = 0;
  iter.i = -1;
  ++iter;
  return iter;
}

WordScanner::Iterator WordScanner::end() const {
  Iterator iter;
  iter.in = NULL;
  iter.s = 0;
  iter.i = 0;
  return iter;
}

void WordScanner::Split(vector<StringPiece>* o) {
  for (StringPiece t : *this)
    o->push_back(t);
}

WordWriter::WordWriter(string* o) : out_(o), needs_space_(false) {}

void WordWriter::MaybeAddWhitespace() {
  if (needs_space_) {
    out_->push_back(' ');
  } else {
    needs_space_ = true;
  }
}

void WordWriter::Write(StringPiece s) {
  MaybeAddWhitespace();
  AppendString(s, out_);
}

ScopedTerminator::ScopedTerminator(StringPiece s) : s_(s), c_(s[s.size()]) {
  const_cast<char*>(s_.data())[s_.size()] = '\0';
}

ScopedTerminator::~ScopedTerminator() {
  const_cast<char*>(s_.data())[s_.size()] = c_;
}

void AppendString(StringPiece str, string* out) {
  out->append(str.begin(), str.end());
}

bool HasPrefix(StringPiece str, StringPiece prefix) {
  ssize_t size_diff = str.size() - prefix.size();
  return size_diff >= 0 && str.substr(0, prefix.size()) == prefix;
}

bool HasSuffix(StringPiece str, StringPiece suffix) {
  ssize_t size_diff = str.size() - suffix.size();
  return size_diff >= 0 && str.substr(size_diff) == suffix;
}

bool HasWord(StringPiece str, StringPiece w) {
  size_t found = str.find(w);
  if (found == string::npos)
    return false;
  if (found != 0 && !isSpace(str[found - 1]))
    return false;
  size_t end = found + w.size();
  if (end != str.size() && !isSpace(str[end]))
    return false;
  return true;
}

StringPiece TrimPrefix(StringPiece str, StringPiece prefix) {
  ssize_t size_diff = str.size() - prefix.size();
  if (size_diff < 0 || str.substr(0, prefix.size()) != prefix)
    return str;
  return str.substr(prefix.size());
}

StringPiece TrimSuffix(StringPiece str, StringPiece suffix) {
  ssize_t size_diff = str.size() - suffix.size();
  if (size_diff < 0 || str.substr(size_diff) != suffix)
    return str;
  return str.substr(0, size_diff);
}

Pattern::Pattern(StringPiece pat) : pat_(pat), percent_index_(pat.find('%')) {}

bool Pattern::Match(StringPiece str) const {
  if (percent_index_ == string::npos)
    return str == pat_;
  return MatchImpl(str);
}

bool Pattern::MatchImpl(StringPiece str) const {
  return (HasPrefix(str, pat_.substr(0, percent_index_)) &&
          HasSuffix(str, pat_.substr(percent_index_ + 1)));
}

StringPiece Pattern::Stem(StringPiece str) const {
  if (!Match(str))
    return "";
  return str.substr(percent_index_,
                    str.size() - (pat_.size() - percent_index_ - 1));
}

void Pattern::AppendSubst(StringPiece str,
                          StringPiece subst,
                          string* out) const {
  if (percent_index_ == string::npos) {
    if (str == pat_) {
      AppendString(subst, out);
      return;
    } else {
      AppendString(str, out);
      return;
    }
  }

  if (MatchImpl(str)) {
    size_t subst_percent_index = subst.find('%');
    if (subst_percent_index == string::npos) {
      AppendString(subst, out);
      return;
    } else {
      AppendString(subst.substr(0, subst_percent_index), out);
      AppendString(str.substr(percent_index_, str.size() - pat_.size() + 1),
                   out);
      AppendString(subst.substr(subst_percent_index + 1), out);
      return;
    }
  }
  AppendString(str, out);
}

void Pattern::AppendSubstRef(StringPiece str,
                             StringPiece subst,
                             string* out) const {
  if (percent_index_ != string::npos && subst.find('%') != string::npos) {
    AppendSubst(str, subst, out);
    return;
  }
  StringPiece s = TrimSuffix(str, pat_);
  out->append(s.begin(), s.end());
  out->append(subst.begin(), subst.end());
}

string NoLineBreak(const string& s) {
  size_t index = s.find('\n');
  if (index == string::npos)
    return s;
  string r = s;
  while (index != string::npos) {
    r = r.substr(0, index) + "\\n" + r.substr(index + 1);
    index = r.find('\n', index + 2);
  }
  return r;
}

StringPiece TrimLeftSpace(StringPiece s) {
  size_t i = 0;
  for (; i < s.size(); i++) {
    if (isSpace(s[i]))
      continue;
    char n = s.get(i + 1);
    if (s[i] == '\\' && (n == '\r' || n == '\n')) {
      i++;
      continue;
    }
    break;
  }
  return s.substr(i, s.size() - i);
}

StringPiece TrimRightSpace(StringPiece s) {
  size_t i = 0;
  for (; i < s.size(); i++) {
    char c = s[s.size() - 1 - i];
    if (isSpace(c)) {
      if ((c == '\r' || c == '\n') && s.get(s.size() - 2 - i) == '\\')
        i++;
      continue;
    }
    break;
  }
  return s.substr(0, s.size() - i);
}

StringPiece TrimSpace(StringPiece s) {
  return TrimRightSpace(TrimLeftSpace(s));
}

StringPiece Dirname(StringPiece s) {
  size_t found = s.rfind('/');
  if (found == string::npos)
    return StringPiece(".");
  if (found == 0)
    return StringPiece("");
  return s.substr(0, found);
}

StringPiece Basename(StringPiece s) {
  size_t found = s.rfind('/');
  if (found == string::npos || found == 0)
    return s;
  return s.substr(found + 1);
}

StringPiece GetExt(StringPiece s) {
  size_t found = s.rfind('.');
  if (found == string::npos)
    return StringPiece("");
  return s.substr(found);
}

StringPiece StripExt(StringPiece s) {
  size_t slash_index = s.rfind('/');
  size_t found = s.rfind('.');
  if (found == string::npos ||
      (slash_index != string::npos && found < slash_index))
    return s;
  return s.substr(0, found);
}

void NormalizePath(string* o) {
  if (o->empty())
    return;
  size_t start_index = 0;
  if ((*o)[0] == '/')
    start_index++;
  size_t j = start_index;
  size_t prev_start = start_index;
  for (size_t i = start_index; i <= o->size(); i++) {
    char c = (*o)[i];
    if (c != '/' && c != 0) {
      (*o)[j] = c;
      j++;
      continue;
    }

    StringPiece prev_dir = StringPiece(o->data() + prev_start, j - prev_start);
    if (prev_dir == ".") {
      j--;
    } else if (prev_dir == ".." && j != 2 /* .. */) {
      if (j == 3) {
        // /..
        j = start_index;
      } else {
        size_t orig_j = j;
        j -= 4;
        j = o->rfind('/', j);
        if (j == string::npos) {
          j = start_index;
        } else {
          j++;
        }
        if (StringPiece(o->data() + j, 3) == "../") {
          j = orig_j;
          (*o)[j] = c;
          j++;
        }
      }
    } else if (!prev_dir.empty()) {
      if (c) {
        (*o)[j] = c;
        j++;
      }
    }
    prev_start = j;
  }
  if (j > 1 && (*o)[j - 1] == '/')
    j--;
  o->resize(j);
}

void AbsPath(StringPiece s, string* o) {
  if (s.get(0) == '/') {
    o->clear();
  } else {
    char buf[PATH_MAX];
    if (!getcwd(buf, PATH_MAX)) {
      fprintf(stderr, "getcwd failed\n");
      CHECK(false);
    }

    CHECK(buf[0] == '/');
    *o = buf;
    *o += '/';
  }
  AppendString(s, o);
  NormalizePath(o);
}

template <typename Cond>
size_t FindOutsideParenImpl(StringPiece s, Cond cond) {
  bool prev_backslash = false;
  stack<char> paren_stack;
  for (size_t i = 0; i < s.size(); i++) {
    char c = s[i];
    if (cond(c) && paren_stack.empty() && !prev_backslash) {
      return i;
    }
    switch (c) {
      case '(':
        paren_stack.push(')');
        break;
      case '{':
        paren_stack.push('}');
        break;

      case ')':
      case '}':
        if (!paren_stack.empty() && c == paren_stack.top()) {
          paren_stack.pop();
        }
        break;
    }
    prev_backslash = c == '\\' && !prev_backslash;
  }
  return string::npos;
}

size_t FindOutsideParen(StringPiece s, char c) {
  return FindOutsideParenImpl(s, [&c](char d) { return c == d; });
}

size_t FindTwoOutsideParen(StringPiece s, char c1, char c2) {
  return FindOutsideParenImpl(
      s, [&c1, &c2](char d) { return d == c1 || d == c2; });
}

size_t FindThreeOutsideParen(StringPiece s, char c1, char c2, char c3) {
  return FindOutsideParenImpl(
      s, [&c1, &c2, &c3](char d) { return d == c1 || d == c2 || d == c3; });
}

size_t FindEndOfLine(StringPiece s, size_t e, size_t* lf_cnt) {
  while (e < s.size()) {
    e += SkipUntil(s.data() + e, s.size() - e, "\n\\");  // skip to line end
    if (e >= s.size()) {
      CHECK(s.size() == e);
      break;
    }
    char c = s[e];
    if (c == '\0')
      break;
    if (c == '\\') {
      if (s[e + 1] == '\n') {
        e += 2;
        ++*lf_cnt;
      } else if (s[e + 1] == '\r' && s[e + 2] == '\n') {
        e += 3;
        ++*lf_cnt;
      } else if (s[e + 1] == '\\') {
        e += 2;
      } else {
        e++;
      }
    } else if (c == '\n') {
      ++*lf_cnt;
      return e;
    }
  }
  return e;
}

StringPiece TrimLeadingCurdir(StringPiece s) {
  while (s.substr(0, 2) == "./")
    s = s.substr(2);
  return s;
}

void FormatForCommandSubstitution(string* s) {
  while ((*s)[s->size() - 1] == '\n')
    s->pop_back();
  for (size_t i = 0; i < s->size(); i++) {
    if ((*s)[i] == '\n')
      (*s)[i] = ' ';
  }
}

string SortWordsInString(StringPiece s) {
  vector<string> toks;
  for (StringPiece tok : WordScanner(s)) {
    toks.push_back(tok.as_string());
  }
  sort(toks.begin(), toks.end());
  return JoinStrings(toks, " ");
}

string ConcatDir(StringPiece b, StringPiece n) {
  string r;
  if (!b.empty() && (n.empty() || n[0] != '/')) {
    b.AppendToString(&r);
    r += '/';
  }
  n.AppendToString(&r);
  NormalizePath(&r);
  return r;
}

string EchoEscape(const string& str) {
  const char* in = str.c_str();
  string buf;
  for (; *in; in++) {
    switch (*in) {
      case '\\':
        buf += "\\\\\\\\";
        break;
      case '\n':
        buf += "\\n";
        break;
      case '"':
        buf += "\\\"";
        break;
      default:
        buf += *in;
    }
  }
  return buf;
}

void EscapeShell(string* s) {
  static const char delimiters[] = "\"$\\`";
  size_t prev = 0;
  size_t i = SkipUntil(s->c_str(), s->size(), delimiters);
  if (i == s->size())
    return;

  string r;
  for (; i < s->size();) {
    StringPiece(*s).substr(prev, i - prev).AppendToString(&r);
    char c = (*s)[i];
    r += '\\';
    if (c == '$') {
      if ((*s)[i + 1] == '$') {
        r += '$';
        i++;
      }
    }
    r += c;
    i++;
    prev = i;
    i += SkipUntil(s->c_str() + i, s->size() - i, delimiters);
  }
  StringPiece(*s).substr(prev).AppendToString(&r);
  s->swap(r);
}

bool IsInteger(StringPiece s) {
  if (s.size() == 0) {
    return false;
  }
  for (auto c : s) {
    if (c < '0' || c > '9') {
      return false;
    }
  }
  return true;
}
<|endoftext|>