]> git.armaanb.net Git - gen-shell.git/blob - src/libshared/src/format.h
added install instructions
[gen-shell.git] / src / libshared / src / format.h
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26
27 #ifndef INCLUDED_FORMAT
28 #define INCLUDED_FORMAT
29
30 #include <sstream>
31 #include <algorithm>
32 #include <string>
33 #include <vector>
34
35 const std::string format (std::string&);
36 const std::string format (const char*);
37 const std::string formatHex (int);
38 const std::string format (float, int, int);
39 const std::string format (double, int, int);
40 const std::string format (double);
41
42 void replace_positional (std::string&, const std::string&, const std::string&);
43
44 template<typename T>
45 const std::string format (T value)
46 {
47     std::stringstream s;
48     s << value;
49     return s.str ();
50 }
51
52 template<typename T>
53 const std::string format (int fmt_num, const std::string& fmt, T arg)
54 {
55     std::string output = fmt;
56     replace_positional (output, '{' + format (fmt_num) + '}', format (arg));
57     return output;
58 }
59
60 template<typename T, typename... Args>
61 const std::string format (int fmt_num, const std::string& fmt, T arg, Args... args)
62 {
63     const std::string fmt_replaced (format (fmt_num, fmt, arg));
64     return format (fmt_num+1, fmt_replaced, args...);
65 }
66
67 template<typename... Args>
68 const std::string format (const std::string& fmt, Args... args)
69 {
70     return format (1, fmt, args...);
71 }
72
73 std::string leftJustify (const int, const int);
74 std::string leftJustify (const std::string&, const int);
75 std::string rightJustifyZero (const int, const int);
76 std::string rightJustify (const int, const int);
77 std::string rightJustify (const std::string&, const int);
78
79 std::string commify (const std::string&);
80 std::string formatBytes (size_t);
81 std::string formatTime (time_t);
82 std::string printable (const std::string&);
83 std::string printable (char);
84
85 std::string obfuscateText (const std::string&);
86
87 #endif