00001
00002
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00022
00029 #include <exotkUtils.hxx>
00030 #ifndef _Quantity_Color_HeaderFile
00031 #include <Quantity_Color.hxx>
00032 #endif
00033 #include <stdio.h>
00034 #ifndef _TCollection_AsciiString_HeaderFile
00035 #include <TCollection_AsciiString.hxx>
00036 #endif
00037 #ifndef _OSD_Process_HeaderFile
00038 #include <OSD_Process.hxx>
00039 #endif
00040
00041 #ifndef RGB
00042
00045 #define RGB(r, g ,b) ((unsigned long) (((unsigned char) (r) | ((unsigned short) (g) << 8)) | (((unsigned long) (unsigned char) (b)) << 16)))
00046 #endif
00047 #ifndef GetRValue
00048
00051 #define GetRValue(rgb) ((unsigned char)(rgb))
00052 #endif
00053
00054 #ifndef GetGValue
00055
00059 #define GetGValue(rgb) ((unsigned char)(((unsigned short)(rgb)) >> 8))
00060 #endif
00061
00062 #ifndef GetBValue
00063
00067 #define GetBValue(rgb) ((unsigned char)((rgb)>>16))
00068 #endif
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 Quantity_Color exotkUtils::ConvertColor(const exotkUtils_ColorRef& aColorRef)
00080 {
00081
00082 Standard_Real R,G,B;
00083 R = GetRValue(aColorRef);
00084 G = GetGValue(aColorRef);
00085 B = GetBValue(aColorRef);
00086 return Quantity_Color(R/255.,G/255.,B/255,Quantity_TOC_RGB);
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 exotkUtils_ColorRef exotkUtils::ConvertColor(const Quantity_Color& aColor)
00100 {
00101
00102 Standard_Real R,G,B;
00103 R = aColor.Red();
00104 G = aColor.Green();
00105 B = aColor.Blue();
00106 return RGB(R*255,G*255,B*255);
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 Standard_Boolean exotkUtils::AsciiStringToReal(const TCollection_AsciiString &aStringToConvert, Standard_Real &aRealResult)
00118 {
00119 Standard_Character lp[10];
00120 if (sscanf(aStringToConvert.ToCString(), "%lf%s", &aRealResult, lp) != 1)
00121 {
00122 return Standard_False;
00123 }
00124 else
00125 {
00126 return Standard_True;
00127 }
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 Standard_Boolean exotkUtils::AsciiStringToInteger(const TCollection_AsciiString& aStringToConvert,Standard_Integer& anIntegerResult)
00140 {
00141 Standard_Character lp[10];
00142 if (sscanf(aStringToConvert.ToCString(), "%d%s", &anIntegerResult, lp) != 1)
00143 {
00144 return Standard_False;
00145 }
00146 else
00147 {
00148 return Standard_True;
00149 }
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 Standard_Real exotkUtils::TruncateReal(const Standard_Real aRealToTruncate,const Standard_Integer aNegNb)
00162 {
00163 Standard_Integer theMult = (Standard_Integer)floor(pow(10,aNegNb));
00164 Standard_Real tmpReal = aRealToTruncate*theMult;
00165 Standard_Boolean isNeg = !(aRealToTruncate >= 0 );
00166 Standard_Integer tmpInt = (Standard_Integer)floor(fabs(tmpReal));
00167 Standard_Real tmpReal2 = (Standard_Real)tmpInt / (Standard_Real)theMult;
00168 if ( isNeg )
00169 return -tmpReal2;
00170 else
00171 return tmpReal2;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 TCollection_AsciiString exotkUtils::TruncateRealToAsciiString(const Standard_Real aReal,const Standard_Integer NegNb,const Standard_Integer NbChar )
00185 {
00186 char* theTmpString = new char[256];
00187
00188 switch ( NegNb )
00189 {
00190 case 0:
00191 sprintf(theTmpString,"%.0f",aReal);
00192 break;
00193 case 1:
00194 sprintf(theTmpString,"%.1f",aReal);
00195 break;
00196 case 2:
00197 sprintf(theTmpString,"%.2f",aReal);
00198 break;
00199 case 3:
00200 sprintf(theTmpString,"%.3f",aReal);
00201 break;
00202 case 4:
00203 sprintf(theTmpString,"%.4f",aReal);
00204 break;
00205 case 5:
00206 sprintf(theTmpString,"%.5f",aReal);
00207 break;
00208 case 6:
00209 sprintf(theTmpString,"%.6f",aReal);
00210 break;
00211 case 7:
00212 sprintf(theTmpString,"%.7f",aReal);
00213 break;
00214 case 8:
00215 sprintf(theTmpString,"%.8f",aReal);
00216 break;
00217 case 9:
00218 sprintf(theTmpString,"%.9f",aReal);
00219 break;
00220 default:
00221 sprintf(theTmpString,"%.3f",aReal);
00222 break;
00223 }
00224
00225
00226 TCollection_AsciiString theString = TCollection_AsciiString(theTmpString);
00227 delete theTmpString;
00228 if ( NbChar != -1 )
00229 {
00230 theString.RightJustify(NbChar,' ');
00231 }
00232 return theString;
00233 }
00234 TCollection_AsciiString exotkUtils::ColorToAsciiString(const Quantity_Color& aColor)
00235 {
00236 TCollection_AsciiString theString = TruncateRealToAsciiString(aColor.Red()) + TCollection_AsciiString("\t") + TruncateRealToAsciiString(aColor.Green()) + TCollection_AsciiString("\t") + TruncateRealToAsciiString(aColor.Blue()) + TCollection_AsciiString("\t") ;
00237 return theString;
00238 }
00239 Standard_Boolean exotkUtils::AsciiStringToColor(const TCollection_AsciiString& aString,Quantity_Color& aColor)
00240 {
00241 if ( aString.IsEmpty() )
00242 return Standard_False;
00243
00244 TCollection_AsciiString theStringRed,theStringBlue,theStringGreen;
00245 theStringRed = aString.Token("\t",1);
00246 theStringGreen = aString.Token("\t",2);
00247 theStringBlue = aString.Token("\t",3);
00248
00249 if ( theStringBlue.IsEmpty() || theStringRed.IsEmpty() || theStringGreen.IsEmpty() )
00250 return Standard_False;
00251
00252 Standard_Real R,G,B;
00253 if ( AsciiStringToReal(theStringRed,R) && AsciiStringToReal(theStringGreen,G) && AsciiStringToReal(theStringBlue,B) )
00254 {
00255 aColor = Quantity_Color(R,G,B,Quantity_TOC_RGB);
00256 return Standard_True;
00257 }
00258 else
00259 return Standard_False;
00260 }
00261
00262 TCollection_AsciiString exotkUtils::BooleanToAsciiString(const Standard_Boolean aBoolean)
00263 {
00264 if ( aBoolean )
00265 return TCollection_AsciiString("true");
00266 else
00267 return TCollection_AsciiString("false");
00268 }
00269
00270 Standard_Boolean exotkUtils::AsciiStringToBoolean(const TCollection_AsciiString &aString, Standard_Boolean &aBoolean)
00271 {
00272 TCollection_AsciiString theString = aString;
00273 theString.LowerCase();
00274 if ( (theString == "true") || (theString == "standard_true") )
00275 {
00276 aBoolean = Standard_True;
00277 return Standard_True;
00278 }
00279 else if ( (theString == "false") || (theString == "standard_false") )
00280 {
00281 aBoolean = Standard_False;
00282 return Standard_True;
00283 }
00284 else
00285 return Standard_False;
00286
00287 }
00288 #ifndef _Quantity_Date_HeaderFile
00289 #include <Quantity_Date.hxx>
00290 #endif
00291
00292
00299 TCollection_AsciiString IntegerToAsciiStringWithTwoDigits(const Standard_Integer anInteger)
00300 {
00301 TCollection_AsciiString theString(anInteger);
00302 theString.RightJustify(2,'0');
00303 return theString;
00304 };
00305 TCollection_AsciiString exotkUtils::DateToAsciiString(const enum exotkUtils_DateFormat aDateFormat,const Quantity_Date &aDate)
00306 {
00307 Standard_Integer year,month,day,hour,min,sec,millisec,microsec;
00308 aDate.Values(month,day,year,hour,min,sec,millisec,microsec);
00309 TCollection_AsciiString theStringDate;
00310 if ( aDateFormat==exotkUtils_dfFile )
00311 {
00312 theStringDate += TCollection_AsciiString(year);
00313 theStringDate += TCollection_AsciiString("-") + IntegerToAsciiStringWithTwoDigits(month);
00314 theStringDate += TCollection_AsciiString("-") + IntegerToAsciiStringWithTwoDigits(day);
00315 theStringDate += TCollection_AsciiString("-") + IntegerToAsciiStringWithTwoDigits(hour);
00316 theStringDate += TCollection_AsciiString("h") + IntegerToAsciiStringWithTwoDigits(min);
00317 theStringDate += TCollection_AsciiString("m") + IntegerToAsciiStringWithTwoDigits(sec);
00318 }
00319 else if ( aDateFormat==exotkUtils_dfText )
00320 {
00321 theStringDate += TCollection_AsciiString(year);
00322 theStringDate += TCollection_AsciiString("/") + IntegerToAsciiStringWithTwoDigits(month);
00323 theStringDate += TCollection_AsciiString("/") + IntegerToAsciiStringWithTwoDigits(day);
00324 theStringDate += TCollection_AsciiString(" ") + IntegerToAsciiStringWithTwoDigits(hour);
00325 theStringDate += TCollection_AsciiString(":") + IntegerToAsciiStringWithTwoDigits(min);
00326 theStringDate += TCollection_AsciiString(":") + IntegerToAsciiStringWithTwoDigits(sec);
00327 }
00328 return theStringDate;
00329 }
00330 TCollection_AsciiString exotkUtils::CurrentDateToAsciiString(const enum exotkUtils_DateFormat aDateFormat)
00331 {
00332 OSD_Process theProcess;
00333 return DateToAsciiString(aDateFormat,theProcess.SystemDate());
00334 }