First public contribution.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Implementation of the classes responsible for the settings handling in the .ini file
22 #include "settings_manager.h"
23 #include "win_media_device.h"
25 //-----------------------------------------------------------------------------
26 TSettingsManager::TSettingsManager(const char* apFileName, TInt aEpocDrvNumber)
28 //-- store ini file name
29 ASSERT(lstrlenA(apFileName) < KMaxFileName);
30 lstrcpyA(iIniFileName, apFileName);
32 //-- make the drive section name e.g. [Drive_D]
33 ASSERT(aEpocDrvNumber >= EDriveA && aEpocDrvNumber <= EDriveZ);
34 wsprintfA(iDrvSectionName, "Drive_%C", 'A'+aEpocDrvNumber);
38 //-----------------------------------------------------------------------------
42 TSettingsManager* TSettingsManager::Create(TInt aEpocDrvNumber)
44 TSettingsManager* pThis = NULL;
46 const char* KDefaultEpocRootName = "EPOCROOT";
48 //-- try to find the configuration file. It must be: "\\epoc32\\data\\win_drive.ini"
49 char buf[KMaxFileName];
51 TInt nRes = GetEnvironmentVariableA(KDefaultEpocRootName, buf, sizeof(buf));
54 __LOG(_L("#-- TSettingsManager::Create() env. var. 'EPOCROOT' is not set!"));
58 lstrcatA(buf, "epoc32\\data\\win_drive.ini");
60 //-- try to locate ini file
62 HANDLE hFile = FindFirstFileA(buf, &wfd);
63 const TBool bIniFilePresent = (hFile != INVALID_HANDLE_VALUE);
68 __LOG(_L("#-- TSettingsManager::Create() ini file is not present, aborting!"));
73 pThis = new TSettingsManager(buf, aEpocDrvNumber);
78 //-----------------------------------------------------------------------------
80 Read a string from the ini file.
82 @param aAppName pointer to the name of the section
83 @param aKeyName pointer to the name of the key
84 @param aStrDefault pointer to the string with the default value
85 @param aStrDest pointer to the destination buffer
86 @param aSize size of the buffer, where the string to bee copied to.
88 @return length of the read string or length of the aStrDefault if the value hasn't been found
90 DWORD TSettingsManager::ReadString (const char* aAppName, const char* aKeyName, const char* aStrDefault, char* aStrDest, DWORD aSize) const
92 return GetPrivateProfileStringA(aAppName, aKeyName, aStrDefault, aStrDest, aSize, IniFileName());
97 Read integer value from the ini file.
99 @param aAppName pointer to the name of the section
100 @param aKeyName pointer to the name of the key
101 @param aDefaultIntVal default value, the result if section, key or value is not found
103 @return integer value from appropriate section and key, or aDefaultIntVal
105 int TSettingsManager::ReadInt(const char* aAppName, const char* aKeyName, int aDefaultIntVal) const
107 return GetPrivateProfileIntA(aAppName, aKeyName, aDefaultIntVal, IniFileName());
111 Read a boolean value from the ini file.
113 @param aAppName pointer to the name of the section
114 @param aKeyName pointer to the name of the key
115 @param aDefaultBoolVal default value, the result if section, key of value is not found
116 @return the boolean value or aDefaultBoolVal if section, key or value is not found
118 TBool TSettingsManager::ReadBool(const char* aAppName, const char* aKeyName, int aDefaultBoolVal) const
120 return (ReadInt(aAppName, aKeyName, aDefaultBoolVal ? 1 : 0) != 0);
125 read a hexadecimal value from the ini file.
127 @param aAppName pointer to the name of the section
128 @param aKeyName pointer to the name of the key
129 @param aVal default value input, if there is a valid value in th eini file, it will be returned here
130 @return ETrue if the valid value from ini file is read; EFalse otherwise.
133 TBool TSettingsManager::ReadUintFromHex(const char* aAppName, const char* aKeyName, TUint32& aVal) const
135 unsigned char buff[80];
137 DWORD dwLen = ReadString(aAppName, aKeyName, "", (char*)buff, sizeof(buff));
139 return EFalse; //-- the value is not set
141 //-- try to convert the string from hexadecimal representation to a number.
145 //-- skip '0x' prefix if it is present by chance
146 TPtrC8 rem = lex.Remainder();
147 TInt offset = rem.FindF(_L8("x"));
152 if(lex.Val(val32, EHex) == KErrNone)
161 //-----------------------------------------------------------------------------
163 Get media sector size in bytes.
164 Usually it is 512 bytes, other values are not recommended.
166 '0' means "auto", i.e. if the media allows, it will be queried for the sector size,
167 otherwise 512 bytes will be used.
169 @return media sector size in bytes.
171 TUint32 TSettingsManager::MediaSectorSize() const
174 const char KeyName[]="BytesPerSector";
175 const TInt KDefaultSPC = 0; //-- default value, means "use media settings or 512 bytes by default"
177 return ReadInt(DriveSection(), KeyName, KDefaultSPC);
180 //-----------------------------------------------------------------------------
182 Get media size in sectors. See also MediaSectorSize().
183 '0' means "auto". i.e. size will be taken from windows media device (partition or image file)
185 @return media size in sectors.
187 TUint32 TSettingsManager::MediaSizeInSectors() const
190 const char KeyName[]="MediaSizeInSectors";
191 const TInt KDefaultSzSec = 0;
193 return ReadInt(DriveSection(), KeyName, KDefaultSzSec);
196 //-----------------------------------------------------------------------------
198 Get windows device name that will be used for media emulation.
199 like "\\\\.\\Z:" for a physical volume or "K:\\epoc32\\data\\media\\mmccrd0a.bin" for a image file.
201 @param apszName pointer to the buffer, where the name will be copied to.
202 @param aBufLen length of the input buffer
203 @return ETrue on success
205 TBool TSettingsManager::WinDeviceName(char* apszName, TUint aBufLen) const
207 const char KeyName[]="DeviceName";
208 const char KDefaultValue[]="";
210 TInt nRes = ReadString(DriveSection(), KeyName, KDefaultValue, apszName, aBufLen);
214 __LOG(_L("#-- TSettingsManager: 'DeviceName' key value isn't set !"));
221 //-----------------------------------------------------------------------------
223 @return ETrue if the device is Read-Only
225 TBool TSettingsManager::IsReadOnly() const
227 const char KeyName[]="ReadOnly";
228 const TBool KDefaultValue = EFalse;
230 return ReadBool(DriveSection(), KeyName, KDefaultValue);
233 //-----------------------------------------------------------------------------
235 @return Media type override value from the ini file or 0 if it is not set there
237 TUint32 TSettingsManager::TMediaType_Override() const
239 const char KeyName[]="MediaTypeOverride";
240 const TInt KDefaultVal = 0;
242 return ReadInt(DriveSection(), KeyName, KDefaultVal);
245 //-----------------------------------------------------------------------------
248 Get Media Attributes override "AND" and "OR" bitmasks. (the need to be in hex in the ini file).
250 @param aAndMask On return contains "And" mask; default value == 0xFFFFFFFF
251 @param aOrMask On return contains "OR" mask; default value == 0x00
253 void TSettingsManager::MediaAtt_OverrideMasks(TUint32 &aAndMask, TUint32 &aOrMask) const
257 //-- 1. read "AND" mask
259 const char KeyName1[]="MediaAttOverride_AND";
260 TBool bRes = ReadUintFromHex(DriveSection(), KeyName1, defVal);
263 //-- 2. read "OR" mask
264 const char KeyName2[]="MediaAttOverride_OR";
266 bRes = ReadUintFromHex(DriveSection(), KeyName2, defVal);
270 //-----------------------------------------------------------------------------
272 Get Drive Attributes override "AND" and "OR" bitmasks. (the need to be in hex in the ini file).
274 @param aAndMask On return contains "And" mask; default value == 0xFFFFFFFF
275 @param aOrMask On return contains "OR" mask; default value == 0x00
277 void TSettingsManager::DriveAtt_OverrideMasks(TUint32 &aAndMask, TUint32 &aOrMask) const
281 //-- 1. read "AND" mask
283 const char KeyName1[]="DriveAttOverride_AND";
284 TBool bRes = ReadUintFromHex(DriveSection(), KeyName1, defVal);
287 //-- 2. read "OR" mask
288 const char KeyName2[]="DriveAttOverride_OR";
290 bRes = ReadUintFromHex(DriveSection(), KeyName2, defVal);