Merge "Add ability to quote responses as needed."

This commit is contained in:
Robert Greenwalt 2012-04-24 09:47:35 -07:00 committed by Android (Google) Code Review
commit e36df300dc
2 changed files with 26 additions and 0 deletions

View file

@ -64,6 +64,9 @@ public:
void incRef();
bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
// return a new string in quotes with '\\' and '\"' escaped for "my arg" transmissions
static char *quoteArg(const char *arg);
private:
// Send null-terminated C strings
int sendMsg(const char *msg);

View file

@ -107,6 +107,29 @@ int SocketClient::sendCode(int code) {
return sendData(buf, sizeof(buf));
}
char *SocketClient::quoteArg(const char *arg) {
int len = strlen(arg);
char *result = (char *)malloc(len * 2 + 3);
char *current = result;
const char *end = arg + len;
*(current++) = '"';
while (arg < end) {
switch (*arg) {
case '\\':
case '"':
*(current++) = '\\'; // fallthrough
default:
*(current++) = *(arg++);
}
}
*(current++) = '"';
*(current++) = '\0';
result = (char *)realloc(result, current-result);
return result;
}
int SocketClient::sendMsg(const char *msg) {
if (mSocket < 0) {
errno = EHOSTUNREACH;