MPQData::MPQData(MPQFile *file) : error(ERROR_SUCCESS) {
	DWORD dwBytes = 1;
	std::string buffer;
	char szBuffer[0x10000];
	while (dwBytes > 0) {
		SFileReadFile(file->GetHandle(), szBuffer, sizeof(szBuffer), &dwBytes, NULL);
		if (dwBytes > 0) {
			buffer.append(szBuffer, dwBytes);
		}
	}

	// TODO: need to remove \r, \n chars here
	if (error == ERROR_SUCCESS) {
		std::stringstream ss(buffer);
		std::string line;
		std::string field;
		if (std::getline(ss, line)) {  // read the header first
			std::stringstream hss(line);
			while (std::getline(hss, field, '\t')) {
				fields.push_back(field);
			}
			while (std::getline(ss, line)) {
				std::map<std::string, std::string> linedata;
				std::stringstream fss(line);
				for (std::vector<std::string>::iterator it = fields.begin(); it != fields.end(); it++) {
					if (!std::getline(fss, field, '\t')) {
						field.clear();
					}
					linedata[(*it)] = field;
				}
				data.push_back(linedata);
			}
		}
	}
}
void HttpStringStreamTester::testWriteAllAtOnce(const SampleResponse &resp)
{
    InStringStream iss(sampleGetReq.GetRequest());
    OutStringStream oss;
    HttpStringStream hss(&iss, &oss);

    const std::string &response = resp.GetResponse();
    ssize_t bytes = hss.Write(response.data(), response.size());
    assert(bytes == response.size());
    assert(oss.GetOutput() == response);
}
void HttpStringStreamTester::testReadAllAtOnce(const SampleRequest &req)
{
    const std::string &request = req.GetRequest();
    InStringStream iss(request);
    OutStringStream oss;
    HttpStringStream hss(&iss, &oss);

    char buf[4096];  // large enough to read in a single call
    ssize_t bytes = hss.Read(buf, sizeof(buf));
    assert(bytes == request.size());
    assert(request.compare(0, request.npos, buf, bytes) == 0);

    bytes = hss.Read(buf, sizeof(buf));
    assert(bytes == 0);  // 0 means EOF
}
void HttpStringStreamTester::testWriteUptoFirstBodyByte(const SampleResponse &resp)
{
    // Response header should be buffered completed, only to be written out on Flush or first byte of body.

    InStringStream iss(sampleGetReq.GetRequest());
    OutStringStream oss;
    HttpStringStream hss(&iss, &oss);

    const std::string &response = resp.GetResponse();
    ssize_t bytes = hss.Write(response.data(), resp.GetAnsHeaderSize());
    assert(bytes == sampleGetResp.GetAnsHeaderSize());
    assert(oss.GetOutput().empty());

    bytes = hss.Write(response.data() + resp.GetAnsHeaderSize(), 1);
    assert(bytes == 1);
    assert(oss.GetOutput().compare(0, std::string::npos, response, 0, resp.GetAnsHeaderSize() + 1) == 0);
}
void HttpStringStreamTester::testReadAfterReadAttr()
{
    {
        const std::string &request = samplePutReq.GetRequest();
        InStringStream iss(request);
        OutStringStream oss;
        HttpStringStream hss(&iss, &oss);
        std::string method;
        int err = hss.GetMethod(method);
        assert(!err);
        assert(method == "PUT");
        char buf[4096];  // large enough to read in one call
        ssize_t bytes = hss.Read(buf, sizeof(buf));
        assert(bytes == request.size());
        assert(memcmp(buf, request.data(), bytes) == 0);
    }
}
void HttpStringStreamTester::testSetRespAttrsThenBody()
{
    InStringStream _iss(sampleGetReq.GetRequest());
    OutStringStream _oss;
    HttpStringStream hss(&_iss, &_oss);

    int err;
    err = hss.SetStatusCode(200);
    assert(!err);
    assert(_oss.GetOutput().empty());
    err = hss.SetRespHeader("Content-Length", "10");
    assert(!err);
    assert(_oss.GetOutput().empty());

    ssize_t bytes = hss.Write("1234567890", 10);
    assert(bytes == 10);
    assert(_oss.GetOutput().compare("HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\n1234567890") == 0);
}
void HttpStringStreamTester::testSetRespAttrs()
{
    InStringStream _iss(sampleGetReq.GetRequest());
    OutStringStream _oss;
    HttpStringStream hss(&_iss, &_oss);

    int err;
    err = hss.SetStatusCode(200);
    assert(!err);
    assert(_oss.GetOutput().empty());
    err = hss.SetRespHeader("Content-Length", "10");
    assert(!err);
    assert(_oss.GetOutput().empty());

    err = hss.Flush();
    assert(!err);
    assert(_oss.GetOutput().compare("HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\n") == 0);
}
void HttpStringStreamTester::testWriteOnlyHeader(const SampleResponse &resp)
{
    // Response header should be buffered completed, only to be written out on Flush or first byte of body.
    // Thus, if only the response buffer is written, nothing should be written out to the underlying stream.

    InStringStream iss(sampleGetReq.GetRequest());
    OutStringStream oss;
    HttpStringStream hss(&iss, &oss);

    const std::string &response = resp.GetResponse();
    ssize_t bytes = hss.Write(response.data(), resp.GetAnsHeaderSize());
    assert(bytes == resp.GetAnsHeaderSize());
    assert(oss.GetOutput().empty());

    int err = hss.Flush();
    assert(!err);
    std::string out = oss.GetOutput();
    assert(response.compare(0, out.size(), out) == 0);
}
void HttpStringStreamTester::testRead_TwoReqsInStream(const SampleRequest &req1, const SampleRequest &req2)
{
    // Case: Two requests in the underlying stream.
    //       Read() should only return the first request and then report EOF.

    const std::string &request1 = req1.GetRequest();
    const std::string &request2 = req2.GetRequest();
    InStringStream iss(request1 + request2);  // two reqs back-to-back
    OutStringStream oss;
    HttpStringStream hss(&iss, &oss);
    char buf[4096];  // large enough to read in once call
    ssize_t bytes;

    bytes = hss.Read(buf, sizeof(buf));
    assert(bytes == request1.size());
    assert (memcmp(buf, request1.data(), request1.size()) == 0);

    bytes = hss.Read(buf, sizeof(buf));
    assert(bytes == 0);  // 0 means EOF
}
void HttpStringStreamTester::testReadAttributes()
{
    {
        const std::string &request = sampleGetReq.GetRequest();
        InStringStream iss(request);
        OutStringStream oss;
        HttpStringStream hss(&iss, &oss);
        std::string method;
        int err = hss.GetMethod(method);
        assert(!err);
        assert(method == sampleGetReq.GetAnsMethod());
        std::string uri;
        err = hss.GetUri(uri);
        assert(!err);
        assert(uri == sampleGetReq.GetAnsUri());
        std::string version;
        err = hss.GetVersion(version);
        assert(!err);
        assert(version == sampleGetReq.GetAnsVersion());
    }
}
void HttpStringStreamTester::testReadInBits(const SampleRequest &req)
{
    const std::string &request = req.GetRequest();
    InStringStream _iss(request);
    OutStringStream _oss;
    HttpStringStream hss(&_iss, &_oss);

    char buf[2];  // small enough to require multiple reads
    ssize_t bytes;
    std::ostringstream oss;
    while (1) {
        bytes = hss.Read(buf, sizeof(buf));
        assert(bytes >= 0);
        if (bytes == 0) break;
        oss.write(buf, bytes);
    }
    assert(oss.str().compare(request) == 0);

    bytes = hss.Read(buf, sizeof(buf));
    assert(bytes == 0);  // 0 means EOF
}
MPQData::MPQData(MPQFile *file) : error(ERROR_SUCCESS) {
	DWORD dwBytes = 1;
	std::string buffer;
	char szBuffer[0x10000];
	while (dwBytes > 0) {
		SFileReadFile(file->GetHandle(), szBuffer, sizeof(szBuffer), &dwBytes, NULL);
		if (dwBytes > 0) {
			buffer.append(szBuffer, dwBytes);
		}
	}

	// TODO: need to remove \r, \n chars here
	if (error == ERROR_SUCCESS) {
		FILE *phil = fopen("C:\\philtest.txt", "a+"); fprintf(phil, "STARTING A NEW FILE %s\n", file->name.c_str());
		std::stringstream ss(buffer);
		std::string line;
		std::string field;
		if (std::getline(ss, line)) {  // read the header first
			std::stringstream hss(line);
			while (std::getline(hss, field, '\t')) {
				fields.push_back(field);
				fprintf(phil, "Got header field name: %s\n", field.c_str());
			}
			while (std::getline(ss, line)) {
				std::map<std::string, std::string> linedata;
				std::stringstream fss(line);
				for (std::vector<std::string>::iterator it = fields.begin(); it != fields.end(); it++) {
					if (!std::getline(fss, field, '\t')) {
						field.clear();
					}
					linedata[(*it)] = field;
				}
				data.push_back(linedata);
			}
			fprintf(phil, "Number of data lines: %d\n", data.size());
		}
		fclose(phil);
	}
}
Beispiel #13
0
void LatticeBuilder::Init(Input &in)
{

  istringstream iss(in.getAttribute<string>("dims"));
  copy(istream_iterator<int>(iss), istream_iterator<int>(), back_inserter(TileMat));

  nD = TileMat.size();
  int id = 0;
  Site unitCell;
  vector<Input> siteList = in.getChild("Site").getChildList();
  for (int i=0; i<siteList.size(); ++i) {
    Subsite ss;
    ss.id = id;
    id += 1;
    ss.V = siteList[i].getAttribute<RealType>("V");
    ss.U = siteList[i].getAttribute<RealType>("U");

    istringstream hss(siteList[i].getAttribute<string>("J") );
    copy(istream_iterator<RealType>(hss), istream_iterator<RealType>(), back_inserter(ss.J_ij));

    istringstream iss(siteList[i].getAttribute<string>("T") );
    copy(istream_iterator<RealType>(iss), istream_iterator<RealType>(), back_inserter(ss.T_ij));

    vector<int> siteData;
    istringstream jss(siteList[i].getText("Topology"));
    copy(istream_iterator<int>(jss), istream_iterator<int>(), back_inserter(siteData));
    int nn = siteData.size()/(nD+1.);

    for(int i=0; i<nn; i++) {
      Neighbor n;
      n.dL.resize(nD);
      n.ss = siteData[i*(nD+1)];
      for(int j=0; j<nD; j++)
        n.dL[j] = siteData[1+j + i*(nD+1)];
      ss.addNeighbor(n);
    }
    unitCell.add(ss);
  }
  unitCell.id = 0;

  nLatticeSites = 1;
  for(int i=0; i<nD; i++)
    nLatticeSites *= TileMat[i];
  Sites.resize(nLatticeSites, unitCell);

  nSubSites = unitCell.nSubSites();
  nSites = nLatticeSites*nSubSites;

  for(int i=0;i<nLatticeSites;i++)
  {
    Sites[i].id=i;
    for(int j=0;j<nSubSites;j++)
    {
      Sites[i].SubSites[j].globalId=i*nSubSites+j;
      Sites[i].SubSites[j].nn_ij.resize(Sites[i].SubSites[j].T_ij.size());
      for(int k=0;k<Sites[i].SubSites[j].T_ij.size();k++)
        Sites[i].SubSites[j].nn_ij[k] = GetNeighborId(i, Sites[i].SubSites[j].nn[k])*nSubSites + Sites[i].SubSites[j].nn[k].ss;
    }
  }

};