Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cube_formatting.hpp
Go to the documentation of this file.
1 #pragma once
3 
4 #include <cstdarg>
5 #include <cstdio>
6 #include <stdarg.h>
7 
8 #ifdef __GNUC__
9 #define PRINTFARGS(fmt, args) __attribute__((format(printf, fmt, args)))
10 #else
11 #define PRINTFARGS(fmt, args)
12 #endif
13 
15 inline void vformatstring(char *d, const char *fmt, va_list v, int len) { _vsnprintf(d, len, fmt, v); d[len-1] = 0; }
16 template<size_t N> inline void vformatstring(char(&d)[N], const char *fmt, va_list v) { vformatstring(d, fmt, v, N); }
17 
18 
19 inline char *concatstring(char *d, const char *s, size_t len) { size_t used = strlen(d); return used < len ? copystring(d+used, s, len-used) : d; }
20 template<size_t N> inline char *concatstring(char(&d)[N], const char *s) { return concatstring(d, s, N); }
21 
22 inline char *prependstring(char *d, const char *s, size_t len)
23 {
24  size_t slen = std::min(strlen(s), len);
25  memmove(&d[slen], d, std::min(len - slen, strlen(d) + 1));
26  memcpy(d, s, slen);
27  d[len-1] = 0;
28  return d;
29 }
30 template<size_t N> inline char *prependstring(char(&d)[N], const char *s) { return prependstring(d, s, N); }
31 
32 inline void nformatstring(char *d, int len, const char *fmt, ...) PRINTFARGS(3, 4);
33 inline void nformatstring(char *d, int len, const char *fmt, ...)
34 {
35  va_list v;
36  va_start(v, fmt);
37  vformatstring(d, fmt, v, len);
38  va_end(v);
39 }
40 
41 extern char *tempformatstring(const char *fmt, ...) PRINTFARGS(1, 2);
42 
43 template<size_t N> inline void formatstring(char(&d)[N], const char *fmt, ...) PRINTFARGS(2, 3);
44 template<size_t N> inline void formatstring(char(&d)[N], const char *fmt, ...)
45 {
46  va_list v;
47  va_start(v, fmt);
48  vformatstring(d, fmt, v, int(N));
49  va_end(v);
50 }
51 
52 template<size_t N> inline void concformatstring(char(&d)[N], const char *fmt, ...) PRINTFARGS(2, 3);
53 template<size_t N> inline void concformatstring(char(&d)[N], const char *fmt, ...)
54 {
55  va_list v;
56  va_start(v, fmt);
57  int len = strlen(d);
58  vformatstring(d + len, fmt, v, int(N) - len);
59  va_end(v);
60 }
61 
62 #define defformatstring(d,...) char d[260]; formatstring(d, __VA_ARGS__)
63 #define defvformatstring(d,last,fmt) char d[260]; { va_list ap; va_start(ap, last); vformatstring(d, fmt, ap); va_end(ap); }
64 
66 extern char *tempformatstring(const char *fmt, ...) PRINTFARGS(1, 2);
67 
StringFormatter::FmtMark fmt
Marker that starts formatting a string.
Definition: StringFormatter.cpp:5
char * prependstring(char *d, const char *s, size_t len)
Definition: cube_formatting.hpp:22
void vformatstring(char *d, const char *fmt, va_list v, int len)
Internal: format string using variable parameter lists (va_list)
Definition: cube_formatting.hpp:15
#define PRINTFARGS(fmt, args)
Definition: cube_formatting.hpp:11
char * tempformatstring(const char *fmt,...) PRINTFARGS(1
formatstring using some static globals, faster but less safe.
#define _vsnprintf
Definition: cube_tools.hpp:52
void nformatstring(char *d, int len, const char *fmt,...) PRINTFARGS(3
Definition: cube_formatting.hpp:33
char void formatstring(char(&d)[N], const char *fmt,...) PRINTFARGS(2
const T & min(const inexor::rpc::SharedVar< T > &a, const T &b)
Definition: SharedVar.hpp:210
void concformatstring(char(&d)[N], const char *fmt,...) PRINTFARGS(2
int d
Definition: octaedit.cpp:1749
char * copystring(char *d, const char *s, size_t len)
Definition: cube_tools.hpp:56
char * concatstring(char *d, const char *s, size_t len)
Definition: cube_formatting.hpp:19