/************************************************************************* * * $RCSfile: parser.cxx,v $ * * $Revision: 1.3 $ * * last change: $Author: nf $ $Date: 2001-05-07 14:31:30 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses * * - GNU Lesser General Public License Version 2.1 * - Sun Industry Standards Source License Version 1.1 * * Sun Microsystems Inc., October, 2000 * * GNU Lesser General Public License Version 2.1 * ============================================= * Copyright 2000 by Sun Microsystems, Inc. * 901 San Antonio Road, Palo Alto, CA 94303, USA * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 2.1, as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * * Sun Industry Standards Source License Version 1.1 * ================================================= * The contents of this file are subject to the Sun Industry Standards * Source License Version 1.1 (the "License"); You may not use this file * except in compliance with the License. You may obtain a copy of the * License at http://www.openoffice.org/license.html. * * Software provided under this License is provided on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. * See the License for the specific provisions governing your rights and * obligations concerning the Software. * * The Initial Developer of the Original Code is: Sun Microsystems, Inc. * * Copyright: 2000 by Sun Microsystems, Inc. * * All Rights Reserved. * * Contributor(s): _______________________________________ * * ************************************************************************/ #include #include #include #include "iparser.hxx" #include "geninfo.hxx" // // class InformationParser // const char InformationParser::cKeyLevelChars = '\t'; /*****************************************************************************/ InformationParser::InformationParser( BOOL bReplace ) /*****************************************************************************/ : pActStream( NULL ), nErrorCode( 0 ), nErrorLine( 0 ), nActLine( 0 ), bRecover( FALSE ), sOldLine( "" ), sErrorText( "" ), bReplaceVariables( bReplace ), nLevel( 0 ), sUPD( "" ), sVersion( "" ) { } /*****************************************************************************/ InformationParser::~InformationParser() /*****************************************************************************/ { } /*****************************************************************************/ ByteString &InformationParser::ReadLine() /*****************************************************************************/ { ByteString sLine; if ( bRecover ) { bRecover = FALSE; } else { if ( !pActStream->IsEof()) { pActStream->ReadLine( sLine ); ULONG nLen; do { nLen = sLine.Len(); sLine.EraseLeadingChars( 0x09 ); sLine.EraseLeadingChars( ' ' ); } while ( nLen != sLine.Len()); do { nLen = sLine.Len(); sLine.EraseTrailingChars( 0x09 ); sLine.EraseTrailingChars( ' ' ); } while ( nLen != sLine.Len()); if (( sLine.Search( "#" ) == 0 ) || ( !sLine.Len())) { if ( sCurrentComment.Len()) sCurrentComment += "\n"; sCurrentComment += sLine; return ReadLine(); } else { if ( bReplaceVariables ) { while( sLine.SearchAndReplace( "%UPD", sUPD ) != (USHORT)-1 ); while( sLine.SearchAndReplace( "%VERSION", sVersion ) != (USHORT)-1 ); } } } sOldLine = sLine; nActLine++; } return sOldLine; } /*****************************************************************************/ GenericInformation *InformationParser::ReadKey( GenericInformationList *pExistingList ) /*****************************************************************************/ { // this method has no error handling yet, but it works very fast. // it is used to create whole informations and sub informations in // a simple data format in memory, readed in a configuration file with // following format: /* key [value] { key [value] key [value] { key [value] ... ... } } key [value] ... ... */ GenericInformation *pInfo = NULL; ByteString sLine( ReadLine()); ByteString sKey; ByteString sValue; ByteString sComment( sCurrentComment ); sCurrentComment = ""; // key separated from value by tab? USHORT nWSPos = sLine.Search( ' ' ); if ( sLine.Search( '\t' ) < nWSPos ) { nWSPos = sLine.Search( '\t' ); sLine.SearchAndReplace( "\t", " " ); } if ( sLine.GetTokenCount( ' ' ) > 1 ) { sKey = sLine.GetToken( 0, ' ' ); sValue = sLine.Copy( sKey.Len() + 1 ); while (( sValue.Search( ' ' ) == 0 ) || ( sValue.Search( '\t' ) == 0 )) { sValue.Erase( 0, 1 ); } } else sKey=sLine; if ( bReplaceVariables && !nLevel ) { sUPD = sKey.Copy( sKey.Len() - 3 ); sVersion = sKey; } if ( ReadLine() == "{" ) { nLevel++; GenericInformationList *pSubList = new GenericInformationList(); while ( ReadLine() != "}" ) { Recover(); ReadKey( pSubList ); } nLevel--; pInfo = new GenericInformation( sKey, sValue, pExistingList, pSubList ); pInfo->SetComment( sComment ); } else { Recover(); pInfo = new GenericInformation( sKey, sValue, pExistingList ); pInfo->SetComment( sComment ); } return pInfo; } /*****************************************************************************/ void InformationParser::Recover() /*****************************************************************************/ { bRecover = TRUE; } /*****************************************************************************/ BOOL InformationParser::Save( SvStream &rOutStream, const GenericInformationList *pSaveList, USHORT nLevel ) /*****************************************************************************/ { USHORT i; ULONG nInfoListCount; ByteString sTmpStr; GenericInformation *pGenericInfo; GenericInformationList *pGenericInfoList; for ( nInfoListCount = 0; nInfoListCount < pSaveList->Count(); nInfoListCount++) { // Key-Value Paare schreiben pGenericInfo = pSaveList->GetObject( nInfoListCount ); sTmpStr = ""; for( ULONG j=0; jGetComment().GetTokenCount( '\n' ); i++ ) { sTmpStr += pGenericInfo->GetComment().GetToken( i, '\n' ); sTmpStr += "\n"; for( ULONG j=0; jGetBuffer(); sTmpStr += ' '; sTmpStr += pGenericInfo->GetValue(); if ( !rOutStream.WriteLine( sTmpStr ) ) return FALSE; // wenn vorhanden, bearbeite recursive die Sublisten if (( pGenericInfoList = pGenericInfo->GetSubList() ) != 0) { // oeffnende Klammer sTmpStr = ""; for( i=0; iGetFileName(); sErrorText = ByteString( sStreamName, gsl_getSystemTextEncoding()); sErrorText += ByteString( " (" ); sErrorText += ByteString( nErrorLine ); sErrorText += ByteString( "): " ); switch ( nErrorCode ) { case IP_NO_ERROR: sErrorText += ByteString( "Keine Fehler aufgetereten" ); break; case IP_UNEXPECTED_EOF: sErrorText += ByteString( "Ungültiges Dateiende!" ); break; } return sErrorText; }