1 /* 2 * Copyright 2015, Rene Gollent, rene@gollent.com. 3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 4 * Distributed under the terms of the MIT License. 5 */ 6 #include "StringValueFormatter.h" 7 8 #include <stdio.h> 9 10 #include <String.h> 11 12 #include "Value.h" 13 14 15 StringValueFormatter::StringValueFormatter() 16 : 17 ValueFormatter() 18 { 19 } 20 21 22 StringValueFormatter::~StringValueFormatter() 23 { 24 } 25 26 27 status_t 28 StringValueFormatter::FormatValue(Value* value, BString& _output) 29 { 30 _output = "\""; 31 BString tempString; 32 if (!value->ToString(tempString)) 33 return B_BAD_VALUE; 34 35 for (int32 i = 0; i < tempString.Length(); i++) { 36 if (tempString[i] < 31) { 37 switch (tempString[i]) { 38 case '\0': 39 _output << "\\0"; 40 break; 41 case '\a': 42 _output << "\\a"; 43 break; 44 case '\b': 45 _output << "\\b"; 46 break; 47 case '\t': 48 _output << "\\t"; 49 break; 50 case '\r': 51 _output << "\\r"; 52 break; 53 case '\n': 54 _output << "\\n"; 55 break; 56 case '\f': 57 _output << "\\f"; 58 break; 59 default: 60 { 61 char buffer[5]; 62 snprintf(buffer, sizeof(buffer), "\\x%x", 63 tempString.String()[i]); 64 _output << buffer; 65 break; 66 } 67 } 68 } else if (tempString[i] == '\"') 69 _output << "\\\""; 70 else 71 _output << tempString[i]; 72 } 73 74 _output += "\""; 75 76 return B_OK; 77 } 78