|
1 /* |
|
2 Copyright 2014 Teemu Piippo |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions |
|
7 are met: |
|
8 |
|
9 1. Redistributions of source code must retain the above copyright |
|
10 notice, this list of conditions and the following disclaimer. |
|
11 2. Redistributions in binary form must reproduce the above copyright |
|
12 notice, this list of conditions and the following disclaimer in the |
|
13 documentation and/or other materials provided with the distribution. |
|
14 3. Neither the name of the copyright holder nor the names of its |
|
15 contributors may be used to endorse or promote products derived from |
|
16 this software without specific prior written permission. |
|
17 |
|
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
|
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER |
|
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 */ |
|
30 |
|
31 #include <cstdio> |
|
32 #include "format.h" |
|
33 |
|
34 // |
|
35 // Throws an error while formatting the string |
|
36 // |
|
37 static auto format_error (String fmtstr, const String errdescribe, int pos) -> void |
|
38 { |
|
39 fmtstr.replace ("\n", " "); |
|
40 fmtstr.replace ("\t", " "); |
|
41 String errmsg ("With format string:\n" + fmtstr + "\n"); |
|
42 |
|
43 for (int x = 0; x < pos; ++x) |
|
44 errmsg += "-"; |
|
45 |
|
46 errmsg += "^\n" + errdescribe; |
|
47 // throw std::logic_error (errmsg.stdString()); |
|
48 std::fprintf (stderr, "%s", errmsg.chars()); |
|
49 std::exit (EXIT_FAILURE); |
|
50 } |
|
51 |
|
52 // |
|
53 // Main formatter algorithm autoion. Processes @fmtstr with @args and returns |
|
54 // the result. |
|
55 // |
|
56 auto format_args (const String& fmtstr, const Vector<String>& args) -> String |
|
57 { |
|
58 String fmt = fmtstr; |
|
59 int pos = 0; |
|
60 |
|
61 while ((pos = fmt.find ("%", pos)) != -1) |
|
62 { |
|
63 if (fmt[pos + 1] == '%') |
|
64 { |
|
65 fmt.replace (pos, 2, "%"); |
|
66 pos++; |
|
67 continue; |
|
68 } |
|
69 |
|
70 int ofs = 1; |
|
71 char mod = '\0'; |
|
72 |
|
73 // handle modifiers |
|
74 if (fmt[pos + ofs] == 's' or fmt[pos + ofs] == 'x' or fmt[pos + ofs] == 'd') |
|
75 { |
|
76 mod = fmt[pos + ofs]; |
|
77 ofs++; |
|
78 } |
|
79 |
|
80 if (fmt[pos + ofs] < '1' or fmt[pos + ofs] > '9') |
|
81 { |
|
82 format_error (fmtstr, "bad format string, expected non-zero digit with " |
|
83 "optional modifier after '%%'", pos); |
|
84 } |
|
85 |
|
86 int i = fmt[pos + ofs] - '0'; |
|
87 |
|
88 if (i > args.size()) |
|
89 format_error (fmtstr, String ("Format argument #") + i + " used but not defined.", pos); |
|
90 |
|
91 String replacement = args[i - 1]; |
|
92 |
|
93 switch (mod) |
|
94 { |
|
95 case 's': replacement = (replacement == "1") ? "" : "s"; break; |
|
96 case 'd': replacement.sprintf ("%d", replacement[0]); break; |
|
97 case 'x': replacement.sprintf ("0x%x", replacement.to_int()); break; |
|
98 default: break; |
|
99 } |
|
100 |
|
101 fmt.replace (pos, 1 + ofs, replacement); |
|
102 pos += replacement.length(); |
|
103 } |
|
104 |
|
105 return fmt; |
|
106 } |
|
107 |