summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/pfmtlog.cpp
blob: d776e0133da96812aa15511f4e379796c2ba57e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// license:GPL-2.0+
// copyright-holders:Couriersud

#include "pfmtlog.h"
#include "palloc.h"
#include "pstonum.h"
#include "pstrutil.h"

#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>

namespace plib {

#if 0
struct ptemporary_locale
{
	ptemporary_locale(std::locale tlocale)
	: new_locale(tlocale), old_clocale(std::setlocale(LC_ALL, nullptr))
	{
		if (old_locale != tlocale)
			std::locale::global(tlocale);
		if (old_clocale != tlocale.name().c_str())
			std::setlocale(LC_ALL, tlocale.name().c_str());
	}

	~ptemporary_locale()
	{
		if (old_clocale != new_locale.name().c_str())
			std::setlocale(LC_ALL, old_clocale.c_str());
		if (old_locale != new_locale)
			std::locale::global(old_locale);
	}
private:
	std::locale new_locale;
	std::locale old_locale;
	pstring old_clocale;
};
#endif

pfmt::rtype pfmt::setfmt(std::stringstream &strm, char32_t cfmt_spec)
{
	pstring fmt;
	pstring search("{");
	search += plib::to_string(m_arg);

	rtype r;

	r.sl = search.size();
	r.p = m_str.find(search + ':');
	r.sl++; // ":"
	if (r.p == pstring::npos) // no further specifiers
	{
		r.p = m_str.find(search + '}');
		if (r.p == pstring::npos) // not found try default
		{
			r.sl = 2;
			r.p = m_str.find("{}");
		}
		else
			// found absolute positional place holder
			r.ret = 1;
		if (r.p == pstring::npos)
		{
			r.sl=2;
			r.p = m_str.find("{:");
			if (r.p != pstring:: npos)
			{
				auto p1 = m_str.find('}', r.p);
				if (p1 != pstring::npos)
				{
					r.sl = p1 - r.p + 1;
					fmt += m_str.substr(r.p+2, p1 - r.p - 2);
				}
			}
		}
	}
	else
	{
		// found absolute positional place holder
		auto p1 = m_str.find('}', r.p);
		if (p1 != pstring::npos)
		{
			r.sl = p1 - r.p + 1;
			fmt += ((m_arg>=10) ? m_str.substr(r.p+4, p1 - r.p - 4) : m_str.substr(r.p+3, p1 - r.p - 3));
			r.ret = 1;
		}
	}
	if (r.p != pstring::npos)
	{
		// a.b format here ...
		char32_t pend(0);
		int width(0);
		if (fmt != "" && pstring("duxofge").find(static_cast<pstring::value_type>(cfmt_spec)) != pstring::npos)
		{
			pend = static_cast<char32_t>(fmt.at(fmt.size() - 1));
			if (pstring("duxofge").find(static_cast<pstring::value_type>(pend)) == pstring::npos)
				pend = cfmt_spec;
			else
				fmt = plib::left(fmt, fmt.size() - 1);
		}
		else
			// FIXME: Error
			pend = cfmt_spec;

		auto pdot(fmt.find('.'));

		if (pdot==0)
			strm << std::setprecision(pstonum_ne_def<int>(fmt.substr(1), 6));
		else if (pdot != pstring::npos)
		{
			strm << std::setprecision(pstonum_ne_def<int>(fmt.substr(pdot + 1), 6));
			width = pstonum_ne_def<int>(left(fmt,pdot), 0);
		}
		else if (fmt != "")
			width = pstonum_ne_def<int>(fmt, 0);

		auto aw(plib::abs(width));

		strm << std::setw(aw);
		if (width < 0)
			strm << std::left;

		switch (pend)
		{
			case 'x':
				strm << std::hex;
				break;
			case 'o':
				strm << std::oct;
				break;
			case 'f':
				strm << std::fixed;
				break;
			case 'e':
				strm << std::scientific;
				break;
			default:
				break;
		}
	}
	else
		r.ret = -1;
	return r;

}

} // namespace plib