libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/stdutil.h
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019    File:       stdutil.h
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef stdutil_h
00026 #define stdutil_h
00027 
00028 #include <iosfwd>
00029 
00030 #include <cstdio>
00031 #include <cstdarg>
00032 
00033 #include <string>
00034 #include <vector>
00035 #include <iostream>
00036 
00037 
00038 namespace stdutil
00039 {
00040     inline std::string vform( const char * format, va_list ap, va_list ap1 )
00041     {
00042         char * buf = new char[vsnprintf( NULL, 0, format, ap ) + 1];
00043         vsprintf( buf, format, ap1 );
00044         std::string val( buf );
00045         delete [] buf;
00046         return val;
00047     }
00048 
00049     inline std::string form( const char * format, ... )
00050         __attribute__(( format( printf, 1, 2 ) ) );
00051 
00052     inline std::string form( const char * format, ... )
00053     {
00054         va_list ap;
00055         va_list ap1;
00056         va_start( ap, format );
00057         va_start( ap1, format );
00058         std::string val( vform( format, ap, ap1 ) );
00059         va_end( ap );
00060         va_end( ap1 );
00061         return val;
00062     }
00063 
00064     inline std::string numstring( char n,          int w = 0 ) { return form( "%*hhd", w, n ); }
00065 
00066     inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
00067 
00068     inline std::string numstring( int n,           int w = 0 ) { return form( "%*d",   w, n ); }
00069 
00070     inline std::string numstring( unsigned n,      int w = 0 ) { return form( "%*u",   w, n ); }
00071 
00072     inline std::string numstring( long n,          int w = 0 ) { return form( "%*ld",  w, n ); }
00073 
00074     inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu",  w, n ); }
00075 
00076     inline std::string hexstring( char n,          int w = 4 ) { return form( "%#0*hhx", w, n ); }
00077 
00078     inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
00079 
00080     inline std::string hexstring( int n,           int w = 10 ) { return form( "%#0*x",   w, n ); }
00081 
00082     inline std::string hexstring( unsigned n,      int w = 10 ) { return form( "%#0*x",   w, n ); }
00083 
00084     inline std::string hexstring( long n,          int w = 10 ) { return form( "%#0*lx",  w, n ); }
00085 
00086     inline std::string hexstring( unsigned long n, int w = 10 ) { return form( "%#0*lx",  w, n ); }
00087 
00088     inline std::string octstring( char n,          int w = 4 ) { return form( "%#0*hho", w, n ); }
00089 
00090     inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
00091 
00092     inline std::string octstring( int n,           int w = 0 ) { return form( "%#*o",    w, n ); }
00093 
00094     inline std::string octstring( unsigned n,      int w = 0 ) { return form( "%#*o",    w, n ); }
00095 
00096     inline std::string octstring( long n,          int w = 0 ) { return form( "%#*lo",   w, n ); }
00097 
00098     inline std::string octstring( unsigned long n, int w = 0 ) { return form( "%#*lo",   w, n ); }
00099 
00100 } // namespace stdutil
00101 
00102 #endif // stdutil_h
 All Classes Functions Variables