JayRJoshi | 17654da | 2016-03-23 19:04:15 +0530 | [diff] [blame] | 1 | #include "tests.h" |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | |
| 6 | /* |
| 7 | * Based on string_quote() from util.c. |
| 8 | * Assumes instr is NUL-terminated. |
| 9 | */ |
| 10 | |
| 11 | void |
| 12 | print_quoted_string(const char *instr) |
| 13 | { |
| 14 | const unsigned char *str = (const unsigned char*) instr; |
| 15 | int c; |
| 16 | |
| 17 | while ((c = *(str++))) { |
| 18 | switch (c) { |
| 19 | case '\"': |
| 20 | printf("\\\""); |
| 21 | break; |
| 22 | case '\\': |
| 23 | printf("\\\\"); |
| 24 | break; |
| 25 | case '\f': |
| 26 | printf("\\f"); |
| 27 | break; |
| 28 | case '\n': |
| 29 | printf("\\n"); |
| 30 | break; |
| 31 | case '\r': |
| 32 | printf("\\r"); |
| 33 | break; |
| 34 | case '\t': |
| 35 | printf("\\t"); |
| 36 | break; |
| 37 | case '\v': |
| 38 | printf("\\v"); |
| 39 | break; |
| 40 | default: |
| 41 | if (c >= ' ' && c <= 0x7e) |
| 42 | putchar(c); |
| 43 | else { |
| 44 | putchar('\\'); |
| 45 | |
| 46 | char c1 = '0' + (c & 0x7); |
| 47 | char c2 = '0' + ((c >> 3) & 0x7); |
| 48 | char c3 = '0' + (c >> 6); |
| 49 | |
| 50 | if (*str >= '0' && *str <= '9') { |
| 51 | /* Print \octal */ |
| 52 | putchar(c3); |
| 53 | putchar(c2); |
| 54 | } else { |
| 55 | /* Print \[[o]o]o */ |
| 56 | if (c3 != '0') |
| 57 | putchar(c3); |
| 58 | if (c3 != '0' || c2 != '0') |
| 59 | putchar(c2); |
| 60 | } |
| 61 | putchar(c1); |
| 62 | } |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | } |