Code Fragment Collection
Web
asperix.org
Home
Contact
Categories
C++
C
PHP
BASH
Sockets
APL
SQL
All
Documentation
License
Related
C++ Articles
APL Library
Math Library
Geo Library
mirzoyan.com
C++ Articles
Standard error catch statement
This is general template for building error catching and handling mechanism.
try { ... your code here... } catch(std::exception &e) { // error handling std::cerr << e.what() << std::endl ; } catch(...) { // unknown exception std::cerr << "unknown exception" << std::endl ; }
Load file content
#include
{ std::string content ; apl::AFile<> file ; file.load("file.dat",content) ; }
Read directory
#include
using namespace apl ; { ADir<> dir ; ADir<>::file_list_type files ; dir.open(dirname) ; dir.read(filemask,files) ; for ( ADir<>::file_list_type::iterator f = files.begin(), f_end = files.end() ; f != f_end ; ++f ) { cout << f->name << endl ; // process the file } }
Make directory including parents
#include
using namespace apl ; { AFileSystem<> fs ; fs.mkdir(dirname,fs.mkdir_parents) ; }
Example of using typemap
#include
class Key1{} ; class Key2{} ; class Key3{} ; class Value1{} ; class Value2{} ; class Value3{} ; { using namespace apl ; typedef typemap::insert
::X Map2 ; typedef typemap::insert
::X Map3 ; typedef typemap::insert
::X Map4 ; cout << typeid(typemap::find
::X::Value).name() << endl ; }
HTTP Request
#include
{ ASocket<> sock ; AHttpClient
> http(sock) ; http.get("http://www.example.com/") ; std::cout << http.response.body << std::endl ; }
Strip directory name and suffix
name=`basename $file_path $suffix`
IFS default value
On production scripts for security reasons it is always good to set IFS variable to its default value. By default IFS is set to tab, new line, and space.
old_IFS=$IFS IFS= IFS=`echo -n -e "\t\n\x20"` # ... your code here ... IFS=$old_IFS
Copyright (C) 2006 Andrey Mirzoyan