File: UTILS\U_FILES.CPP

    1 //#############################################################################
    2 //                                                                           //
    3 // U_FILES.CPP                                                               //
    4 // Subsystem: Utilities - Dateizugriff                                       //
    5 // Deklaration: U_FILES.H                                                    //
    6 //---------------------------------------------------------------------------//
    7 // Autoren: Thomas Kullmann, Günther Reinecker                               //
    8 // Stand:   03.08.2002                                                       //
    9 //                                                                           //
   10 //#############################################################################
   11 
   12 #include <stdlib.h> // für: min(), max()
   13 
   14 #include "utils\u_files.h" // SCHNITTSTELLE für diese Datei
   15 #include "utils\u_values.h" // IsDouble(), Double2String()
   16 
   17 //--||--\\--||--//--||--\\--||--//--||--\\--||--//--||--\\--||--//--||--\\--||--
   18 
   19 //#############################################################################
   20 // globale Methoden (Lesen/ Schreiben von String-, Double- und Long-Werten aus/ in ini-Dateien)
   21 //#############################################################################
   22 
   23 void _CURVECLASS WINAPI IniReadString ( char *aReturn,
   24                 LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
   25                 LPCSTR aDef, const UINT aLength )
   26 {
   27         GetPrivateProfileString( aSection, aKey, aDef,
   28                                                          aReturn, aLength, aFile );
   29 }
   30 //-----------------------------------------------------------------------------
   31 
   32 BOOL _CURVECLASS WINAPI IniWriteString ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
   33                 LPCSTR aString )
   34 {
   35         return WritePrivateProfileString( aSection, aKey, aString, aFile );
   36 }
   37 //*****************************************************************************
   38 
   39 BOOL _CURVECLASS WINAPI IniReadBool ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
   40                 const BOOL aDef )
   41 {
   42         return IniReadLong( aFile, aSection, aKey,
   43                                                 0, (LONG)aDef, 1 );
   44 }
   45 //-----------------------------------------------------------------------------
   46 
   47 BOOL _CURVECLASS WINAPI IniWriteBool ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
   48                 const BOOL aBool )
   49 {
   50         return IniWriteLong( aFile, aSection, aKey,
   51                                                  (LONG)aBool );
   52 }
   53 //*****************************************************************************
   54 
   55 LONG _CURVECLASS WINAPI IniReadLong ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
   56                 const LONG aDef )
   57 { //WIE IniReadDouble, nur ohne Nachkommastellen
   58         return (LONG)IniReadDouble( aFile, aSection, aKey,
   59                                                                 (LONG)aDef, 0);
   60 }
   61 //-----------------------------------------------------------------------------
   62 
   63 LONG _CURVECLASS WINAPI IniReadLong ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
   64                 const LONG aMin, const LONG aDef, const LONG aMax )
   65 { //WIEVOR, jedoch mit Intervallgrenzen
   66         LONG result= IniReadLong( aFile, aSection, aKey,
   67                                                            aDef );
   68         //gelesenen Wert ins Intervall [<aMin>..<aMax>] einpassen
   69         return max( aMin, min( aMax, result ) );
   70 }
   71 //-----------------------------------------------------------------------------
   72 
   73 BOOL _CURVECLASS WINAPI IniWriteLong( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
   74                 const LONG aLong )
   75 {
   76         return IniWriteDouble( aFile, aSection, aKey,
   77                                                    (LONG)aLong, 0 );
   78 }
   79 //*****************************************************************************
   80 
   81 float _CURVECLASS WINAPI IniReadFloat ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
   82                 const float aDef, const UINT aDigits )
   83 {
   84         return (float)IniReadDouble( aFile, aSection, aKey,
   85                                                       Float2Double(aDef, aDigits), aDigits );
   86 }
   87 //-----------------------------------------------------------------------------
   88 
   89 float _CURVECLASS WINAPI IniReadFloat ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
   90                 const float aMin, const float aDef, const float aMax, const UINT aDigits )
   91 { //WIEVOR, jedoch mit Intervallgrenzen
   92         float result= IniReadFloat( aFile, aSection, aKey,
   93                                                                  aDef, aDigits );
   94         //gelesenen Wert ins Intervall [<aMin>..<aMax>] einpassen
   95         return max( aMin, min( aMax, result ) );
   96 }
   97 //-----------------------------------------------------------------------------
   98 
   99 BOOL _CURVECLASS WINAPI IniWriteFloat ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
  100                 const float aFloat, const UINT aDigits )
  101 {
  102         return IniWriteDouble( aFile, aSection, aKey,
  103                                                    Float2Double(aFloat, aDigits), aDigits );
  104 }
  105 
  106 //*****************************************************************************
  107 
  108 double _CURVECLASS WINAPI IniReadDouble ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
  109                 const double aDef, const UINT aDigits )
  110 {
  111         //String aus ini-Datei lesen
  112         char value[51]; //max. 50 Zeichen + Nullterminierung
  113         IniReadString( value, aFile, aSection, aKey, "", 50 );
  114         if ( !IsDouble( value ) )
  115                 return aDef; //Eintrag ist nicht vorhanden oder kein Double
  116 
  117         return String2Double( value, aDigits, FALSE, '.' );
  118 }
  119 //-----------------------------------------------------------------------------
  120 
  121 double _CURVECLASS WINAPI IniReadDouble ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
  122                 const double aMin, const double aDef, const double aMax, const UINT aDigits )
  123 { //WIEVOR, jedoch mit Intervallgrenzen
  124         double result= IniReadDouble( aFile, aSection, aKey,
  125                                                                    aDef, aDigits );
  126         //gelesenen Wert ins Intervall [<aMin>..<aMax>] einpassen
  127         return max( aMin, min( aMax, result ) );
  128 }
  129 //-----------------------------------------------------------------------------
  130 
  131 BOOL _CURVECLASS WINAPI IniWriteDouble ( LPCSTR aFile, LPCSTR aSection, LPCSTR aKey,
  132                 const double aDouble, const UINT aDigits )
  133 {
  134         char value[51]; //max. 50 Zeichen + Nullterminierung
  135         Double2String( value, aDouble, aDigits, FALSE, '.' ); //Double nach String konvertieren
  136         return IniWriteString( aFile, aSection, aKey, value ); //String in ini-Datei speichern
  137 }
  138 
  139 //#############################################################################
  140 // TTxtRead
  141 //#############################################################################
  142 
  143 TTxtRead::TTxtRead ( const char *aFilename ) : m_File(0), m_Line(0)
  144 {
  145         m_File= fopen( aFilename, "r" );
  146         m_Exists= (m_File != 0);
  147         m_Eof= ( (m_File == 0) || (feof(m_File)) ); //nicht vorhanden oder leer
  148 
  149         //Anzahl der Zeilen ermitteln und zurück zum Dateianfang
  150         m_Lines= 0;
  151         if ( (!m_Exists) || (m_Eof) )
  152                 return;
  153 
  154         do
  155         {
  156                 char c= fgetc( m_File );
  157                 if ( (feof(m_File)) || (c == '\n') || (c == '\r') )
  158                         m_Lines++; //eof || new line || carrige return
  159         } while ( !feof( m_File) );
  160         Restart();
  161 }
  162 //-----------------------------------------------------------------------------
  163 
  164 TTxtRead::~TTxtRead ()
  165 {
  166         _FREELIST(m_Line);
  167         if ( m_File )
  168         {
  169                 fclose( m_File );
  170                 m_File= 0;
  171         }
  172 }
  173 //-----------------------------------------------------------------------------
  174 
  175 void TTxtRead::Restart() {
  176         _FREELIST(m_Line);
  177         if ( m_File ) fseek(m_File, 0, SEEK_SET);
  178 }
  179 //-----------------------------------------------------------------------------
  180 
  181 BOOL TTxtRead::NextLine (string &result)
  182 {
  183         result= "";
  184         _FREELIST(m_Line);
  185         if ( m_Eof )
  186                 return FALSE;
  187 
  188         //Zeile einlesen
  189         BOOL eol= FALSE;
  190         do
  191         {
  192                 char c= fgetc(m_File);
  193                 if ( !feof(m_File) )
  194                 {
  195                         if ( (c != '\n') && (c != '\r') )
  196                         {
  197                                 string temp= "1"; // genau ein Zeichen !!!
  198                                 temp[0]= c;
  199                                 result += temp; // eigentlich reicht result+= c;
  200                         }
  201                         else
  202                                 eol= TRUE; //new line || carrige return
  203                 }
  204                 else
  205                         m_Eof= TRUE;
  206         } while ( (!m_Eof) && (!eol) );
  207         clearerr(m_File);
  208         return TRUE;
  209 }
  210 //-----------------------------------------------------------------------------
  211 
  212 char *TTxtRead::NextLine ()
  213 {
  214         string temp;
  215         if ( !NextLine(temp) )
  216                 return 0; // Fehler oder Dateiende
  217 
  218         // in char* konvertieren, temp wird freigegeben
  219         m_Line= new char[ temp.length() + 1 ]; // + Nullterminierung
  220 
  221         string::size_type i;
  222 
  223         for ( i= 0; i < temp.length(); i++)
  224                 m_Line[i]= temp[i];
  225         m_Line[i]= '\0'; // Nullterminierung nicht vergessen
  226         return m_Line;
  227 }
  228 //-----------------------------------------------------------------------------
  229 
  230 BOOL TTxtRead::IsEOF ()
  231 {
  232         return m_Eof;
  233 }
  234 //-----------------------------------------------------------------------------
  235 
  236 BOOL TTxtRead::Exists ()
  237 {
  238         return m_Exists;
  239 }
  240 //-----------------------------------------------------------------------------
  241 
  242 UINT TTxtRead::GetLines ()
  243 {
  244         return m_Lines;
  245 }
  246 
  247 //#############################################################################
  248 // LASTLINE
  249 //#############################################################################
  250