Pregunta

In Pascal Lazarus/Delphi, we have a function QuotedStr() that wraps any string within single quotes.

Here's an example of my current C++ code:

//I need to quote tblCustomers
pqxx::result r = txn.exec( "Select * from \"tblCustomers\" "); 

Another one:

//I need to quote cCustomerName
std::cout << "Name: " << r[a]["\"cCustomerName\""];

Similar to the above, I have to frequently double-quote strings. Typing this in is kind of slowing me down. Is there a standard function I can use for this?

BTW, I develop using Ubuntu/Windows with Code::Blocks. The technique used must be compatible across both platforms. If there's no function, this means that I must write one.

¿Fue útil?

Solución 3

String str = "tblCustomers";
str = "'" + str + "'";

See more options here

Otros consejos

C++14 added std::quoted which does exactly that, and more actually: it takes care of escaping quotes and backslashes in output streams, and of unescaping them in input streams. It is efficient, in that it does not create a new string, it's really a IO manipulator. (So you don't get a string, as you'd like.)

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
  std::string in = "\\Hello \"Wörld\"\\\n";

  std::stringstream ss;
  ss << std::quoted(in);
  std::string out;
  ss >> std::quoted(out);
  std::cout << '{' << in << "}\n"
            << '{' << ss.str() << "}\n"
            << '{' << out << "}\n";
}

gives

{\Hello "Wörld"\
}
{"\\Hello \"Wörld\"\\
"}
{\Hello "Wörld"\
}

As described in its proposal, it was really designed for round-tripping of strings.

Using C++11 you can create user defined literals like this:

#include <iostream>
#include <string>
#include <cstddef>

// Define user defined literal "_quoted" operator.
std::string operator"" _quoted(const char* text, std::size_t len) {
    return "\"" + std::string(text, len) + "\"";
}

int main() {
    std::cout << "tblCustomers"_quoted << std::endl;
    std::cout << "cCustomerName"_quoted << std::endl;
}

Output:

"tblCustomers"
"cCustomerName"

You can even define the operator with a shorter name if you want, e.g.:

std::string operator"" _q(const char* text, std::size_t len) { /* ... */ }
// ...
std::cout << "tblCustomers"_q << std::endl;

More info on user-defined literals

No standard function, unless you count std::basic_string::operator+(), but writing it is trivial.

I'm somewhat confused by what's slowing you down - quoted( "cCustomerName" ) is more characters, no? :>

You could use your own placeholder character to stand for the quote, some ASCII symbol that will never be used, and replace it with " just before you output the strings.

#include <iostream>
#include <string>

struct quoted
{
    const char * _text;
    quoted( const char * text ) : _text(text) {}

    operator std::string () const
    {
        std::string quotedStr = "\"";
        quotedStr += _text;
        quotedStr += "\"";
        return quotedStr;
    }
};

std::ostream & operator<< ( std::ostream & ostr, const quoted & q )
{
    ostr << "\"" << q._text << "\"";
    return ostr;
}

int main ( int argc, char * argv[] )
{
    std::string strq = quoted( "tblCustomers" );
    std::cout << strq << std::endl;

    std::cout << quoted( "cCustomerName" ) << std::endl;
    return 0;
}

With this you get what you want.

What about using some C function and backslash to escape the quotes? Like sprintf_s:

#define BUF_SIZE 100
void execute_prog() {

  //Strings that will be predicted by quotes 
  string param1 = "C:\\users\\foo\\input file.txt", string param2 = "output.txt";

  //Char array with any buffer size 
  char command[BUF_SIZE];

  //Concating my prog call in the C string.
  //sprintf_s requires a buffer size for security reasons
  sprintf_s(command, BUF_SIZE, "program.exe  \"%s\" \"%s\"", param1.c_str(), 
    param2.c_str());

  system(command);

}

Resulting string is:

program.exe "C:\users\foo\input file.txt" "output.txt"

Here is the documentation.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top