How to parse a pathname

This topic describes how to parse a path name for the individual components

Retrieving individual components

The following code fragment shows how the individual components are extracted using the TParseBase functions:

       
        
       
       _LIT(KPath,"c:\\wrd\\meeting.wrd");
...
TParse p;
p.Set(KPath,NULL,NULL);
p.Name();       // gives "meeting"
p.NameAndExt(); // gives "meeting.wrd"
...
      

Component masking

TParse can be used to select components from different file and path specifications. The Set() function takes three arguments. They are each parsed into four components: drive, path, filename and extension.

The example below sets up the TParse object so that it can be used to yield useful information.

       
        
       
       _LIT(KSpec,"A:file1");
_LIT(KRelated,"C:\\path1\\related.xxx");
TParse fp;
fp.Set(KSpec,&KRelated,NULL);
      
 

drive

path

filename

extension

file specification

A:

 

file1

 

related name

C:

\path1\

related

.xxx

default name

       

result

A:

\path1\

file1

.xxx

The resulting fp.fullname will be A:\path1\file1.xxx. All the components that are specified in the file specification (drive and filename) are put into the result; any missing components (path and extension) are taken from the related file specification which has next order of precedence; nothing is specified in the default.

The second example shows how wildcards are allowed in the filename and extension.

       
        
       
       TParse fp;
    _LIT(KSpec,"A:file1");
    _LIT(KDefault,"C:\\path1\\*.*");
    fp.Set(KSpec,NULL,&KDefault);
      

drive

path

filename

extension

file specification

A:

 

file1

 

related filename

       

default filename

C:

\path1\

*

.*

result

A:

\path1\

file1

.*

The resulting fp.fullname will be A:\path1\file1.*

Parsing with reference to the session default directory

TParse operations occur without reference to the file server. Two RFs::Parse() functions perform a TParse::Set() using the session path as the aDefault argument : one of these variants specifies an aRelated , the other does not. Use these RFs::Parse() functions to parse filenames with reference to the session path.

Wildcard characters

The ? and * wildcard characters are supported to indicate a single character, and any sequence of characters. Both wildcard characters can be used any number of times in any part of any component of the file specification, except the drive. Although there is no intention that these will ever change, they are defined as the constants KMatchOne and KMatchAny .

Illegal paths

The following restrictions apply to path components. If any are violated, TParse will return KErrBadName :

  • a path component which is followed by a trailing backslash must also be preceded by a backslash. For example, the following code will return an error because DIR\ is neither a directory nor a file.

       
        
       
       _LIT(KText,"DIR\\");
...
TInt r=parse.Set(KText,NULL,NULL);
      
  • the length of any component (and the total path length) must not exceed 256 characters

  • spaces between the drive and the first directory in the path are illegal, although there may be spaces between other path components, for instance between the path or drive and filename, or within path components. The following code will return an error:

       
        
       
       _LIT(KBadText,"C: \\DIR\\");
...
r=parse.Set(KBadText,NULL,NULL);
      
  • when using TParse::SetNoWild() no wildcard character may be present in the filename or extension.