This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cpp
211 lines (191 loc) · 4.92 KB
/
utils.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <wx/regex.h>
#include <wx/numformatter.h>
// helper function
// from Alatar's project without modifications
wxString RemoveQuotes(const wxString& s)
{
wxString str = s;
wxUniChar quote_char = s[0];
if (((quote_char != '\'') && (quote_char != '"')) || (s[s.Len() - 1] != quote_char))
{
return str.Trim(true).Trim(false);
}
str = s.Mid(1, s.Len() - 2);
str = str.Trim(true).Trim(false);
return str;
}
// helper function
// from Alatar's project without modifications
wxString ParseNominal(wxString a_designator, wxString a_value)
{
char ch;
double tmp_val;
double factor = 1;
wxString val, tmp_unit;
double value = -1; // Номинальное значение для резисторов/еондёров/etc.
wxString unit; // Размерность номинального значения
wxString value_postfix; // Утточнение номинала (например вольтаж кондёра)
wxString result; // Утточнение номинала (например вольтаж кондёра)
ch = a_designator.Upper()[0];
switch (ch)
{
case 'R':
unit = "";
break;
case 'C':
unit = "F";
factor = 1e-6;
break;
case 'L':
unit = "H";
break;
default:
return a_value;
}
//wxLogVerbose(_T(">> Input is %s for %s"), a_value, a_designator);
wxRegEx re_format1("^([[:digit:]]+)([pnumkKMrRfFhH]{1,2})([[:digit:]]+)(.*)$"); //1p1F bla-bla-bla
wxRegEx re_format2("^([[:digit:]]+[.,]?[[:digit:]]*)([pnumkKM]?)[fFhH]?(.*)$"); //1.1pF bla-bla-bla
if (re_format1.Matches(a_value))
{
wxString tmp_str = re_format1.GetMatch(a_value, 1) + "." + re_format1.GetMatch(a_value, 3) + re_format1.GetMatch(a_value, 2) + re_format1.GetMatch(a_value, 4);
a_value = tmp_str;
}
if (!re_format2.Matches(a_value))
{
value = -1;
return wxEmptyString;
}
val = re_format2.GetMatch(a_value, 1); val.Replace(",", ".");
tmp_unit = re_format2.GetMatch(a_value, 2);
value_postfix = re_format2.GetMatch(a_value, 3);
//wxLogVerbose(_T(">> Input is %s, Val is %s, Unit is %s, Postfix is %s"), a_value, val, tmp_unit, value_postfix);
if ((!val.ToCDouble(&tmp_val)) || (tmp_val < 0))
{
value = -1;
//wxLogVerbose(_T("Convert fail! Val is %s, converted val is %f"), val, tmp_val);
return wxEmptyString;
}
value = tmp_val;
ch = tmp_unit[0];
switch (ch)
{
case 'p':
factor = 1e-12;
break;
case 'n':
factor = 1e-9;
break;
case 'u':
factor = 1e-6;
break;
case 'm':
factor = 1e-3;
break;
case 'k':
case 'K': //кривое название, но встречается часто
factor = 1e3;
break;
case 'M':
factor = 1e6;
break;
}
//wxLogVerbose(_T("Scale: Val is %f, factor is %f"), value, factor);
value *= factor;
//wxLogVerbose(_T("PnP_convFrame::NormalizeNominal %f - %s"), value, unit);
if (value == 0)
{
//wxLogVerbose(_T("PnP_convFrame::NormalizeNominal Zero on input: %f - %s"), value, unit);
}
else if (value < 1e-9)
{
value /= 1e-12;
unit.Prepend("p");
//wxLogVerbose(_T("Step 1e-9 -> %f - %s"), value, unit);
}
else if (value < 1e-6)
{
if ("F" == unit)
{
if (value < 1e-8)
{
value /= 1e-12;
unit.Prepend("p");
}
else
{
value /= 1e-6;
unit.Prepend("u");
}
}
else
{
value /= 1e-9;
unit.Prepend("n");
}
//wxLogVerbose(_T("Step 1e-6 -> %f - %s"), value, unit);
}
else if (value < 1e-3)
{
value /= 1e-6;
unit.Prepend("u");
//wxLogVerbose(_T("Step 1e-3 -> %f - %s"), value, unit);
}
else if (value < 1)
{
if (("F" == unit) && (value < 1e-1))
{
value /= 1e-6;
unit.Prepend("u");
}
else
{
value /= 1e-3;
unit.Prepend("m");
}
//wxLogVerbose(_T("Step 1e-0 -> %f - %s"), value, unit);
}
else if (value < 1e3)
{
// value /= 1e0;
// unit.Prepend("");
//wxLogVerbose(_T("Step 1e3 -> %f - %s"), value, unit);
}
else if (value < 1e6)
{
value /= 1e3;
unit.Prepend("k");
//wxLogVerbose(_T("Step 1e6 -> %f - %s"), value, unit);
}
else
{
value /= 1e6;
unit.Prepend("M");
//wxLogVerbose(_T("Step 1e9 -> %f - %s"), value, unit);
}
result = wxNumberFormatter::ToString(value, 8, wxNumberFormatter::Style_NoTrailingZeroes)/*.Replace(",", ".")*/ + unit + value_postfix;
//wxLogVerbose(_T("<< Result is %s"), result);
return result;
}
// Алфавитно-цифровое сравнение двух строк
int compare_alphanum( const wxString& str1, const wxString& str2)
{
long nb1 = 0;
long nb2 = 0;
wxString s1;
wxString s2;
wxRegEx regEx("([^0-9]+)([0-9]+)");
if (regEx.Matches(str1))
{
s1 = regEx.GetMatch(str1, 1);
regEx.GetMatch(str1, 2).ToLong(&nb1);
}
if (regEx.Matches(str2))
{
s2 = regEx.GetMatch(str2, 1);
regEx.GetMatch(str2, 2).ToLong(&nb2);
}
if (s1 == s2)
return nb1 < nb2;
else
return s1 < s2;
}