epoc32/include/testconfigfileparser.h
branchSymbian3
changeset 4 837f303aceeb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/testconfigfileparser.h	Wed Mar 31 12:33:34 2010 +0100
     1.3 @@ -0,0 +1,244 @@
     1.4 +/**
     1.5 + * @file testscripts.h Defines classes for reading a configuration file
     1.6 + *
     1.7 + * @note Configuration File Format:
     1.8 + * 
     1.9 + * [Defaults]
    1.10 + * defaults= another_config_file.txt
    1.11 + *
    1.12 + * [SectionName]
    1.13 + * variable= value
    1.14 + * variable2= value2
    1.15 + * variable= value3
    1.16 + *
    1.17 + * [SectionName2]
    1.18 + * variable= value
    1.19 + *
    1.20 + * endscript
    1.21 + *
    1.22 + * 
    1.23 + * @note Explanation:
    1.24 + *
    1.25 + * A configuration file is made up of a number of "sections", each of which can contain a number of "items" (name, value combination).
    1.26 + * 
    1.27 + * "Sections" must have a name and be surrounded by square backets, e.g.:
    1.28 + *
    1.29 + *      [SectionName]
    1.30 + *
    1.31 + *
    1.32 + * Each "item" consists of consists of a name, followed by an equals sign, followed by blank space, followed by the value to assign to that variable.
    1.33 + * 
    1.34 + * The value can be of any length, contain whitespace and span multiple lines. The value ends when the next item or section is found. E.g:
    1.35 + * 
    1.36 + * Simple Item:
    1.37 + *
    1.38 + *      variable= value
    1.39 + *
    1.40 + * Two items on one line:
    1.41 + *
    1.42 + *      variable= value variable2= value2
    1.43 + *
    1.44 + * Multi-line item:
    1.45 + *
    1.46 + *      variable= This variable
    1.47 + *      spans multiple
    1.48 + *      lines
    1.49 + *
    1.50 + * 
    1.51 + * @note Parsing stops at End-Of-File or if the tag "endscript" (without the quotes) appears in the file.
    1.52 + *
    1.53 + * @note A section may take some default values from another section or config file
    1.54 + *
    1.55 + * To specify default values for all sections, add a section at the start of the config file called [Defaults], e.g.:
    1.56 + *
    1.57 + *      [Defaults]
    1.58 + *      sc= +447785016005
    1.59 + *
    1.60 + * To read default values from another config file, add an item with name "defaults" and value is the name of the file. E.g.:
    1.61 + *
    1.62 + *      defaults= another_config_file.txt
    1.63 +
    1.64 + */
    1.65 +
    1.66 +
    1.67 +#ifndef __TEST_CONFIG_FILE_PARSER_H__
    1.68 +#define __TEST_CONFIG_FILE_PARSER_H__
    1.69 +
    1.70 +#include <e32std.h>
    1.71 +#include <e32base.h>
    1.72 +
    1.73 +class CTestConfigSection;
    1.74 +class CTestConfigItem;
    1.75 +class RFs;
    1.76 +class TParse;
    1.77 +class RFile;
    1.78 +
    1.79 +_LIT(KScriptPanic, "TEST-SCRIPT");
    1.80 +_LIT(KScriptPathSep,"\\");
    1.81 +_LIT8(KScriptSectionStart, "[");
    1.82 +_LIT8(KScriptSectionEnd, "]");
    1.83 +_LIT8(KScriptCRLF, "\r\n");
    1.84 +_LIT8(KScriptCRLF8, "\r\n");
    1.85 +_LIT8(KScriptLF, "\n");
    1.86 +_LIT8(KScriptCR, "\r");
    1.87 +_LIT8(KScriptItemEnd, "=");
    1.88 +_LIT8(KScriptItemEnd8, "=");
    1.89 +_LIT8(KScriptSpace8, " ");
    1.90 +_LIT8(KScriptDefaults, "Defaults");
    1.91 +_LIT8(KScriptDefault1, "Def");
    1.92 +_LIT8(KScriptDefault2, "Default");
    1.93 +_LIT8(KScriptCommentStart, "#");
    1.94 +const TInt KScriptLFChar = '\n';
    1.95 +const TInt KScriptCRChar = '\r';
    1.96 +
    1.97 +class CTestConfig : public CBase
    1.98 +/**
    1.99 + * @internalComponent
   1.100 + * @deprecated
   1.101 + */
   1.102 +	{
   1.103 +	public:
   1.104 +		IMPORT_C static CTestConfig* NewLC(RFs& aFs, const TDesC& aComponent, const TDesC& aScriptFile);
   1.105 +		IMPORT_C static CTestConfig* NewLC(RFs& aFs, const TDesC& aComponent);
   1.106 +		IMPORT_C ~CTestConfig();
   1.107 +
   1.108 +		IMPORT_C const TDesC8& ItemValue(const TDesC8& aSection, const TDesC8& aItem, const TDesC8& aDefault) const;
   1.109 +		IMPORT_C TInt ItemValue(const TDesC8& aSection, const TDesC8& aItem, const TInt aDefault) const;
   1.110 +		
   1.111 +		IMPORT_C void ReadScriptL(const TDesC& aScript);
   1.112 +
   1.113 +		inline const RPointerArray<CTestConfigSection>& Sections() const;
   1.114 +		inline RPointerArray<CTestConfigSection>& Sections();
   1.115 +
   1.116 +		IMPORT_C const CTestConfigSection* Section(const TDesC8& aSectionName) const; //return NULL if section not found
   1.117 +		IMPORT_C CTestConfigSection* Section(const TDesC8& aSectionName); //return NULL if section not found
   1.118 +		inline const CTestConfigSection& operator[](TInt aIndex) const {return *iSections[aIndex];}
   1.119 +		inline CTestConfigSection& operator[](TInt aIndex) {return *iSections[aIndex];}
   1.120 +
   1.121 +		IMPORT_C static TInt CountElements(const TDesC8& aInput, TChar aDelimiter);
   1.122 +		IMPORT_C static TInt GetElement(const TDesC8& aInput, TChar aDelimiter, TInt aIndex, TInt& aOutput);
   1.123 +		IMPORT_C static TInt GetElement(const TDesC8& aInput, TChar aDelimiter, TInt aIndex, TPtrC8& aOutput, TBool aTrimOutput = ETrue);
   1.124 +		IMPORT_C static TPtrC8 Trim(const TDesC8& aInput);
   1.125 +		IMPORT_C static TPtrC8 TrimLeft(const TDesC8& aInput);
   1.126 +		IMPORT_C static TPtrC8 TrimRight(const TDesC8& aInput);
   1.127 +
   1.128 +		IMPORT_C static HBufC8* ReplaceLC(const TDesC8& aOld, const TDesC8& aNew, const TDesC8& aOldString);
   1.129 +		IMPORT_C static TInt ResolveFile(RFs& aFs, const TDesC& aComponent, const TDesC& aFileName, TParse& aParseOut);
   1.130 +
   1.131 +		IMPORT_C void WriteFileL(const TDesC& aFileName);
   1.132 +		IMPORT_C TBool operator==(const CTestConfig& aFile) const;
   1.133 +
   1.134 +		IMPORT_C void AddSectionL(CTestConfigSection& aSection);
   1.135 +
   1.136 +	protected:
   1.137 +
   1.138 +		CTestConfig(RFs& aFs);
   1.139 +		void ConstructL(const TDesC& aComponent);
   1.140 +
   1.141 +		TPtrC8 ParseValue(const TDesC8& aText, const TLex8& aInput, TInt aCurrentItemStart) const;
   1.142 +		void ParseAndSetItemValueL(const TDesC8& aText, const TLex8& aInput, TInt aCurrentItemStart, CTestConfigItem*& arCurrentItem);
   1.143 +		void CopyInDefaultsL(CTestConfigSection& aSection, const TDesC& aDefaultFile);
   1.144 +
   1.145 +		HBufC8* ReadFileL(const TDesC& aFile) const;
   1.146 +
   1.147 +		TBool IsDefaultSection(const TDesC8& aSectionName) const;
   1.148 +		static TInt GetNextElement(TLex8& aInput, TChar aDelimiter, TPtrC8& aOutput);
   1.149 +		TBool IsNewSection(const TDesC8& aSource, const TLex8& aInput) const;
   1.150 +		TBool IsNewItem(const TDesC8& aSource, const TLex8& aLex, TPtrC8& aItem, TInt& aStartOfValue) const;
   1.151 +		TBool IsNewComment(const TDesC8& aSource, const TLex8& aLex) const;
   1.152 +		TBool IsAtStartOfNewLine(const TDesC8& aSource, const TLex8& aLex, TBool aIgnoreSpaces) const;
   1.153 +		void SkipToNextLine(TLex8& aInput) const;
   1.154 +
   1.155 +
   1.156 +	protected:
   1.157 +
   1.158 +		RFs& iFs;
   1.159 +		HBufC* iComponent;
   1.160 +		RPointerArray<CTestConfigSection> iSections;
   1.161 +	};
   1.162 +
   1.163 +class CTestConfigSection : public CBase
   1.164 +/**
   1.165 + * @internalComponent
   1.166 + * @deprecated
   1.167 + */
   1.168 +	{
   1.169 +	friend class CTestConfig;
   1.170 +
   1.171 +	public:
   1.172 +		IMPORT_C static CTestConfigSection* NewLC(const TDesC8& aSectionName);
   1.173 +		IMPORT_C static CTestConfigSection* NewLC(const TDesC8& aSectionName, CTestConfigSection& aDefaults);
   1.174 +		IMPORT_C ~CTestConfigSection();
   1.175 +		
   1.176 +		inline const TDesC8& SectionName() const;
   1.177 +
   1.178 +		IMPORT_C const CTestConfigItem* Item(const TDesC8& aItemTag) const; //return NULL if the item does not exist
   1.179 +		IMPORT_C CTestConfigItem* Item(const TDesC8& aItemTag); //return NULL if the item does not exist
   1.180 +		IMPORT_C const CTestConfigItem* Item(const TDesC8& aItemTag,TInt aIndex) const; //return NULL if the item does not exist
   1.181 +		IMPORT_C CTestConfigItem* Item(const TDesC8& aItemTag,TInt aIndex); //return NULL if the item does not exist
   1.182 +
   1.183 +		IMPORT_C const TDesC8& ItemValue(const TDesC8& aItemTag, const TDesC8& aDefault) const;
   1.184 +		IMPORT_C TInt ItemValue(const TDesC8& aItemTag, TInt aDefault) const;
   1.185 +
   1.186 +		IMPORT_C CTestConfigItem& AddItemL(const TDesC8& aItemTag, const TDesC8& aValue);
   1.187 +		IMPORT_C void DeleteItemsL(const TDesC8& aItem);
   1.188 +
   1.189 +		inline const RPointerArray<CTestConfigItem>& Items() const {return iItems;}
   1.190 +		inline RPointerArray<CTestConfigItem>& Items() {return iItems;}
   1.191 +
   1.192 +		IMPORT_C TInt ItemCount(const TDesC8& aItemTag) const;
   1.193 +		IMPORT_C void ItemsL(RPointerArray<CTestConfigItem>& aArray, const TDesC8& aItemTag);
   1.194 +		IMPORT_C void ItemsL(RPointerArray<const CTestConfigItem>& aArray, const TDesC8& aItemTag) const;
   1.195 +
   1.196 +		inline const CTestConfigItem& operator[](TInt aIndex) const  {return *iItems[aIndex];}
   1.197 +
   1.198 +		inline void SetDefaultsL(const CTestConfigSection& aDefaults);
   1.199 +		inline CTestConfigSection* Defaults() const {return iDefaults;}
   1.200 +
   1.201 +		IMPORT_C CTestConfigSection* CopyLC() const;
   1.202 +
   1.203 +		void WriteL(RFile& aFile) const;
   1.204 +		TBool operator==(const CTestConfigSection& aFile) const;
   1.205 +
   1.206 +	private:
   1.207 +		void ConstructL(const TDesC8& aSectionName);
   1.208 +		CTestConfigSection();
   1.209 +		RPointerArray<CTestConfigItem> iItems;
   1.210 +		HBufC8* iSectionName;
   1.211 +		CTestConfigSection* iDefaults;
   1.212 +	};
   1.213 +
   1.214 +class CTestConfigItem : public CBase
   1.215 +/**
   1.216 + * @internalComponent
   1.217 + * @deprecated
   1.218 + */
   1.219 +	{
   1.220 +	friend class CTestConfigSection;
   1.221 +	friend class CTestConfig;
   1.222 +
   1.223 +	public:
   1.224 +		IMPORT_C static CTestConfigItem* NewLC(CTestConfigSection& aParent, const TDesC8& aItem, const TDesC8& aValue);
   1.225 +		inline CTestConfigItem* CopyLC() const;
   1.226 +
   1.227 +		IMPORT_C ~CTestConfigItem();
   1.228 +		inline const TDesC8& Item() const;
   1.229 +		inline const TDesC8& Value() const;
   1.230 +
   1.231 +		void WriteL(RFile& aFile) const;
   1.232 +		TBool operator==(const CTestConfigItem& aItem) const {return Item() == aItem.Item() && Value() == aItem.Value();}
   1.233 +
   1.234 +	public:
   1.235 +
   1.236 +		CTestConfigSection& iParent;
   1.237 +		
   1.238 +	private:
   1.239 +		CTestConfigItem(CTestConfigSection& aParent);
   1.240 +		void ConstructL(const TDesC8& aItem, const TDesC8& aValue);
   1.241 +		HBufC8* iItem;
   1.242 +		HBufC8* iValue;
   1.243 +	};
   1.244 +
   1.245 +#include "testconfigfileparser.inl"
   1.246 +
   1.247 +#endif