libyui-ncurses  2.44.1
 All Classes Functions Variables
stdutil.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: stdutil.h
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #ifndef stdutil_h
26 #define stdutil_h
27 
28 #include <iosfwd>
29 
30 #include <cstdio>
31 #include <cstdarg>
32 
33 #include <string>
34 #include <vector>
35 #include <iostream>
36 
37 
38 namespace stdutil
39 {
40  inline std::string vform( const char * format, va_list ap, va_list ap1 )
41  {
42  char * buf = new char[vsnprintf( NULL, 0, format, ap ) + 1];
43  vsprintf( buf, format, ap1 );
44  std::string val( buf );
45  delete [] buf;
46  return val;
47  }
48 
49  inline std::string form( const char * format, ... )
50  __attribute__(( format( printf, 1, 2 ) ) );
51 
52  inline std::string form( const char * format, ... )
53  {
54  va_list ap;
55  va_list ap1;
56  va_start( ap, format );
57  va_start( ap1, format );
58  std::string val( vform( format, ap, ap1 ) );
59  va_end( ap );
60  va_end( ap1 );
61  return val;
62  }
63 
64  inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
65 
66  inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
67 
68  inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
69 
70  inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
71 
72  inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
73 
74  inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
75 
76  inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
77 
78  inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
79 
80  inline std::string hexstring( int n, int w = 10 ) { return form( "%#0*x", w, n ); }
81 
82  inline std::string hexstring( unsigned n, int w = 10 ) { return form( "%#0*x", w, n ); }
83 
84  inline std::string hexstring( long n, int w = 10 ) { return form( "%#0*lx", w, n ); }
85 
86  inline std::string hexstring( unsigned long n, int w = 10 ) { return form( "%#0*lx", w, n ); }
87 
88  inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
89 
90  inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
91 
92  inline std::string octstring( int n, int w = 0 ) { return form( "%#*o", w, n ); }
93 
94  inline std::string octstring( unsigned n, int w = 0 ) { return form( "%#*o", w, n ); }
95 
96  inline std::string octstring( long n, int w = 0 ) { return form( "%#*lo", w, n ); }
97 
98  inline std::string octstring( unsigned long n, int w = 0 ) { return form( "%#*lo", w, n ); }
99 
100 } // namespace stdutil
101 
102 #endif // stdutil_h