Generating a File Name from the URI

This tutorial describes the steps to generate a file name from URI.

Introduction

TUriC8 provides functionality to generate a fully qualified file name from a file URI object. This function supports only URIs with file scheme.

URI path component

The path component encodes the file's location on the file system. The generic path component is of the form:

"/" [keyword "/" ] [drive *("/"keyword)["/"filename ]]

where,

  • keyword indicates path limitations. The only valid value of this is private, which indicates that the path is to be resolved relative to the application’s private directory.

  • drive is either the drive letter (upper or lowercase), or the term “ext-media”, indicating that the file is on a removable drive.

Examples of file names resolved from variants of URIs using TUriC8::GetFileNameL() are as follows:

File type File URI File name after resolving

File on a fixed drive

file:///c/path/to/file/name.ext

c:\path\to\file\name.ext

File on a removable media drive

file:///ext-media/path/to/file/name.ext

f:\path\to\file\name.ext

Private file on a fixed drive

file:///private/c/path/to/file/name.ext

c:\private\0x1001234f\path\to\file\name.ext

Private file on a removable drive

file:///private/ext-media/path/to/file/name.ext

h:\private\0x1001234f\path\to\file\name.ext

Procedure

The following code generates the file name from the file URI provided.

//Parse the URI descriptor
TUriParser8* uriComponent; 
_LIT8( KUriDes,"http://web.intra/Dev/SysDoc/devlib.htm" );  
uriComponent->Parse( KUriDes );
// Extract the file name from the file URI
HBufC* fileName = uriComponent->GetFileNameL();  //returns the string 'devlib.htm'

Another variant of TUriC8::GetFileNameL() is used to get either the full file name, only the file path or only the file name.

HBufC16* filename = HBufC* TUriC8::GetFileNameL( EUriFileNameFull )    

The code returns a full file name.

Note: UriC8::GetFileNameL() can be used only for file URIs and will leave if used on any other scheme.

Other two options available are as follows:

  1. EUriFileNamePath generates the filename containing only the path component.

    For example, http://www.foo.com/MyDir/MyFiles/MyFile.doc;param1;param2 retrieves MyDir\MyFiles\MyFile.doc.

  2. EUriFileNameTail generates the file name containing just the file name.

    For example, http://www.foo.com/MyDir/MyFiles/MyFile.doc;param1;param2 retrieves MyFile.doc.