1 // 2 // srecord - Manipulate EPROM load files 3 // Copyright (C) 2013 Peter Miller 4 // 5 // This program is free software; you can redistribute it and/or modify it 6 // under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation; either version 3 of the License, or (at 8 // your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 // 18 19 #include <srecord/string.h> 20 21 22 std::string string_quote_c(const std::string & arg)23srecord::string_quote_c(const std::string &arg) 24 { 25 std::string result; 26 result += '"'; 27 const char *cp = arg.c_str(); 28 for (;;) 29 { 30 unsigned char c = *cp++; 31 switch (c) 32 { 33 case '\0': 34 result += '"'; 35 return result; 36 37 case '\t': 38 result += "\\t"; 39 break; 40 41 case '\n': 42 result += "\\n"; 43 break; 44 45 case '\r': 46 result += "\\r"; 47 break; 48 49 case '\f': 50 result += "\\f"; 51 break; 52 53 case '\\': 54 result += "\\\\"; 55 break; 56 57 default: 58 if (isprint((unsigned char)c)) 59 result += c; 60 else 61 { 62 result += '\\'; 63 result += '0' + ((c >> 6) & 3); 64 result += '0' + ((c >> 3) & 7); 65 result += '0' + (c & 7); 66 } 67 break; 68 } 69 } 70 } 71 72 73 // vim: set ts=8 sw=4 et : 74