void OSGA_Archive::IndexBlock::write(std::ostream& out)
{
    pos_type currentPos = ARCHIVE_POS( out.tellp() );

    if (_filePosition==pos_type(0))
    {
        OSG_INFO<<"OSGA_Archive::IndexBlock::write() setting _filePosition"<<std::endl;
        _filePosition = currentPos;
    }
    else
    {
         out.seekp( STREAM_POS( _filePosition ) );
    }
    OSG_INFO<<"OSGA_Archive::IndexBlock::write() to _filePosition"<< ARCHIVE_POS( out.tellp() )<<std::endl;

    out.write(reinterpret_cast<char*>(&_blockSize), sizeof(_blockSize));
    out.write(reinterpret_cast<char*>(&_filePositionNextIndexBlock), sizeof(_filePositionNextIndexBlock));
    out.write(reinterpret_cast<char*>(&_offsetOfNextAvailableSpace), sizeof(_offsetOfNextAvailableSpace));

    out.write(reinterpret_cast<char*>(_data),_blockSize);

    if( _filePosition < currentPos ) // move file ptr to the end of file
        out.seekp( STREAM_POS( currentPos ) );

    OSG_INFO<<"OSGA_Archive::IndexBlock::write() end"<<std::endl;
}
Example #2
0
	void recursive_treewrite(std::ostream &out, radix_tree_node<std::string, std::unordered_set<zsr::filecount>> *n, zsr::offset treestart)
	{
		static int nwritten = 0;
		logb(++nwritten);
		treesize nval = 0;
		if (n->m_children.count("") && n->m_children[""]->m_value) nval = n->m_children[""]->m_value->second.size();
		treesize nchild = 0;
		for (const std::pair<const std::string, radix_tree_node<std::string, std::unordered_set<zsr::filecount>> *> child : n->m_children) if (child.first != "" && child.second) nchild++; // TODO Probably a faster way?
		zsr::serialize(out, nchild);
		std::deque<zsr::offset> childpos{};
		for (const std::pair<const std::string, radix_tree_node<std::string, std::unordered_set<zsr::filecount>> *> child : n->m_children)
		{
			if (child.first == "" || ! child.second) continue;
			uint16_t namelen = child.first.size();
			zsr::serialize(out, namelen);
			out.write(&child.first[0], namelen);
			childpos.push_back(static_cast<zsr::offset>(out.tellp()));
			zsr::serialize(out, ptrfill);
		}
		zsr::serialize(out, nval);
		if (n->m_children.count("") && n->m_children[""]->m_value)
			for (const zsr::filecount &i : n->m_children[""]->m_value->second) zsr::serialize(out, i);
		for (const std::pair<const std::string, radix_tree_node<std::string, std::unordered_set<zsr::filecount>> *> child : n->m_children)
		{
			if (child.first == "" || ! child.second) continue;
			zsr::offset childstart = static_cast<zsr::offset>(out.tellp()) - treestart;
			out.seekp(childpos.front());
			zsr::serialize(out, childstart);
			out.seekp(0, std::ios_base::end);
			recursive_treewrite(out, child.second, treestart);
			childpos.pop_front();
		}
	}
Example #3
0
void WorldRegion::saveToStream(std::ostream& regionData, std::ostream& chunkData)
{
	// Save the Pillars
	bio::gzip_compressor gzComp;
	bio::filtering_ostream gzOut;
	gzOut.push(gzComp);

	for (auto pillarIt = mPillars.begin(); pillarIt != mPillars.end(); ++pillarIt) {
		if ((*pillarIt != 0) && (*pillarIt)->isModified()) {
			std::ostringstream pillarData;
			gzOut.push(pillarData);
			(*pillarIt)->saveToStream(gzOut);
			gzOut.pop();

			uint32_t pillarDataSize = static_cast<uint32_t>(pillarData.str().size());
			uint32_t sectorOffset = findFreeRegionSectorOffset(*pillarIt, pillarDataSize);
			if (sectorOffset == 0) {
				throw std::exception("Couldn't find big enough free sector in RegionFile!");
			}

			uint32_t offset = getSectorFromOffset(sectorOffset) * RegionFileSectorSize;
			regionData.seekp(offset);
			regionData.write(pillarData.str().c_str(), pillarDataSize);
		}
	}

	// Save RegionFile Header
	regionData.seekp(0);
	regionData.write(reinterpret_cast<char*>(mPillarOffsets.data()), mPillarOffsets.size() * sizeof(mPillarOffsets[0]));

	// Now save the chunks
	saveChunksToStream(chunkData);
}
Example #4
0
	inline std::ostream::pos_type get_size(std::ostream& stream)
	{
		auto original_pos = stream.tellp();
		stream.seekp(0, std::ios::end);
		auto pos = stream.tellp();
		stream.seekp(original_pos);
		return pos;
	}
