Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

exotkUtils.cxx

Go to the documentation of this file.
00001 
00002 //   exotkUtils.cxx
00004 //    Copyright (C) 2001  Stephane Routelous
00005 //
00006 //    This file is part of exoTK.
00007 //
00008 //    exoTK is free software; you can redistribute it and/or modify
00009 //    it under the terms of the GNU General Public License as published by
00010 //    the Free Software Foundation; either version 2 of the License, or
00011 //    (at your option) any later version.
00012 //
00013 //    exoTK is distributed in the hope that it will be useful,
00014 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 //    GNU General Public License for more details.
00017 //
00018 //    You should have received a copy of the GNU General Public License
00019 //    along with exoTK; if not, write to the Free Software
00020 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 // Function name        : exotkUtils::ConvertColor
00073 //==================================================================================
00074 // Written by       : Stephane Routelous - 2001-11-08 19:01:46
00075 // Description      : 
00076 // Return type          : Quantity_Color 
00077 //==================================================================================
00078 // Argument         : const exotkUtils_ColorRef& aColorRef
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 // Function name        : exotkUtils::ConvertColor
00093 //==================================================================================
00094 // Written by       : Stephane Routelous - 2001-11-08 19:01:48
00095 // Description      : 
00096 // Return type          : exotkUtils_ColorRef 
00097 //==================================================================================
00098 // Argument         : const Quantity_Color& aColor
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 // Function name        : exotkUtils::AsciiStringToReal
00110 //==================================================================================
00111 // Written by       : Stephane Routelous - 16/04/2001 22:44:36
00112 // Description      : 
00113 // Return type          : Standard_Boolean 
00114 //==================================================================================
00115 // Argument         : const TCollection_AsciiString &aStringToConvert
00116 // Argument         : Standard_Real &aRealResult
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 // Function name        : exotkUtils::AsciiStringToInteger
00132 //==================================================================================
00133 // Written by       : Stephane Routelous - 16/04/2001 22:44:37
00134 // Description      : 
00135 // Return type          : Standard_Boolean 
00136 //==================================================================================
00137 // Argument         : const TCollection_AsciiString& aStringToConvert
00138 // Argument         : Standard_Integer& anIntegerResult
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 // Function name        : exotkUtils::TruncateReal
00154 //==================================================================================
00155 // Written by       : Stephane Routelous - 2001-11-29 15:43:57
00156 // Description      : 
00157 // Return type          : Standard_Real 
00158 //==================================================================================
00159 // Argument         : const Standard_Real aRealToTruncate
00160 // Argument         : const Standard_Integer aNegNb/* = 3*/
00161 Standard_Real exotkUtils::TruncateReal(const Standard_Real aRealToTruncate,const Standard_Integer aNegNb/* = 3*/)
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 // Function name        : exotkUtils::TruncateRealToAsciiString
00176 //==================================================================================
00177 // Written by       : Stephane Routelous - 2001-11-29 15:56:27
00178 // Description      : 
00179 // Return type          : TCollection_AsciiString 
00180 //==================================================================================
00181 // Argument         : const Standard_Real aReal
00182 // Argument         : const Standard_Integer NegNb/* = 3*/
00183 // Argument         : const Standard_Integer NbChar /*= -1*/
00184 TCollection_AsciiString exotkUtils::TruncateRealToAsciiString(const Standard_Real aReal,const Standard_Integer NegNb/* = 3*/,const Standard_Integer NbChar /*= -1*/)
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 }

Generated on Wed Jan 23 12:16:45 2002 for exotk by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001