sl@0: // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // f32\sfsrv\cl_parse.cpp sl@0: // sl@0: // sl@0: sl@0: #include "cl_std.h" sl@0: sl@0: const TInt KLexComponents=4; sl@0: const TInt KLexNames=3; sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TParseBase::TParseBase() sl@0: : iWild(0) sl@0: /** sl@0: Default constructor. sl@0: */ sl@0: { sl@0: sl@0: Mem::FillZ(&iField[0],sizeof(iField)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: TInt TParseBase::ParseDrive(TLex& aName,TBool& aDone) sl@0: // sl@0: // Parse the drive name. sl@0: // sl@0: { sl@0: sl@0: TPtrC d=aName.RemainderFromMark(); sl@0: if (d.Length()<2 || d[1]!=KDriveDelimiter) sl@0: return(KErrNone); //must be Drive delimeter and longer that tow to be valid drive sl@0: TCharF c=d[0]; sl@0: if (!c.IsAlpha()) //must be alphaber letter sl@0: return(KErrBadName); sl@0: if (!aDone) sl@0: { sl@0: if(iMod) sl@0: NameBuf()+=d.Left(2); sl@0: aDone=ETrue; sl@0: } sl@0: aName.SkipAndMark(2); sl@0: return(KErrNone); sl@0: } sl@0: sl@0: TInt TParseBase::ParsePath(TLex& aName,TBool& aDone) sl@0: // sl@0: // Parse the path. sl@0: // sl@0: { sl@0: sl@0: TPtrC d=aName.RemainderFromMark(); sl@0: if (d.Length() && d[0]!=KPathDelimiter) sl@0: return(KErrNone); // Require first char of path to be a '\' sl@0: TInt n=d.LocateReverse(KPathDelimiter)+1; sl@0: if (n && !aDone) sl@0: { sl@0: if(iMod) sl@0: { sl@0: if (NameBuf().Length()+n>KMaxFileName) sl@0: return(KErrBadName); sl@0: NameBuf()+=d.Left(n); sl@0: } sl@0: aDone=ETrue; sl@0: } sl@0: aName.SkipAndMark(n); sl@0: return(KErrNone); sl@0: } sl@0: sl@0: LOCAL_C TBool IsSpace(const TDesC& aDes) sl@0: // sl@0: // Returns ETrue if aDes only contains spaces or is zero length sl@0: // sl@0: { sl@0: sl@0: TInt len=aDes.Length(); sl@0: for (TInt i=0;iKMaxFileName) sl@0: return(KErrBadName); sl@0: NameBuf()+=v; sl@0: if (n==d.Length()) sl@0: NameBuf().TrimRight(); sl@0: } sl@0: aDone=ETrue; sl@0: } sl@0: aName.SkipAndMark(n); sl@0: return(KErrNone); sl@0: } sl@0: sl@0: TInt TParseBase::ParseExt(TLex& aName,TBool& aDone) sl@0: // sl@0: // Parse the extension. sl@0: // sl@0: { sl@0: sl@0: TPtrC d=aName.RemainderFromMark(); sl@0: if (d.Length() && !IsSpace(d) && !aDone) sl@0: { sl@0: if (d.Locate(KMatchOne)!=KErrNotFound) // Found ? in the name sl@0: iWild|=(EWildExt|EWildEither|EWildIsKMatchOne); sl@0: if (d.Locate(KMatchAny)!=KErrNotFound) // Found * in the name sl@0: iWild|=(EWildExt|EWildEither|EWildIsKMatchAny); sl@0: sl@0: if(iMod) sl@0: { sl@0: if (NameBuf().Length()+d.Length()>KMaxFileName) sl@0: return(KErrBadName); sl@0: NameBuf()+=d; sl@0: NameBuf().TrimRight(); sl@0: } sl@0: else sl@0: aName.SkipAndMark(d.Length()); sl@0: aDone=ETrue; sl@0: } sl@0: return(KErrNone); sl@0: } sl@0: sl@0: TInt TParseBase::Set(const TDesC* aName,const TDesC* aRelated,const TDesC* aDefault,TBool allowWild) sl@0: // sl@0: // Parse a name. Optionally allow wild cards. sl@0: // sl@0: { sl@0: sl@0: TInt (TParseBase::*parse[KLexComponents])(TLex& aName,TBool& aDone); sl@0: parse[0]=&TParseBase::ParseDrive; sl@0: parse[1]=&TParseBase::ParsePath; sl@0: parse[2]=&TParseBase::ParseName; sl@0: parse[3]=&TParseBase::ParseExt; sl@0: sl@0: iWild=0; sl@0: sl@0: Mem::FillZ(&iField[0],sizeof(iField)); sl@0: sl@0: TLex name(*aName); sl@0: TLex def; sl@0: TLex rel; sl@0: TInt lexnames; sl@0: if(iMod) sl@0: { sl@0: if (aRelated) sl@0: rel=(*aRelated); sl@0: if (aDefault) sl@0: def=(*aDefault); sl@0: NameBuf().Zero(); sl@0: lexnames = KLexNames; sl@0: } sl@0: else sl@0: { sl@0: lexnames = 1; sl@0: } sl@0: sl@0: TLex* lex[KLexNames]; sl@0: lex[0]=(&name); sl@0: lex[1]=(&rel); sl@0: lex[2]=(&def); sl@0: sl@0: name.Mark(); sl@0: rel.Mark(); sl@0: def.Mark(); sl@0: sl@0: TInt r; sl@0: TInt pos=0; sl@0: sl@0: for (TInt i=0;i*parse[i])(*lex[j],done))NameBuf().MaxLength()) sl@0: return(KErrGeneral); sl@0: TInt pos=aName.Locate(KPathDelimiter); sl@0: if (pos!=KErrNotFound) sl@0: return(KErrBadName); sl@0: TFileName n=aName; sl@0: n.Append(KPathDelimiter); sl@0: NameBuf().Insert(iField[EName].pos,n); sl@0: iField[EPath].len=(TUint8)(iField[EPath].len+len); sl@0: iField[EName].pos=(TUint8)(iField[EName].pos+len); sl@0: iField[EExt].pos=(TUint8)(len+iField[EExt].pos); sl@0: if (IsRoot()) sl@0: iWild&=~EIsRoot; sl@0: return(KErrNone); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C const TDesC& TParseBase::FullName() const sl@0: /** sl@0: Gets the complete file specification. sl@0: sl@0: This is in the form: sl@0: sl@0: drive-letter: \\path\\filename.extension sl@0: sl@0: @return The fully parsed file specification. sl@0: */ sl@0: { sl@0: sl@0: return(NameBufC()); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TPtrC TParseBase::Drive() const sl@0: /** sl@0: Gets the drive letter. sl@0: sl@0: The drive letter is in the form: sl@0: sl@0: drive-letter: sl@0: sl@0: Note that the drive letter is folded. sl@0: sl@0: @return The drive letter and colon. sl@0: */ sl@0: { sl@0: sl@0: const SField& f=iField[EDrive]; sl@0: return(NameBufC().Mid(f.pos,f.len)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TPtrC TParseBase::Path() const sl@0: /** sl@0: Gets the path. sl@0: sl@0: The path is in the form: sl@0: sl@0: \\path\\ sl@0: sl@0: @return The path. It always begins and ends in a backslash. sl@0: */ sl@0: { sl@0: sl@0: const SField& f=iField[EPath]; sl@0: return(NameBufC().Mid(f.pos,f.len)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TPtrC TParseBase::DriveAndPath() const sl@0: /** sl@0: Gets the drive letter and path. sl@0: sl@0: This is in the form sl@0: sl@0: drive-letter:\\path\\ sl@0: sl@0: Note that the drive letter is folded sl@0: sl@0: @return The drive and path. sl@0: */ sl@0: { sl@0: sl@0: const SField& f=iField[EDrive]; sl@0: return(NameBufC().Mid(f.pos,f.len+iField[EPath].len)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TPtrC TParseBase::Name() const sl@0: /** sl@0: Gets the filename. sl@0: sl@0: This is in the form sl@0: sl@0: filename sl@0: sl@0: @return The filename. sl@0: */ sl@0: { sl@0: sl@0: const SField& f=iField[EName]; sl@0: return(NameBufC().Mid(f.pos,f.len)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TPtrC TParseBase::Ext() const sl@0: /** sl@0: Gets the extension. sl@0: sl@0: This is in the form: sl@0: sl@0: .extension sl@0: sl@0: @return The extension and preceding dot. sl@0: */ sl@0: { sl@0: sl@0: const SField& f=iField[EExt]; sl@0: return(NameBufC().Mid(f.pos,f.len)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TPtrC TParseBase::NameAndExt() const sl@0: /** sl@0: Gets the filename and extension. sl@0: sl@0: This is in the form: sl@0: sl@0: filename.ext sl@0: sl@0: @return The filename and extension. sl@0: */ sl@0: { sl@0: sl@0: const SField& f=iField[EName]; sl@0: return(NameBufC().Mid(f.pos,f.len+iField[EExt].len)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::DrivePresent() const sl@0: /** sl@0: Tests whether a drive is present. sl@0: sl@0: Note that this function refers to a component sl@0: in the aName argument specified in calls to TParse::Set(), TParse::SetNoWild() sl@0: or RFs::Parse(), not to the resulting fully parsed file specification. sl@0: sl@0: @return True if a drive present, false if not. sl@0: sl@0: @see TParse sl@0: @see RFs sl@0: */ sl@0: { sl@0: sl@0: return(iField[EDrive].present); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::PathPresent() const sl@0: /** sl@0: Tests whether a path is present. sl@0: sl@0: Note that this function refers to a component sl@0: in the aName argument specified in calls to TParse::Set(), TParse::SetNoWild() sl@0: or RFs::Parse(), not to the resulting fully parsed file specification. sl@0: sl@0: @return True if a path present, false if not. sl@0: sl@0: @see TParse sl@0: @see RFs sl@0: */ sl@0: { sl@0: sl@0: return(iField[EPath].present); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::NamePresent() const sl@0: /** sl@0: Tests whether a file name is present. sl@0: sl@0: Note that this function refers to a component sl@0: in the aName argument specified in calls to TParse::Set(), TParse::SetNoWild() sl@0: or RFs::Parse(), not to the resulting fully parsed file specification. sl@0: sl@0: This function returns true even if the filename specified in aName contains sl@0: only wildcards. It only returns false if nothing is specified. sl@0: sl@0: @return True if a name present, false if not. sl@0: */ sl@0: { sl@0: sl@0: return(iField[EName].present); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::ExtPresent() const sl@0: /** sl@0: Tests whether an extension is present. sl@0: sl@0: Note that this function refers to a component sl@0: in the aName argument specified in calls to TParse::Set(), TParse::SetNoWild() sl@0: or RFs::Parse(), not to the resulting fully parsed file specification. sl@0: sl@0: This function returns true even if the extension contains only wildcards. sl@0: It only returns false if nothing is specified. sl@0: sl@0: @return True if an extension present, false if not. sl@0: */ sl@0: { sl@0: sl@0: return(iField[EExt].present); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::NameOrExtPresent() const sl@0: /** sl@0: Tests whether a filename or an extension are present. sl@0: sl@0: Note that this function refers to a component in the aName argument sl@0: specified in calls to TParse::Set(), TParse::SetNoWild() or RFs::Parse(), not sl@0: to the resulting fully parsed file specification. sl@0: sl@0: This function returns true even if the filename or extension specified in sl@0: aName contain only wildcards. It only returns false if nothing is specified. sl@0: sl@0: @return True if either a name or an extension or both are present, sl@0: otherwise false. sl@0: */ sl@0: { sl@0: sl@0: return(iField[EName].present || iField[EExt].present); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::IsRoot() const sl@0: /** sl@0: Tests whether the path in the fully parsed specification is the root directory. sl@0: sl@0: @return True if path is root, false if not. sl@0: */ sl@0: { sl@0: sl@0: return(iWild&EIsRoot); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::IsWild() const sl@0: /** sl@0: Tests whether the filename or the extension in the fully parsed specification sl@0: contains one or more wildcard characters. sl@0: sl@0: @return True if wildcards are present, false if not. sl@0: */ sl@0: { sl@0: sl@0: return(iWild&EWildEither); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::IsKMatchOne() const sl@0: /** sl@0: Tests whether the name or the extension contains a question mark wildcard. sl@0: sl@0: @return True if either the name or extension has a ? wild card, sl@0: false otherwise. sl@0: */ sl@0: { sl@0: sl@0: return(iWild&EWildIsKMatchOne); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::IsKMatchAny() const sl@0: /** sl@0: Tests whether the name or the extension contains asterisk wildcards. sl@0: sl@0: @return True if either the name or extension has a * wild card, sl@0: false otherwise. sl@0: */ sl@0: { sl@0: sl@0: return(iWild&EWildIsKMatchAny); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::IsNameWild() const sl@0: /** sl@0: Tests whether the filename in the fully parsed specification contains one or sl@0: more wildcard characters. sl@0: sl@0: @return True if the filename contains wildcard characters, false if not. sl@0: */ sl@0: { sl@0: sl@0: return(iWild&EWildName); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TBool TParseBase::IsExtWild() const sl@0: /** sl@0: Tests whether the extension in the fully parsed specification contains one sl@0: or more wildcard characters. sl@0: sl@0: @return True if the extension contains wildcard characters, false if not. sl@0: */ sl@0: { sl@0: sl@0: return(iWild&EWildExt); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TParse::TParse() sl@0: /** sl@0: Default constructor. sl@0: */ sl@0: { sl@0: iMod=1; sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TInt TParse::Set(const TDesC& aName,const TDesC* aRelated,const TDesC* aDefault) sl@0: /** sl@0: Parses a file specification, allowing wildcards in the filename and extension. sl@0: sl@0: This function sets up the TParse object so that it can be used to provide sl@0: useful information. sl@0: sl@0: @param aName The file specification to be parsed. sl@0: @param aRelated The related file specification. This is optional, sl@0: set to NULL to omit. sl@0: @param aDefault The default file specification. This is optional, sl@0: set to NULL to omit. sl@0: sl@0: @return KErrNone, if successful, otherwise one of the other system-wide error sl@0: codes. sl@0: */ sl@0: { sl@0: sl@0: return(TParseBase::Set(&aName,aRelated,aDefault,ETrue)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TInt TParse::SetNoWild(const TDesC& aName,const TDesC* aRelated,const TDesC* aDefault) sl@0: /** sl@0: Parses a file specification; disallows wildcards in any part of the file name sl@0: or extension. sl@0: sl@0: If you need to specify wildcards use Set(). Otherwise, this sl@0: function behaves in the same way as Set(). sl@0: sl@0: @param aName The file specification to be parsed. sl@0: @param aRelated The related file specification. This is optional, sl@0: set to NULL to omit. sl@0: @param aDefault The default file specification. This is optional, sl@0: set to NULL to omit. sl@0: sl@0: @return KErrNone, if successful, otherwise one of the other system-wide error sl@0: codes. sl@0: sl@0: @see TParse::Set sl@0: */ sl@0: { sl@0: sl@0: return(TParseBase::Set(&aName,aRelated,aDefault,EFalse)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TDes& TParse::NameBuf() sl@0: /** sl@0: Gets a reference to the descriptor containing the file specification passed to sl@0: the constructor of this object. sl@0: sl@0: @return A reference to the descriptor containing the filename. sl@0: */ sl@0: { sl@0: sl@0: return(iNameBuf); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C const TDesC& TParse::NameBufC() const sl@0: /** sl@0: Gets a const reference to the descriptor containing the file specification sl@0: passed to the constructor of this object. sl@0: sl@0: @return A const reference to the descriptor containing the file specification. sl@0: */ sl@0: { sl@0: sl@0: return(iNameBuf); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TParsePtr::TParsePtr(TDes& aName) sl@0: : iNameBuf((TText*)aName.Ptr(),aName.Length(),aName.MaxLength()) sl@0: /** sl@0: Constructor taking a reference to a filename. sl@0: sl@0: The specified filename is parsed and if this fails, a panic is raised. sl@0: sl@0: @param aName Reference to the filename to be parsed. On return contains sl@0: the fully parsed path specification. If a filename and extension sl@0: are specified, they may both contain wildcards. sl@0: The maximum length is KMaxFileName characters. sl@0: sl@0: @panic FSCLIENT 24 if the the specified name fails to parse. sl@0: sl@0: @see KMaxFileName sl@0: */ sl@0: { sl@0: iMod=1; sl@0: TInt r=TParseBase::Set(&aName,NULL,NULL,ETrue); sl@0: __ASSERT_ALWAYS(r==KErrNone,Panic(EParsePtrBadDescriptor0)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TDes& TParsePtr::NameBuf() sl@0: /** sl@0: Gets a reference to the descriptor containing the filename passed to sl@0: the constructor of this object. sl@0: sl@0: @return A reference to the descriptor containing the filename. sl@0: */ sl@0: { sl@0: sl@0: return(iNameBuf); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C const TDesC& TParsePtr::NameBufC() const sl@0: /** sl@0: Gets a const reference to the descriptor containing the filename passed to sl@0: the constructor of this object. sl@0: sl@0: @return A const reference to the descriptor containing the filename. sl@0: */ sl@0: { sl@0: sl@0: return(iNameBuf); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TParsePtrC::TParsePtrC(const TDesC& aName) sl@0: /** sl@0: Constructor taking a constant reference to a filename. sl@0: sl@0: The filename is parsed and if this fails, a panic is raised. sl@0: Note that the filename cannot be modified using this class. sl@0: sl@0: @param aName Constant reference to the filename to be parsed. sl@0: On return contains the fully parsed filename. sl@0: If a file and extension are specified, they may both sl@0: contain wildcards. sl@0: The maximum length is KMaxFileName characters. sl@0: sl@0: @panic FSCLIENT 24 if the the specified name fails to parse. sl@0: sl@0: @see KMaxFileName sl@0: */ sl@0: { sl@0: iMod=0; sl@0: iNameBuf.Set(aName); sl@0: TInt r = TParseBase::Set(&aName,NULL,NULL,ETrue); sl@0: __ASSERT_ALWAYS(r==KErrNone,Panic(EParsePtrBadDescriptor0)); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C TDes& TParsePtrC::NameBuf() sl@0: /** sl@0: Gets a reference to the descriptor containing the filename passed to sl@0: the constructor of this object. sl@0: sl@0: @return A reference to the descriptor containing the filename. sl@0: */ sl@0: { sl@0: sl@0: Panic(EParsePtrCAccessError); sl@0: return(*(TDes*)&iNameBuf); sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C const TDesC& TParsePtrC::NameBufC() const sl@0: /** sl@0: Gets a const reference to the descriptor containing the filename passed to sl@0: the constructor of this object. sl@0: sl@0: @return A const reference to the descriptor containing the filename. sl@0: */ sl@0: { sl@0: sl@0: return(iNameBuf); sl@0: } sl@0: