csvfileloader.h

来自「series60 应用程序开发的源代码 series60 应用程序开发的源代码」· C头文件 代码 · 共 115 行

H
115
字号
#ifndef CSVFILELOADER_H
#define CSVFILELOADER_H

// INCLUDES

// System includes
#include <e32base.h>
#include <f32file.h>

// User includes
#include "elementsmain.h"

// CONSTANTS
const TInt KMaxElementStringSize = 100;

// FORWARD DECLARATIONS
class MCsvFileLoaderObserver;
class CElementList;

// CLASS DECLARATION

/**
*
* @class    CCsvFileLoader csvfileloader.h
* @brief    Class to load in a csv (comma separated value) file containing an arbitrary number of
*            elements, one to a line, each specified thus:
*            name,symbol,atomic_number,relative_atomic_mass,type,radioactive
*            where:
*            name: the name of the element;
*            symbol: the symbol of the element (maximum of KMaxSymbolLength characters);
*            atomic_number: the atomic number (integer);
*            relative_atomic_mass: the relative atomic mass (real);
*            type: either m (for metal), s (for semi-metal), n (for non-metal)
*            radioactive: either r (for radioactive) or n (for non-radioactive)
*            e.g. a file might start:
*            Hydrogen,H,1,1.00794,n,n
*            Helium,He,2,4.0026,n,n
*            Lithium,Li,3,6.941,m,n
*            ...
*            Note that functionality has been added to deliberately slow down the operation of the
*            class to demonstrate it's asynchronous nature more clearly...
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/
class CCsvFileLoader : public CActive
{
public:
    /**
    * Destructor
    */
    virtual ~CCsvFileLoader();

    /**
    * Canonical NewL function. Constructs a new CCsvFileLoader in a leave-safe manner.
    * @param aFs Reference to a handle on the filesystem.
    * @param aElementList The element list to load the elements into. It is not assumed to be empty.
    * @param aObserver The observer to notify as each element is loaded, and finally to notify of the 
    * final completion state.
    * @param aFileName The fully qualified filename of the csv file containing the elements to load.
    * @return The newly constructed CCsvFileLoader.
    */
    static CCsvFileLoader* NewL(RFs& aFs, CElementList& aElementList, MCsvFileLoaderObserver& aObserver,
        const TDesC& aFileName);

    /**
    * Canonical NewLC function. Constructs a new CCsvFileLoader in a leave-safe manner, and leaves a pointer
    * to it on the cleanup stack
    * @param aFs Reference to a handle on the filesystem.
    * @param aElementList The element list to load the elements into. It is not assumed to be empty.
    * @param aObserver The observer to notify as each element is loaded, and finally to notify of the 
    * final completion state.
    * @param aFileName The fully qualified filename of the csv file containing the elements to load.
    * @return The newly constructed CCsvFileLoader.
    */
    static CCsvFileLoader* NewLC(RFs& aFs, CElementList& aElementList, MCsvFileLoaderObserver& aObserver,
        const TDesC& aFileName);

    /**
    * Starts the asynchronous loading of the element data. This function will return immediately.
    * As data is loaded, the observer's NotifyElementLoaded function will be called each time an element
    * is loaded with the index of the newly loaded element. Once loading has completed (either successfully
    * or otherwise, the observer's NotifyLoadCompleted callback function will be called with the completion
    * status and a reference to this loader for identification purposes (in the case of multiple or 
    * simultaneous file loads, for instance).
    */
    void Start();
    
private:
    CCsvFileLoader(RFs& aFs, CElementList& aElementList, MCsvFileLoaderObserver& aObserver);
    void ConstructL(const TDesC& aFileName);
    TPtrC8 ExtractToNewLineL(const TDesC8& aBuffer) const;
    void FillReadBufferL();
    //From CActive
    void RunL();
    TInt RunError(TInt aError);
    void DoCancel();

private:
    TFileName iFileName;
    RFs& iFs;
    RFile iFile;
    CElementList& iElementList;
    MCsvFileLoaderObserver& iObserver;
    TBuf8<KMaxElementStringSize> iReadBuffer;
    TInt iFilePos;
    TBool iWastingTime;
    TBool iHaveTriedToLoad;
    RTimer iTimeWaster;
};

#endif // #ifndef CSVFILELOADER_H

// End of File

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?