2

I'm trying to execute CMD commands which I'm getting deserializing a JSON message.

When I deserialize message, I store the value in a std::string variable whose value is "tzutil /s \"Romance Standard Time_dstoff\"":

I would like to remove backslash characters ('\') when I receive commands with floating quotes parameters (e.g."tzutil /s "Romance Standard Time_dstoff"").

std::string command = "tzutil /s \"Romance Standard Time_dstoff\""; //Problem
system(command.c_str());

Are there any way to do it?

I will appreciate any kind of help.

Alberto Bricio
  • 402
  • 1
  • 6
  • 22
  • You mean to replace all the occurrences of \ character occurrences to replaced by something else or you want to replace them with quotes? Your question suggests the latter while the example suggests the former. – Pratik Sampat Apr 09 '19 at 10:27
  • 1
    How are you seeing the backslash characters? In a print or in a debugger? You might find that they are not actually there ... – Fantastic Mr Fox Apr 09 '19 at 10:30
  • I pretend to replace/remove all the occurrences of '\' to have arguments with quotes. I have already updated my post. – Alberto Bricio Apr 09 '19 at 10:32
  • @FantasticMrFox I am seeing it in debug mode – Alberto Bricio Apr 09 '19 at 10:33
  • Note that `std::string command = "tzutil /s \"Romance Standard Time_dstoff\""` has no slashes in it. I understand that your real string does, but the example is incorrect. – HolyBlackCat Apr 09 '19 at 10:36
  • You need to loop the string character by character. Put a flag for double qoutes and put it in a stack. Put a condition statement like; `for(auto &c : command){ if(c == '\'){ ..neglect } else{ newStr.pushback(c); }}` – Master James Apr 09 '19 at 10:39
  • Probably interesting to know: [using \ in a string as literal instead of an escape](https://stackoverflow.com/questions/12103445/using-in-a-string-as-literal-instead-of-an-escape) – user1810087 Apr 09 '19 at 10:39
  • I have been investigating the problem, and seems that `system(...)` call are considering the escape character '\' of quotes. Are there any way to filter this properly? – Alberto Bricio Apr 10 '19 at 08:16
  • Has one of these solutions worked for you? Remember to mark as answer if so. If not, please expand on your question or comment on answers why they don't work for your situation. – TheBeardedQuack May 03 '19 at 07:59

4 Answers4

2

If you wish to remove all occurrences the character then you may use

#include <algorithm>

str.erase(std::remove(str.begin(), str.end(), char_to_remove), str.end());

If you wish to replace them with another character then try

#include <algorithm>

std::replace(str.begin(), str.end(), old_char, new_char); 
Pratik Sampat
  • 330
  • 1
  • 12
1

Although the source code of your program does contain, the string represented by the literal doesn't contain any backslashes, as demonstrated by the following example:

std::string command = "tzutil /s \"Romance Standard Time_dstoff\""; //Problem
std::cout << command;

// output:
tzutil /s "Romance Standard Time_dstoff"

As such, there is nothing to remove from the string.

Backslash is an escape character. \" is an escape sequence that represents a single character, the double quote. It is a way to type a double quote character within a string literal without that quote being interpreted as the end of the string instead.


To write a backslash into a string literal, you can by escaping it with a backslash. The following string does contain backslashes: "tzutil /s \\"Romance Standard Time_dstoff\\"". In this case, removing all backslashes can be done like so:

command.erase(std::remove(command.begin(), command.end(), '\\'), command.end());

However, simply removing all instances of the character might not be sensible. If your string contains escape sequences, what you probably should want to do instead is to unescape them. This is somewhat more complicated. You wouldn't want to remove all backslashes, but instead replace \" with " and \\ with \ and \n with a newline and so on.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

Here is a function I made in C++ for one of my own projects for replacing sub-strings.

std::string
Replace(std::string str,
    const std::string& oldStr,
    const std::string& newStr)
{

    size_t index = str.find(oldStr);
    while(index != str.npos)
    {
        str = str.substr(0, index) +
            newStr + str.substr(index + oldStr.size());
        index = str.find(oldStr, index + newStr.size());
    }
    return str;
}

int main(){
    std::string command = GetCommandFromJsonSource();
    command = Replace(command, "\\\"", "\""); // unescape only double quotes
}
TheBeardedQuack
  • 449
  • 4
  • 15
0

You can use std::quoted to convert from and to a string literal.

#include <iomanip> // -> std::quoted
#include <iostream>
#include <sstream>

int main() {
    std::istringstream s("\"Hello world\\n\"");
    std::string hello;
    s >> std::quoted(hello);
    std::cout << std::quoted(s) << ": " << s;
}
cmdLP
  • 1,658
  • 9
  • 19