telephonyserverplugins/simtsy/testconfigfileparser/inc/testconfigfileparser.h
changeset 68 d0d9cc530d21
equal deleted inserted replaced
62:8ef833fbf5aa 68:d0d9cc530d21
       
     1 /*
       
     2 * Copyright (c) 2009 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  * @file testscripts.h Defines classes for reading a configuration file
       
    19  *
       
    20  * @note Configuration File Format:
       
    21  * 
       
    22  * [Defaults]
       
    23  * defaults= another_config_file.txt
       
    24  *
       
    25  * [SectionName]
       
    26  * variable= value
       
    27  * variable2= value2
       
    28  * variable= value3
       
    29  *
       
    30  * [SectionName2]
       
    31  * variable= value
       
    32  *
       
    33  * endscript
       
    34  *
       
    35  * 
       
    36  * @note Explanation:
       
    37  *
       
    38  * A configuration file is made up of a number of "sections", each of which can contain a number of "items" (name, value combination).
       
    39  * 
       
    40  * "Sections" must have a name and be surrounded by square backets, e.g.:
       
    41  *
       
    42  *      [SectionName]
       
    43  *
       
    44  *
       
    45  * 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.
       
    46  * 
       
    47  * 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:
       
    48  * 
       
    49  * Simple Item:
       
    50  *
       
    51  *      variable= value
       
    52  *
       
    53  * Two items on one line:
       
    54  *
       
    55  *      variable= value variable2= value2
       
    56  *
       
    57  * Multi-line item:
       
    58  *
       
    59  *      variable= This variable
       
    60  *      spans multiple
       
    61  *      lines
       
    62  *
       
    63  * 
       
    64  * @note Parsing stops at End-Of-File or if the tag "endscript" (without the quotes) appears in the file.
       
    65  *
       
    66  * @note A section may take some default values from another section or config file
       
    67  *
       
    68  * To specify default values for all sections, add a section at the start of the config file called [Defaults], e.g.:
       
    69  *
       
    70  *      [Defaults]
       
    71  *      sc= +447785016005
       
    72  *
       
    73  * To read default values from another config file, add an item with name "defaults" and value is the name of the file. E.g.:
       
    74  *
       
    75  *      defaults= another_config_file.txt
       
    76 
       
    77  */
       
    78 
       
    79 
       
    80 #ifndef __TEST_CONFIG_FILE_PARSER_H__
       
    81 #define __TEST_CONFIG_FILE_PARSER_H__
       
    82 
       
    83 #include <e32std.h>
       
    84 #include <e32base.h>
       
    85 
       
    86 class CTestConfigSection;
       
    87 class CTestConfigItem;
       
    88 class RFs;
       
    89 class TParse;
       
    90 class RFile;
       
    91 
       
    92 _LIT(KScriptPanic, "TEST-SCRIPT");
       
    93 _LIT(KScriptPathSep,"\\");
       
    94 _LIT8(KScriptSectionStart, "[");
       
    95 _LIT8(KScriptSectionEnd, "]");
       
    96 _LIT8(KScriptCRLF, "\r\n");
       
    97 _LIT8(KScriptCRLF8, "\r\n");
       
    98 _LIT8(KScriptLF, "\n");
       
    99 _LIT8(KScriptCR, "\r");
       
   100 _LIT8(KScriptItemEnd, "=");
       
   101 _LIT8(KScriptItemEnd8, "=");
       
   102 _LIT8(KScriptSpace8, " ");
       
   103 _LIT8(KScriptDefaults, "Defaults");
       
   104 _LIT8(KScriptDefault1, "Def");
       
   105 _LIT8(KScriptDefault2, "Default");
       
   106 _LIT8(KScriptCommentStart, "#");
       
   107 const TInt KScriptLFChar = '\n';
       
   108 const TInt KScriptCRChar = '\r';
       
   109 
       
   110 class CTestConfig : public CBase
       
   111 /**
       
   112  * @internalComponent
       
   113  * @deprecated
       
   114  */
       
   115 	{
       
   116 	public:
       
   117 		IMPORT_C static CTestConfig* NewLC(RFs& aFs, const TDesC& aComponent, const TDesC& aScriptFile);
       
   118 		IMPORT_C static CTestConfig* NewLC(RFs& aFs, const TDesC& aComponent);
       
   119 		IMPORT_C ~CTestConfig();
       
   120 
       
   121 		IMPORT_C const TDesC8& ItemValue(const TDesC8& aSection, const TDesC8& aItem, const TDesC8& aDefault) const;
       
   122 		IMPORT_C TInt ItemValue(const TDesC8& aSection, const TDesC8& aItem, const TInt aDefault) const;
       
   123 		
       
   124 		IMPORT_C void ReadScriptL(const TDesC& aScript);
       
   125 
       
   126 		inline const RPointerArray<CTestConfigSection>& Sections() const;
       
   127 		inline RPointerArray<CTestConfigSection>& Sections();
       
   128 
       
   129 		IMPORT_C const CTestConfigSection* Section(const TDesC8& aSectionName) const; //return NULL if section not found
       
   130 		IMPORT_C CTestConfigSection* Section(const TDesC8& aSectionName); //return NULL if section not found
       
   131 		inline const CTestConfigSection& operator[](TInt aIndex) const {return *iSections[aIndex];}
       
   132 		inline CTestConfigSection& operator[](TInt aIndex) {return *iSections[aIndex];}
       
   133 
       
   134 		IMPORT_C static TInt CountElements(const TDesC8& aInput, TChar aDelimiter);
       
   135 		IMPORT_C static TInt GetElement(const TDesC8& aInput, TChar aDelimiter, TInt aIndex, TInt& aOutput);
       
   136 		IMPORT_C static TInt GetElement(const TDesC8& aInput, TChar aDelimiter, TInt aIndex, TPtrC8& aOutput, TBool aTrimOutput = ETrue);
       
   137 		IMPORT_C static TPtrC8 Trim(const TDesC8& aInput);
       
   138 		IMPORT_C static TPtrC8 TrimLeft(const TDesC8& aInput);
       
   139 		IMPORT_C static TPtrC8 TrimRight(const TDesC8& aInput);
       
   140 
       
   141 		IMPORT_C static HBufC8* ReplaceLC(const TDesC8& aOld, const TDesC8& aNew, const TDesC8& aOldString);
       
   142 		IMPORT_C static TInt ResolveFile(RFs& aFs, const TDesC& aComponent, const TDesC& aFileName, TParse& aParseOut);
       
   143 
       
   144 		IMPORT_C void WriteFileL(const TDesC& aFileName);
       
   145 		IMPORT_C TBool operator==(const CTestConfig& aFile) const;
       
   146 
       
   147 		IMPORT_C void AddSectionL(CTestConfigSection& aSection);
       
   148 
       
   149 	protected:
       
   150 
       
   151 		CTestConfig(RFs& aFs);
       
   152 		void ConstructL(const TDesC& aComponent);
       
   153 
       
   154 		TPtrC8 ParseValue(const TDesC8& aText, const TLex8& aInput, TInt aCurrentItemStart) const;
       
   155 		void ParseAndSetItemValueL(const TDesC8& aText, const TLex8& aInput, TInt aCurrentItemStart, CTestConfigItem*& arCurrentItem);
       
   156 		void CopyInDefaultsL(CTestConfigSection& aSection, const TDesC& aDefaultFile);
       
   157 
       
   158 		HBufC8* ReadFileL(const TDesC& aFile) const;
       
   159 
       
   160 		TBool IsDefaultSection(const TDesC8& aSectionName) const;
       
   161 		static TInt GetNextElement(TLex8& aInput, TChar aDelimiter, TPtrC8& aOutput);
       
   162 		TBool IsNewSection(const TDesC8& aSource, const TLex8& aInput) const;
       
   163 		TBool IsNewItem(const TDesC8& aSource, const TLex8& aLex, TPtrC8& aItem, TInt& aStartOfValue) const;
       
   164 		TBool IsNewComment(const TDesC8& aSource, const TLex8& aLex) const;
       
   165 		TBool IsAtStartOfNewLine(const TDesC8& aSource, const TLex8& aLex, TBool aIgnoreSpaces) const;
       
   166 		void SkipToNextLine(TLex8& aInput) const;
       
   167 
       
   168 
       
   169 	protected:
       
   170 
       
   171 		RFs& iFs;
       
   172 		HBufC* iComponent;
       
   173 		RPointerArray<CTestConfigSection> iSections;
       
   174 	};
       
   175 
       
   176 class CTestConfigSection : public CBase
       
   177 /**
       
   178  * @internalComponent
       
   179  * @deprecated
       
   180  */
       
   181 	{
       
   182 	friend class CTestConfig;
       
   183 
       
   184 	public:
       
   185 		IMPORT_C static CTestConfigSection* NewLC(const TDesC8& aSectionName);
       
   186 		IMPORT_C static CTestConfigSection* NewLC(const TDesC8& aSectionName, CTestConfigSection& aDefaults);
       
   187 		IMPORT_C ~CTestConfigSection();
       
   188 		
       
   189 		inline const TDesC8& SectionName() const;
       
   190 
       
   191 		IMPORT_C const CTestConfigItem* Item(const TDesC8& aItemTag) const; //return NULL if the item does not exist
       
   192 		IMPORT_C CTestConfigItem* Item(const TDesC8& aItemTag); //return NULL if the item does not exist
       
   193 		IMPORT_C const CTestConfigItem* Item(const TDesC8& aItemTag,TInt aIndex) const; //return NULL if the item does not exist
       
   194 		IMPORT_C CTestConfigItem* Item(const TDesC8& aItemTag,TInt aIndex); //return NULL if the item does not exist
       
   195 
       
   196 		IMPORT_C const TDesC8& ItemValue(const TDesC8& aItemTag, const TDesC8& aDefault) const;
       
   197 		IMPORT_C TInt ItemValue(const TDesC8& aItemTag, TInt aDefault) const;
       
   198 
       
   199 		IMPORT_C CTestConfigItem& AddItemL(const TDesC8& aItemTag, const TDesC8& aValue);
       
   200 		IMPORT_C void DeleteItemsL(const TDesC8& aItem);
       
   201 
       
   202 		inline const RPointerArray<CTestConfigItem>& Items() const {return iItems;}
       
   203 		inline RPointerArray<CTestConfigItem>& Items() {return iItems;}
       
   204 
       
   205 		IMPORT_C TInt ItemCount(const TDesC8& aItemTag) const;
       
   206 		IMPORT_C void ItemsL(RPointerArray<CTestConfigItem>& aArray, const TDesC8& aItemTag);
       
   207 		IMPORT_C void ItemsL(RPointerArray<const CTestConfigItem>& aArray, const TDesC8& aItemTag) const;
       
   208 
       
   209 		inline const CTestConfigItem& operator[](TInt aIndex) const  {return *iItems[aIndex];}
       
   210 
       
   211 		inline void SetDefaultsL(const CTestConfigSection& aDefaults);
       
   212 		inline CTestConfigSection* Defaults() const {return iDefaults;}
       
   213 
       
   214 		IMPORT_C CTestConfigSection* CopyLC() const;
       
   215 
       
   216 		void WriteL(RFile& aFile) const;
       
   217 		TBool operator==(const CTestConfigSection& aFile) const;
       
   218 
       
   219 	private:
       
   220 		void ConstructL(const TDesC8& aSectionName);
       
   221 		CTestConfigSection();
       
   222 		RPointerArray<CTestConfigItem> iItems;
       
   223 		HBufC8* iSectionName;
       
   224 		CTestConfigSection* iDefaults;
       
   225 	};
       
   226 
       
   227 class CTestConfigItem : public CBase
       
   228 /**
       
   229  * @internalComponent
       
   230  * @deprecated
       
   231  */
       
   232 	{
       
   233 	friend class CTestConfigSection;
       
   234 	friend class CTestConfig;
       
   235 
       
   236 	public:
       
   237 		IMPORT_C static CTestConfigItem* NewLC(CTestConfigSection& aParent, const TDesC8& aItem, const TDesC8& aValue);
       
   238 		inline CTestConfigItem* CopyLC() const;
       
   239 
       
   240 		IMPORT_C ~CTestConfigItem();
       
   241 		inline const TDesC8& Item() const;
       
   242 		inline const TDesC8& Value() const;
       
   243 
       
   244 		void WriteL(RFile& aFile) const;
       
   245 		TBool operator==(const CTestConfigItem& aItem) const {return Item() == aItem.Item() && Value() == aItem.Value();}
       
   246 
       
   247 	public:
       
   248 
       
   249 		CTestConfigSection& iParent;
       
   250 		
       
   251 	private:
       
   252 		CTestConfigItem(CTestConfigSection& aParent);
       
   253 		void ConstructL(const TDesC8& aItem, const TDesC8& aValue);
       
   254 		HBufC8* iItem;
       
   255 		HBufC8* iValue;
       
   256 	};
       
   257 
       
   258 #include "testconfigfileparser.inl"
       
   259 
       
   260 #endif