973c2be1 |
1 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
2 | // |
973c2be1 |
3 | // This file is part of Open CASCADE Technology software library. |
b311480e |
4 | // |
d5f74e42 |
5 | // This library is free software; you can redistribute it and/or modify it under |
6 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
7 | // by the Free Software Foundation, with special exception defined in the file |
8 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
9 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
10 | // |
973c2be1 |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
b311480e |
13 | |
7fd59977 |
14 | //#52 rln 23.12.98 |
7fd59977 |
15 | |
42cf5bc1 |
16 | #include <Interface_FloatWriter.hxx> |
7fd59977 |
17 | |
42cf5bc1 |
18 | #include <stdio.h> |
b311480e |
19 | Interface_FloatWriter::Interface_FloatWriter (const Standard_Integer chars) |
7fd59977 |
20 | { |
21 | SetDefaults(chars); |
22 | } |
23 | |
24 | // .... Controle d Envoi des Flottants .... |
25 | |
26 | void Interface_FloatWriter::SetFormat |
27 | (const Standard_CString form, const Standard_Boolean reset) |
28 | { |
29 | strcpy(themainform,form); |
30 | if (!reset) return; |
31 | therange1 = therange2 = 0.; // second form : inhibee |
32 | thezerosup = Standard_False; |
33 | } |
34 | |
35 | void Interface_FloatWriter::SetFormatForRange |
36 | (const Standard_CString form, |
37 | const Standard_Real R1, const Standard_Real R2) |
38 | { |
39 | strcpy(therangeform,form); |
40 | therange1 = R1; |
41 | therange2 = R2; |
42 | } |
43 | |
44 | void Interface_FloatWriter::SetZeroSuppress (const Standard_Boolean mode) |
45 | { thezerosup = mode; } |
46 | |
47 | void Interface_FloatWriter::SetDefaults (const Standard_Integer chars) |
48 | { |
49 | if (chars <= 0) { |
50 | strcpy(themainform ,"%E"); |
51 | strcpy(therangeform ,"%f"); |
52 | } else { |
53 | char pourcent = '%'; char point = '.'; |
91322f44 |
54 | Sprintf(themainform, "%c%d%c%dE",pourcent,chars+2,point,chars); |
55 | Sprintf(therangeform, "%c%d%c%df",pourcent,chars+2,point,chars); |
7fd59977 |
56 | } |
57 | therange1 = 0.1; therange2 = 1000.; |
58 | thezerosup = Standard_True; |
59 | } |
60 | |
61 | void Interface_FloatWriter::Options |
62 | (Standard_Boolean& zerosup, Standard_Boolean& range, |
63 | Standard_Real& R1, Standard_Real& R2) const |
64 | { |
65 | zerosup = thezerosup; |
66 | range = (therange2 >= therange1 && therange1 >= 0.); |
67 | R1 = therange1; |
68 | R2 = therange2; |
69 | } |
70 | |
71 | Standard_CString Interface_FloatWriter::MainFormat () const |
72 | { const Standard_CString mainform = Standard_CString(&themainform[0]); return mainform; } |
73 | |
74 | Standard_CString Interface_FloatWriter::FormatForRange () const |
75 | { const Standard_CString rangeform = Standard_CString(&therangeform[0]); return rangeform; } |
76 | |
77 | // ######################################################################## |
78 | |
79 | Standard_Integer Interface_FloatWriter::Write |
80 | (const Standard_Real val, const Standard_CString text) const |
81 | { |
82 | const Standard_CString mainform = Standard_CString(themainform); |
83 | const Standard_CString rangeform = Standard_CString(therangeform); |
84 | return Convert |
85 | (val,text,thezerosup,therange1,therange2,mainform,rangeform); |
86 | } |
87 | |
88 | //======================================================================= |
89 | //function : Convert |
90 | //purpose : |
91 | //======================================================================= |
92 | Standard_Integer Interface_FloatWriter::Convert (const Standard_Real val, |
93 | const Standard_CString text, |
94 | const Standard_Boolean zsup, |
95 | const Standard_Real R1, |
96 | const Standard_Real R2, |
97 | const Standard_CString mainform, |
98 | const Standard_CString rangeform) |
99 | { |
100 | // Valeur flottante, expurgee de "0000" qui trainent et de "E+00" |
a8195d65 |
101 | const Standard_Integer anMasSize = 5; // change 6 to 5: index 5 is not used below |
102 | char lxp[anMasSize], *pText; |
1d47d8d0 |
103 | int i0 = 0, j0 = 0; |
a8195d65 |
104 | |
105 | for (Standard_Integer i = 0; i < anMasSize; ++i) |
106 | lxp[i] = '\0'; |
7fd59977 |
107 | |
7fd59977 |
108 | pText=(char *)text; |
109 | // |
a8195d65 |
110 | if ( (val >= R1 && val < R2) || (val <= -R1 && val > -R2) ) |
91322f44 |
111 | Sprintf(pText,rangeform,val); |
a8195d65 |
112 | else |
91322f44 |
113 | Sprintf(pText,mainform,val); |
7fd59977 |
114 | |
a8195d65 |
115 | if (zsup) |
116 | { |
117 | for (int i = 0; i < 16; i ++) |
118 | { |
7fd59977 |
119 | i0 = i; |
a8195d65 |
120 | if (text[i] == 'e' || text[i] == 'E') |
121 | { |
122 | lxp[0] = 'E'; |
123 | lxp[1] = text[i+1]; |
124 | lxp[2] = text[i+2]; |
125 | lxp[3] = text[i+3]; |
126 | lxp[4] = text[i+4]; |
127 | |
128 | if (lxp[1] == '+' && lxp[2] == '0' && lxp[3] == '0' && lxp[4] == '\0') |
129 | lxp[0] = '\0'; |
130 | |
131 | pText[i] = '\0'; |
7fd59977 |
132 | } |
a8195d65 |
133 | if (text[i] == '\0') break; |
7fd59977 |
134 | } |
135 | //#52 rln 23.12.98 converting 1e-07 throws exception |
a8195d65 |
136 | for (int j = i0-1; j >= 0; j --) |
137 | { |
7fd59977 |
138 | j0 = j; |
a8195d65 |
139 | |
140 | if (text[j] != '0') |
141 | break; |
142 | |
7fd59977 |
143 | pText[j] = '\0'; |
144 | } |
a8195d65 |
145 | |
7fd59977 |
146 | pText[j0+1] = lxp[0]; |
147 | pText[j0+2] = lxp[1]; |
148 | pText[j0+3] = lxp[2]; |
149 | pText[j0+4] = lxp[3]; |
150 | pText[j0+5] = lxp[4]; |
151 | pText[j0+6] = '\0'; |
152 | } |
7dc9e047 |
153 | return (Standard_Integer)strlen(text); |
7fd59977 |
154 | } |