Example #5
0
/** Write data to a file section.
 *
 *   @param f       Output steam to which to write
 *   @param offset  Byte offset relative to start of this section
 *   @param bufsize Size of @p buf in bytes
 *   @param buf     Buffer of bytes to be written
 *
 *  @returns Returns the section-relative byte offset for the first byte beyond what would have been written if all bytes
 *  of the buffer were written.
 *
 *  The buffer is allowed to extend past the end of the section as long as the part that extends beyond is all zeros. The
 *  zeros will not be written to the output file.  Furthermore, any trailing zeros that extend beyond the end of the file will
 *  not be written (end-of-file is determined by SgAsmGenericFile::get_orig_size()) */
rose_addr_t
SgAsmGenericSection::write(std::ostream &f, rose_addr_t offset, size_t bufsize, const void *buf) const
{
    size_t nwrite, nzero;

    ROSE_ASSERT(this != NULL);

    /* Don't write past end of section */
    if (offset>=get_size()) {
        nwrite = 0;
        nzero  = bufsize;
    } else if (offset+bufsize<=get_size()) {
        nwrite = bufsize;
        nzero = 0;
    } else {
        nwrite = get_size() - offset;
        nzero = bufsize - nwrite;
    }

    /* Don't write past end of current EOF if we can help it. */
    f.seekp(0, std::ios::end);
    rose_addr_t filesize = f.tellp();
    while (nwrite>0 && 0==((const char*)buf)[nwrite-1] && get_offset()+offset+nwrite>filesize)
        --nwrite;

    /* Write bytes to file. This is a good place to set a break point if you're trying to figure out what section is writing
     * to a particular file address. For instance, if byte 0x7c is incorrect in the unparsed file you would set a conditional
     * breakpoint for o<=0x7c && o+nwrite>0x7c */
    ROSE_ASSERT(f);
    off_t o = get_offset() + offset;
    f.seekp(o);
    ROSE_ASSERT(f);
    f.write((const char*)buf, nwrite);
    ROSE_ASSERT(f);

    /* Check that truncated data is all zero and fail if it isn't */
    for (size_t i=nwrite; i<bufsize; i++) {
        if (((const char*)buf)[i]) {
            char mesg[1024];
            sprintf(mesg, "non-zero value truncated: buf[0x%zx]=0x%02x", i, ((const unsigned char*)buf)[i]);
            fprintf(stderr, "SgAsmGenericSection::write: error: %s", mesg);
            fprintf(stderr, " in [%d] \"%s\"\n", get_id(), get_name()->get_string(true).c_str());
            fprintf(stderr, "    section is at file offset 0x%08"PRIx64" (%"PRIu64"), size 0x%"PRIx64" (%"PRIu64") bytes\n", 
                    get_offset(), get_offset(), get_size(), get_size());
            fprintf(stderr, " write %" PRIuPTR " byte%s at section offset 0x%08"PRIx64"\n", bufsize, 1==bufsize?"":"s", offset);
            fprintf(stderr, "      ");
            HexdumpFormat hf;
            hf.prefix = "      ";
            hexdump(stderr, get_offset()+offset, (const unsigned char*)buf, bufsize, hf);
            fprintf(stderr, "\n");
            throw SgAsmGenericFile::ShortWrite(this, offset, bufsize, mesg);
        }
    }

    return offset+bufsize;
}
/** Write data to a file section.
 *
 *   @param f       Output steam to which to write
 *   @param offset  Byte offset relative to start of this section
 *   @param bufsize Size of @p buf in bytes
 *   @param buf     Buffer of bytes to be written
 *
 *  @returns Returns the section-relative byte offset for the first byte beyond what would have been written if all bytes
 *  of the buffer were written.
 *
 *  The buffer is allowed to extend past the end of the section as long as the part that extends beyond is all zeros. The
 *  zeros will not be written to the output file.  Furthermore, any trailing zeros that extend beyond the end of the file will
 *  not be written (end-of-file is determined by SgAsmGenericFile::get_orig_size()) */
