1 /* $Id: string.hh,v 1.5 2003/08/15 11:38:30 atterer Exp $ -*- C++ -*-
3 |_) /| Copyright (C) 2001-2002 | richard@
4 | \/¯| Richard Atterer | atterer.net
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2. See
8 the file COPYING for details.
10 subst("Format %1, %2", arg1, arg2) creates strings with the arguments
11 filled in, and does so in a safer way than sprintf() and friends.
21 //______________________________________________________________________
23 /** Convert a number x to characters and append to a string. */
24 string& append(string& s, double x);
25 string& append(string& s, int x);
26 string& append(string& s, unsigned x);
27 string& append(string& s, long x);
28 string& append(string& s, unsigned long x);
29 #if HAVE_UNSIGNED_LONG_LONG
30 string& append(string& s, unsigned long long x);
32 /** Convert a number x to characters and append to a string, padding
33 at front with space characters. width must be <40*/
34 string& append(string& s, unsigned x, int width);
35 string& append(string& s, unsigned long x, int width);
36 #if HAVE_UNSIGNED_LONG_LONG
37 string& append(string& s, unsigned long long x, int width);
39 //______________________________________________________________________
43 Subst(int x) { type = INT; val.intVal = x; }
44 Subst(unsigned x) { type = UNSIGNED; val.unsignedVal = x; }
45 Subst(long x) { type = LONG; val.longVal = x; }
46 Subst(unsigned long x) { type = ULONG; val.ulongVal = x; }
47 # if HAVE_UNSIGNED_LONG_LONG
48 Subst(unsigned long long x) { type = ULONGLONG; val.ulonglongVal = x; }
50 Subst(double x) { type = DOUBLE; val.doubleVal = x; }
51 Subst(char x) { type = CHAR; val.charVal = x; }
52 Subst(const char* x) { type = CHAR_P; val.charPtr = x; }
53 Subst(const string& x) { type = STRING_P; val.stringPtr = &x; }
54 Subst(const string* x) { type = STRING_P; val.stringPtr = x; }
55 Subst(const void* x) { type = POINTER; val.pointerVal = x; }
56 static string subst(const char* format, int args, const Subst arg[]);
61 static inline void doSubst(string& result, const Subst arg[], int n,
65 INT, UNSIGNED, LONG, ULONG, ULONGLONG, DOUBLE, CHAR, CHAR_P, STRING_P,
72 unsigned long ulongVal;
73 # if HAVE_UNSIGNED_LONG_LONG
74 unsigned long long ulonglongVal;
79 const string* stringPtr;
80 const void* pointerVal;
83 //______________________________________________________________________
86 cout << subst("file `%1' not found: %2",
87 string("foo"), strerror(errno)); */
89 inline string subst(const char* format, Subst a) {
90 return Subst::subst(format, 1, &a);
92 inline string subst(const char* format, Subst a, Subst b) {
93 Subst arg[] = { a, b };
94 return Subst::subst(format, 2, arg);
96 inline string subst(const char* format, Subst a, Subst b, Subst c) {
97 Subst arg[] = { a, b, c };
98 return Subst::subst(format, 3, arg);
100 inline string subst(const char* format, Subst a, Subst b, Subst c, Subst d) {
101 Subst arg[] = { a, b, c, d };
102 return Subst::subst(format, 4, arg);
104 inline string subst(const char* format, Subst a, Subst b, Subst c, Subst d,
106 Subst arg[] = { a, b, c, d, e };
107 return Subst::subst(format, 5, arg);
109 inline string subst(const char* format, Subst a, Subst b, Subst c, Subst d,
111 Subst arg[] = { a, b, c, d, e, f };
112 return Subst::subst(format, 6, arg);
114 inline string subst(const char* format, Subst a, Subst b, Subst c, Subst d,
115 Subst e, Subst f, Subst g) {
116 Subst arg[] = { a, b, c, d, e, f, g };
117 return Subst::subst(format, 7, arg);
119 inline string subst(const char* format, Subst a, Subst b, Subst c, Subst d,
120 Subst e, Subst f, Subst g, Subst h) {
121 Subst arg[] = { a, b, c, d, e, f, g, h };
122 return Subst::subst(format, 8, arg);
124 inline string subst(const char* format, Subst a, Subst b, Subst c, Subst d,
125 Subst e, Subst f, Subst g, Subst h, Subst i) {
126 Subst arg[] = { a, b, c, d, e, f, g, h, i };
127 return Subst::subst(format, 9, arg);