cgv
Loading...
Searching...
No Matches
ostream_printf.cxx
1#include "ostream_printf.h"
2
3#include <stdlib.h>
4
5using namespace std;
6
7namespace cgv {
8 namespace utils {
9
10//------------------------------------------------------------------------
11void ostream_printf::parseFormatString()
12{
13 printStaticData();
14
15 /* convert printf and iso format */
16 int i = m_pos;
17
18 int width = 0;
19 int precision = 6;
20 int flags = 0;
21 char fill = ' ';
22 bool alternate = false;
23
24 while (m_format[i] != 0) {
25 i++;
26
27 /* these flags can run through multiple characters */
28 bool more = true;
29
30 while (more) {
31 switch(m_format[i]) {
32 case '+':
33 flags |= ios::showpos;
34 break;
35 case '-':
36 flags |= ios::left;
37 break;
38 case '0':
39 flags |= ios::internal;
40 fill = '0';
41 break;
42 case '#':
43 alternate = true;
44 break;
45 default:
46 more = false;
47 break;
48 }
49 if (more)
50 i++;
51 }
52
53 /* width specifier */
54 if ( isdigit(m_format[i]) ) {
55 width = atoi(m_format+i);
56 do{ i++; }while( isdigit(m_format[i]) );
57 }
58
59 /* output precision */
60 if (m_format[i] == '.') {
61 i++;
62 precision = atoi(m_format+i);
63 do{ i++; }while( isdigit(m_format[i]) );
64 }
65
66 /* type based settings */
67 bool format_ok = true;
68
69 switch(m_format[i]) {
70 case 'p':
71 break;
72 case 's':
73 break;
74 case 'c':
75 case 'C':
76 break;
77 case 'i':
78 case 'u':
79 case 'd':
80 flags |= ios::dec;
81 break;
82 case 'x':
83 flags |= ios::hex;
84 if (alternate) flags |= ios::showbase;
85 break;
86 case 'X':
87 flags |= ios::hex | ios::uppercase;
88 if (alternate) flags |= ios::showbase;
89 break;
90 case 'o':
91 flags |= ios::hex;
92 if (alternate) flags |= ios::showbase;
93 break;
94 case 'f':
95 flags |= ios::fixed;
96 if (alternate) flags |= ios::showpoint;
97 break;
98 case 'e':
99 flags |= ios::scientific;
100 if (alternate) flags |= ios::showpoint;
101 break;
102 case 'E':
103 flags |= ios::scientific | ios::uppercase;
104 if (alternate) flags |= ios::showpoint;
105 break;
106 case 'g':
107 if (alternate) flags |= ios::showpoint;
108 break;
109 case 'G':
110 flags |= ios::uppercase;
111 if (alternate) flags |= ios::showpoint;
112 break;
113 default:
114 /* if we encountered an unknown type specifier, skip current
115 format string and try parsing next one... */
116 format_ok = false;
117 break;
118 }
119
120 /* if formatting string was recognized set stream options*/
121 if (format_ok) {
122 m_stream.unsetf(ios::adjustfield | ios::basefield | ios::floatfield);
123 m_stream.setf((std::ios_base::fmtflags)flags);
124 m_stream.width(width);
125 m_stream.precision(precision);
126 m_stream.fill(fill);
127 break;
128 }
129 }
130
131 /* skip type specifier and set formatting string position */
132 m_pos = i+1;
133}
134//------------------------------------------------------------------------
135void ostream_printf::printStaticData()
136{
137 while ( m_format[m_pos] != '\0' )
138 {
139 if ( m_format[m_pos] == '%' )
140 {
141 if ( m_format[m_pos+1] == '%' )
142 m_pos++;
143 else break;
144 }
145 m_stream << m_format[m_pos];
146 m_pos++;
147 }
148}
149 }
150}
the cgv namespace
Definition print.h:11