void BackgroundRemovedImage::writeHeader(std::ofstream& os) { HeaderInfoT hi; hi.headersize = headerSizeInBytes; hi.idcode = idCode(); //fill in header with 0s std::ofstream::pos_type cur_loc = os.tellp(); char zero[headerSizeInBytes] = {0}; os.write(zero, headerSizeInBytes); std::ofstream::pos_type end_loc = os.tellp(); //return to the beginning of the header and write data os.seekp(cur_loc); if (differencesFromBackground.empty()) { hi.depth = 0; hi.nchannels = 0; hi.numims = 0; } else { hi.depth = differencesFromBackground.front().second->depth; hi.nchannels = differencesFromBackground.front().second->nChannels; hi.numims = differencesFromBackground.size(); } os.write((char *) &hi, sizeof(hi)); if (metadata != NULL) { metadata->toDisk(os); } os.seekp(end_loc); }
unsigned int fileSize(std::ofstream& file) { unsigned int oldpos = file.tellp(); file.seekp(0, std::ios::end); unsigned int filesize = file.tellp(); file.seekp(oldpos, std::ios::beg); return filesize; }
static void writeArrayToBinaryFileStream(std::ofstream& fileStream, const T* array, const size_t sizeOfArray) { assert_gt(sizeOfArray, 0); auto bufferSize = (std::streamsize)(sizeOfArray * sizeof(T)); auto previousNumberOfBytes = fileStream.tellp(); assert_true(fileStream.write(reinterpret_cast<const char*>(array), bufferSize)); auto currentNumberOfBytes = fileStream.tellp(); assert_eq(bufferSize, currentNumberOfBytes - previousNumberOfBytes); }
// Write out the LLVM symbol table as an archive member to the file. void Archive::writeSymbolTable(std::ofstream& ARFile) { // Construct the symbol table's header ArchiveMemberHeader Hdr; Hdr.init(); memcpy(Hdr.name,ARFILE_LLVM_SYMTAB_NAME,16); uint64_t secondsSinceEpoch = sys::TimeValue::now().toEpochTime(); char buffer[32]; sprintf(buffer, "%-8o", 0644); memcpy(Hdr.mode,buffer,8); sprintf(buffer, "%-6u", sys::Process::GetCurrentUserId()); memcpy(Hdr.uid,buffer,6); sprintf(buffer, "%-6u", sys::Process::GetCurrentGroupId()); memcpy(Hdr.gid,buffer,6); sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch)); memcpy(Hdr.date,buffer,12); sprintf(buffer,"%-10u",symTabSize); memcpy(Hdr.size,buffer,10); // Write the header ARFile.write((char*)&Hdr, sizeof(Hdr)); #ifndef NDEBUG // Save the starting position of the symbol tables data content. unsigned startpos = ARFile.tellp(); #endif // Write out the symbols sequentially for ( Archive::SymTabType::iterator I = symTab.begin(), E = symTab.end(); I != E; ++I) { // Write out the file index writeInteger(I->second, ARFile); // Write out the length of the symbol writeInteger(I->first.length(), ARFile); // Write out the symbol ARFile.write(I->first.data(), I->first.length()); } #ifndef NDEBUG // Now that we're done with the symbol table, get the ending file position unsigned endpos = ARFile.tellp(); #endif // Make sure that the amount we wrote is what we pre-computed. This is // critical for file integrity purposes. assert(endpos - startpos == symTabSize && "Invalid symTabSize computation"); // Make sure the symbol table is even sized if (symTabSize % 2 != 0 ) ARFile << ARFILE_PAD; }
std::streamoff fileSize(std::ofstream& file) { std::streamoff curr = file.tellp(); file.seekp(0, std::ios::end); std::streamoff size = file.tellp(); file.seekp(0, std::ios::beg); size -= file.tellp(); file.seekp(curr, std::ios::beg); return size; }
void end(char*,std::size_t){ file <<"{" <<"\"type\":\"FeatureCollection\"," <<"\"features\":["; bool done=false; for(std::size_t idx=0;idx<lon.size()&&!done;++idx){ file <<"{" <<"\"type\":\"Feature\"," <<"\"geometry\":" <<"{" <<"\"type\":\"Point\"," <<"\"coordinates\":["<<lon[idx]<<","<<lat[idx]<<"]" <<"}" <<"}"; file.flush(); if((std::size_t)(file.tellp())+file_thresh>=file_lim) done=true; else file<<","; } file <<"]" <<"}"; }
void BezierCurves::write(std::ofstream& file) { int type = BEZIER_CURVES; file.write((char*)&type,sizeof(int)); file.write((char*)&numTimeSteps,sizeof(int)); file.write((char*)&numVertices,sizeof(int)); file.write((char*)&numCurves,sizeof(int)); for (size_t j=0; j<numTimeSteps; j++) { while ((file.tellp() % 16) != 0) { char c = 0; file.write(&c,1); } for (size_t i=0; i<numVertices; i++) file.write((char*)&vertex(i,j),sizeof(Vec3fa)); } while ((file.tellp() % 16) != 0) { char c = 0; file.write(&c,1); } for (size_t i=0; i<numCurves; i++) file.write((char*)&curve(i),sizeof(int)); }
static void _mark_session (std::ofstream& fs) { if (!fs) return; std::time_t t = std::time (nullptr); struct tm lt; localtime_r (&t, <); if (fs.tellp () > 0) fs << "\n\n" << std::flush; fs << "********************************************************************************\n" " New Session:\n" " @ " << std::setfill ('0') << std::setw (2) << lt.tm_hour << ":" << std::setw (2) << lt.tm_min << ":" << std::setw (2) << lt.tm_sec << " " << std::setw (2) << lt.tm_mon << "/" << std::setw (2) << lt.tm_mday << "/" << (lt.tm_year + 1900) << std::setfill (' ') << "\n" << "********************************************************************************\n" << std::endl; }
std::ostream& LoggerImpl::getAppender() { if (pipe) return *pipe; else if (!fname.empty()) { if (!outfile.is_open()) { outfile.clear(); outfile.open(fname.c_str(), std::ios::out | std::ios::app); counter.resetCount(outfile.tellp()); } if (maxfilesize > 0) { if (counter.getCount() > maxfilesize) { doRotate(); counter.resetCount(); } return tee; } else return outfile; } else if (loghost.isConnected()) return udpmessage; else return std::cerr; }
static void append( std::ofstream & out, std::string const & filename, std::vector< IndexEntry > & index ) { uint64_t const indexpos = IndexLoader::getIndexPos(filename); libmaus2::autoarray::AutoArray< IndexEntry > subindex = IndexLoader::loadIndex(filename); if ( subindex.size() ) { uint64_t const datapos = subindex[0].pos; uint64_t const datalen = indexpos-datapos; // shift all towards zero for ( uint64_t i = 0; i < subindex.size(); ++i ) subindex[i].pos -= datapos; uint64_t const ndatapos = out.tellp(); for ( uint64_t i = 0; i < subindex.size(); ++i ) subindex[i].pos += ndatapos; std::ifstream istr(filename.c_str(),std::ios::binary); istr.seekg(datapos,std::ios::beg); ::libmaus2::util::GetFileSize::copy ( istr, out, datalen, 1 ); for ( uint64_t i = 0; i < subindex.size(); ++i ) index.push_back(subindex[i]); } }
//Function that writes the line with name age sex and time taken by the player into the file puzzle_NxNyyy void write_resultes_into_the_file(std::ofstream& myfile, string time){ int ii = 0; vector<string> vec = Tokenize(time); int here = myfile.tellp(); myfile << " "; for (std::vector<std::string>::size_type a = 0; a < vec.size(); ++a){ switch (ii) { case 0: myfile.seekp ((here + 4) ); myfile << vec.at(a); break; case 1: myfile.seekp ((here + 25 )); myfile << vec.at(a); break; case 2: myfile.seekp ((here + 29 )); myfile << vec.at(a); break; case 3: myfile.seekp ((here + 31 )); myfile << vec.at(a); break; } ii++; } }
std::ofstream& Logger::LogStart(TLogLevel level){ char buffer[64]; if (logger_os.tellp() >= logger_max_file_size){ logger_os.close(); std::string renamed_file = logger_filename; renamed_file.append(".backup"); if (file_exists(renamed_file.c_str())) remove (renamed_file.c_str()); rename(logger_filename.c_str(), renamed_file.c_str()); remove (logger_filename.c_str()); } if (!file_exists(logger_filename.c_str())){ logger_os.close(); logger_os.open(logger_filename.c_str(), std::ios::out | std::ios::app); if (logger_os.is_open()){ logger_messageLevel = loggger_wantedLevel; }else{ logger_messageLevel = logNone; //prevent from sending data into the not opened stream } } time_t thetime = time(0); tm * ptm = std::localtime(& thetime); strftime(buffer, sizeof(buffer), "%d.%m.%Y %H:%M:%S", ptm);// Format: 15.06.2009 20:20:00 logger_os << "-" << buffer << " " << ToString(level) << ": "; return logger_os; }
bool ZipArchive::writeCentralDirectory(std::ofstream& ofs, const std::vector<ZipArchive::ArchivedFile>& files) { std::streampos offsetCD = ofs.tellp(); size_t sizeCD = 0; bool error = false; for (size_t i = 0; i < files.size(); ++i) { const ZipFileHeader& fileHeader = files[i].zipFileHader_; ofs.write(reinterpret_cast<const char*>(&fileHeader), SIZE_ZIPFILEHEADER); sizeCD += SIZE_ZIPFILEHEADER; error = ofs.fail(); size_t stringSize = files[i].fileName_.size(); sizeCD += stringSize; if ((error == false) && (stringSize > 0) && (stringSize == fileHeader.filenameLength)) { ofs.write(files[i].fileName_.c_str(), stringSize); error = ofs.fail(); } stringSize = files[i].fileExtra_.size(); sizeCD += stringSize; if ((error == false) && (stringSize > 0) && (stringSize == fileHeader.extraFieldLength)) { ofs.write(files[i].fileExtra_.c_str(), stringSize); error = ofs.fail(); } stringSize = files[i].fileComment_.size(); sizeCD += stringSize; if ((error == false) && (stringSize > 0) && (stringSize == fileHeader.fileCommentLength)) { ofs.write(files[i].fileComment_.c_str(), stringSize); error = ofs.fail(); } if (error == true) { LERROR("writeCentralDirectory(): failed to write Central Directory Entry in zip archive!"); break; } } // for ZipEOCDHeaderRecord eocd; eocd.signature = SIGNATURE_ZIPEOCDHEADERRECORD; eocd.numberOfDisk = 0; eocd.numberOfDiskWithStartOfCD = 0; eocd.numberOfEntriesInThisCD = static_cast<uint16_t>(files.size()); eocd.numberOfEntriesInCD = static_cast<uint16_t>(files.size()); eocd.sizeOfCD = static_cast<uint32_t>(sizeCD); eocd.offsetStartCD = (offsetCD >= 0) ? static_cast<uint32_t>(offsetCD) : 0; eocd.commmentLength = 0; if (error == false) { ofs.write(reinterpret_cast<char*>(&eocd), SIZE_ZIPEOCDRECORD); error = ofs.fail(); } return (! error); }
void QuadMesh::write(std::ofstream& file) { int type = QUAD_MESH; file.write((char*)&type,sizeof(int)); file.write((char*)&numTimeSteps,sizeof(int)); int numVerts = numVertices(); file.write((char*)&numVerts,sizeof(int)); int numQuads = quads.size(); file.write((char*)&numQuads,sizeof(int)); for (size_t j=0; j<numTimeSteps; j++) { while ((file.tellp() % 16) != 0) { char c = 0; file.write(&c,1); } for (size_t i=0; i<numVerts; i++) file.write((char*)vertexPtr(i,j),sizeof(Vec3fa)); } while ((file.tellp() % 16) != 0) { char c = 0; file.write(&c,1); } for (size_t i=0; i<numQuads; i++) file.write((char*)&quad(i),sizeof(Quad)); }
void TriangleMesh::write(std::ofstream& file) { int type = TRIANGLE_MESH; file.write((char*)&type,sizeof(int)); file.write((char*)&numTimeSteps,sizeof(int)); int numVerts = numVertices(); file.write((char*)&numVerts,sizeof(int)); int numTriangles = triangles.size(); file.write((char*)&numTriangles,sizeof(int)); for (size_t j=0; j<numTimeSteps; j++) { while ((file.tellp() % 16) != 0) { char c = 0; file.write(&c,1); } for (size_t i=0; i<numVerts; i++) file.write((char*)vertexPtr(i,j),sizeof(Vec3fa)); } while ((file.tellp() % 16) != 0) { char c = 0; file.write(&c,1); } for (size_t i=0; i<numTriangles; i++) file.write((char*)&triangle(i),sizeof(Triangle)); }
int _LogAppenderBase::getLogSizeKBytes() { if (!out.is_open()) { return 0; } else { return out.tellp() / 1024; } }
void LineSegments::write(std::ofstream& file) { int type = LINE_SEGMENTS; file.write((char*)&type,sizeof(int)); file.write((char*)&numTimeSteps,sizeof(int)); size_t numVerts = numVertices(); file.write((char*)&numVerts,sizeof(int)); file.write((char*)&numPrimitives,sizeof(int)); for (size_t j=0; j<numTimeSteps; j++) { while ((file.tellp() % 16) != 0) { char c = 0; file.write(&c,1); } for (size_t i=0; i<vertices[j].size(); i++) { Vec3fa v = vertex(i,j); file.write((char*)&v,sizeof(Vec3fa)); } } while ((file.tellp() % 16) != 0) { char c = 0; file.write(&c,1); } for (size_t i=0; i<numPrimitives; i++) file.write((char*)&segment(i),sizeof(int)); }
void open(const std::string& a_fname, bool a_append = false) { using std::ios; using std::ios_base; if (m_open) return; m_file.exceptions(ios::failbit | ios::badbit); ios_base::openmode l_mode = ios::out | ios::binary; if (a_append) l_mode |= ios::app; else l_mode |= ios::trunc; m_file.open(a_fname.c_str(), l_mode); m_offset = m_file.tellp(); m_open = true; m_fname = a_fname; m_buf.reset(); BOOST_ASSERT(m_buf.capacity() > 0); }
void create_header(std::vector<fs::path> const& files, std::vector<boost::uintmax_t> const& file_sizes, std::ofstream& archive) { std::uint32_t const body_end = static_cast<std::uint32_t>(archive.tellp()); write_trivial(archive, files.size()); auto const size = files.size(); std::int32_t pos = 0; for (auto i = 0U; i < size; ++i) { write_trivial(archive, pos); std::uint32_t s = static_cast<std::uint32_t>(file_sizes[i]); write_trivial(archive, s); s = static_cast<std::uint32_t>(files[i].string().size()); write_trivial(archive, s); archive.write(files[i].string().c_str(), files[i].string().size()); pos += static_cast<int>(file_sizes[i]); } write_trivial(archive, body_end); }
void HistoryManager::appendEntryToFile(std::ofstream &os, bool isSent, const boost::posix_time::ptime &saveTime, const std::string &content) { if(saveTime.is_not_a_date_time()) return; if(os.tellp() != 0) os << std::endl; os << "[" << FormattingUtils::dateToStr(saveTime) << "]"; if(isSent) { os << "-S-"; } else { os << "-R-"; } os << content; }
/* Handles Parameter creation / addition / write */ static void __SetupAndWriteAnimationParameter(shared_ptr <GLTFAnimation> cvtAnimation, const std::string& parameterSID, const std::string& parameterType, shared_ptr <GLTFBufferView> bufferView, std::ofstream &animationsOutputStream) { //setup shared_ptr <GLTFAnimation::Parameter> parameter(new GLTFAnimation::Parameter(parameterSID)); parameter->setCount(cvtAnimation->getCount()); parameter->setType(parameterType); __SetupSamplerForParameter(cvtAnimation, parameter.get()); cvtAnimation->parameters()->push_back(parameter); //write parameter->setByteOffset(static_cast<size_t>(animationsOutputStream.tellp())); animationsOutputStream.write((const char*)( bufferView->getBufferDataByApplyingOffset()), bufferView->getByteLength()); }
Printer(std::string f){ i=0; sub = n.subscribe("/icp/world", 20, &Printer::CloudPrinter_Callback_onefile,this); ply_file.open (f.data()); ply_file << "ply\n"; ply_file << "format ascii 1.0\n"; ply_file << "element vertex "; pos=ply_file.tellp(); ply_file << " \n"; ply_file << "property float x\n"; ply_file << "property float y\n"; ply_file << "property float z\n"; ply_file << "property uchar red\n"; ply_file << "property uchar green\n"; ply_file << "property uchar blue\n"; ply_file << "end_header\n"; }
void LoggerImpl::doRotate() { outfile.clear(); outfile.close(); // ignore unlink- and rename-errors. In case of failure the // original file is reopened std::string newfilename = mkfilename(maxbackupindex); ::unlink(newfilename.c_str()); for (unsigned idx = maxbackupindex; idx > 0; --idx) { std::string oldfilename = mkfilename(idx - 1); ::rename(oldfilename.c_str(), newfilename.c_str()); newfilename = oldfilename; } ::rename(fname.c_str(), newfilename.c_str()); outfile.open(fname.c_str(), std::ios::out | std::ios::app); counter.resetCount(outfile.tellp()); }
void processFileProcess (std::ofstream &outfile, FileProcess &f, std::list<std::string> &compression_types, int64_t &total_compressed_size, int64_t &total_uncompressed_size) { unsigned char buffer[1024*8]; // Get location of beginning of file f.start = outfile.tellp(); std::cout << "Processing " << f.file << " at location " << f.start << std::endl; // Check if this is an extension that should be compressed std::string ext = f.file.substr(f.file.find_last_of('.')+1); if ( std::find(compression_types.begin(), compression_types.end(), ext) != compression_types.end()) { std::cout << " Compressing... "; // Read the entire file into a buffer std::vector<unsigned char> uncompressed_buffer; std::vector<unsigned char> compressed_buffer; std::string path = f.path + SEP + f.file; std::ifstream infile( path.c_str(), std::ios::in | std::ios::binary ); int size = -1; while (!infile.eof() && size) { infile.read((char *) buffer, sizeof(buffer)); size = infile.gcount(); uncompressed_buffer.insert(uncompressed_buffer.end(), buffer, buffer+size); } // Use zlib to compress the buffer z_stream strm; strm.zalloc = 0; strm.zfree = 0; strm.next_in = reinterpret_cast<Bytef *>(&uncompressed_buffer[0]); strm.avail_in = uncompressed_buffer.size(); strm.next_out = buffer; strm.avail_out = sizeof(buffer); deflateInit(&strm, Z_BEST_COMPRESSION); // Compress file while (strm.avail_in != 0) { int res = deflate(&strm, Z_NO_FLUSH); assert(res == Z_OK); if (strm.avail_out == 0) { compressed_buffer.insert(compressed_buffer.end(), buffer, buffer + sizeof(buffer)); strm.next_out = buffer; strm.avail_out = sizeof(buffer); } } // Finish off int deflate_res = Z_OK; while (deflate_res == Z_OK) { if (strm.avail_out == 0) { compressed_buffer.insert(compressed_buffer.end(), buffer, buffer + sizeof(buffer)); strm.next_out = buffer; strm.avail_out = sizeof(buffer); } deflate_res = deflate(&strm, Z_FINISH); } // End the stream assert(deflate_res == Z_STREAM_END); compressed_buffer.insert(compressed_buffer.end(), buffer, buffer + sizeof(buffer) - strm.avail_out); deflateEnd(&strm); // write compressed file to package outfile.write( (char*) &compressed_buffer[0], compressed_buffer.size()); // Location of end of file f.length = (int64_t) (outfile.tellp()) - f.start; f.uncompressed_length = (int64_t) uncompressed_buffer.size(); std::cout << "Done! Before " << uncompressed_buffer.size() << " bytes, after " << compressed_buffer.size() << " bytes (" << compressed_buffer.size() * 100 / uncompressed_buffer.size() << "%)" << std::endl; total_compressed_size += compressed_buffer.size(); total_uncompressed_size += uncompressed_buffer.size(); } else { // Copy file into package std::string path = f.path + SEP + f.file; std::ifstream infile( path.c_str(), std::ios::in | std::ios::binary ); assert(infile.good()); int size = -1; while (!infile.eof() && size) { infile.read((char *) buffer, sizeof(buffer)); size = infile.gcount(); outfile.write((char *) buffer, size); } infile.close(); // Location of end of file f.length = (int64_t) (outfile.tellp()) - f.start; f.uncompressed_length = (int64_t) f.length; total_compressed_size += f.length; total_uncompressed_size += f.length; } }
bool GLTFMesh::writeAllBuffers(std::ofstream& verticesOutputStream, std::ofstream& indicesOutputStream) { typedef map<std::string , shared_ptr<GLTF::GLTFBuffer> > IDToBufferDef; IDToBufferDef IDToBuffer; shared_ptr <AccessorVector> allAccessors = this->accessors(); shared_ptr <GLTFBufferView> dummyBuffer(new GLTFBufferView()); PrimitiveVector primitives = this->getPrimitives(); unsigned int primitivesCount = (unsigned int)primitives.size(); for (unsigned int i = 0 ; i < primitivesCount ; i++) { shared_ptr<GLTF::GLTFPrimitive> primitive = primitives[i]; shared_ptr <GLTF::GLTFIndices> uniqueIndices = primitive->getUniqueIndices(); /* Convert the indices to unsigned short and write the blob */ unsigned int indicesCount = (unsigned int)uniqueIndices->getCount(); shared_ptr <GLTFBufferView> indicesBufferView = uniqueIndices->getBufferView(); unsigned char* uniqueIndicesBufferPtr = (unsigned char*)indicesBufferView->getBuffer()->getData(); uniqueIndicesBufferPtr += indicesBufferView->getByteOffset(); unsigned int* uniqueIndicesBuffer = (unsigned int*) uniqueIndicesBufferPtr; if (indicesCount <= 0) { // FIXME: report error } else { size_t indicesLength = sizeof(unsigned short) * indicesCount; unsigned short* ushortIndices = (unsigned short*)calloc(indicesLength, 1); for (unsigned int idx = 0 ; idx < indicesCount ; idx++) { ushortIndices[idx] = (unsigned short)uniqueIndicesBuffer[idx]; } uniqueIndices->setByteOffset(static_cast<size_t>(indicesOutputStream.tellp())); indicesOutputStream.write((const char*)ushortIndices, indicesLength); //now that we wrote to the stream we can release the buffer. uniqueIndices->setBufferView(dummyBuffer); free(ushortIndices); } } for (unsigned int j = 0 ; j < allAccessors->size() ; j++) { shared_ptr <GLTFAccessor> accessor = (*allAccessors)[j]; shared_ptr <GLTFBufferView> bufferView = accessor->getBufferView(); shared_ptr <GLTFBuffer> buffer = bufferView->getBuffer(); if (!bufferView.get()) { // FIXME: report error return false; } if (!IDToBuffer[bufferView->getBuffer()->getID().c_str()].get()) { // FIXME: this should be internal to accessor when a Data buffer is set // for this, add a type to buffers , and check this type in setBuffer , then call computeMinMax accessor->computeMinMax(); accessor->setByteOffset(static_cast<size_t>(verticesOutputStream.tellp())); verticesOutputStream.write((const char*)(static_pointer_cast <GLTFBuffer> (buffer)->getData()), buffer->getByteLength()); //now that we wrote to the stream we can release the buffer. accessor->setBufferView(dummyBuffer); IDToBuffer[bufferView->getBuffer()->getID().c_str()] = buffer; } } return true; }
bool const JSONMesh::writeAllBuffers(std::ofstream& fileOutputStream) { typedef map<std::string , shared_ptr<JSONExport::JSONBuffer> > IDToBufferDef; IDToBufferDef IDToBuffer; vector <shared_ptr< JSONExport::JSONAccessor> > allAccessors = this->remappedAccessors(); shared_ptr <JSONBuffer> dummyBuffer(new JSONBuffer(0)); std::vector< shared_ptr<JSONExport::JSONPrimitive> > primitives = this->getPrimitives(); unsigned int primitivesCount = (unsigned int)primitives.size(); for (unsigned int i = 0 ; i < primitivesCount ; i++) { shared_ptr<JSONExport::JSONPrimitive> primitive = primitives[i]; shared_ptr <JSONExport::JSONIndices> uniqueIndices = primitive->getUniqueIndices(); /* Convert the indices to unsigned short and write the blob */ unsigned int indicesCount = (unsigned int)uniqueIndices->getCount(); unsigned int* uniqueIndicesBuffer = (unsigned int*) static_pointer_cast <JSONDataBuffer> (uniqueIndices->getBuffer())->getData(); if (indicesCount <= 0) { // FIXME: report error } else { size_t indicesLength = sizeof(unsigned short) * indicesCount; unsigned short* ushortIndices = (unsigned short*)malloc(indicesLength); for (unsigned int idx = 0 ; idx < indicesCount ; idx++) { ushortIndices[idx] = (unsigned short)uniqueIndicesBuffer[idx]; } uniqueIndices->setByteOffset(static_cast<size_t>(fileOutputStream.tellp())); fileOutputStream.write((const char*)ushortIndices, indicesLength); //now that we wrote to the stream we can release the buffer. uniqueIndices->setBuffer(dummyBuffer); free(ushortIndices); } } for (unsigned int j = 0 ; j < allAccessors.size() ; j++) { shared_ptr <JSONExport::JSONAccessor> accessor = allAccessors[j]; shared_ptr <JSONExport::JSONBuffer> buffer = accessor->getBuffer(); if (!buffer.get()) { // FIXME: report error return false; } if (!IDToBuffer[buffer->getID().c_str()].get()) { // FIXME: this should be internal to accessor when a Data buffer is set // for this, add a type to buffers , and check this type in setBuffer , then call computeMinMax accessor->computeMinMax(); accessor->setByteOffset(static_cast<size_t>(fileOutputStream.tellp())); fileOutputStream.write((const char*)(static_pointer_cast <JSONDataBuffer> (buffer)->getData()), buffer->getByteSize()); //now that we wrote to the stream we can release the buffer. accessor->setBuffer(dummyBuffer); IDToBuffer[buffer->getID()] = buffer; } } return true; }
unsigned int ilf::createILF( std::istream &infile, std::ofstream &outfile ) { unsigned int total = 0; char temp[512]; // Write form with dummy size unsigned int form0Position = outfile.tellp(); writeFormHeader( outfile, 0, "INLY" ); // Write form with dummy size unsigned int form1Position = outfile.tellp(); writeFormHeader( outfile, 0, "0000" ); while( !infile.eof() ) { unsigned int nodeSize = 0; infile.getline( temp, 512, ':' ); if( infile.eof() ) { break; }; std::string objectFilename; infile >> objectFilename; std::cout << objectFilename << std::endl; nodeSize += static_cast<unsigned int>( objectFilename.size() + 1 ); infile.getline( temp, 512, ':' ); std::string objectZone; infile >> objectZone; std::cout << objectZone << std::endl; nodeSize += static_cast<unsigned int>( objectZone.size() + 1 ); // 'Transform matrix:' line infile.getline( temp, 512, ':' ); std::cout.flags ( std::ios_base::showpoint ); std::cout << std::dec; std::cout.flags( std::ios_base::fixed ); float x[12]; for( unsigned int i = 0; i < 12; ++i ) { std::cout.width( 10 ); infile >> x[i]; nodeSize += sizeof( float ); std::cout << x[i] << " "; } std::cout << std::endl; // Blank line infile.getline( temp, 512 ); total += writeRecordHeader( outfile, "NODE", nodeSize ); outfile.write( objectFilename.c_str(), static_cast<unsigned int>( objectFilename.size()+1 ) ); outfile.write( objectZone.c_str(), static_cast<unsigned int>( objectZone.size()+1 ) ); outfile.write( (char*)x, sizeof( float ) * 12 ); total += nodeSize; } // Rewrite form with proper size. outfile.seekp( form1Position, std::ios_base::beg ); total += writeFormHeader( outfile, total+4, "0000" ); // Rewrite form with proper size. outfile.seekp( form0Position, std::ios_base::beg ); total += writeFormHeader( outfile, total+4, "INLY" ); return total; }
void DataSetHeaderWriter::WriteNextDataSetOffset(std::ofstream &os, u_int32_t pos) { nextDataSetPos = os.tellp(); FileOutput::WriteInt32(os, pos); }
int main(int argc, char *argv[]) { UDPConnectionServer connection(2368); AcquisitionThread<DataPacket> thread(connection); Calibration calib; std::shared_ptr<DataPacket> packet; IPC_RETURN_TYPE err; char dump_filename[4096]; carmen_velodyne_ipc_initialize(argc, argv); carmen_velodyne_read_parameters(argc, argv); carmen_velodyne_set_spin_rate(spin_rate); calib_file.open(calib_path); if (!calib_file.is_open()) carmen_die("\nError: Could not open %s for reading\n", calib_filename); calib_file >> calib; calib_file.close(); signal(SIGINT, carmen_velodyne_sigint_handler); thread.start(); double time; double start_time = carmen_get_time(); int num_packets = 0; int start_num_packets = 0; int num_lost_packets = 0; int start_num_lost_packets = 0; while (!quit) { try { while (dump_enabled || points_publish) { packet = thread.getBuffer().dequeue(); ++num_packets; if (dump_enabled) { if (!dump_file.is_open()) { char dump_time[1024]; time_t local = packet->getTimestamp(); struct tm* time = localtime(&local); strftime(dump_time, sizeof(dump_time), "%Y-%m-%d-%H%M%S", time); sprintf(dump_filename, "%s/%s.bin", dump_path, dump_time); dump_file.open(dump_filename, std::ios::out | std::ios::binary); } if (dump_file.is_open()) { long dump_filepos = dump_file.tellp(); dump_file << *packet; carmen_velodyne_publish_packet(id, dump_filename, dump_filepos, packet->getTimestamp()); } else { carmen_warn("\nWarning: Could not open %s for writing\n", dump_filename); } } if (points_publish) { VdynePointCloud pointCloud; Converter::toPointCloud(*packet, calib, pointCloud); int num_points = pointCloud.getSize(); float x[num_points], y[num_points], z[num_points]; std::vector<VdynePointCloud::Point3D>::const_iterator it; int i = 0; for (it = pointCloud.getPointBegin(); it != pointCloud.getPointEnd(); ++it, ++i) { x[i] = it->mX; y[i] = it->mY; z[i] = it->mZ; } carmen_velodyne_publish_pointcloud(id, num_points, x, y, z, packet->getTimestamp()); } double time = carmen_get_time(); if (time-start_time >= 1.0) { num_lost_packets = thread.getBuffer().getNumDroppedElements(); float period_num_packets = num_packets-start_num_packets; float period_num_lost_packets = num_lost_packets- start_num_lost_packets; double packet_loss = period_num_lost_packets/ (period_num_packets+period_num_lost_packets); fprintf(stdout, "\rPacket loss: %6.2f%%", packet_loss*100.0); fflush(stdout); start_time = time; start_num_packets = num_packets; start_num_lost_packets = num_lost_packets; } } } catch (IOException& exception) { // buffer underrun } carmen_ipc_sleep(0.0); } if (dump_file.is_open()) dump_file.close(); fprintf(stdout, "\n"); return 0; }
// Write one member out to the file. bool Archive::writeMember( const ArchiveMember& member, std::ofstream& ARFile, bool CreateSymbolTable, bool TruncateNames, bool ShouldCompress, std::string* ErrMsg ) { unsigned filepos = ARFile.tellp(); filepos -= 8; // Get the data and its size either from the // member's in-memory data or directly from the file. size_t fSize = member.getSize(); const char *data = (const char*)member.getData(); MemoryBuffer *mFile = 0; if (!data) { mFile = MemoryBuffer::getFile(member.getPath().c_str(), ErrMsg); if (mFile == 0) return true; data = mFile->getBufferStart(); fSize = mFile->getBufferSize(); } // Now that we have the data in memory, update the // symbol table if its a bitcode file. if (CreateSymbolTable && member.isBitcode()) { std::vector<std::string> symbols; std::string FullMemberName = archPath.str() + "(" + member.getPath().str() + ")"; Module* M = GetBitcodeSymbols((const unsigned char*)data,fSize, FullMemberName, Context, symbols, ErrMsg); // If the bitcode parsed successfully if ( M ) { for (std::vector<std::string>::iterator SI = symbols.begin(), SE = symbols.end(); SI != SE; ++SI) { std::pair<SymTabType::iterator,bool> Res = symTab.insert(std::make_pair(*SI,filepos)); if (Res.second) { symTabSize += SI->length() + numVbrBytes(SI->length()) + numVbrBytes(filepos); } } // We don't need this module any more. delete M; } else { delete mFile; if (ErrMsg) *ErrMsg = "Can't parse bitcode member: " + member.getPath().str() + ": " + *ErrMsg; return true; } } int hdrSize = fSize; // Compute the fields of the header ArchiveMemberHeader Hdr; bool writeLongName = fillHeader(member,Hdr,hdrSize,TruncateNames); // Write header to archive file ARFile.write((char*)&Hdr, sizeof(Hdr)); // Write the long filename if its long if (writeLongName) { ARFile.write(member.getPath().str().data(), member.getPath().str().length()); } // Write the (possibly compressed) member's content to the file. ARFile.write(data,fSize); // Make sure the member is an even length if ((ARFile.tellp() & 1) == 1) ARFile << ARFILE_PAD; // Close the mapped file if it was opened delete mFile; return false; }