int main(int argc, char* argv[]) { // read the file std::ifstream read(argv[1], std::ios::binary); std::string buffer((std::istreambuf_iterator<char>(read)), (std::istreambuf_iterator<char>())); // Make empty file if original was empty if (!buffer.size()) { std::ofstream ofs(argv[2], std::ios::binary); exit(0); } // Get frequences std::vector<int> frequences(256, 0); for (size_t i = 0; i < buffer.size(); ++i) { byte b = buffer[i]; ++frequences[b]; } // build the HCTree HCTree hct; hct.build(frequences); // output the compressed file std::ofstream ofs(argv[2], std::ios::binary); BitOutputStream bos(ofs); Header(ofs, frequences); for (size_t i = 0; i < buffer.size(); ++i) { hct.encode(buffer[i], bos); } return 0; }
int main(int argc, char* argv[]) { // read the file into a buffer std::ifstream ifs(argv[1], std::ios::binary); std::string buffer((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); // create a new file if we're trying to comrpess an empty file if (!buffer.size()) { std::ofstream ofs(argv[2], std::ios::binary); exit(0); } // count the occurences of each char std::vector<int> freqs(256, 0); for (size_t i = 0; i < buffer.size(); ++i) { byte b = buffer[i]; ++freqs[b]; } // build the HCTree HCTree hct; hct.build(freqs); // output the compressed file std::ofstream ofs(argv[2], std::ios::binary); BitOutputStream bos(ofs); makeHeader(ofs, freqs); for (size_t i = 0; i < buffer.size(); ++i) { hct.encode(buffer[i], bos); } return 0; }
symbol::operator std::string() const{ if( eos() ){ return std::string("\\z"); }else if( bos() ){ return std::string("\\A"); }else if( _val < 30 || _val > 0x7f ){ std::stringstream result; if( _val > 0xffff ){ result << "\\U"; result.width(6); }else{ result << "\\u"; result.width(4); } result.fill('0'); result << std::right << std::hex << _val; return result.str(); }else{ std::string result; if( op() ){ result += "\\"; } result += static_cast<char>(_val); return result; } }
int main (int argc, char *argv[]) { // Cube std::list<Plane> planes; planes.push_back(Plane(1, 0, 0, -1)); planes.push_back(Plane(-1, 0, 0, -1)); planes.push_back(Plane(0, 1, 0, -1)); planes.push_back(Plane(0, -1, 0, -1)); planes.push_back(Plane(0, 0, 1, -1)); planes.push_back(Plane(0, 0, -1, -1)); std::vector<Point> points; int N, steps; // Number of points if (argc > 1) { N = atoi(argv[1]); } else { N = 50; } // Number of steps if (argc > 2) { steps = atoi(argv[2]); } else { steps = 10; } CGAL::Random_points_in_sphere_3<Point> g; for (int i = 0; i < N; i++) { Point p = *g++; points.push_back(p); } std::ofstream bos("before_lloyd.xyz"); std::copy(points.begin(), points.end(), std::ostream_iterator<Point>(bos, "\n")); // Apply Lloyd algorithm: will generate points // uniformly sampled inside a cube. for (int i = 0; i < steps; i++) { std::cout << "iteration " << i + 1 << std::endl; CGAL::Timer timer; timer.start(); lloyd_algorithm(planes.begin(), planes.end(), points); timer.stop(); std::cout << "Execution time : " << timer.time() << "s\n"; } std::ofstream aos("after_lloyd.xyz"); std::copy(points.begin(), points.end(), std::ostream_iterator<Point>(aos, "\n")); return 0; }
/** * Saves XCSoars own FLARM details into the * corresponding file (xcsoar-flarm.txt) */ static void SaveSecondary(FlarmNameDatabase &flarm_names) try { FileOutputStream fos(LocalPath(_T("xcsoar-flarm.txt"))); BufferedOutputStream bos(fos); SaveFlarmNameFile(bos, flarm_names); bos.Flush(); fos.Commit(); } catch (const std::runtime_error &e) { LogError(e); }
void BasicEvaluate::WriteRankingResult() { if (mEvaluateOption->EvaluateRanking() || mEvaluateOption->EvaluateCoverage() || mEvaluateOption->EvaluateNovelty() || mEvaluateOption->EvaluateDiversity()) { Log::I("recsys", "BasicEvaluate::WriteRankingResult()"); Log::I("recsys", "writing result to file = " + mRankingResultFilepath); BinaryOutputStream bos(mRankingResultFilepath); bos << mTestData->NumUser() << mEvaluateOption->CurrentRankingListSize(); for (int uid = 0; uid < mTestData->NumUser(); ++uid) { ItemIdList itemList = mPredict->PredictTopNItem(uid, mEvaluateOption->CurrentRankingListSize()); bos.Write(itemList.data(), itemList.size()); } } }
void PopTrain::Train() { Log::I("recsys", "PopTrain::Train()"); Log::I("recsys", "loading train rating from file %s...", mRatingTrainFilepath.c_str()); RatingList rlist = RatingList::LoadFromBinaryFile(mRatingTrainFilepath); Log::I("recsys", "computing item popularity & average..."); RatingTrait rtrait; rtrait.Init(rlist); Log::I("recsys", "saving model to file %s...", mModelFilepath.c_str()); BinaryOutputStream bos(mModelFilepath); bos << rlist.NumItem(); for (int iid = 0; iid < rlist.NumItem(); ++iid) { bos << rtrait.ItemAverage(iid); } for (int iid = 0; iid < rlist.NumItem(); ++iid) { bos << rtrait.ItemPopularity(iid); } bos << rlist.NumUser(); for (int uid = 0; uid < rlist.NumUser(); ++uid) { bos << rtrait.UserAverage(uid); } for (int uid = 0; uid < rlist.NumUser(); ++uid) { bos << rtrait.UserActivity(uid); } }
const bool symbol::op() const{ if( eos() || bos() ) return true; if( _val > 0x7f ){ return false; } switch(static_cast<char>(_val)){ case '[' : case ']' : case '{' : case '}' : case '(' : case ')' : case '-' : case '+' : case '.' : case '*' : case '?' : case '^' : case '$' : case '\\' : return true; } return false; }
/** * Encodes a set of data with DC's version of huffman encoding.. * @todo Use real streams maybe? or something else than string (operator[] contains a compare, slow...) */ void CryptoManager::encodeHuffman(const string& is, string& os) { // We might as well expect this much data as huffman encoding doesn't go very far... os.reserve(is.size()); if(is.length() == 0) { os.append("HE3\x0d"); // Nada... os.append(7, '\0'); return; } // First, we count all characters u_int8_t csum = 0; int count[256]; memset(count, 0, sizeof(count)); int chars = countChars(is, count, csum); // Next, we create a set of nodes and add it to a list, removing all characters that never occur. list<Node*> nodes; int i; for(i=0; i<256; i++) { if(count[i] > 0) { nodes.push_back(new Node(i, count[i])); } } nodes.sort(greaterNode()); #ifdef _DEBUG for(list<Node*>::iterator it = nodes.begin(); it != nodes.end(); ++it) dcdebug("%.02x:%d, ", (*it)->chr, (*it)->weight); dcdebug("\n"); #endif walkTree(nodes); dcassert(nodes.size() == 1); Node* root = nodes.front(); vector<u_int8_t> lookup[256]; // Build a lookup table for fast character lookups buildLookup(lookup, root); delete root; // Reserve some memory to avoid all those copies when appending... os.reserve(is.size() * 3 / 4); os.append("HE3\x0d"); // Checksum os.append(1, csum); string::size_type sz = is.size(); os.append((char*)&sz, 4); // Character count os.append((char*)&chars, 2); // The characters and their bitlengths for(i=0; i<256; i++) { if(count[i] > 0) { os.append(1, (u_int8_t)i); os.append(1, (u_int8_t)lookup[i].size()); } } BitOutputStream bos(os); // The tree itself, ie the bits of each character for(i=0; i<256; i++) { if(count[i] > 0) { bos.put(lookup[i]); } } dcdebug("u_int8_ts: %d\n", os.size()); bos.skipToByte(); for(string::size_type j=0; j<is.size(); j++) { dcassert(lookup[(u_int8_t)is[j]].size() != 0); bos.put(lookup[(u_int8_t)is[j]]); } bos.skipToByte(); }
namespace detail { class basic_oarchive_impl { friend class basic_oarchive; unsigned int m_flags; ////////////////////////////////////////////////////////////////////// // information about each serialized object saved // keyed on address, class_id struct aobject { const void * address; class_id_type class_id; object_id_type object_id; bool operator<(const aobject &rhs) const { assert(NULL != address); assert(NULL != rhs.address); if( address < rhs.address ) return true; if( address > rhs.address ) return false; return class_id < rhs.class_id; } aobject & operator=(const aobject & rhs) { address = rhs.address; class_id = rhs.class_id; object_id = rhs.object_id; return *this; } aobject( const void *a, class_id_type class_id_, object_id_type object_id_ ) : address(a), class_id(class_id_), object_id(object_id_) {} aobject() : address(NULL){} }; // keyed on class_id, address typedef std::set<aobject> object_set_type; object_set_type object_set; ////////////////////////////////////////////////////////////////////// // information about each serialized class saved // keyed on type_info struct cobject_type { const basic_oserializer * m_bos_ptr; const class_id_type m_class_id; bool m_initialized; cobject_type( std::size_t class_id, const basic_oserializer & bos ) : m_bos_ptr(& bos), m_class_id(class_id), m_initialized(false) {} cobject_type(const basic_oserializer & bos) : m_bos_ptr(& bos) {} cobject_type( const cobject_type & rhs ) : m_bos_ptr(rhs.m_bos_ptr), m_class_id(rhs.m_class_id), m_initialized(rhs.m_initialized) {} // the following cannot be defined because of the const // member. This will generate a link error if an attempt // is made to assign. This should never be necessary // use this only for lookup argument cobject_type & operator=(const cobject_type &rhs); bool operator<(const cobject_type &rhs) const { return *m_bos_ptr < *(rhs.m_bos_ptr); } }; // keyed on type_info typedef std::set<cobject_type> cobject_info_set_type; cobject_info_set_type cobject_info_set; // list of objects initially stored as pointers - used to detect errors // keyed on object id std::set<object_id_type> stored_pointers; // address of the most recent object serialized as a poiner // whose data itself is now pending serialization const void * pending_object; const basic_oserializer * pending_bos; basic_oarchive_impl(unsigned int flags) : m_flags(flags), pending_object(NULL), pending_bos(NULL) {} const cobject_type & find(const basic_oserializer & bos); const basic_oserializer * find(const serialization::extended_type_info &ti) const; //public: const cobject_type & register_type(const basic_oserializer & bos); void save_object( basic_oarchive & ar, const void *t, const basic_oserializer & bos ); void save_pointer( basic_oarchive & ar, const void * t, const basic_pointer_oserializer * bpos ); }; ////////////////////////////////////////////////////////////////////// // basic_oarchive implementation functions // given a type_info - find its bos // return NULL if not found inline const basic_oserializer * basic_oarchive_impl::find(const serialization::extended_type_info & ti) const { #ifdef BOOST_MSVC # pragma warning(push) # pragma warning(disable : 4511 4512) #endif class bosarg : public basic_oserializer { bool class_info() const { assert(false); return false; } // returns true if objects should be tracked bool tracking(const unsigned int) const { assert(false); return false; } // returns class version version_type version() const { assert(false); return version_type(0); } // returns true if this class is polymorphic bool is_polymorphic() const{ assert(false); return false; } void save_object_data( basic_oarchive & /*ar*/, const void * /*x*/ ) const { assert(false); } public: bosarg(const serialization::extended_type_info & eti) : boost::archive::detail::basic_oserializer(eti) {} }; #ifdef BOOST_MSVC #pragma warning(pop) #endif bosarg bos(ti); cobject_info_set_type::const_iterator cit = cobject_info_set.find(cobject_type(bos)); // it should already have been "registered" - see below if(cit == cobject_info_set.end()){ // if an entry is not found in the table it is because a pointer // of a derived class has been serialized through its base class // but the derived class hasn't been "registered" return NULL; } // return pointer to the real class return cit->m_bos_ptr; } inline const basic_oarchive_impl::cobject_type & basic_oarchive_impl::find(const basic_oserializer & bos) { std::pair<cobject_info_set_type::iterator, bool> cresult = cobject_info_set.insert(cobject_type(cobject_info_set.size(), bos)); return *(cresult.first); } inline const basic_oarchive_impl::cobject_type & basic_oarchive_impl::register_type( const basic_oserializer & bos ){ cobject_type co(cobject_info_set.size(), bos); std::pair<cobject_info_set_type::const_iterator, bool> result = cobject_info_set.insert(co); return *(result.first); } inline void basic_oarchive_impl::save_object( basic_oarchive & ar, const void *t, const basic_oserializer & bos ){ // if its been serialized through a pointer and the preamble's been done if(t == pending_object && pending_bos == & bos){ // just save the object data ar.end_preamble(); (bos.save_object_data)(ar, t); return; } // get class information for this object const cobject_type & co = register_type(bos); if(bos.class_info()){ if( ! co.m_initialized){ ar.vsave(class_id_optional_type(co.m_class_id)); ar.vsave(tracking_type(bos.tracking(m_flags))); ar.vsave(version_type(bos.version())); (const_cast<cobject_type &>(co)).m_initialized = true; } } // we're not tracking this type of object if(! bos.tracking(m_flags)){ // just windup the preamble - no object id to write ar.end_preamble(); // and save the data (bos.save_object_data)(ar, t); return; } // look for an existing object id object_id_type oid(object_set.size()); // lookup to see if this object has already been written to the archive basic_oarchive_impl::aobject ao(t, co.m_class_id, oid); std::pair<basic_oarchive_impl::object_set_type::const_iterator, bool> aresult = object_set.insert(ao); oid = aresult.first->object_id; // if its a new object if(aresult.second){ // write out the object id ar.vsave(oid); ar.end_preamble(); // and data (bos.save_object_data)(ar, t); return; } // check that it wasn't originally stored through a pointer if(stored_pointers.end() != stored_pointers.find(oid)){ // this has to be a user error. loading such an archive // would create duplicate objects boost::serialization::throw_exception( archive_exception(archive_exception::pointer_conflict) ); } // just save the object id ar.vsave(object_reference_type(oid)); ar.end_preamble(); return; } // save a pointer to an object instance inline void basic_oarchive_impl::save_pointer( basic_oarchive & ar, const void * t, const basic_pointer_oserializer * bpos_ptr ){ const basic_oserializer & bos = bpos_ptr->get_basic_serializer(); std::size_t original_count = cobject_info_set.size(); const cobject_type & co = register_type(bos); if(! co.m_initialized){ ar.vsave(co.m_class_id); // if its a previously unregistered class if((cobject_info_set.size() > original_count)){ if(bos.is_polymorphic()){ const serialization::extended_type_info *eti = & bos.get_eti(); const char * key = NULL; if(NULL != eti) key = eti->get_key(); if(NULL != key){ // the following is required by IBM C++ compiler which // makes a copy when passing a non-const to a const. This // is permitted by the standard but rarely seen in practice const class_name_type cn(key); // write out the external class identifier ar.vsave(cn); } else // without an external class name // we won't be able to de-serialize it so bail now boost::serialization::throw_exception( archive_exception(archive_exception::unregistered_class) ); } } if(bos.class_info()){ ar.vsave(tracking_type(bos.tracking(m_flags))); ar.vsave(version_type(bos.version())); } (const_cast<cobject_type &>(co)).m_initialized = true; } else{ ar.vsave(class_id_reference_type(co.m_class_id)); } // if we're not tracking if(! bos.tracking(m_flags)){ // just save the data itself ar.end_preamble(); serialization::state_saver<const void *> x(pending_object); serialization::state_saver<const basic_oserializer *> y(pending_bos); pending_object = t; pending_bos = & bpos_ptr->get_basic_serializer(); bpos_ptr->save_object_ptr(ar, t); return; } object_id_type oid(object_set.size()); // lookup to see if this object has already been written to the archive basic_oarchive_impl::aobject ao(t, co.m_class_id, oid); std::pair<basic_oarchive_impl::object_set_type::const_iterator, bool> aresult = object_set.insert(ao); oid = aresult.first->object_id; // if the saved object already exists if(! aresult.second){ // append the object id to he preamble ar.vsave(object_reference_type(oid)); // and windup. ar.end_preamble(); return; } // append id of this object to preamble ar.vsave(oid); ar.end_preamble(); // and save the object itself serialization::state_saver<const void *> x(pending_object); serialization::state_saver<const basic_oserializer *> y(pending_bos); pending_object = t; pending_bos = & bpos_ptr->get_basic_serializer(); bpos_ptr->save_object_ptr(ar, t); // add to the set of object initially stored through pointers stored_pointers.insert(oid); } } // namespace detail
void test_binary_ostream (OutputDevice & dev, byte_string & buffer) { typedef pfs::binary_ostream<OutputDevice> binary_ostream; ADD_TESTS(21); pfs::endian order = pfs::endian::network_order(); binary_ostream bos(dev, order); int i = 0; bos << bool(true); bos << bool(false); TEST_OK(buffer[i++] == 1); TEST_OK(buffer[i++] == 0); bos << 'A' << static_cast<int8_t>(0xDE) << static_cast<uint8_t>(0xAD); TEST_OK(buffer[i++] == 'A'); TEST_OK(buffer[i++] == 0xDE); TEST_OK(buffer[i++] == 0xAD); bos << static_cast<int16_t>(0xDEAD) << static_cast<uint16_t>(0xBEEF); TEST_OK(buffer[i++] == 0xDE); TEST_OK(buffer[i++] == 0xAD); TEST_OK(buffer[i++] == 0xBE); TEST_OK(buffer[i++] == 0xEF); bos << static_cast<int32_t>(0xDEADBEEF) << static_cast<uint32_t>(0xABADBABE); TEST_OK(buffer[i++] == 0xDE); TEST_OK(buffer[i++] == 0xAD); TEST_OK(buffer[i++] == 0xBE); TEST_OK(buffer[i++] == 0xEF); TEST_OK(buffer[i++] == 0xAB); TEST_OK(buffer[i++] == 0xAD); TEST_OK(buffer[i++] == 0xBA); TEST_OK(buffer[i++] == 0xBE); #if PFS_HAVE_INT64 ADD_TESTS(16) bos << static_cast<int64_t>(0xDEADBEEFABADBABE) << static_cast<uint64_t>(0xABADBABEDEADBEEF); TEST_OK(buffer[i++] == 0xDE); TEST_OK(buffer[i++] == 0xAD); TEST_OK(buffer[i++] == 0xBE); TEST_OK(buffer[i++] == 0xEF); TEST_OK(buffer[i++] == 0xAB); TEST_OK(buffer[i++] == 0xAD); TEST_OK(buffer[i++] == 0xBA); TEST_OK(buffer[i++] == 0xBE); TEST_OK(buffer[i++] == 0xAB); TEST_OK(buffer[i++] == 0xAD); TEST_OK(buffer[i++] == 0xBA); TEST_OK(buffer[i++] == 0xBE); TEST_OK(buffer[i++] == 0xDE); TEST_OK(buffer[i++] == 0xAD); TEST_OK(buffer[i++] == 0xBE); TEST_OK(buffer[i++] == 0xEF); #endif bos << static_cast<real32_t>(0x4E9D3A75); TEST_OK(buffer[i++] == 0x4E); TEST_OK(buffer[i++] == 0x9D); TEST_OK(buffer[i++] == 0x3A); TEST_OK(buffer[i++] == 0x75); #if PFS_HAVE_INT64 ADD_TESTS(8); bos << static_cast<real64_t>(0x43D0F43D0F43D0F4); TEST_OK(buffer[i++] == 0x43); TEST_OK(buffer[i++] == 0xD0); TEST_OK(buffer[i++] == 0xF4); TEST_OK(buffer[i++] == 0x3D); TEST_OK(buffer[i++] == 0x0F); TEST_OK(buffer[i++] == 0x43); TEST_OK(buffer[i++] == 0xD0); TEST_OK(buffer[i++] == 0xF4); #endif }
void Trimmer::CutTrack(string outputFLACFile, unsigned int leftSecond, unsigned int rightSecond) { /* if (bs.GetString(3) == "ID3") { bs.Skip(3); uint32_t id3size = 0; uint32_t b1 = 0, b2 = 0, b3 = 0, b4 = 0; bs.GetInteger(&b1, 8); bs.GetInteger(&b2, 8); bs.GetInteger(&b3, 8); bs.GetInteger(&b4, 8); id3size = (b1 << 21) | (b2 << 14) | (b3 << 7) | b4; bs.Skip(id3size); }*/ BitIStream bis(fileName); BitOStream bos(outputFLACFile); FLACMetaStreamInfo msi; if (bis.ReadString(4) != "fLaC") { throw NoFLACFile(); } bos.WriteString("fLaC"); FLACMetaBlockHeader mbh; mbh.IsLastBlock = false; while (!mbh.IsLastBlock) { ReadMetaBlockHeader(bis, &mbh); WriteMetaBlockHeader(bos, &mbh); switch (mbh.BlockType) { case FLAC_META_STREAMINFO: ReadStreamInfo(bis, &msi); WriteStreamInfo(bos, &msi); break; case FLAC_META_SEEKTABLE: case FLAC_META_APPLICATION: case FLAC_META_VORBIS_COMMENT: case FLAC_META_CUESHEET: case FLAC_META_PADDING: CopyBytes(bis, bos, mbh.BlockSize); break; default: cerr << "Warning: unknown block type" << endl; CopyBytes(bis, bos, mbh.BlockSize); break; } } for (int i = 0; i < 10; i++) { //Frame start FLACFrameHeader fh; ReadFrameHeader(bis, &fh); WriteFrameHeader(bos, &fh); if (fh.SyncCode != 0b11111111111110) { cout << "Invalid sync code" << endl; throw NoFLACFile(); } for (int subframeN = 0; subframeN <= msi.ChannelsCount; subframeN++) { FLACSubframeHeader sfh; ReadSubframeHeader(bis, &sfh); WriteSubframeHeader(bos, &sfh); switch (sfh.Type) { case FLAC_SF_CONSTANT: cout << "const" << endl; CopyConstantSubframe(bis, bos, &fh, &msi); break; case FLAC_SF_VERBATIM: cout << "verbatim" << endl; CopyVerbatimSubframe(bis, bos, &fh, &msi); break; case FLAC_SF_FIXED: cout << "fixed" << endl; CopyFixedSubframe(bis, bos, &fh, &msi, &sfh); break; case FLAC_SF_LPC: cout << "lpc" << endl; CopyLPCSubframe(bis, bos, &fh, &msi, &sfh); break; default: break; } } bos.AlignByte(); uint16_t crc16; bis.ReadInteger(&crc16, 16); bos.WriteInteger(crc16, 16); /*ReadFrameHeader(bis, &fh); cout << fh.SyncCode << endl;*/ //Frame end } }
Seq * keyword(const char x) { auto s = seq(bos(), lit(x), eos()); s->tokenize(); return s; }