rose_addr_t
SgAsmGenericSection::write(std::ostream &f, rose_addr_t offset, size_t bufsize, const void *buf) const
{
    size_t nwrite;

    ROSE_ASSERT(this != NULL);

    /* Don't write past end of section */
    if (offset>=get_size()) {
        nwrite = 0;
    } else if (offset+bufsize<=get_size()) {
        nwrite = bufsize;
    } else {
        nwrite = get_size() - offset;
    }

    /* Don't write past end of current EOF if we can help it. */
    f.seekp(0, std::ios::end);
    rose_addr_t filesize = f.tellp();
    while (nwrite>0 && 0==((const char*)buf)[nwrite-1] && get_offset()+offset+nwrite>filesize)
        --nwrite;

    /* Write bytes to file. This is a good place to set a break point if you're trying to figure out what section is writing
     * to a particular file address. For instance, if byte 0x7c is incorrect in the unparsed file you would set a conditional
     * breakpoint for o<=0x7c && o+nwrite>0x7c */
    ROSE_ASSERT(f);
    off_t o = get_offset() + offset;
    f.seekp(o);
    ROSE_ASSERT(f);
    f.write((const char*)buf, nwrite);
    ROSE_ASSERT(f);

    /* Check that truncated data is all zero and fail if it isn't */
    for (size_t i=nwrite; i<bufsize; i++) {
        if (((const char*)buf)[i]) {
            char mesg[1024];
            sprintf(mesg, "non-zero value truncated: buf[0x%zx]=0x%02x", i, ((const unsigned char*)buf)[i]);
            mlog[ERROR] <<"SgAsmGenericSection::write: error: " <<mesg
                        <<" in [" <<get_id() <<"] \"" <<get_name()->get_string(true) <<"\"\n"
                        <<"    section is at file offset " <<StringUtility::addrToString(get_offset())
                        <<" size " <<StringUtility::plural(get_size(), "bytes") <<"\n"
                        <<"    write " <<StringUtility::plural(bufsize, "bytes")
                        <<" at section offset " <<StringUtility::addrToString(offset) <<"\n";
            HexdumpFormat hf;
            hf.prefix = "      ";
            hexdump(mlog[ERROR], get_offset()+offset, (const unsigned char*)buf, bufsize, hf);
            mlog[ERROR] <<"\n";
            throw SgAsmGenericFile::ShortWrite(this, offset, bufsize, mesg);
        }
    }

    return offset+bufsize;
}
Example #7
0
WError WMaterial::SaveToStream(WFile* file, std::ostream& outputStream) {
	if (!Valid())
		return WError(W_NOTVALID);

	VkDevice device = m_app->GetVulkanDevice();

	// write the UBO data
	uint tmp = m_uniformBuffers.size();
	outputStream.write((char*)&tmp, sizeof(tmp));
	for (uint i = 0; i < m_uniformBuffers.size(); i++) {
		UNIFORM_BUFFER_INFO* UBO = &m_uniformBuffers[i];
		outputStream.write((char*)&UBO->descriptor.range, sizeof(UBO->descriptor.range));
		void* data;
		VkResult vkRes = vkMapMemory(device, m_uniformBuffers[i].memory, 0, UBO->descriptor.range, 0, (void **)&data);
		if (vkRes)
			return WError(W_UNABLETOMAPBUFFER);
		outputStream.write((char*)data, UBO->descriptor.range);
		vkUnmapMemory(device, m_uniformBuffers[0].memory);
	}

	// write the texture data
	tmp = m_sampler_info.size();
	outputStream.write((char*)&tmp, sizeof(tmp));
	std::streampos texturesOffset = outputStream.tellp();
	for (uint i = 0; i < m_sampler_info.size(); i++) {
		SAMPLER_INFO* SI = &m_sampler_info[i];
		tmp = 0;
		outputStream.write((char*)&tmp, sizeof(tmp));
		outputStream.write((char*)&SI->sampler_info->binding_index, sizeof(SI->sampler_info->binding_index));
	}
	outputStream.write((char*)&tmp, sizeof(tmp)); // effect id
	_MarkFileEnd(file, outputStream.tellp());

	// save dependencies
	for (uint i = 0; i < m_sampler_info.size(); i++) {
		SAMPLER_INFO* SI = &m_sampler_info[i];
		if (SI->img) {
			WError err = file->SaveAsset(SI->img, &tmp);
			if (!err)
				return err;
			outputStream.seekp(texturesOffset + std::streamoff(i * (2 * sizeof(uint))));
			outputStream.write((char*)&tmp, sizeof(tmp));
		}
	}
	WError err = file->SaveAsset(m_effect, &tmp);
	if (!err)
		return err;
	outputStream.seekp(texturesOffset + std::streamoff(m_sampler_info.size() * (2 * sizeof(uint))));
	outputStream.write((char*)&tmp, sizeof(tmp));

	return WError(W_SUCCEEDED);
}
Example #8
0
void VSTPresets::SaveProgram(std::ostream &f, CVstPlugin &plugin)
//---------------------------------------------------------------
{
	bool writeChunk = plugin.ProgramsAreChunks();
	ChunkHeader header;
	header.chunkMagic = cMagic;
	header.version = 1;
	header.fxID = plugin.GetUID();
	header.fxVersion = plugin.GetVersion();

	// Write unfinished header... We need to update the size once we're done writing.
	std::streamoff start = f.tellp();
	Write(header, f);

	const uint32 numParams = plugin.GetNumParameters();
	WriteBE(numParams, f);

	char name[MAX(kVstMaxProgNameLen + 1, 256)];
	plugin.Dispatch(effGetProgramName, 0, 0, name, 0);
	f.write(name, 28);

	if(writeChunk)
	{
		char *chunk = nullptr;
		uint32 chunkSize = mpt::saturate_cast<uint32>(plugin.Dispatch(effGetChunk, 1, 0, &chunk, 0));
		if(chunkSize && chunk)
		{
			WriteBE(chunkSize, f);
			f.write(chunk, chunkSize);
		} else
		{
			// The plugin returned no chunk! Gracefully go back and save parameters instead...
			writeChunk = false;
		}
	}
	if(!writeChunk)
	{
		for(uint32 p = 0; p < numParams; p++)
		{
			WriteBE(plugin.GetParameter(p), f);
		}
	}

	// Now we know the correct chunk size.
	std::streamoff end = f.tellp();
	header.byteSize = static_cast<VstInt32>(end - start - 8);
	header.fxMagic = writeChunk ? chunkPresetMagic : fMagic;
	header.ConvertEndianness();
	f.seekp(start);
	Write(header, f);
	f.seekp(end);
}
Example #9
0
void WorldRegion::saveChunksToStream(std::ostream& chunksDataStream)
{
	// Save the chunks
	bio::gzip_compressor gzComp;
	bio::filtering_ostream gzOut;
	gzOut.push(gzComp);
	std::vector<Chunk*> chunksToSave;
	
	for (auto pillarIt = mPillars.begin(); pillarIt != mPillars.end(); ++pillarIt) {
		
		if (*pillarIt == 0) {
			continue;
		}

		// Get all chunks to save from the pillar
		(*pillarIt)->getChunksToSave(chunksToSave);

		for (size_t numChunks = 0; numChunks < chunksToSave.size(); ++numChunks) {
			Chunk* curChunk = chunksToSave[numChunks];
			std::ostringstream chunkData;
			gzOut.push(chunkData);
			if (!curChunk->saveToStream(gzOut)) {
				// No data or error occured, skip chunk
				gzOut.pop();
				continue;
			}
			gzOut.pop();

			uint32_t chunkDataSize = static_cast<uint32_t>(chunkData.str().size());
			log << "Chunk x:" << (int) curChunk->mX << " y:" << (int) curChunk->mY << " z:" << (int) curChunk->mZ << " Size: " << (unsigned int) chunkDataSize << std::endl;
			uint32_t sectorOffset = findFreeChunkSectorOffset(curChunk, chunkDataSize);
			if (sectorOffset == 0) {
				throw std::exception("Couldn't find big enough free sector in ChunkFile!");
			}

			uint32_t offset = getSectorFromOffset(sectorOffset) * ChunkFileSectorSize;
			chunksDataStream.seekp(offset);
			chunksDataStream.write(chunkData.str().c_str(), chunkDataSize);
		}
		chunksToSave.clear();
	}


	// Save ChunkFile Header
	for (auto chunkIt = mChunkOffsets.begin(); chunkIt != mChunkOffsets.end(); ++chunkIt) {
		if (chunkIt->second > 0) {
			chunksDataStream.seekp(chunkIt->first * sizeof(chunkIt->second));
			chunksDataStream.write(reinterpret_cast<char*>(&chunkIt->second), sizeof(chunkIt->second));
		}
	}
}
Example #10
0
void AVR::write(std::ostream& os, const AVRTexture& texture)
{
	os.write("TEXT", 4);
	auto sizePos = os.tellp();
	os.seekp(4, std::ios::cur);
	auto startPos = os.tellp();

	os.write(texture.mPath.c_str(), texture.mPath.length() + 1);

	auto endPos = os.tellp();
	uint32_t size = endPos - startPos;
	os.seekp(sizePos);
	os.write((char*) &size, 4);
	os.seekp(endPos);
}
Example #11
0
WError WEffect::SaveToStream(WFile* file, std::ostream& outputStream) {
	if (!Valid())
		return WError(W_NOTVALID);

	uint tmp;

	outputStream.write((char*)&m_topology, sizeof(m_topology));
	outputStream.write((char*)&m_depthStencilState, sizeof(m_depthStencilState));
	outputStream.write((char*)&m_rasterizationState, sizeof(m_rasterizationState));
	tmp = m_blendStates.size();
	outputStream.write((char*)&tmp, sizeof(tmp));
	outputStream.write((char*)m_blendStates.data(), m_blendStates.size() * sizeof(VkPipelineColorBlendAttachmentState));
	tmp = m_shaders.size();
	outputStream.write((char*)&tmp, sizeof(tmp));
	std::streampos shaderIdsBegin = outputStream.tellp();
	for (uint i = 0; i < m_shaders.size(); i++) {
		tmp = 0;
		outputStream.write((char*)&tmp, sizeof(tmp));
	}
	_MarkFileEnd(file, outputStream.tellp());
	for (uint i = 0; i < m_shaders.size(); i++) {
		WError err = file->SaveAsset(m_shaders[i], &tmp);
		if (!err)
			return err;
		outputStream.seekp(shaderIdsBegin + std::streamoff(i * sizeof(uint)));
		outputStream.write((char*)&tmp, sizeof(tmp));
	}

	return WError(W_SUCCEEDED);
}
Example #12
0
bool VSTPresets::SaveFile(std::ostream &f, CVstPlugin &plugin, bool bank)
//-----------------------------------------------------------------------
{
	if(!bank)
	{
		SaveProgram(f, plugin);
	} else
	{
		bool writeChunk = plugin.ProgramsAreChunks();
		ChunkHeader header;
		header.chunkMagic = cMagic;
		header.version = 2;
		header.fxID = plugin.GetUID();
		header.fxVersion = plugin.GetVersion();

		// Write unfinished header... We need to update the size once we're done writing.
		Write(header, f);

		uint32 numProgs = std::max(plugin.GetNumPrograms(), VstInt32(1)), curProg = plugin.GetCurrentProgram();
		WriteBE(numProgs, f);
		WriteBE(curProg, f);
		char reserved[124];
		MemsetZero(reserved);
		Write(reserved, f);

		if(writeChunk)
		{
			char *chunk = nullptr;
			uint32 chunkSize = mpt::saturate_cast<uint32>(plugin.Dispatch(effGetChunk, 0, 0, &chunk, 0));
			if(chunkSize && chunk)
			{
				WriteBE(chunkSize, f);
				f.write(chunk, chunkSize);
			} else
			{
				// The plugin returned no chunk! Gracefully go back and save parameters instead...
				writeChunk = false;
			}
		}
		if(!writeChunk)
		{
			for(uint32 p = 0; p < numProgs; p++)
			{
				plugin.SetCurrentProgram(p);
				SaveProgram(f, plugin);
			}
			plugin.SetCurrentProgram(curProg);
		}

		// Now we know the correct chunk size.
		std::streamoff end = f.tellp();
		header.byteSize = static_cast<VstInt32>(end - 8);
		header.fxMagic = writeChunk ? chunkBankMagic : bankMagic;
		header.ConvertEndianness();
		f.seekp(0);
		Write(header, f);
	}

	return true;
}
Example #13
0
void Display::reset(std::ostream& os) {
	pthread_mutex_lock(&Display::mutex);
	os.flush();
        os.clear();
        os.seekp(0);
	pthread_mutex_unlock(&Display::mutex);
}
Example #14
0
 inline void advance(size_t s) {
   if (out == NULL) {
     expand_buf(s);
     off += s;
   } else {
     out->seekp(s, std::ios_base::cur);
   }
 }
