src/format.cpp

changeset 119
bdf8d46c145f
child 124
a7b769a0e537
equal deleted inserted replaced
118:e3361cf7cbf4 119:bdf8d46c145f
1 /*
2 Copyright 2012-2014 Santeri 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. The name of the author may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <cstdio>
30 #include "main.h"
31 #include "format.h"
32 #include "lexer.h"
33
34 // =============================================================================
35 //
36 static void formatError (String fmtstr, const String errdescribe, int pos)
37 {
38 fmtstr.replace ("\n", " ");
39 fmtstr.replace ("\t", " ");
40 String errmsg ("With format string:\n" + fmtstr + "\n");
41
42 for (int x = 0; x < pos; ++x)
43 errmsg += "-";
44
45 errmsg += "^\n" + errdescribe;
46 throw std::logic_error (errmsg.stdString());
47 }
48
49 // =============================================================================
50 //
51 String formatArgs (const String& fmtstr, const std::vector<String>& args)
52 {
53 String fmt = fmtstr;
54 String out;
55 int pos = 0;
56
57 while ((pos = fmt.firstIndexOf ("%", pos)) != -1)
58 {
59 if (fmt[pos + 1] == '%')
60 {
61 fmt.replace (pos, 2, "%");
62 pos++;
63 continue;
64 }
65
66 int ofs = 1;
67 char mod = '\0';
68
69 // handle modifiers
70 if (fmt[pos + ofs] == 's' || fmt[pos + ofs] == 'x' || fmt[pos + ofs] == 'd')
71 {
72 mod = fmt[pos + ofs];
73 ofs++;
74 }
75
76 if (!isdigit (fmt[pos + ofs]))
77 formatError (fmtstr, "bad format string, expected digit with optional "
78 "modifier after '%%'", pos);
79
80 int i = fmt[pos + ofs] - '0';
81
82 if (i > static_cast<signed> (args.size()))
83 formatError (fmtstr, String ("Format argument #") + i + " used but not defined.", pos);
84
85 String replacement = args[i - 1];
86
87 switch (mod)
88 {
89 case 's': replacement = (replacement == "1") ? "" : "s"; break;
90 case 'd': replacement.sprintf ("%d", replacement[0]); break;
91 case 'x': replacement.sprintf ("0x%X", replacement.toLong()); break;
92 default: break;
93 }
94
95 fmt.replace (pos, 1 + ofs, replacement);
96 pos += replacement.length();
97 }
98
99 return fmt;
100 }
101
102 // =============================================================================
103 //
104 void error (String msg)
105 {
106 Lexer* lx = Lexer::getCurrentLexer();
107 String fileinfo;
108
109 if (lx != null && lx->hasValidToken())
110 {
111 Lexer::TokenInfo* tk = lx->token();
112 fileinfo = format ("%1:%2:%3: ", tk->file, tk->line, tk->column);
113 }
114
115 throw std::runtime_error (fileinfo + msg);
116 }

mercurial