First public contribution.
1 // Copyright (c) 1996-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 the License "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.
18 //-----------------------------------------------------------------------------
20 TTime DosTimeToTTime(TInt aDosTime,TInt aDosDate)
22 // Deciphers the dos time/date entry information and converts to TTime
29 TInt monthMask=0x01E0;
32 TInt secs=(aDosTime&secMask)*2;
33 TInt mins=(aDosTime&minMask)>>5;
34 TInt hrs=(aDosTime&hrMask)>>11;
35 TInt days=(aDosDate&dayMask)-1;
36 TMonth months=(TMonth)(((aDosDate&monthMask)>>5)-1);
37 TInt years=((aDosDate&yearMask)>>9)+1980;
40 TInt ret=datetime.Set(years,months,days,hrs,mins,secs,0);
42 return(TTime(datetime));
46 TInt DosTimeFromTTime(const TTime& aTime)
48 // Converts a TTime to a dos time
51 TDateTime dateTime=aTime.DateTime();
52 TInt dosSecs=dateTime.Second()/2;
53 TInt dosMins=dateTime.Minute()<<5;
54 TInt dosHrs=dateTime.Hour()<<11;
55 return dosSecs|dosMins|dosHrs;
58 TInt DosDateFromTTime(const TTime& aTime)
60 // Converts a TTime to a dos date
64 TDateTime dateTime=aTime.DateTime();
65 TInt dosDays=dateTime.Day()+1;
66 TInt dosMonths=(dateTime.Month()+1)<<5;
67 TInt dosYears=(dateTime.Year()-1980)<<9;
68 return dosDays|dosMonths|dosYears;
71 TBuf8<12> DosNameToStdFormat(const TDesC8& aDosName)
73 // Converts xxx.yyy to standard format aaaaaaaayyy
77 __ASSERT_DEBUG(aDosName.Length()>=0 && aDosName.Length()<=12,Fault(EFatBadDosFormatName));
79 Mem::Fill((TUint8*)result.Ptr(),result.MaxSize(),' ');
80 TInt dotPos=aDosName.Locate('.');
81 if (dotPos==KErrNotFound)
87 result=aDosName.Left(dotPos);
89 TPtr8 ext(&result[8],3);
90 ext=aDosName.Right(aDosName.Length()-dotPos-1);
94 TBuf8<12> DosNameFromStdFormat(const TDesC8& aStdFormatName)
96 // Converts aaaaaaaayyy to dos name format xxx.yyy
100 __ASSERT_DEBUG(aStdFormatName.Length()==11,Fault(EFatBadStdFormatName));
102 TInt nameLen=aStdFormatName.Locate(' ');
103 if (nameLen>8 || nameLen==KErrNotFound)
105 result=aStdFormatName.Left(nameLen);
106 TPtrC8 ext(&aStdFormatName[8],3);
107 TInt extLen=ext.Locate(' ');
109 result.Append(TChar('.'));
110 if (extLen==KErrNotFound)
112 result.Append(ext.Left(extLen));
113 if(result.Length() && result[0]==0x05 )
120 TInt NumberOfVFatEntries(TInt aNameLength)
122 // Return the number of VFat entries required to describe a filename of length aNameLength
125 TInt numberOfEntries=0;
126 if (aNameLength%KMaxVFatEntryName)
127 aNameLength++; // Include a zero terminator
128 // If aNameLength is a exact multiple of KMaxVFatEntryName, don't bother
129 // with a zero terminator - it just adds an unnecessary directory entry
131 numberOfEntries=(1+(aNameLength/KMaxVFatEntryName));
133 if (aNameLength%KMaxVFatEntryName)
136 return(numberOfEntries);
139 //-----------------------------------------------------------------------------
141 Calculate DOS short name checksum
142 @param aShortName short name descriptor (must be at least 11 bytes long)
145 TUint8 CalculateShortNameCheckSum(const TDesC8& aShortName)
148 ASSERT(aShortName.Length() >= KFatDirNameSize);
149 const TUint8* pName = aShortName.Ptr();
151 const TUint32 w0 = ((const TUint32*)pName)[0];
152 const TUint32 w1 = ((const TUint32*)pName)[1];
154 TUint32 chkSum = w0 & 0xFF;
156 chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w0 << 16) >> 24));
157 chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w0 << 8) >> 24));
158 chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ( w0 >> 24));
160 chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + (w1) & 0xFF);
161 chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w1 << 16) >> 24));
162 chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ((w1 << 8) >> 24));
163 chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + ( w1 >> 24));
165 chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + pName[8]);
166 chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + pName[9]);
167 chkSum = (TUint8)(((chkSum<<7) | (chkSum>>1)) + pName[10]);
169 return (TUint8)chkSum;