DynamicAny any("99"); int i = any; // i == 99 any = 65536; string s = any; // s == "65536" char c = any; // , RangeException
const char *date = "05.06.1977"; //Apple 2 birthday int Diff; DateTime time = DateTimeParser::parse("%d.%m.%Y", date, Diff); Timespan diff = DateTime() - time; cout << "Apple 2 " << diff.days() << " " << DateTimeFormatter::format(time, "%e %B %Y, %W", Diff) << endl; // : "5 June 1977, Sunday" cout << " " << (DateTime::isLeapYear(time.year()) ? "" : " ");
#include "Poco/BasicEvent.h" #include "Poco/Delegate.h" #include <iostream> using BasicEvent; using Delegate; class Source { public: BasicEvent<int> theEvent; void fireEvent(int n) { theEvent(this, n); } }; class Target { public: void onEvent(const void* pSender, int& arg) { cout << "onEvent: " << arg << endl; } }; int main(int argc, char** argv) { Source source; Target target; // Event' source.theEvent += Delegate<Target, int>( &target, &Target::onEvent); // source.fireEvent(42); // source.theEvent -= Delegate<Target, int>( &target, &Target::onEvent); return 0; }
RegularExpression regular("([0-9]+) ([0-9]+)"); vector<string> arr; regular.split("123 456", 0, arr); //arr = {β123 456β, β123β, β456β} string s = "123 456"; regular.subst(s, "$2 $1"); // s == "456 123"
SharedLibrary lib("C:\\Windows\\System32\\shell32.dll"); if (lib.isLoaded() && lib.hasSymbol("Control_RunDLL")) { auto fun = static_cast<void (*)()>(lib.getSymbol("Control_RunDLL")); fun(); // lib.unload(); }
// class A {}; class B : public A {} ; class C : public A {}; β¦ SharedPtr<A> pA; SharedPtr<B> pB(new B); pA = pB; // , B A pA = new B; // // pB = pA; // , SharedPtr pB = pA.cast<B>(); // SharedPtr<C> pC(new C); pB = pC.cast<B>(); // pB null // DynamicFactory<A> factory; factory.registerClass<B>("B"); // Instantiator<B, A> factory.registerClass<C>("C"); // Instantiator<C, A> SharedPtr<A> pA = factory.createInstance("B"); SharedPtr<A> pB = factory.createInstance("C"); factory.unregisterClass("C"); bool isA = factory.isClass("B"), // true isB = factory.isClass("C"), // false ( ) isC = factory.isClass("habrahabr"); // false ( ?!) // Buffer<char> buffer(1024); // 1024 cin.read(buffer.begin(), buffer.size()); // 1024 stdin streamsize n = cin.gcount(); string s(buffer.begin(), n); // cout << s << endl; // // MemoryPool pool(1024); // 1024 // MemoryPool pool(1024, 4, 16); // 16 ; 4 char* buffer = static_cast<char*>(pool.get()); // cin.read(buffer, pool.blockSize()); // stdin streamsize n = cin.gcount(); string s(buffer, n); // pool.release(buffer); // cout << s << endl; //
class Singleton { public: int add(int i, int p) { return i+p; } static Singleton& instance() { static SingletonHolder<Singleton> single; return *single.get(); } }; β¦ int res = Singleton::instance().add(1,2);
//printf-style formating string str = format("In %[0]d was %[1]s of %[2]s %[3]d", DateTime(1977,6,5).year(), // %[0]d string("b-day"), // %[1]s string("Apple"), // %[2]s 2); // %[3]d //str = βIn 1977 was b-day of Apple 2β
cout << " : " << Environment::osName() << "\n : " << Environment::osVersion() << "\n: " << Environment::osArchitecture() << "\n. : " << Environment::nodeName() << "\nMAC-: " << Environment::nodeId(); if (Environment::has("HOME")) cout << "\n .: " << Environment::get("HOME") << endl; Environment::set("POCO", "foo"); // POCO=foo
ofstream zipper("text.gz", ios::binary); ofstream text("text.txt", ios::binary); text << "Hello my little computer world. I can zip this string. It's wonderful."; text.close(); Zip::Compress zipc(zipper,true); zipc.addFile (Path("text.txt"), "text.txt"); zipc.close(); ifstream dezipper("text.gz", ios::binary); poco_assert (dezipper); Zip::Decompress zipd(dezipper,Path().append("\\text.dz")); zipd.decompressAllFiles();
Random rnd; rnd.seed(); cout << ": " << rnd.next() << "\n: " << rnd.next(10) << "\n: " << rnd.nextChar() << "\n: " << rnd.nextBool() << "\n . : " << rnd.nextDouble(); RandomInputStream ri; string rs; ri >> rs;
string message(" "); string passphrase("anl!sfsd9!_3g2g?f73"); // //HMAC = Hash-based message authentication code ;) HMACEngine<SHA1Engine> encoder(passphrase); encoder.update(message); // // encoder.digest() string encodedstr(DigestEngine::digestToHex(encoder.digest())); // MD5 MD5Engine md5; DigestOutputStream ostr(md5); ostr << " "; ostr.flush(); // HEX string result = DigestEngine::digestToHex(md5.digest());
Data::SQLite::Connector::registerConnector(); Data::Session ses("SQLite", "mydb.db"); int count = 0; ses << "SELECT COUNT(*) FROM PERSON", into(count), now; cout << "People in DB " << count; string firstName(""; string lastName(""); int age = 0; ses << "INSERT INTO PERSON VALUES (:fn, :ln, :age)", use(firstName), use(lastName), use(age), now; ses << "SELECT (firstname, lastname, age) FROM Person", into(firstName), into(lastName), into(age, -1), now; Data::SQLite::Connector::unregisterConnector();
Statement select(session); select << "SELECT * FROM Person"; select.execute(); RecordSet rs(select);
struct Person { string fullname, city; size_t age; }; β¦ typedef Tuple<string, string, int> Person; vector<Person> people; people.push_back(Person("Bart Simpson", "Springfield", 12)); people.push_back(Person("Lisa Simpson", "Springfield", 10)); Statement insert(session); insert << "INSERT INTO Person VALUES(:name, :address, :age)", use(people), now;
Path p_wind("C:\\Windows\\system32\\cmd.exe"); Path p_unix("/bin/sh"); p_wind = "projects\\poco"; p_unix = "projects/poco"; p_unix.parse("/usr/include/stdio.h", Path::PATH_UNIX); bool ok = p_unix.tryParse("/usr/*/stdio.h"); ok = p_unix.tryParse("/usr/include/stdio.h", Path::PATH_UNIX); ok = p_unix.tryParse("/usr/include/stdio.h", Path::PATH_WINDOWS); ok = p_unix.tryParse("DSK$PROJ:[POCO]BUILD.COM", Path::PATH_GUESS);
// AutoPtr<SimpleFileChannel> pChannel(new SimpleFileChannel); pChannel->setProperty("path", "log.log"); pChannel->setProperty("rotation", "2 K"); Logger::root().setChannel(pChannel); Logger& logger = Logger::get("TestLogger"); // TestLogger for (int i = 0; i < 13; ++i) logger.information(" 13 "); // AutoPtr<FileChannel> pChannel(new FileChannel); pChannel->setProperty("path", " log.log"); pChannel->setProperty("rotation", "2 K"); pChannel->setProperty("archive", "timestamp"); Logger::root().setChannel(pChannel); Logger& logger = Logger::get("TestLogger"); // TestLogger for (int i = 0; i < 13; ++i) logger.information(" 13 "); // AutoPtr<ConsoleChannel> pCons(new ConsoleChannel); AutoPtr<AsyncChannel> pAsync(new AsyncChannel(pCons)); Logger::root().setChannel(pAsync); Logger& logger = Logger::get("TestLogger"); // TestLogger for (int i = 0; i < 13; ++i) logger.information(" 13 "); //- AutoPtr<ConsoleChannel> pCons(new ConsoleChannel); AutoPtr<SimpleFileChannel> pFile(new SimpleFileChannel("test.log")); AutoPtr<SplitterChannel> pSplitter(new SplitterChannel); pSplitter->addChannel(pCons); pSplitter->addChannel(pFile); Logger::root().setChannel(pSplitter); Logger::root().information(" "); Logger& logger = Logger::get("TestLogger"); LogStream lstr(logger); lstr << " " << endl; // AutoPtr<ConsoleChannel> pCons(new ConsoleChannel); AutoPtr<PatternFormatter> pPF(new PatternFormatter); // "-- :: : " pPF->setProperty("pattern", "%Y-%m-%d %H:%M:%S %s: %t"); AutoPtr<FormattingChannel> pFC(new FormattingChannel(pPF, pCons)); Logger::root().setChannel(pFC); Logger::get("TestChannel").information(" ");
// Runnable. Hello JAVA. class ThreadRunner: public Runnable { virtual void run() { cout << " " << endl; } }; // class ThreadRunnerNoRunnable { void norun() { cout << " " << endl; } }; β¦ ThreadRunner runnable; Thread thread; thread.start(runnable); // thread.join(); // ThreadRunnerNoRunnable norunnable; RunnableAdapter< ThreadRunnerNoRunnable > runnable2(norunnable, & ThreadRunnerNoRunnable::norun); Thread thread2; Thread2.start(norunnable); // Thread2.join(); // // class ThreadRunner: public Runnable { virtual void run() { static int num; cout << " " << endl; for (int i=0; i<100; ++i) ++num; } }; β¦ ThreadRunner runnable1, runnable2, runnable3; // ThreadPool::defaultPool().start(runnable1); ThreadPool::defaultPool().start(runnable2); ThreadPool::defaultPool().start(runnable3); // ThreadPool::defaultPool().joinAll();
class ThreadRunner: public Runnable { public: ThreadRunner(int &num, Mutex& mutex) : num(num), mutex(mutex) {} virtual void run() { while (num < 100) { ScopedLock<Mutex> lock(mutex); // , for (int i=0; i<5; ++i) cout << "TID:" << Thread::currentTid() << " NUM:" << ++num << endl; Thread::sleep(100); } } private: int& num; Mutex& mutex; }; ... int num = 0; Mutex mutex; Thread th1; ThreadRunner tr1(num,mutex); th1.start(tr1); while (num < 100) { ScopedLock<Mutex> lock(mutex); // , for (int i=0; i<5; ++i) cout << "TID:" << Thread::currentTid() << " NUM:" << ++num << endl; Thread::sleep(100); } th1.join();
Net::SocketAddress sa("www.habrahabr.ru", 80); Net::StreamSocket socket(sa); Net::SocketStream str(socket); str << "GET / HTTP/1.1\r\n" "Host: habrahabr.ru\r\n" "\r\n"; str.flush(); StreamCopier::copyStream(str, cout); // Net::ServerSocket srv(8080); // while(true) { Net::StreamSocket ss = srv.acceptConnection(); { Net::SocketStream str(ss); str << "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n" "\r\n" "<html><head><title> </title></head>" "<body><h1><a href=\"http://habrahabr.ru\"> .</a></h1></body></html>" << flush; } }
const HostEntry& entry = DNS::hostByName("www.habrahabr.ru"); cout << " : " << entry.name() << endl; const HostEntry::AliasList& aliases = entry.aliases(); for (HostEntry::AliasList::const_iterator it1 = aliases.begin(); it1 != aliases.end(); ++it1) cout << ": " << *it1 << endl; const HostEntry::AddressList& addrs = entry.addresses(); for (HostEntry::AddressList::const_iterator it2 = addrs.begin(); it2 != addrs.end(); ++it2) cout << "IP: " << it2->toString() << endl;
cout << " PID: " << Process::id() << endl; vector<string> args; args.push_back("shell32"); args.push_back("ShellAboutA"); Process::launch("rundll32", args);
File file("Shared.dat"); SharedMemory mem(file, SharedMemory::AM_READ); cout << &*mem.begin() << endl;
Base64Encoder encoder(cout); encoder << " Base64 stdout";
Net::HTTPStreamFactory::registerFactory(); auto_ptr<istream> input( URIStreamOpener::defaultOpener().open("http://habrahabr.ru") ); cout << input->rdbuf();
string utf8(" UTF-8 ."); UTF8Encoding decoder; TextIterator end(utf8); for (TextIterator iter(utf8, decoder); iter != end; ++iter) int unicode = *iter;
string latin(" Latin1 ."); Latin1Encoding decoder; UTF8Encoding encoder; OutputStreamConverter converter(cout, decoder, encoder); converter << latin << endl; // latin - UTF-8
Source: https://habr.com/ru/post/148173/
All Articles