src/format.cc

changeset 72
03e4d9db3fd9
child 73
1ee9b312dc18
equal deleted inserted replaced
71:11f23fabf8a6 72:03e4d9db3fd9
1 #include <cstdio>
2 #include "main.h"
3 #include "format.h"
4
5 static void draw_pos (const string& fmt, int pos)
6 {
7 string rep (fmt);
8 rep.replace ("\n", "↵");
9 rep.replace ("\t", "⇥");
10
11 fprintf (stderr, "%s\n", rep.chars());
12
13 for (int x = 0; x < pos; ++x)
14 fprintf (stderr, "-");
15
16 fprintf (stderr, "^\n");
17 }
18
19 string format_args (const list<format_arg>& args)
20 {
21 const string& fmtstr = args[0].as_string();
22 assert (args.size() >= 1);
23
24 if (args.size() == 1)
25 return args[0].as_string();
26
27 string fmt = fmtstr;
28 string out;
29 int pos = 0;
30
31 while ((pos = fmt.first ("%", pos)) != -1)
32 {
33 if (fmt[pos + 1] == '%')
34 {
35 fmt.replace (pos, 2, "%");
36 pos++;
37 continue;
38 }
39
40 int ofs = 1;
41 char mod = '\0';
42
43 // handle modifiers
44 if (fmt[ pos + ofs ] == 's' || fmt[ pos + ofs ] == 'x')
45 {
46 mod = fmt[ pos + ofs ];
47 ofs++;
48 }
49
50 if (!isdigit (fmt[ pos + ofs ]))
51 {
52 fprintf (stderr, "bad format string, expected digit with optional "
53 "modifier after '%%':\n");
54 draw_pos (fmt, pos);
55 return fmt;
56 }
57
58 int i = fmt[ pos + ofs ] - '0';
59
60 if (i >= args.size())
61 {
62 fprintf (stderr, "format arg #%d used but not defined: %s\n", i, fmtstr.chars());
63 return fmt;
64 }
65
66 string repl = args[i].as_string();
67
68 if (mod == 's')
69 {
70 repl = (repl == "1") ? "" : "s";
71 }
72
73 elif (mod == 'd')
74 {
75 repl.sprintf ("%d", repl[0]);
76 }
77 elif (mod == 'x')
78 {
79 // modifier x: reinterpret the argument as hex
80 repl.sprintf ("0x%X", strtol (repl.chars(), null, 10));
81 }
82
83 fmt.replace (pos, 1 + ofs, repl);
84 pos += repl.length();
85 }
86
87 return fmt;
88 }
89
90 void print_args (FILE* fp, const list<format_arg>& args)
91 {
92 string out = format_args (args);
93 fprintf (fp, "%s", out.chars());
94 }

mercurial