libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2009, 2011-2013 Peter Miller 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU Lesser General Public License as 00007 * published by the Free Software Foundation; either version 3 of the 00008 * License, or (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public License 00016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 #include <libexplain/string_buffer.h> 00020 00021 00022 void 00023 explain_string_buffer_puts_quoted_n(explain_string_buffer_t *sb, 00024 const char *s, size_t n) 00025 { 00026 if (!s) 00027 { 00028 explain_string_buffer_puts(sb, "NULL"); 00029 return; 00030 } 00031 explain_string_buffer_putc(sb, '"'); 00032 while (n > 0) 00033 { 00034 unsigned char c; 00035 00036 c = *s++; 00037 --n; 00038 switch (c) 00039 { 00040 case '\0': 00041 explain_string_buffer_putc(sb, '"'); 00042 return; 00043 00044 case '?': 00045 /* 00046 * Watch out for C string contents that could look like a 00047 * trigraph, the second question mark will need to be quoted. 00048 */ 00049 explain_string_buffer_putc(sb, '?'); 00050 if (n >= 2 && s[0] == '?') 00051 { 00052 switch (s[1]) 00053 { 00054 case '!': 00055 case '\'': 00056 case '(': 00057 case ')': 00058 case '-': 00059 case '/': 00060 case '<': 00061 case '=': 00062 case '>': 00063 ++s; 00064 --n; 00065 explain_string_buffer_putc(sb, '\\'); 00066 explain_string_buffer_putc(sb, '?'); 00067 break; 00068 00069 default: 00070 /* not a trigraph */ 00071 break; 00072 } 00073 } 00074 break; 00075 00076 default: 00077 explain_string_buffer_putc_escaped(sb, c, '"'); 00078 break; 00079 } 00080 } 00081 explain_string_buffer_putc(sb, '"'); 00082 } 00083 00084 00085 void 00086 explain_string_buffer_putsu_quoted_n(explain_string_buffer_t *sb, 00087 const unsigned char *s, size_t n) 00088 { 00089 explain_string_buffer_puts_quoted_n(sb, (const char *)s, n); 00090 } 00091 00092 00093 /* vim: set ts=8 sw=4 et : */