21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

GetFullPathName( ) requires the length of the buffer to be specified in<br />

characters, not bytes. Likewise, the return value from the function will<br />

be in units of characters rather than bytes. When allocating memory<br />

for the buffer, be sure to multiply the number of characters by<br />

sizeof(TCHAR).<br />

If an error occurs in resolving the path, GetFullPathName( ) will return 0, and you can<br />

call GetLastError( ) to determine the cause of the error; otherwise, it will return the<br />

number of characters written into lpBuffer.<br />

In the following example, SpcResolvePath( ) demonstrates how to use<br />

GetFullPathName( ) properly. If it is successful, it will return a dynamically allocated<br />

buffer that contains the resolved path; otherwise, it will return NULL. The allocated<br />

buffer must be freed by calling LocalFree( ).<br />

#include <br />

LPTSTR SpcResolvePath(LPCTSTR lpFileName) {<br />

DWORD dwLastError, nBufferLength;<br />

LPTSTR lpBuffer, lpFilePart;<br />

if (!(nBufferLength = GetFullPathName(lpFileName, 0, 0, &lpFilePart))) return 0;<br />

if (!(lpBuffer = (LPTSTR)LocalAlloc(LMEM_FIXED, sizeof(TCHAR) * nBufferLength)))<br />

return 0;<br />

if (!GetFullPathName(lpFileName, nBufferLength, lpBuffer, &lpFilePart)) {<br />

dwLastError = GetLastError( );<br />

LocalFree(lpBuffer);<br />

SetLastError(dwLastError);<br />

return 0;<br />

}<br />

return lpBuffer;<br />

}<br />

3.8 Evaluating URL Encodings<br />

<strong>Problem</strong><br />

You need to decode a Uniform Resource Locator (URL).<br />

Solution<br />

Iterate over the characters in the URLlooking for a percent symbol followed by two<br />

hexadecimal digits. When such a sequence is encountered, combine the hexadecimal<br />

digits to obtain the character with which to replace the entire sequence. For<br />

example, in the ASCII character set, the letter “A” has the value 0x41, which could be<br />

encoded as “%41”.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Evaluating URL Encodings | 99

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!