|
Nokia N97 SDK Example Applications Guide |
00001 /** \file DriveInfo.cpp */ 00002 00003 // 00004 // Copyright (C) Symbian Software Ltd 2000-2006. All rights reserved. 00005 // 00006 00007 #include <f32file.h> 00008 #include <bacline.h> 00009 #include "CommonFramework.h" 00010 00011 00012 LOCAL_D RFs fsSession; 00013 00014 // example functions 00015 00016 void PrintAllDrivesVolInfo(); 00017 void PrintDriveVolInfo( TPtrC &drive ); 00018 00019 // utility functions 00020 00021 void PrintDriveInfo( TInt driveNumber ); 00022 void FormatDriveInfo(TDes& aBuffer, const TDriveInfo aDriveInfo); 00023 void FormatVolumeInfo(TDes& aBuffer, const TVolumeInfo aVolumeInfo); 00024 00025 namespace 00026 { 00027 _LIT(KMessage,"Drive Info\n"); 00028 _LIT(KValidDriveMsg,"\nValid drives as names (and as numbers) are:\n"); 00029 _LIT(KDriveCharAndNum,"%c(%d) "); 00030 _LIT(KNewLine,"\n"); 00031 _LIT(KAvailDriveMsg,"\nUsing DriveList(), available drives are:\n"); 00032 _LIT(KDriveAtts," %c: %02x\n"); 00033 } // empty namespace 00034 00035 void WaitForKey() 00036 { 00037 _LIT(KPressAnyKey,"\n[Press any key to continue]\n"); 00038 console->Printf(KPressAnyKey); 00039 console->Getch(); 00040 } 00041 00042 /** \fn void doExampleL() 00043 * 00044 * main-function of the DriveInfo console application 00045 * 00046 * checks commandline arguments and then displays drive information either 00047 * for all available drives or the ones specified in commandline 00048 */ 00049 LOCAL_C void doExampleL() 00050 { 00051 CCommandLineArguments *cmdline = CCommandLineArguments::NewLC(); 00052 00053 // Connect to file server 00054 User::LeaveIfError(fsSession.Connect()); // Start session 00055 CleanupClosePushL( fsSession ); 00056 00057 TInt count = cmdline->Count(); 00058 if (count > 1) 00059 { 00060 // skip commandline argument 0, because that is the name of the exe 00061 for (TInt i=1; i<count; ++i) 00062 { 00063 TPtrC arg = cmdline->Arg(i); 00064 PrintDriveVolInfo(arg); 00065 } 00066 } 00067 else // show all 00068 { 00069 PrintAllDrivesVolInfo(); 00070 } 00071 00072 CleanupStack::PopAndDestroy(2, cmdline); // cmdline, fsSession 00073 } 00074 00075 /** \fn void PrintAllDrivesVolInfo() 00076 * 00077 * shows all valid drive identifications and available drives in current 00078 * device. Then drive and volume information are shown for each available 00079 * drive with the help of PrintDriveInfo() 00080 */ 00081 void PrintAllDrivesVolInfo() 00082 { 00083 console->Printf(KMessage); 00084 00085 // Print the valid drives as characters and as numbers. 00086 // Then print the drive list (list of available drives), and 00087 // information about each drive in the list using Drive(). 00088 // Finally, print volume information. 00089 00090 console->Printf(KValidDriveMsg); 00091 00092 TInt driveNumber=EDriveA; 00093 TChar driveLetter; 00094 for (; driveNumber<=EDriveZ; ++driveNumber) 00095 { 00096 if (fsSession.IsValidDrive(driveNumber)) 00097 { 00098 fsSession.DriveToChar(driveNumber,driveLetter); 00099 fsSession.CharToDrive(driveLetter, driveNumber); 00100 console->Printf(KDriveCharAndNum, TUint(driveLetter), driveNumber); 00101 } 00102 } 00103 console->Printf(KNewLine); 00104 00105 // A TDriveList (the list of available drives), is an array of 00106 // 26 bytes. Each byte with a non zero value signifies that the 00107 // corresponding drive is available. 00108 // 00109 TDriveList drivelist; 00110 User::LeaveIfError(fsSession.DriveList(drivelist)); 00111 console->Printf(KAvailDriveMsg); 00112 for (driveNumber=EDriveA; driveNumber<=EDriveZ; ++driveNumber) 00113 { 00114 // if drive-list entry non-zero, drive is available 00115 if (drivelist[driveNumber]) 00116 { 00117 User::LeaveIfError(fsSession.DriveToChar(driveNumber,driveLetter)); 00118 // The following line prints the drive letter followed by the hex value 00119 // of the integer indicating that drive's attributes 00120 console->Printf(KDriveAtts,TUint(driveLetter), drivelist[driveNumber]); 00121 } 00122 } 00123 console->Printf(KNewLine); 00124 WaitForKey(); 00125 00126 for (driveNumber=EDriveA; driveNumber<=EDriveZ; ++driveNumber) 00127 { 00128 PrintDriveInfo( driveNumber ); 00129 } 00130 } 00131 00132 #define ASCII_ZERO '0' 00133 00134 /** \fn void PrintDriveVolInfo( TPtrC &drive ) 00135 * 00136 * Parses given commandline argument and checks, whether specified drive is 00137 * a valid one. Then drive and volume information are shown for the 00138 * drive with the help of PrintDriveInfo() 00139 * \param drive commandline argument 00140 */ 00141 void PrintDriveVolInfo( TPtrC &drive ) 00142 { 00143 _LIT(KInvalidDrive,"Drive %c is not available!\n"); 00144 _LIT(KInvalidArgument,"\nInvalid argument: %S (not a drive letter or number!)\n\n"); 00145 00146 TInt driveNumber = 0; 00147 TChar driveLetter; 00148 if ((drive.Length() == 1) && (TChar(drive[0]).IsAlpha())) 00149 { 00150 driveLetter = drive[0]; 00151 fsSession.CharToDrive(driveLetter, driveNumber); 00152 } 00153 else 00154 { 00155 // assumption: user gave a number as parameter. 00156 // no fancy parsing; just assume the parameter is ascii-number 00157 for (TInt i = 0; i<drive.Length(); ++i) 00158 { 00159 if (! TChar(drive[i]).IsDigit()) 00160 { 00161 console->Printf(KInvalidArgument, &drive); 00162 return; 00163 } 00164 driveNumber *= 10; 00165 driveNumber += drive[i] - ASCII_ZERO; 00166 } 00167 } 00168 00169 TDriveList drivelist; 00170 User::LeaveIfError(fsSession.DriveList(drivelist)); 00171 00172 // if drive-list entry non-zero, drive is available 00173 if (!drivelist[driveNumber]) 00174 { 00175 console->Printf(KInvalidDrive); 00176 return; 00177 } 00178 00179 PrintDriveInfo( driveNumber ); 00180 } 00181 00182 /** \fn void PrintDriveInfo( TInt driveNumber ) 00183 * 00184 * shows drive and volume information of the specified drive 00185 * \param driveNumber drive specified as number 00186 */ 00187 void PrintDriveInfo( TInt driveNumber ) 00188 { 00189 _LIT(KDriveInfo,"\nDrive information for drive %c:\n%S"); 00190 _LIT(KVolInfo,"\nVolume information for drive %c:\n%S"); 00191 00192 TChar driveLetter; 00193 00194 // Print information about the drive 00195 TBuf<200> buffer; 00196 TDriveInfo driveInfo; 00197 fsSession.Drive(driveInfo, driveNumber); 00198 00199 // test whether drive is available. If not, return 00200 if (driveInfo.iDriveAtt == KDriveAbsent) 00201 { 00202 return; 00203 } 00204 00205 FormatDriveInfo(buffer, driveInfo); 00206 User::LeaveIfError(fsSession.DriveToChar(driveNumber, driveLetter)); 00207 console->Printf(KDriveInfo, TUint(driveLetter), &buffer); 00208 buffer.Zero(); 00209 WaitForKey(); 00210 00211 // Print volume information 00212 TVolumeInfo volumeInfo; 00213 TInt err=fsSession.Volume(volumeInfo, driveNumber); 00214 00215 // Volume() returns KErrNotReady if no volume present. 00216 // In this case, check next drive number 00217 if (err != KErrNotReady) 00218 { 00219 buffer.Zero(); 00220 FormatVolumeInfo(buffer, volumeInfo); 00221 console->Printf(KVolInfo, TUint(driveLetter), &buffer); 00222 WaitForKey(); 00223 } 00224 } 00225 00226 /** \fn void FormatDriveInfo(TDes& aBuffer, const TDriveInfo aDriveInfo) 00227 * 00228 * Interprets passed drive information to user readable form 00229 * \param aBuffer the buffer into which formatted text is written 00230 * \param aDriveInfo drive's information 00231 */ 00232 void FormatDriveInfo(TDes& aBuffer, const TDriveInfo aDriveInfo) 00233 { 00234 // Append battery, media and drive information to aBuffer 00235 // Define descriptor constants using the _LIT macro 00236 00237 _LIT(KFormatString,"iType=%02x\niBattery=%02x\niDriveAtt=%02x\niMediaAtt=%02x\n"); 00238 _LIT(KBatLow,"Battery low\n"); 00239 _LIT(KBatGood,"Battery good\n"); 00240 _LIT(KBatNotSupported,"Battery not supported\n"); 00241 _LIT(KNotPresent,"No media present\n"); 00242 _LIT(KFloppy,"Media is floppy disk\n"); 00243 _LIT(KHard,"Media is hard disk\n"); 00244 _LIT(KCDROM,"Media is CD-ROM\n"); 00245 _LIT(KRam,"Media is RAM\n"); 00246 _LIT(KFlash,"Media is flash\n"); 00247 _LIT(KRom,"Media is ROM\n"); 00248 _LIT(KRemote,"Media is remote\n"); 00249 _LIT(KNANDFlash,"Media is NAND flash\n"); 00250 _LIT(KUnknown,"Media unknown\n"); 00251 _LIT(KDriveAtts,"Drive attributes:"); 00252 _LIT(KLocal," local"); 00253 _LIT(KROMDrive," ROM"); 00254 _LIT(KRedirected," redirected"); 00255 _LIT(KSubstituted," substituted"); 00256 _LIT(KInternal," internal"); 00257 _LIT(KRemovable," removable"); 00258 _LIT(KMediaAtts,"\nMedia attributes:"); 00259 _LIT(KDynamic," dynamic"); 00260 _LIT(KDual," dual-density"); 00261 _LIT(KFormattable," formattable"); 00262 _LIT(KLockable," lockable"); 00263 _LIT(KLocked," locked"); 00264 _LIT(KHasPassword," has password"); 00265 _LIT(KWriteProtected," write-protected"); 00266 _LIT(KNewLine,"\n"); 00267 00268 // aBuffer.AppendFormat(KFormatString, TInt(aDriveInfo.iType), 00269 // TInt(aDriveInfo.iBattery), TInt(aDriveInfo.iDriveAtt), TInt(aDriveInfo.iMediaAtt)); 00270 00271 aBuffer.AppendFormat(KFormatString, aDriveInfo.iType, aDriveInfo.iBattery, 00272 aDriveInfo.iDriveAtt, aDriveInfo.iMediaAtt); 00273 switch (aDriveInfo.iType) 00274 { 00275 case EMediaNotPresent: 00276 aBuffer.Append(KNotPresent); 00277 break; 00278 case EMediaFloppy: 00279 aBuffer.Append(KFloppy); 00280 break; 00281 case EMediaHardDisk: 00282 aBuffer.Append(KHard); 00283 break; 00284 case EMediaCdRom: 00285 aBuffer.Append(KCDROM); 00286 break; 00287 case EMediaRam: 00288 aBuffer.Append(KRam); 00289 break; 00290 case EMediaFlash: 00291 aBuffer.Append(KFlash); 00292 break; 00293 case EMediaRom: 00294 aBuffer.Append(KRom); 00295 break; 00296 case EMediaRemote: 00297 aBuffer.Append(KRemote); 00298 break; 00299 case EMediaNANDFlash: 00300 aBuffer.Append(KNANDFlash); 00301 break; 00302 default: 00303 aBuffer.Append(KUnknown); 00304 } 00305 00306 switch (aDriveInfo.iBattery) 00307 { 00308 case EBatLow: 00309 aBuffer.Append(KBatLow); 00310 break; 00311 case EBatGood: 00312 aBuffer.Append(KBatGood); 00313 break; 00314 default: 00315 aBuffer.Append(KBatNotSupported); 00316 } 00317 00318 aBuffer.Append(KDriveAtts); 00319 if (aDriveInfo.iDriveAtt & KDriveAttLocal) 00320 aBuffer.Append(KLocal); 00321 if (aDriveInfo.iDriveAtt & KDriveAttRom) 00322 aBuffer.Append(KROMDrive); 00323 if (aDriveInfo.iDriveAtt & KDriveAttRedirected) 00324 aBuffer.Append(KRedirected); 00325 if (aDriveInfo.iDriveAtt & KDriveAttSubsted) 00326 aBuffer.Append(KSubstituted); 00327 if (aDriveInfo.iDriveAtt & KDriveAttInternal) 00328 aBuffer.Append(KInternal); 00329 if (aDriveInfo.iDriveAtt & KDriveAttRemovable) 00330 aBuffer.Append(KRemovable); 00331 00332 aBuffer.Append(KMediaAtts); 00333 if (aDriveInfo.iMediaAtt & KMediaAttVariableSize) 00334 aBuffer.Append(KDynamic); 00335 if (aDriveInfo.iMediaAtt & KMediaAttDualDensity) 00336 aBuffer.Append(KDual); 00337 if (aDriveInfo.iMediaAtt & KMediaAttFormattable) 00338 aBuffer.Append(KFormattable); 00339 if (aDriveInfo.iMediaAtt & KMediaAttWriteProtected) 00340 aBuffer.Append(KWriteProtected); 00341 if (aDriveInfo.iMediaAtt & KMediaAttLockable) 00342 aBuffer.Append(KLockable); 00343 if (aDriveInfo.iMediaAtt & KMediaAttLocked) 00344 aBuffer.Append(KLocked); 00345 if (aDriveInfo.iMediaAtt & KMediaAttHasPassword) 00346 aBuffer.Append(KHasPassword); 00347 aBuffer.Append(KNewLine); 00348 } 00349 00350 /** \fn void FormatVolumeInfo(TDes& aBuffer, const TVolumeInfo aVolumeInfo) 00351 * 00352 * Interprets passed volume information to user readable form 00353 * \param aBuffer the buffer into which formatted text is written 00354 * \param aVolumeInfo drive's volume information 00355 */ 00356 void FormatVolumeInfo(TDes& aBuffer, const TVolumeInfo aVolumeInfo) 00357 { 00358 // Append volume information to aBuffer 00359 _LIT(KUID, "Unique ID : %08x\n"); 00360 _LIT(KSize, "Size : %Ld bytes\n"); 00361 _LIT(KFree, "Free space : %Ld bytes\n"); 00362 _LIT(KVolName, "Volume name: %S\n"); 00363 aBuffer.AppendFormat(KUID, aVolumeInfo.iUniqueID); 00364 aBuffer.AppendFormat(KSize, aVolumeInfo.iSize); 00365 aBuffer.AppendFormat(KFree, aVolumeInfo.iFree); 00366 aBuffer.AppendFormat(KVolName, &aVolumeInfo.iName); 00367 }
|
© Nokia 2009 |