Update contrib.
1 // Copyright (c) 1995-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.
14 // f32\sfsrv\cl_parse.cpp
20 const TInt KLexComponents=4;
21 const TInt KLexNames=3;
26 EXPORT_C TParseBase::TParseBase()
33 Mem::FillZ(&iField[0],sizeof(iField));
39 TInt TParseBase::ParseDrive(TLex& aName,TBool& aDone)
41 // Parse the drive name.
45 TPtrC d=aName.RemainderFromMark();
46 if (d.Length()<2 || d[1]!=KDriveDelimiter)
47 return(KErrNone); //must be Drive delimeter and longer that tow to be valid drive
49 if (!c.IsAlpha()) //must be alphaber letter
61 TInt TParseBase::ParsePath(TLex& aName,TBool& aDone)
67 TPtrC d=aName.RemainderFromMark();
68 if (d.Length() && d[0]!=KPathDelimiter)
69 return(KErrNone); // Require first char of path to be a '\'
70 TInt n=d.LocateReverse(KPathDelimiter)+1;
75 if (NameBuf().Length()+n>KMaxFileName)
85 LOCAL_C TBool IsSpace(const TDesC& aDes)
87 // Returns ETrue if aDes only contains spaces or is zero length
91 TInt len=aDes.Length();
92 for (TInt i=0;i<len;i++)
101 TInt TParseBase::ParseName(TLex& aName,TBool& aDone)
107 TPtrC d=aName.RemainderFromMark();
108 if (d.Locate(KPathDelimiter)!=KErrNotFound)
109 return(KErrBadName); // Illegal name - filenames cannot contain a '\'
110 TInt n=d.LocateReverse(KExtDelimiter);
114 if (IsSpace(d.Left(n)))
120 if (v.Locate(KMatchOne)!=KErrNotFound) // Found ? in the name
121 iWild|=(EWildName|EWildEither|EWildIsKMatchOne);
122 if (v.Locate(KMatchAny)!=KErrNotFound) // Found * in the name
123 iWild|=(EWildName|EWildEither|EWildIsKMatchAny);
126 if (NameBuf().Length()+n>KMaxFileName)
130 NameBuf().TrimRight();
134 aName.SkipAndMark(n);
138 TInt TParseBase::ParseExt(TLex& aName,TBool& aDone)
140 // Parse the extension.
144 TPtrC d=aName.RemainderFromMark();
145 if (d.Length() && !IsSpace(d) && !aDone)
147 if (d.Locate(KMatchOne)!=KErrNotFound) // Found ? in the name
148 iWild|=(EWildExt|EWildEither|EWildIsKMatchOne);
149 if (d.Locate(KMatchAny)!=KErrNotFound) // Found * in the name
150 iWild|=(EWildExt|EWildEither|EWildIsKMatchAny);
154 if (NameBuf().Length()+d.Length()>KMaxFileName)
157 NameBuf().TrimRight();
160 aName.SkipAndMark(d.Length());
166 TInt TParseBase::Set(const TDesC* aName,const TDesC* aRelated,const TDesC* aDefault,TBool allowWild)
168 // Parse a name. Optionally allow wild cards.
172 TInt (TParseBase::*parse[KLexComponents])(TLex& aName,TBool& aDone);
173 parse[0]=&TParseBase::ParseDrive;
174 parse[1]=&TParseBase::ParsePath;
175 parse[2]=&TParseBase::ParseName;
176 parse[3]=&TParseBase::ParseExt;
180 Mem::FillZ(&iField[0],sizeof(iField));
193 lexnames = KLexNames;
200 TLex* lex[KLexNames];
212 for (TInt i=0;i<KLexComponents;i++)
215 for (TInt j=0;j<lexnames;j++)
217 if ((r=(this->*parse[i])(*lex[j],done))<KErrNone)
220 iField[i].present=ETrue;
224 len=NameBuf().Length()-pos;
226 len=name.MarkedOffset()-pos;
227 iField[i].len=(TUint8)len;
228 iField[i].pos=(TUint8)pos;
231 if (!allowWild && iWild)
233 if (iField[EPath].len==1)
241 EXPORT_C TInt TParseBase::PopDir()
243 Removes the last directory from the path in the fully parsed specification.
245 This function may be used to navigate up one level in a directory hierarchy.
246 An error is returned if the specified directory is the root.
248 @return KErrNone if successful, otherwise one of the other system-wide error
256 if (iField[EName].pos==0 && NameBuf().Length()==256)
259 len=iField[EName].pos;
260 TPtrC p(NameBuf().Ptr(),len-1);
261 TInt pos=p.LocateReverse(KPathDelimiter)+1;
263 NameBuf().Delete(pos,len);
264 iField[EName].pos=(TUint8)(iField[EName].pos-len);
265 iField[EExt].pos=(TUint8)(iField[EExt].pos-len);
266 iField[EPath].len=(TUint8)(iField[EPath].len-len);
267 if (iField[EPath].len==1)
275 EXPORT_C TInt TParseBase::AddDir(const TDesC& aName)
277 Adds a single directory onto the end of the path in
278 the fully parsed specification.
280 The directory is inserted between the final directory, and the filename, if
283 @param aName The directory to be added. It must not start with a \\ otherwise
284 the function does not recognise it as a valid directory name
285 and an error is returned.
286 The directory name must not end with a \\ since the function
287 adds this automatically. It must not exceed the maximum
288 filename length, KMaxFileName characters, otherwise an error
291 @return KErrNone if successful, otherwise another of the system-wide error
297 if (aName.Length()==0)
299 TInt len=aName.Length()+1;
300 if ((len+NameBuf().Length())>NameBuf().MaxLength())
302 TInt pos=aName.Locate(KPathDelimiter);
303 if (pos!=KErrNotFound)
306 n.Append(KPathDelimiter);
307 NameBuf().Insert(iField[EName].pos,n);
308 iField[EPath].len=(TUint8)(iField[EPath].len+len);
309 iField[EName].pos=(TUint8)(iField[EName].pos+len);
310 iField[EExt].pos=(TUint8)(len+iField[EExt].pos);
319 EXPORT_C const TDesC& TParseBase::FullName() const
321 Gets the complete file specification.
325 drive-letter: \\path\\filename.extension
327 @return The fully parsed file specification.
337 EXPORT_C TPtrC TParseBase::Drive() const
339 Gets the drive letter.
341 The drive letter is in the form:
345 Note that the drive letter is folded.
347 @return The drive letter and colon.
351 const SField& f=iField[EDrive];
352 return(NameBufC().Mid(f.pos,f.len));
358 EXPORT_C TPtrC TParseBase::Path() const
362 The path is in the form:
366 @return The path. It always begins and ends in a backslash.
370 const SField& f=iField[EPath];
371 return(NameBufC().Mid(f.pos,f.len));
377 EXPORT_C TPtrC TParseBase::DriveAndPath() const
379 Gets the drive letter and path.
383 drive-letter:\\path\\
385 Note that the drive letter is folded
387 @return The drive and path.
391 const SField& f=iField[EDrive];
392 return(NameBufC().Mid(f.pos,f.len+iField[EPath].len));
398 EXPORT_C TPtrC TParseBase::Name() const
406 @return The filename.
410 const SField& f=iField[EName];
411 return(NameBufC().Mid(f.pos,f.len));
417 EXPORT_C TPtrC TParseBase::Ext() const
425 @return The extension and preceding dot.
429 const SField& f=iField[EExt];
430 return(NameBufC().Mid(f.pos,f.len));
436 EXPORT_C TPtrC TParseBase::NameAndExt() const
438 Gets the filename and extension.
444 @return The filename and extension.
448 const SField& f=iField[EName];
449 return(NameBufC().Mid(f.pos,f.len+iField[EExt].len));
455 EXPORT_C TBool TParseBase::DrivePresent() const
457 Tests whether a drive is present.
459 Note that this function refers to a component
460 in the aName argument specified in calls to TParse::Set(), TParse::SetNoWild()
461 or RFs::Parse(), not to the resulting fully parsed file specification.
463 @return True if a drive present, false if not.
470 return(iField[EDrive].present);
476 EXPORT_C TBool TParseBase::PathPresent() const
478 Tests whether a path is present.
480 Note that this function refers to a component
481 in the aName argument specified in calls to TParse::Set(), TParse::SetNoWild()
482 or RFs::Parse(), not to the resulting fully parsed file specification.
484 @return True if a path present, false if not.
491 return(iField[EPath].present);
497 EXPORT_C TBool TParseBase::NamePresent() const
499 Tests whether a file name is present.
501 Note that this function refers to a component
502 in the aName argument specified in calls to TParse::Set(), TParse::SetNoWild()
503 or RFs::Parse(), not to the resulting fully parsed file specification.
505 This function returns true even if the filename specified in aName contains
506 only wildcards. It only returns false if nothing is specified.
508 @return True if a name present, false if not.
512 return(iField[EName].present);
518 EXPORT_C TBool TParseBase::ExtPresent() const
520 Tests whether an extension is present.
522 Note that this function refers to a component
523 in the aName argument specified in calls to TParse::Set(), TParse::SetNoWild()
524 or RFs::Parse(), not to the resulting fully parsed file specification.
526 This function returns true even if the extension contains only wildcards.
527 It only returns false if nothing is specified.
529 @return True if an extension present, false if not.
533 return(iField[EExt].present);
539 EXPORT_C TBool TParseBase::NameOrExtPresent() const
541 Tests whether a filename or an extension are present.
543 Note that this function refers to a component in the aName argument
544 specified in calls to TParse::Set(), TParse::SetNoWild() or RFs::Parse(), not
545 to the resulting fully parsed file specification.
547 This function returns true even if the filename or extension specified in
548 aName contain only wildcards. It only returns false if nothing is specified.
550 @return True if either a name or an extension or both are present,
555 return(iField[EName].present || iField[EExt].present);
562 EXPORT_C TBool TParseBase::IsRoot() const
564 Tests whether the path in the fully parsed specification is the root directory.
566 @return True if path is root, false if not.
570 return(iWild&EIsRoot);
576 EXPORT_C TBool TParseBase::IsWild() const
578 Tests whether the filename or the extension in the fully parsed specification
579 contains one or more wildcard characters.
581 @return True if wildcards are present, false if not.
585 return(iWild&EWildEither);
591 EXPORT_C TBool TParseBase::IsKMatchOne() const
593 Tests whether the name or the extension contains a question mark wildcard.
595 @return True if either the name or extension has a ? wild card,
600 return(iWild&EWildIsKMatchOne);
606 EXPORT_C TBool TParseBase::IsKMatchAny() const
608 Tests whether the name or the extension contains asterisk wildcards.
610 @return True if either the name or extension has a * wild card,
615 return(iWild&EWildIsKMatchAny);
621 EXPORT_C TBool TParseBase::IsNameWild() const
623 Tests whether the filename in the fully parsed specification contains one or
624 more wildcard characters.
626 @return True if the filename contains wildcard characters, false if not.
630 return(iWild&EWildName);
636 EXPORT_C TBool TParseBase::IsExtWild() const
638 Tests whether the extension in the fully parsed specification contains one
639 or more wildcard characters.
641 @return True if the extension contains wildcard characters, false if not.
645 return(iWild&EWildExt);
651 EXPORT_C TParse::TParse()
662 EXPORT_C TInt TParse::Set(const TDesC& aName,const TDesC* aRelated,const TDesC* aDefault)
664 Parses a file specification, allowing wildcards in the filename and extension.
666 This function sets up the TParse object so that it can be used to provide
669 @param aName The file specification to be parsed.
670 @param aRelated The related file specification. This is optional,
672 @param aDefault The default file specification. This is optional,
675 @return KErrNone, if successful, otherwise one of the other system-wide error
680 return(TParseBase::Set(&aName,aRelated,aDefault,ETrue));
686 EXPORT_C TInt TParse::SetNoWild(const TDesC& aName,const TDesC* aRelated,const TDesC* aDefault)
688 Parses a file specification; disallows wildcards in any part of the file name
691 If you need to specify wildcards use Set(). Otherwise, this
692 function behaves in the same way as Set().
694 @param aName The file specification to be parsed.
695 @param aRelated The related file specification. This is optional,
697 @param aDefault The default file specification. This is optional,
700 @return KErrNone, if successful, otherwise one of the other system-wide error
707 return(TParseBase::Set(&aName,aRelated,aDefault,EFalse));
713 EXPORT_C TDes& TParse::NameBuf()
715 Gets a reference to the descriptor containing the file specification passed to
716 the constructor of this object.
718 @return A reference to the descriptor containing the filename.
728 EXPORT_C const TDesC& TParse::NameBufC() const
730 Gets a const reference to the descriptor containing the file specification
731 passed to the constructor of this object.
733 @return A const reference to the descriptor containing the file specification.
743 EXPORT_C TParsePtr::TParsePtr(TDes& aName)
744 : iNameBuf((TText*)aName.Ptr(),aName.Length(),aName.MaxLength())
746 Constructor taking a reference to a filename.
748 The specified filename is parsed and if this fails, a panic is raised.
750 @param aName Reference to the filename to be parsed. On return contains
751 the fully parsed path specification. If a filename and extension
752 are specified, they may both contain wildcards.
753 The maximum length is KMaxFileName characters.
755 @panic FSCLIENT 24 if the the specified name fails to parse.
761 TInt r=TParseBase::Set(&aName,NULL,NULL,ETrue);
762 __ASSERT_ALWAYS(r==KErrNone,Panic(EParsePtrBadDescriptor0));
768 EXPORT_C TDes& TParsePtr::NameBuf()
770 Gets a reference to the descriptor containing the filename passed to
771 the constructor of this object.
773 @return A reference to the descriptor containing the filename.
783 EXPORT_C const TDesC& TParsePtr::NameBufC() const
785 Gets a const reference to the descriptor containing the filename passed to
786 the constructor of this object.
788 @return A const reference to the descriptor containing the filename.
798 EXPORT_C TParsePtrC::TParsePtrC(const TDesC& aName)
800 Constructor taking a constant reference to a filename.
802 The filename is parsed and if this fails, a panic is raised.
803 Note that the filename cannot be modified using this class.
805 @param aName Constant reference to the filename to be parsed.
806 On return contains the fully parsed filename.
807 If a file and extension are specified, they may both
809 The maximum length is KMaxFileName characters.
811 @panic FSCLIENT 24 if the the specified name fails to parse.
818 TInt r = TParseBase::Set(&aName,NULL,NULL,ETrue);
819 __ASSERT_ALWAYS(r==KErrNone,Panic(EParsePtrBadDescriptor0));
825 EXPORT_C TDes& TParsePtrC::NameBuf()
827 Gets a reference to the descriptor containing the filename passed to
828 the constructor of this object.
830 @return A reference to the descriptor containing the filename.
834 Panic(EParsePtrCAccessError);
835 return(*(TDes*)&iNameBuf);
841 EXPORT_C const TDesC& TParsePtrC::NameBufC() const
843 Gets a const reference to the descriptor containing the filename passed to
844 the constructor of this object.
846 @return A const reference to the descriptor containing the filename.