0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Escaper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 #include "Common/Compat.h"
27 
28 #include <boost/shared_array.hpp>
29 
30 #include "Escaper.h"
31 
32 using namespace Hypertable;
33 
34 void Hypertable::escape(String &str, const String &escape_chars) {
35  if (str.length() > 0 && escape_chars.length() > 0) {
36  bool escaped[256];
37  boost::shared_array<char> escaped_str(new char [(2 * str.length()) + 1]);
38  char *dst = escaped_str.get();
39 
40  memset(escaped, 0, 256 * sizeof(bool));
41  for (size_t i = 0; i < escape_chars.length(); i++)
42  escaped[(size_t)escape_chars[i]] = true;
43  escaped[(size_t)'\\'] = true;
44 
45  const char *src = str.c_str();
46 
47  while (*src) {
48  if (escaped[(size_t)*src])
49  *dst++ = '\\';
50  *dst++ = *src++;
51  }
52  *dst = 0;
53  str = String(escaped_str.get());
54  }
55 }
56 
58  if (str.length() > 0) {
59  boost::shared_array<char> escaped_str(new char [str.length() + 1]);
60  char *dst = escaped_str.get();
61  for (const char *src = str.c_str(); *src; src++) {
62  if (*src == '\\' && *(src + 1) == '\\')
63  *dst++ = *src++;
64  else if (*src != '\\')
65  *dst++ = *src;
66  }
67  *dst = 0;
68  str = String(escaped_str.get());
69  }
70 }
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
void escape(String &str, const String &escape_chars)
Escapes a string.
Definition: Escaper.cc:34
Compatibility Macros for C/C++.
Hypertable definitions
void unescape(String &str)
Unescapes a string.
Definition: Escaper.cc:57
Escape/Unescape routines for strings.