18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

BUILDING A COMMAND-LINE C++ APPLICATION<br />

#include <br />

#include <br />

// Extract the shapefile name from the full path of the file.<br />

HRESULT GetFileFromFullPath(const char* inFullPath, BSTR* outFileName);<br />

// Remove the filename from the full path and return the directory.<br />

HRESULT GetParentDirFromFullPath(const char* inFullPath,<br />

BSTR* outFilePath);<br />

#endif // __PATHUTILITIES_ESRISCENARIO_H__<br />

4. Implement the path utility functions in the new file PathUtilities.cpp.<br />

#include "PathUtilities.h"<br />

// Function to remove the filename from the full path and return the<br />

// path to the directory. Caller is responsible for freeing the memory<br />

// in outFilePath (or pass in a CComBSTR, which has been cast to a BSTR<br />

// to let the CComBSTR handle memory management).<br />

HRESULT GetParentDirFromFullPath(const char* inFullPath,<br />

BSTR* outFilePath)<br />

{<br />

if (!inFullPath || !outFilePath)<br />

return E_POINTER;<br />

// Initialize output.<br />

*outFilePath = 0;<br />

const char *pathEnd = strrchr(inFullPath, '/');<br />

if (pathEnd == 0)<br />

pathEnd = strrchr(inFullPath, '\\');<br />

// UNIX<br />

// Windows<br />

if (pathEnd == 0)<br />

return E_FAIL;<br />

int size = strlen(inFullPath) - strlen(pathEnd);<br />

char *tmp = new char[size+1];<br />

strncpy(tmp, inFullPath, size);<br />

*(tmp+size) = '\0';<br />

CComBSTR bsTmp (tmp);<br />

delete[] tmp;<br />

if (!bsTmp)<br />

return E_OUTOFMEMORY;<br />

*outFilePath = bsTmp.Detach();<br />

}<br />

return S_OK;<br />

// Function to extract the file (or directory) name from the full path<br />

// of the file. Caller is responsible for freeing the memory in<br />

432 • <strong>ArcGIS</strong> <strong>Engine</strong> <strong>Developer</strong> <strong>Guide</strong>

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

Saved successfully!

Ooh no, something went wrong!