Example #15
0
    bool
    save( std::ostream& stream ) const
    {
        stream.seekp( 0 );
        stream.write( reinterpret_cast<const char*>( &header ), sizeof( header ) );

        return stream.good();
    }
Example #16
0
void Support::rewriteHeader(std::ostream& stream, const SummaryData& data)
{
    // move from header start to "number of point records" field
    stream.seekp(107, std::ios_base::cur);

    {
        boost::uint8_t buf[256];
        boost::uint8_t* p = buf;

        Utils::write_field<boost::uint32_t>(p, data.getTotalNumPoints());

        for (int i=1; i<=SummaryData::s_maxNumReturns; i++)
        {
            Utils::write_field<boost::uint32_t>(p, data.getReturnCount(i));
        }

        Utils::write_n(stream, buf, 4 + 4*SummaryData::s_maxNumReturns);
    }

    // skip over scale/offset fields
    stream.seekp(8*6, std::ios_base::cur);

    {
        boost::uint8_t buf[256];
        boost::uint8_t* p = buf;

        double minX, minY, minZ, maxX, maxY, maxZ;
        data.getBounds(minX, minY, minZ, maxX, maxY, maxZ);
        Utils::write_field<double>(p, maxX);
        Utils::write_field<double>(p, minX);
        Utils::write_field<double>(p, maxY);
        Utils::write_field<double>(p, minY);
        Utils::write_field<double>(p, maxZ);
        Utils::write_field<double>(p, minZ);
        Utils::write_n(stream, buf, 6*8);
    }

    stream.seekp(0, std::ios_base::end);

    return;
}
Example #17
0
bool test_output_seekable(std::ostream& io)
{
    int i;  // old 'for' scope workaround.

    // Test seeking with ios::cur
    for (i = 0; i < data_reps; ++i) {
        for (int j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(-chunk_size, BOOST_IOS::cur);
        io.write(narrow_data(), chunk_size);
    }

    // Test seeking with ios::beg
    std::streamoff off = 0;
    io.seekp(0, BOOST_IOS::beg);
    for (i = 0; i < data_reps; ++i, off += chunk_size) {
        for (int j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(off, BOOST_IOS::beg);
        io.write(narrow_data(), chunk_size);
    }
    
    // Test seeking with ios::end
    io.seekp(0, BOOST_IOS::end);
    off = io.tellp();
    io.seekp(-off, BOOST_IOS::end);
    for (i = 0; i < data_reps; ++i, off -= chunk_size) {
        for (int j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(-off, BOOST_IOS::end);
        io.write(narrow_data(), chunk_size);
    }
    return true;
}
//	压缩功能,统计符号频数,并将其保存到压缩文件,然后构造哈夫曼树,进行压缩,写入文件
void huffman_file_compressor::compress(std::istream& in, std::ostream& out) {
	byte_freq_map table;
	table.load_from_file(in);
	table.save_to_file(out);
	huffman_tree** forest = table.to_simple_huffman_forest();
	huffman_tree* tree = huffman_tree::biuld_huffman_tree(forest, table.size());

	in.clear();
	in.seekg(0, std::ios::beg);

	huffman_file_encoder encoder = tree->to_file_encoder();
	encoder.encode_file(in);
	std::streamoff length_offset = out.tellp();
	out.seekp(sizeof(long long), std::ios::cur);
	long long length = encoder.write_to_file(out);
	out.seekp(length_offset, std::ios::beg);
	out.write((char*)&length, sizeof(long long));

	tree->free_nodes();
	delete forest;
	delete tree;
}
Example #19
0
	bool serialize_json_stream (std::ostream & stream_a)
	{
		auto result (false);
		stream_a.seekp (0);
		try
		{
			boost::property_tree::ptree tree;
			serialize_json (tree);
			boost::property_tree::write_json (stream_a, tree);
		}
		catch (std::runtime_error const &)
		{
			result = true;
		}
		return result;
	}
Example #20
0
void
Model::save(std::ostream & ofs) {
  // write a signature into the file
  char chunk[16];
  if (full) {
    strncpy(chunk, SEGMENTOR_MODEL_FULL, 16);
  } else {
    strncpy(chunk, SEGMENTOR_MODEL_MINIMAL, 16);
  }

  ofs.write(chunk, 16);

  if (full) {
    ofs.write(reinterpret_cast<const char *>(&end_time), sizeof(int));
  }

  int off = ofs.tellp();

  unsigned labels_offset    = 0;
  unsigned lexicon_offset   = 0;
  unsigned feature_offset   = 0;
  unsigned parameter_offset   = 0;

  write_uint(ofs, 0); //  the label offset
  write_uint(ofs, 0); //  the internal lexicon offset
  write_uint(ofs, 0); //  the features offset
  write_uint(ofs, 0); //  the parameter offset

  labels_offset = ofs.tellp();
  labels.dump(ofs);

  lexicon_offset = ofs.tellp();
  internal_lexicon.dump(ofs);

  feature_offset = ofs.tellp();
  space.dump(ofs);

  parameter_offset = ofs.tellp();
  param.dump(ofs, full);

  ofs.seekp(off);
  write_uint(ofs, labels_offset);
  write_uint(ofs, lexicon_offset);
  write_uint(ofs, feature_offset);
  write_uint(ofs, parameter_offset);
}
Example #21
0
bool SND_Format::write_header(std::ostream &f)
{
  f.seekp(0);
  header.magic = SND_MAGIC;
  header.hdr_size = sizeof(header);
  memset(header.info, 0, SND_INFO_LEN);

  bool switch_endian = !is_bigendian(); // if LITTLE_ENDIAN than switch
  write_endian<unsigned int>(f, header.magic, switch_endian);
  write_endian<unsigned int>(f, header.hdr_size, switch_endian);
  write_endian<unsigned int>(f, header.data_size, switch_endian);
  write_endian<unsigned int>(f, header.encoding, switch_endian);
  write_endian<unsigned int>(f, header.sample_rate, switch_endian);
  write_endian<unsigned int>(f, header.channels, switch_endian);
  f.write(reinterpret_cast<char *>(&header.info), SND_INFO_LEN);

  return f.good();
}
Example #22
0
void bob::io::base::detail::TensorFileHeader::write(std::ostream& str) const
{
  // Start writing at the beginning of the stream
  str.seekp(std::ios_base::beg);

  int val;
  val = (int)m_tensor_type;
  str.write( reinterpret_cast<char*>(&val), sizeof(int));
  val = (int)m_n_samples;
  str.write( reinterpret_cast<char*>(&val), sizeof(int));
  val = (int)m_type.nd;
  str.write( reinterpret_cast<char*>(&val), sizeof(int));
  val = (int)m_type.shape[0];
  str.write( reinterpret_cast<char*>(&val), sizeof(int));
  val = (int)m_type.shape[1];
  str.write( reinterpret_cast<char*>(&val), sizeof(int));
  val = (int)m_type.shape[2];
  str.write( reinterpret_cast<char*>(&val), sizeof(int));
  val = (int)m_type.shape[3];
  str.write( reinterpret_cast<char*>(&val), sizeof(int));
}
Example #23
0
std::streamsize VoidElement::write_body(std::ostream& output)
{
    std::streamsize result(0);

    // Write the body size value padded with extra bytes if necessary
    result += tawara::vint::write(size_, output,
                                  tawara::vint::size(size_) + extra_size_);
    if (fill_)
    {
        std::vector<char> zeros(size_, 0);
        output.write(&zeros[0], zeros.size());
        if (!output)
        {
            throw WriteError() << err_pos(output.tellp());
        }
    }
    else
    {
        // Skip ahead in the file to the end of the body
        output.seekp(size_, std::ios::cur);
    }
    return result + size_;
}
Example #24
0
void indexed_binder<T>::write(std::ostream & dest, int indent /* = 0 */) const
{
  // new context always means new indent
  indent += configurable::DEFAULT_INDENT;

  // first, write {
  dest << '{' << std::endl;
  for (int i = 0; i != indent; ++i)
    dest << ' ';

  for (size_t i = 0; i != m_pconfigurables.size(); ++i)
  {
    dest << i << ' ';
    m_pconfigurables[i]->write(dest, indent);
    dest << std::endl;
    for (int i = 0; i != indent; ++i)
      dest << ' ';
  }

  // backup!
  dest.seekp(-configurable::DEFAULT_INDENT, std::ios::cur);
  dest << '}';
}
Example #25
0
void
Model::save(std::ostream & ofs) {
  // write a signature into the file
  char chunk[16] = {'o','t','n','e','r', '\0'};
  ofs.write(chunk, 16);

  int off = ofs.tellp();

  unsigned labels_offset    = 0;
  unsigned lexicon_offset   = 0;
  unsigned feature_offset   = 0;
  unsigned parameter_offset   = 0;

  write_uint(ofs, 0); //  the label offset
  write_uint(ofs, 0); //  the cluster lexicon offset
  write_uint(ofs, 0); //  the features offset
  write_uint(ofs, 0); //  the parameter offset

  labels_offset = ofs.tellp();
  labels.dump(ofs);

  lexicon_offset = ofs.tellp();
  cluster_lexicon.dump(ofs);

  feature_offset = ofs.tellp();
  space.dump(ofs);

  parameter_offset = ofs.tellp();
  param.dump(ofs);

  ofs.seekp(off);
  write_uint(ofs, labels_offset);
  write_uint(ofs, lexicon_offset);
  write_uint(ofs, feature_offset);
  write_uint(ofs, parameter_offset);
}
Example #26
0
void StdOStream::seekp(Imf::Int64 pos)
{
    _os->seekp(pos);
    checkError();
}
Example #27
0
void ZipArchiveEntry::SerializeLocalFileHeader(std::ostream& stream)
{
  // ensure opening the stream
  std::istream* compressedDataStream = nullptr;

  if (!this->IsDirectory())
  {
    if (_inputStream == nullptr)
    {
      if (!_isNewOrChanged)
      {
        // the file was either compressed in immediate mode,
        // or was in previous archive
        compressedDataStream = this->GetRawStream();
      }

      // if file is new and empty or stream has been set to nullptr,
      // just do not set any compressed data stream
    }
    else
    {
      assert(_isNewOrChanged);
      compressedDataStream = _inputStream;
    }
  }

  if (!_hasLocalFileHeader)
  {
    this->FetchLocalFileHeader();
  }

  // save offset of stream here
  _offsetOfSerializedLocalFileHeader = stream.tellp();      

  if (this->IsUsingDataDescriptor())
  {
    _localFileHeader.CompressedSize = 0;
    _localFileHeader.UncompressedSize = 0;
    _localFileHeader.Crc32 = 0;
  }

  _localFileHeader.Serialize(stream);

  // if this entry is a directory, it should not contain any data
  // nor crc.
  assert(
    this->IsDirectory()
    ? !GetCrc32() && !GetSize() && !GetCompressedSize() && !_inputStream
    : true
  );

  if (!this->IsDirectory() && compressedDataStream != nullptr)
  {
    if (_isNewOrChanged)
    {
      this->InternalCompressStream(*compressedDataStream, stream);

      if (this->IsUsingDataDescriptor())
      {
        _localFileHeader.SerializeAsDataDescriptor(stream);
      }
      else
      {
        // actualize local file header
        // make non-seekable version?
        stream.seekp(_offsetOfSerializedLocalFileHeader);
        _localFileHeader.Serialize(stream);
        stream.seekp(this->GetCompressedSize(), std::ios::cur);
      }
    }
    else
    {
      utils::stream::copy(*compressedDataStream, stream);
    }
  }
}
Example #28
0
        std::shared_ptr<Response> request(const std::string& request_type, const std::string& path, std::ostream& content, 
                const std::map<std::string, std::string>& header=std::map<std::string, std::string>()) {
            std::string corrected_path=path;
            if(corrected_path=="")
                corrected_path="/";
            
            content.seekp(0, std::ios::end);
            size_t content_length=content.tellp();
            content.seekp(0, std::ios::beg);
            
            boost::asio::streambuf write_buffer;
            std::ostream write_stream(&write_buffer);
            write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n";
            write_stream << "Host: " << host << "\r\n";
            for(auto& h: header) {
                write_stream << h.first << ": " << h.second << "\r\n";
            }
            if(content_length>0)
                write_stream << "Content-Length: " << std::to_string(content_length) << "\r\n";
            write_stream << "\r\n";
            if(content_length>0)
                write_stream << content.rdbuf();
            
            std::shared_ptr<Response> response(new Response());
            
            try {
                connect();
                              
                boost::asio::write(*socket, write_buffer);
                
                size_t bytes_transferred = boost::asio::read_until(*socket, response->content_buffer, "\r\n\r\n");
                
                size_t num_additional_bytes=response->content_buffer.size()-bytes_transferred;
                
                parse_response_header(response, response->content);
                                
                if(response->header.count("Content-Length")>0) {
                    boost::asio::read(*socket, response->content_buffer, 
                            boost::asio::transfer_exactly(stoull(response->header["Content-Length"])-num_additional_bytes));
                }
                else if(response->header.count("Transfer-Encoding")>0 && response->header["Transfer-Encoding"]=="chunked") {
                    boost::asio::streambuf streambuf;
                    std::ostream content(&streambuf);
                    
                    size_t length;
                    std::string buffer;
                    do {
                        size_t bytes_transferred = boost::asio::read_until(*socket, response->content_buffer, "\r\n");
                        std::string line;
                        getline(response->content, line);
                        bytes_transferred-=line.size()+1;
                        line.pop_back();
                        length=stoull(line, 0, 16);

                        size_t num_additional_bytes=response->content_buffer.size()-bytes_transferred;
                    
                        if((2+length)>num_additional_bytes) {
                            boost::asio::read(*socket, response->content_buffer, 
                                boost::asio::transfer_exactly(2+length-num_additional_bytes));
                        }
                                                
                        buffer.resize(length);
                        response->content.read(&buffer[0], length);
                        content.write(&buffer[0], length);

                        //Remove "\r\n"
                        response->content.get();
                        response->content.get();
                    } while(length>0);
                    
                    std::ostream response_content_output_stream(&response->content_buffer);
                    response_content_output_stream << content.rdbuf();
                }
            }
            catch(const std::exception& e) {
                socket_error=true;
                throw std::invalid_argument(e.what());
            }
            
            return response;
        }
Example #29
0
void write_image(std::ostream& os, const Image* image)
{
  write32(os, image->id());
  write8(os, image->pixelFormat());    // Pixel format
  write16(os, image->width());         // Width
  write16(os, image->height());        // Height
  write32(os, image->maskColor());     // Mask color

  int rowSize = image->getRowStrideSize();
#if 0
  {
    for (int c=0; c<image->height(); c++)
      os.write((char*)image->getPixelAddress(0, c), rowSize);
  }
#else
  {
    std::ostream::pos_type total_output_pos = os.tellp();
    write32(os, 0);    // Compressed size (we update this value later)

    z_stream zstream;
    zstream.zalloc = (alloc_func)0;
    zstream.zfree  = (free_func)0;
    zstream.opaque = (voidpf)0;
    int err = deflateInit(&zstream, Z_DEFAULT_COMPRESSION);
    if (err != Z_OK)
      throw base::Exception("ZLib error %d in deflateInit().", err);

    std::vector<uint8_t> compressed(4096);
    int total_output_bytes = 0;

    for (int y=0; y<image->height(); y++) {
      zstream.next_in = (Bytef*)image->getPixelAddress(0, y);
      zstream.avail_in = rowSize;
      int flush = (y == image->height()-1 ? Z_FINISH: Z_NO_FLUSH);

      do {
        zstream.next_out = (Bytef*)&compressed[0];
        zstream.avail_out = compressed.size();

        // Compress
        err = deflate(&zstream, flush);
        if (err != Z_OK && err != Z_STREAM_END && err != Z_BUF_ERROR)
          throw base::Exception("ZLib error %d in deflate().", err);

        int output_bytes = compressed.size() - zstream.avail_out;
        if (output_bytes > 0) {
          if (os.write((char*)&compressed[0], output_bytes).fail())
            throw base::Exception("Error writing compressed image pixels.\n");

          total_output_bytes += output_bytes;
        }
      } while (zstream.avail_out == 0);
    }

    err = deflateEnd(&zstream);
    if (err != Z_OK)
      throw base::Exception("ZLib error %d in deflateEnd().", err);

    std::ostream::pos_type bak = os.tellp();
    os.seekp(total_output_pos);
    write32(os, total_output_bytes);
    os.seekp(bak);
  }
#endif
}