~ubuntu-branches/ubuntu/utopic/mtbl/utopic-proposed

« back to all changes in this revision

Viewing changes to librsf/print_string.h

  • Committer: Package Import Robot
  • Author(s): Robert S. Edmonds
  • Date: 2014-01-21 16:30:22 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20140121163022-g1077ma2csn1gne8
Tags: 0.4-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 2012 by Internet Systems Consortium, Inc. ("ISC")
3
 
 *
4
 
 * Permission to use, copy, modify, and/or distribute this software for any
5
 
 * purpose with or without fee is hereby granted, provided that the above
6
 
 * copyright notice and this permission notice appear in all copies.
7
 
 *
8
 
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
9
 
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
 
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
11
 
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
 
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
 
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14
 
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
 
 */
16
 
 
17
 
#ifndef RSF_PRINT_STRING_H
18
 
#define RSF_PRINT_STRING_H
19
 
 
20
 
#include <ctype.h>
21
 
#include <stdio.h>
22
 
#include <stdint.h>
23
 
 
24
 
static inline void
25
 
print_string(const void *data, size_t len, FILE *out)
26
 
{
27
 
        uint8_t *str = (uint8_t *) data;
28
 
        fputc('"', out);
29
 
        while (len-- != 0) {
30
 
                unsigned c = *(str++);
31
 
                if (isprint(c)) {
32
 
                        if (c == '"')
33
 
                                fputs("\\\"", out);
34
 
                        else
35
 
                                fputc(c, out);
36
 
                } else {
37
 
                        fprintf(out, "\\x%02x", c);
38
 
                }
39
 
        }
40
 
        fputc('"', out);
41
 
}
42
 
 
43
 
#endif /* RSF_PRINT_STRING_H */