2 * @file testscripts.h Defines classes for reading a configuration file
4 * @note Configuration File Format:
7 * defaults= another_config_file.txt
22 * A configuration file is made up of a number of "sections", each of which can contain a number of "items" (name, value combination).
24 * "Sections" must have a name and be surrounded by square backets, e.g.:
29 * 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.
31 * 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:
37 * Two items on one line:
39 * variable= value variable2= value2
43 * variable= This variable
48 * @note Parsing stops at End-Of-File or if the tag "endscript" (without the quotes) appears in the file.
50 * @note A section may take some default values from another section or config file
52 * To specify default values for all sections, add a section at the start of the config file called [Defaults], e.g.:
57 * To read default values from another config file, add an item with name "defaults" and value is the name of the file. E.g.:
59 * defaults= another_config_file.txt
64 #ifndef __TEST_CONFIG_FILE_PARSER_H__
65 #define __TEST_CONFIG_FILE_PARSER_H__
70 class CTestConfigSection;
71 class CTestConfigItem;
76 _LIT(KScriptPanic, "TEST-SCRIPT");
77 _LIT(KScriptPathSep,"\\");
78 _LIT8(KScriptSectionStart, "[");
79 _LIT8(KScriptSectionEnd, "]");
80 _LIT8(KScriptCRLF, "\r\n");
81 _LIT8(KScriptCRLF8, "\r\n");
82 _LIT8(KScriptLF, "\n");
83 _LIT8(KScriptCR, "\r");
84 _LIT8(KScriptItemEnd, "=");
85 _LIT8(KScriptItemEnd8, "=");
86 _LIT8(KScriptSpace8, " ");
87 _LIT8(KScriptDefaults, "Defaults");
88 _LIT8(KScriptDefault1, "Def");
89 _LIT8(KScriptDefault2, "Default");
90 _LIT8(KScriptCommentStart, "#");
91 const TInt KScriptLFChar = '\n';
92 const TInt KScriptCRChar = '\r';
94 class CTestConfig : public CBase
101 IMPORT_C static CTestConfig* NewLC(RFs& aFs, const TDesC& aComponent, const TDesC& aScriptFile);
102 IMPORT_C static CTestConfig* NewLC(RFs& aFs, const TDesC& aComponent);
103 IMPORT_C ~CTestConfig();
105 IMPORT_C const TDesC8& ItemValue(const TDesC8& aSection, const TDesC8& aItem, const TDesC8& aDefault) const;
106 IMPORT_C TInt ItemValue(const TDesC8& aSection, const TDesC8& aItem, const TInt aDefault) const;
108 IMPORT_C void ReadScriptL(const TDesC& aScript);
110 inline const RPointerArray<CTestConfigSection>& Sections() const;
111 inline RPointerArray<CTestConfigSection>& Sections();
113 IMPORT_C const CTestConfigSection* Section(const TDesC8& aSectionName) const; //return NULL if section not found
114 IMPORT_C CTestConfigSection* Section(const TDesC8& aSectionName); //return NULL if section not found
115 inline const CTestConfigSection& operator[](TInt aIndex) const {return *iSections[aIndex];}
116 inline CTestConfigSection& operator[](TInt aIndex) {return *iSections[aIndex];}
118 IMPORT_C static TInt CountElements(const TDesC8& aInput, TChar aDelimiter);
119 IMPORT_C static TInt GetElement(const TDesC8& aInput, TChar aDelimiter, TInt aIndex, TInt& aOutput);
120 IMPORT_C static TInt GetElement(const TDesC8& aInput, TChar aDelimiter, TInt aIndex, TPtrC8& aOutput, TBool aTrimOutput = ETrue);
121 IMPORT_C static TPtrC8 Trim(const TDesC8& aInput);
122 IMPORT_C static TPtrC8 TrimLeft(const TDesC8& aInput);
123 IMPORT_C static TPtrC8 TrimRight(const TDesC8& aInput);
125 IMPORT_C static HBufC8* ReplaceLC(const TDesC8& aOld, const TDesC8& aNew, const TDesC8& aOldString);
126 IMPORT_C static TInt ResolveFile(RFs& aFs, const TDesC& aComponent, const TDesC& aFileName, TParse& aParseOut);
128 IMPORT_C void WriteFileL(const TDesC& aFileName);
129 IMPORT_C TBool operator==(const CTestConfig& aFile) const;
131 IMPORT_C void AddSectionL(CTestConfigSection& aSection);
135 CTestConfig(RFs& aFs);
136 void ConstructL(const TDesC& aComponent);
138 TPtrC8 ParseValue(const TDesC8& aText, const TLex8& aInput, TInt aCurrentItemStart) const;
139 void ParseAndSetItemValueL(const TDesC8& aText, const TLex8& aInput, TInt aCurrentItemStart, CTestConfigItem*& arCurrentItem);
140 void CopyInDefaultsL(CTestConfigSection& aSection, const TDesC& aDefaultFile);
142 HBufC8* ReadFileL(const TDesC& aFile) const;
144 TBool IsDefaultSection(const TDesC8& aSectionName) const;
145 static TInt GetNextElement(TLex8& aInput, TChar aDelimiter, TPtrC8& aOutput);
146 TBool IsNewSection(const TDesC8& aSource, const TLex8& aInput) const;
147 TBool IsNewItem(const TDesC8& aSource, const TLex8& aLex, TPtrC8& aItem, TInt& aStartOfValue) const;
148 TBool IsNewComment(const TDesC8& aSource, const TLex8& aLex) const;
149 TBool IsAtStartOfNewLine(const TDesC8& aSource, const TLex8& aLex, TBool aIgnoreSpaces) const;
150 void SkipToNextLine(TLex8& aInput) const;
157 RPointerArray<CTestConfigSection> iSections;
160 class CTestConfigSection : public CBase
166 friend class CTestConfig;
169 IMPORT_C static CTestConfigSection* NewLC(const TDesC8& aSectionName);
170 IMPORT_C static CTestConfigSection* NewLC(const TDesC8& aSectionName, CTestConfigSection& aDefaults);
171 IMPORT_C ~CTestConfigSection();
173 inline const TDesC8& SectionName() const;
175 IMPORT_C const CTestConfigItem* Item(const TDesC8& aItemTag) const; //return NULL if the item does not exist
176 IMPORT_C CTestConfigItem* Item(const TDesC8& aItemTag); //return NULL if the item does not exist
177 IMPORT_C const CTestConfigItem* Item(const TDesC8& aItemTag,TInt aIndex) const; //return NULL if the item does not exist
178 IMPORT_C CTestConfigItem* Item(const TDesC8& aItemTag,TInt aIndex); //return NULL if the item does not exist
180 IMPORT_C const TDesC8& ItemValue(const TDesC8& aItemTag, const TDesC8& aDefault) const;
181 IMPORT_C TInt ItemValue(const TDesC8& aItemTag, TInt aDefault) const;
183 IMPORT_C CTestConfigItem& AddItemL(const TDesC8& aItemTag, const TDesC8& aValue);
184 IMPORT_C void DeleteItemsL(const TDesC8& aItem);
186 inline const RPointerArray<CTestConfigItem>& Items() const {return iItems;}
187 inline RPointerArray<CTestConfigItem>& Items() {return iItems;}
189 IMPORT_C TInt ItemCount(const TDesC8& aItemTag) const;
190 IMPORT_C void ItemsL(RPointerArray<CTestConfigItem>& aArray, const TDesC8& aItemTag);
191 IMPORT_C void ItemsL(RPointerArray<const CTestConfigItem>& aArray, const TDesC8& aItemTag) const;
193 inline const CTestConfigItem& operator[](TInt aIndex) const {return *iItems[aIndex];}
195 inline void SetDefaultsL(const CTestConfigSection& aDefaults);
196 inline CTestConfigSection* Defaults() const {return iDefaults;}
198 IMPORT_C CTestConfigSection* CopyLC() const;
200 void WriteL(RFile& aFile) const;
201 TBool operator==(const CTestConfigSection& aFile) const;
204 void ConstructL(const TDesC8& aSectionName);
205 CTestConfigSection();
206 RPointerArray<CTestConfigItem> iItems;
207 HBufC8* iSectionName;
208 CTestConfigSection* iDefaults;
211 class CTestConfigItem : public CBase
217 friend class CTestConfigSection;
218 friend class CTestConfig;
221 IMPORT_C static CTestConfigItem* NewLC(CTestConfigSection& aParent, const TDesC8& aItem, const TDesC8& aValue);
222 inline CTestConfigItem* CopyLC() const;
224 IMPORT_C ~CTestConfigItem();
225 inline const TDesC8& Item() const;
226 inline const TDesC8& Value() const;
228 void WriteL(RFile& aFile) const;
229 TBool operator==(const CTestConfigItem& aItem) const {return Item() == aItem.Item() && Value() == aItem.Value();}
233 CTestConfigSection& iParent;
236 CTestConfigItem(CTestConfigSection& aParent);
237 void ConstructL(const TDesC8& aItem, const TDesC8& aValue);
242 #include "testconfigfileparser.inl"