2

This is probably a novice question but I am not very good at C++ and still an early beginner. MY question is how do I strip bk any trailing backslashes from a char:

extern "C" UINT __stdcall DeleteTrailingBackslash(MSIHANDLE hInstall)
{
    HRESULT hr = S_OK;
    UINT er = ERROR_SUCCESS;
    LPWSTR szValueBuf = NULL;
    char szInstallPath[MAX_PATH];

    hr = WcaInitialize(hInstall, "DeleteTrailingBackslash");
    ExitOnFailure(hr, "Failed to initialize");

    WcaLog(LOGMSG_STANDARD, "Initialized.");

    hr = WcaGetProperty(L"INSTALLLOCATION",&szValueBuf);
    ExitOnFailure(hr, "failed to get Install Location");

    wcstombs(szInstallPath, szValueBuf, 260);

    // I would like to strip back the trailing backslashes
    // and re add the property to my MSI
    hr = MsiSetProperty(hInstall, "INSTALLLOCATION",  szInstallPath);
    ExitOnFailure(hr, "failed to set the install location");

    LExit:
    er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
    return WcaFinalize(er);
}

szInstallPath could have either none, one or two backslashes, I need to remove backslashes if there are any. Can anyone point me in the right direction please for good website tutorials or anything?

Thanks

Natalie Carr
  • 3,707
  • 3
  • 34
  • 68
  • @0A0D Thanks, from reading I see that is using Strings. I want to use a char, Also I only want to remove the ending backslashes(there will be some in the middle) That is why I posted as I am unsure how to achieve this. – Natalie Carr Feb 14 '13 at 14:53
  • A `char` is just one byte. Can you please clarify? –  Feb 14 '13 at 14:55
  • @0A0D I have added my function, maybe this will help. I am using C++ to write a DLL to interact with my MSI. Thanks – Natalie Carr Feb 14 '13 at 14:57
  • You can always take your char array and convert it into a string, then convert back.. –  Feb 14 '13 at 15:01
  • @0a0d I could do that no problem, but I am still lost on how I can check if there are any back slashes at the end. Everything I seem to be finding would delete all occurrences. – Natalie Carr Feb 14 '13 at 15:12
  • Why not just iterate over the char array and check for the existence of such characters? –  Feb 14 '13 at 15:13
  • That's a lot of `wix` code unrelated to the question. – Peter Wood Feb 15 '13 at 11:05

2 Answers2

6

Here's a C++ solution using std::string

...

std::string stdInstallPath = szInstallPath;

while(stdInstallPath.rbegin() != stdInstallPath.rend() && *stdInstallPath.rbegin() == '\\')
    stdInstallPath.pop_back();

hr = MsiSetProperty(hInstall, "INSTALLLOCATION",  stdInstallPath.c_str());

...

This looks at the last character in the string and erases it if it is a backslash, and continues until the last character is not a backslash.

EylM
  • 5,967
  • 2
  • 16
  • 28
badgerr
  • 7,802
  • 2
  • 28
  • 43
2

Something like this might suffice:

const size_t len = strlen(szInstallPath);
if( szInstallPath[len-1] == '\\' || szInstallPath[len-1] == '/' )
{
    // Terminate the string earlier
    szInstallPath[len-1] = 0;
}
metal
  • 6,202
  • 1
  • 34
  • 49