examples/PIPS/hybridapp/src/hybridapp.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008 � list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010 � this list of conditions and the following disclaimer in the documentation
00011 � and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013 � may be used to endorse or promote products derived from this software
00014 � without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: Demonstrates an OE hybrid application.� 
00028 */
00029 
00030 
00031 
00032 
00036 #include <stdio.h>
00037 #include <f32file.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 
00046 char* DescToChar(const TDesC& aDes)
00047         {
00048         // Get the length of the descriptor.
00049         TInt length = aDes.Length();
00050 
00051         // Allocate memory to the array of characters.
00052         // An additional byte of memory is allocated to the array as 
00053         // Symbian descriptors do not end with the end of string '\0' character.
00054         char* ptr = (char*)malloc(sizeof(char)*(length + 1));
00055         if(ptr == NULL)
00056                 {
00057                 // Return NULL if memory was not allocated properly.
00058                 return NULL;
00059                 }
00060         // The loop index.
00061         TInt ix = 0;
00062         for(; ix < length; ix++)
00063                 {
00064                 // Copy the contents of the descriptor into the array of characters.
00065                 ptr[ix] = aDes[ix];
00066                 }
00067         // Append the end of string to the 'C' style string.
00068         ptr[ix] = '\0';
00069 
00070         // return the pointer to the array.
00071         return ptr;
00072         }
00073 
00079 void PrintDriveInfoL(const RFs& aFs, const TInt aDriveNumber)
00080         {
00081         // Get the drive information
00082         TDriveInfo driveInfo;
00083         TInt errDrive = aFs.Drive(driveInfo,aDriveNumber);
00084         if(errDrive != KErrNone)
00085                 return;
00086 
00087         // Get the name of the drive.
00088         TBuf<KMaxFileName> driveName;
00089         TInt errName = aFs.GetDriveName(aDriveNumber,driveName);
00090         if(errName != KErrNone)
00091                 return;
00092 
00093         // Convert the drive name from native Symbian descriptor string to a 'C' string.
00094         char* cptr = DescToChar(driveName);
00095         CleanupStack::PushL(cptr);
00096         // Check if a name has been specified for the drive.
00097         if(strlen(cptr))
00098                 {
00099                 // Print the name of the drive.
00100                 printf("Drive Name :%s\n",cptr);
00101                 }
00102         
00103         // Print the memory information for the drive.
00104         // The memory size is defined for a drive only if it is formattable.
00105         if((driveInfo.iMediaAtt & KMediaAttFormattable))
00106                 {
00107                 TChar driveLetter;
00108                 // if drive-list entry non-zero, drive is available
00109                 TInt errDLetter = aFs.DriveToChar(aDriveNumber,driveLetter);
00110                 if(errDLetter != KErrNone)
00111                         return;
00112                 // The following line prints the drive letter followed by the hex value
00113                 // of the integer indicating that drive's attributes
00114                 printf("Drive Letter: %c\n",TUint(driveLetter));                        
00115         
00116                 // Get the volume information for the formatted device.
00117                 TVolumeInfo volumeInfo;
00118                 TInt errVol = aFs.Volume(volumeInfo,aDriveNumber);
00119                 if(errVol != KErrNone)
00120                         return;
00121 
00122                 // Get the total size and the free space of the drive.
00123                 TInt64 driveTotalSize = volumeInfo.iSize;
00124                 TInt64 driveFreeSize =  volumeInfo.iFree;
00125                 // Print the memory information for the drive.
00126                 printf("Total size of the drive: %d\n", driveTotalSize);
00127                 printf("Free space: %d\n", driveFreeSize);
00128                 
00129                 printf(" [press the enter key to continue]\n");
00130                 // Wait for a key press.
00131                 getchar();
00132                 }
00133         
00134         CleanupStack::PopAndDestroy(cptr);
00135         }
00136 
00137 LOCAL_C void MainL()
00138         {
00139         // The file server session.
00140         RFs fs;
00141         // Connect to the file server.
00142         User::LeaveIfError(fs.Connect());
00143         CleanupClosePushL(fs);
00144         printf("\nValid drives as characters (and as numbers) are:\n");
00145 
00146         // Initialise drive number to the first drive.
00147         TInt driveNumber=EDriveA;
00148         TChar driveLetter;
00149 
00150     for (; driveNumber<=EDriveZ; driveNumber++)
00151         {
00152         if (fs.IsValidDrive(driveNumber))
00153             {
00154                         // Indicates that the drive exists and is valid.
00155             TInt errDrive = fs.DriveToChar(driveNumber,driveLetter);
00156             if(errDrive == KErrNone)
00157                 {
00158                                 // Print the drive letter.
00159                     printf("%c",TUint(driveLetter));
00160                                 // Convert the drive letter to the drive number.
00161                     TInt errChar = fs.CharToDrive(driveLetter, driveNumber);
00162                     if(errChar == KErrNone)
00163                         {
00164                                         // Print the drive number.
00165                             printf("(%d) ",driveNumber);
00166                         }
00167                 }
00168             }
00169         }
00170 
00171     printf("\n");
00172 
00173         // Get a list of the available drives.
00174     TDriveList drivelist;
00175     User::LeaveIfError(fs.DriveList(drivelist));
00176 
00177         // A TDriveList (the list of available drives), is an array of
00178     // 26 bytes. Each byte with a non zero value signifies that the
00179     // corresponding drive is available.
00180 
00181     printf("\nUsing DriveList(), available drives are: \n");
00182     for (driveNumber=EDriveA; driveNumber<=EDriveZ;driveNumber++)
00183         {
00184         if (drivelist[driveNumber])
00185             {
00186                         // if drive-list entry non-zero, drive is available
00187             TInt errDrive = fs.DriveToChar(driveNumber,driveLetter);
00188             if(errDrive == KErrNone)
00189                 {
00190                     // The following line prints the drive letter followed by the hex value
00191                     // of the integer indicating that drive's attributes
00192                     printf("Drive Letter: %c\n",TUint(driveLetter));
00193                                 printf(" [press the enter key to continue]\n");
00194                                 // Wait for a key press.
00195                                 getchar();
00196                 }
00197             }
00198         }
00199     
00200         printf("Memory information for formattable drives:\n");
00201         printf(" [press the enter key to continue]\n");
00202         // Wait for a key press.
00203         getchar();
00204         for (driveNumber=EDriveA; driveNumber<=EDriveZ;driveNumber++)
00205         {
00206         if (drivelist[driveNumber])
00207             {
00208                         PrintDriveInfoL(fs,driveNumber);
00209                         }
00210                 }       
00211 
00212         CleanupStack::PopAndDestroy(&fs);
00213         }
00214 
00215 
00216 GLDEF_C TInt E32Main()
00217         {
00218         // Create cleanup stack
00219         __UHEAP_MARK;
00220         CTrapCleanup* cleanup = CTrapCleanup::New();
00221 
00222         // Run application code inside TRAP harness, wait for key press when terminated
00223         TRAPD(mainError, MainL());
00224         if (mainError)
00225                 {
00226                 printf(" failed, leave code = %d", mainError);
00227                 }
00228 
00229         printf(" [press the enter key to exit]\n");
00230         getchar();
00231 
00232         delete cleanup;
00233         __UHEAP_MARKEND;
00234         return KErrNone;
00235         }
00236 

Generated by  doxygen 1.6.2