zeroconf/zeroconfsharing/cfileparser.cpp
changeset 21 ff5174af067c
equal deleted inserted replaced
14:da856f45b798 21:ff5174af067c
       
     1 /*
       
     2 * Copyright (c) 2001-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cfileparser.h"
       
    19 
       
    20 #include <S32FILE.H>
       
    21 #include <VERSIT.H> 
       
    22 
       
    23 
       
    24 CFileParser::CFileParser()
       
    25     {
       
    26    
       
    27     }
       
    28 
       
    29 CFileParser::~CFileParser()
       
    30     {
       
    31     iFileServ.Close();    
       
    32     
       
    33     }
       
    34 
       
    35 CFileParser* CFileParser::NewLC()
       
    36     {
       
    37     CFileParser* self = new (ELeave) CFileParser();
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL();
       
    40     return self;
       
    41     }
       
    42 
       
    43 CFileParser* CFileParser::NewL()
       
    44     {
       
    45     CFileParser* self = CFileParser::NewLC();
       
    46     CleanupStack::Pop(); // self;
       
    47     return self;
       
    48     }
       
    49 
       
    50 void CFileParser::ConstructL()
       
    51     {
       
    52     iFileServ.Connect();
       
    53     iStart = false;
       
    54     SetMediaType(CFileParser::ENone);   //no  specific folder
       
    55     
       
    56     }
       
    57 
       
    58 void CFileParser::SetMediaType(TMediaType aType)
       
    59     {
       
    60     iCurMediaType = aType;
       
    61     }
       
    62     
       
    63 CFileParser::TMediaType CFileParser::GetMediaType()
       
    64     {
       
    65     return iCurMediaType;
       
    66     }
       
    67 
       
    68 /*
       
    69  * aFileName - is the file to be parsed
       
    70  * this contain the text in the form
       
    71  * [photos]
       
    72  * --
       
    73  * --
       
    74  * [/photos]
       
    75  * [music]
       
    76  * --
       
    77  * --[/music]
       
    78  * [videos]
       
    79  * ---
       
    80  * --
       
    81  * [/videos]
       
    82  * ParseFile function takes such a file & parses the sections
       
    83  * and writes the photo filename to aPhotoFile,
       
    84  * music file names to aMusicFile &
       
    85  * video file names to aVideoFile
       
    86  * 
       
    87  */
       
    88 
       
    89 
       
    90 void CFileParser::ParseFile( const TDesC& aFileName,const TDesC& aPhotoFile,const TDesC& aMusicFile,const TDesC& aVideoFile )
       
    91     {
       
    92     TFileText lineReader;
       
    93     TFileName name;
       
    94     RBuf8 fileName;
       
    95     RFile photoFile, musicFile, videoFile;
       
    96     
       
    97     //declare the start & end tags
       
    98     _LIT(KPhotoStart,"[photos]");
       
    99     _LIT(KMusicStart,"[music]");
       
   100     _LIT(KVideoStart,"[videos]");
       
   101     _LIT(KPhotoEnd,"[/photos]");
       
   102     _LIT(KMusicEnd,"[/music]");
       
   103     _LIT(KVideoEnd,"[/videos]");
       
   104     
       
   105     _LIT(KStartTag,"[%s]");
       
   106     _LIT(KEndTag,"[/%s]");
       
   107     
       
   108     TBuf<KMaxFileName> buf;
       
   109     name.Copy(aFileName);
       
   110     RFileReadStream readStream;
       
   111     readStream.PushL();
       
   112 
       
   113     User::LeaveIfError(readStream.Open(iFileServ, name, EFileRead));
       
   114     CLineReader* myReader = CLineReader::NewL(readStream);
       
   115     TInt aPos = 0;
       
   116     TInt aErr = 0;
       
   117     
       
   118     photoFile.Replace(iFileServ,aPhotoFile,EFileWrite);
       
   119     musicFile.Replace(iFileServ,aMusicFile,EFileWrite);
       
   120     videoFile.Replace(iFileServ,aVideoFile,EFileWrite);
       
   121     
       
   122     while(aErr!=KErrEof)
       
   123        {
       
   124           myReader->ReadLineL(aPos, aErr);
       
   125         // Do your parsing here ..          
       
   126           fileName.CreateL(KMaxFileName);
       
   127           buf.Copy(myReader->iBufPtr);          
       
   128           //compare buf with the start & end tags
       
   129           //if it is the start or end tag, dont write to the file          
       
   130           if( buf.Compare(KPhotoStart)== 0){  //photo tag start
       
   131               SetMediaType(CFileParser::EPhotos); 
       
   132               iStart = true;
       
   133           }
       
   134           else if( buf.Compare(KPhotoEnd)== 0){        //photo tag end
       
   135               SetMediaType(CFileParser::ENone);  
       
   136           }
       
   137           else if( buf.Compare(KMusicStart)== 0 ){       //music tag start
       
   138               SetMediaType(CFileParser::EMusic);
       
   139               iStart = true;
       
   140           }
       
   141           else if( buf.Compare(KMusicEnd)== 0){
       
   142               SetMediaType(CFileParser::ENone);       //music tag end
       
   143           }
       
   144           else if( buf.Compare(KVideoStart)== 0 ){
       
   145               SetMediaType(CFileParser::EVideos);  
       
   146               iStart = true;//video tag start
       
   147           }
       
   148           else if( buf.Compare(KVideoEnd)== 0 ){       
       
   149               SetMediaType(CFileParser::ENone);       //video tag end
       
   150           }
       
   151           else 
       
   152               {
       
   153                   fileName.Copy(buf);
       
   154                   switch( GetMediaType() )
       
   155                     {
       
   156                       case EPhotos:
       
   157                           {
       
   158                           if(!iStart){
       
   159                               photoFile.Write(_L8("\n"));
       
   160                           }
       
   161                           else {
       
   162                               iStart = false;
       
   163                           }
       
   164                           photoFile.Write(fileName);
       
   165                           }            
       
   166                               break;
       
   167                       case EMusic:
       
   168                           {
       
   169                           if(!iStart){
       
   170                               musicFile.Write(_L8("\n"));
       
   171                           }
       
   172                           else {
       
   173                               iStart = false;
       
   174                           }
       
   175                           musicFile.Write(fileName);
       
   176                           }
       
   177                               break;
       
   178                       case EVideos:
       
   179                           {
       
   180                           if(!iStart){
       
   181                               videoFile.Write(_L8("\n"));
       
   182                           }
       
   183                           else {
       
   184                                iStart = false;
       
   185                            }
       
   186                           videoFile.Write(fileName);
       
   187                           }
       
   188                               break;
       
   189                       default:
       
   190                               break;
       
   191                     }
       
   192               }
       
   193           
       
   194        }
       
   195     //close all file handles
       
   196     photoFile.Close();
       
   197     musicFile.Close();
       
   198     videoFile.Close();
       
   199     //close the filename rbuf
       
   200     fileName.Close();
       
   201     CleanupStack::PopAndDestroy();    
       
   202     delete myReader;
       
   203     }