os/boardsupport/emulator/emulatorbsp/win_drive/settings_manager.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // Implementation of the classes responsible for the settings handling in the .ini file
    15 // 
    16 //
    17 
    18 /**
    19  @file
    20 */
    21 
    22 #include "settings_manager.h"
    23 #include "win_media_device.h"
    24 
    25 //-----------------------------------------------------------------------------
    26 TSettingsManager::TSettingsManager(const char* apFileName, TInt aEpocDrvNumber)
    27 {
    28     //-- store ini file name
    29     ASSERT(lstrlenA(apFileName) < KMaxFileName);
    30     lstrcpyA(iIniFileName, apFileName);
    31 
    32     //-- make the drive section name e.g. [Drive_D]
    33     ASSERT(aEpocDrvNumber >= EDriveA && aEpocDrvNumber <= EDriveZ);
    34     wsprintfA(iDrvSectionName, "Drive_%C", 'A'+aEpocDrvNumber);
    35 }
    36 
    37 
    38 //-----------------------------------------------------------------------------
    39 /** 
    40     Factory function
    41 */
    42 TSettingsManager* TSettingsManager::Create(TInt aEpocDrvNumber)
    43 {
    44     TSettingsManager* pThis = NULL;
    45 
    46     const char* KDefaultEpocRootName = "EPOCROOT"; 
    47 
    48     //-- try to find the configuration file. It must be: "\\epoc32\\data\\win_drive.ini"
    49     char buf[KMaxFileName];
    50 
    51     TInt nRes = GetEnvironmentVariableA(KDefaultEpocRootName, buf, sizeof(buf));
    52     if(!nRes)
    53     {
    54      __LOG(_L("#-- TSettingsManager::Create() env. var. 'EPOCROOT' is not set!"));   
    55      return NULL;
    56     }
    57 
    58     lstrcatA(buf, "epoc32\\data\\win_drive.ini");
    59 
    60     //-- try to locate ini file
    61     WIN32_FIND_DATAA wfd;
    62     HANDLE hFile = FindFirstFileA(buf, &wfd);
    63     const TBool bIniFilePresent = (hFile != INVALID_HANDLE_VALUE);
    64     FindClose(hFile);
    65 
    66     if(!bIniFilePresent)
    67     {
    68      __LOG(_L("#-- TSettingsManager::Create() ini file is not present, aborting!"));   
    69      __LOGF(buf); 
    70      return NULL;
    71     }
    72 
    73     pThis = new TSettingsManager(buf, aEpocDrvNumber);
    74     
    75     return pThis;
    76 }
    77 
    78 //-----------------------------------------------------------------------------
    79 /**
    80     Read a string from the ini file.
    81  
    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.
    87  
    88     @return length of the read string or length of the aStrDefault if the value hasn't been found
    89 */
    90 DWORD TSettingsManager::ReadString (const char* aAppName, const char* aKeyName, const char* aStrDefault, char* aStrDest, DWORD aSize) const
    91 {
    92     return GetPrivateProfileStringA(aAppName, aKeyName, aStrDefault, aStrDest, aSize, IniFileName());
    93 }
    94 
    95 
    96 /**
    97     Read integer value from the ini file.
    98  
    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
   102  
   103     @return integer value from appropriate section and key, or aDefaultIntVal
   104 */
   105 int TSettingsManager::ReadInt(const char* aAppName, const char* aKeyName, int aDefaultIntVal) const
   106 {
   107     return GetPrivateProfileIntA(aAppName, aKeyName, aDefaultIntVal, IniFileName());
   108 }
   109 
   110 /**
   111     Read a boolean value from the ini file.
   112  
   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
   117 */
   118 TBool TSettingsManager::ReadBool(const char* aAppName, const char* aKeyName, int  aDefaultBoolVal) const
   119 {
   120     return (ReadInt(aAppName, aKeyName, aDefaultBoolVal ? 1 : 0) != 0);
   121 }
   122 
   123 
   124 /**
   125     read a hexadecimal value from the ini file.
   126  
   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.
   131 
   132 */
   133 TBool TSettingsManager::ReadUintFromHex(const char* aAppName, const char* aKeyName, TUint32& aVal) const
   134 {
   135     unsigned char buff[80];
   136     
   137     DWORD dwLen = ReadString(aAppName, aKeyName, "", (char*)buff, sizeof(buff));
   138     if(!dwLen)
   139         return EFalse;  //-- the value is not set
   140         
   141     //-- try to convert the string from hexadecimal representation to a number.
   142     TLex8 lex(buff);
   143     lex.SkipSpace();
   144     
   145     //-- skip '0x' prefix if it is present by chance
   146     TPtrC8 rem = lex.Remainder();
   147     TInt offset = rem.FindF(_L8("x"));
   148     if(offset >= 0)
   149         lex.Inc(offset+1);
   150 
   151     TUint32 val32;
   152     if(lex.Val(val32, EHex) == KErrNone)
   153     {
   154         aVal = val32;
   155         return ETrue;
   156     }        
   157 
   158     return EFalse;
   159 }
   160 
   161 //-----------------------------------------------------------------------------
   162 /** 
   163     Get media sector size in bytes. 
   164     Usually it is 512 bytes, other values are not recommended.
   165     
   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.
   168 
   169     @return media sector size in bytes. 
   170 */
   171 TUint32 TSettingsManager::MediaSectorSize() const
   172 {
   173     
   174     const char KeyName[]="BytesPerSector";
   175     const TInt KDefaultSPC = 0; //-- default value, means "use media settings or 512 bytes by default"
   176     
   177     return ReadInt(DriveSection(), KeyName, KDefaultSPC);
   178 }
   179 
   180 //-----------------------------------------------------------------------------
   181 /**
   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)
   184     
   185     @return media size in sectors.  
   186 */
   187 TUint32 TSettingsManager::MediaSizeInSectors() const
   188 {
   189    
   190     const char KeyName[]="MediaSizeInSectors";
   191     const TInt KDefaultSzSec = 0; 
   192 
   193     return ReadInt(DriveSection(), KeyName, KDefaultSzSec);
   194 }
   195 
   196 //-----------------------------------------------------------------------------
   197 /**
   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.
   200 
   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
   204 */
   205 TBool TSettingsManager::WinDeviceName(char* apszName, TUint aBufLen) const
   206 {
   207     const char KeyName[]="DeviceName";
   208     const char KDefaultValue[]="";
   209 
   210     TInt nRes = ReadString(DriveSection(), KeyName, KDefaultValue, apszName, aBufLen);
   211     
   212     if(nRes == 0)
   213     {
   214     __LOG(_L("#-- TSettingsManager: 'DeviceName' key value isn't set !"));   
   215     }
   216 
   217     return ETrue;
   218 }
   219 
   220 
   221 //-----------------------------------------------------------------------------
   222 /**
   223     @return ETrue if the device is Read-Only
   224 */
   225 TBool TSettingsManager::IsReadOnly() const
   226 {
   227     const char KeyName[]="ReadOnly";
   228     const TBool KDefaultValue = EFalse; 
   229 
   230     return ReadBool(DriveSection(), KeyName, KDefaultValue);
   231 }
   232 
   233 //-----------------------------------------------------------------------------
   234 /**
   235     @return Media type override value from the ini file or 0 if it is not set there
   236 */
   237 TUint32 TSettingsManager::TMediaType_Override() const
   238 {
   239     const char KeyName[]="MediaTypeOverride";
   240     const TInt KDefaultVal = 0; 
   241 
   242     return ReadInt(DriveSection(), KeyName, KDefaultVal);
   243 }
   244 
   245 //-----------------------------------------------------------------------------
   246 
   247 /**
   248     Get Media Attributes override "AND" and "OR" bitmasks. (the need to be in hex in the ini file).
   249     
   250     @param  aAndMask    On return contains "And" mask; default value == 0xFFFFFFFF
   251     @param  aOrMask     On return contains "OR" mask;  default value == 0x00
   252 */
   253 void TSettingsManager::MediaAtt_OverrideMasks(TUint32 &aAndMask, TUint32 &aOrMask) const
   254 {
   255     TUint32 defVal;
   256 
   257     //-- 1. read "AND" mask
   258     defVal = KMaxTUint;
   259     const char KeyName1[]="MediaAttOverride_AND";
   260     TBool bRes = ReadUintFromHex(DriveSection(), KeyName1, defVal);
   261     aAndMask = defVal;
   262 
   263     //-- 2. read "OR" mask
   264     const char KeyName2[]="MediaAttOverride_OR";
   265     defVal = 0;
   266     bRes = ReadUintFromHex(DriveSection(), KeyName2, defVal);
   267     aOrMask = defVal;
   268 }
   269 
   270 //-----------------------------------------------------------------------------
   271 /**
   272     Get Drive Attributes override "AND" and "OR" bitmasks. (the need to be in hex in the ini file).
   273     
   274     @param  aAndMask    On return contains "And" mask; default value == 0xFFFFFFFF
   275     @param  aOrMask     On return contains "OR" mask;  default value == 0x00
   276 */
   277 void TSettingsManager::DriveAtt_OverrideMasks(TUint32 &aAndMask, TUint32 &aOrMask) const
   278 {
   279     TUint32 defVal;
   280 
   281     //-- 1. read "AND" mask
   282     defVal = KMaxTUint;
   283     const char KeyName1[]="DriveAttOverride_AND";
   284     TBool bRes = ReadUintFromHex(DriveSection(), KeyName1, defVal);
   285     aAndMask = defVal;
   286 
   287     //-- 2. read "OR" mask
   288     const char KeyName2[]="DriveAttOverride_OR";
   289     defVal = 0;
   290     bRes = ReadUintFromHex(DriveSection(), KeyName2, defVal);
   291     aOrMask = defVal;
   292 }
   293 
   294 
   295 
   296 
   297 
   298 
   299 
   300 
   301 
   302 
   303 
   304 
   305 
   306 
   307 
   308 
   309 
   310 
   311 
   312 
   313 
   314 
   315 
   316 
   317 
   318 
   319