Ilya Moiseenko | c115fba | 2011-08-01 10:53:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ccn_charbuf.h |
| 3 | * Abstraction |
| 4 | * |
| 5 | * Created by Ilya on 7/29/11. |
| 6 | * Copyright 2011 __MyCompanyName__. All rights reserved. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * @file ccn/charbuf.h |
| 12 | * |
| 13 | * Expandable character buffer for counted sequences of arbitrary octets. |
| 14 | * |
| 15 | * Part of the CCNx C Library. |
| 16 | * |
| 17 | * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc. |
| 18 | * |
| 19 | * This library is free software; you can redistribute it and/or modify it |
| 20 | * under the terms of the GNU Lesser General Public License version 2.1 |
| 21 | * as published by the Free Software Foundation. |
| 22 | * This library is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | * Lesser General Public License for more details. You should have received |
| 26 | * a copy of the GNU Lesser General Public License along with this library; |
| 27 | * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, |
| 28 | * Fifth Floor, Boston, MA 02110-1301 USA. |
| 29 | */ |
| 30 | |
| 31 | #ifndef CCN_CHARBUF_DEFINED |
| 32 | #define CCN_CHARBUF_DEFINED |
| 33 | |
| 34 | #include <stddef.h> |
| 35 | #include <time.h> |
| 36 | |
| 37 | struct ccn_charbuf { |
| 38 | size_t length; |
| 39 | size_t limit; |
| 40 | unsigned char *buf; |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * ccn_charbuf_create: allocate a new charbuf |
| 45 | * ccn_charbuf_destroy: destroy a charbuf |
| 46 | */ |
| 47 | struct ccn_charbuf *ccn_charbuf_create(void); |
| 48 | void ccn_charbuf_destroy(struct ccn_charbuf **cbp); |
| 49 | |
| 50 | /* |
| 51 | * ccn_charbuf_reserve: reserve some space in the buffer |
| 52 | * Grows c->buf if needed and returns a pointer to the new region. |
| 53 | * Does not modify c->length |
| 54 | */ |
| 55 | unsigned char *ccn_charbuf_reserve(struct ccn_charbuf *c, size_t n); |
| 56 | |
| 57 | /* |
| 58 | * ccn_charbuf_reset: reset to empty for reuse |
| 59 | * Sets c->length to 0 |
| 60 | */ |
| 61 | void ccn_charbuf_reset(struct ccn_charbuf *c); |
| 62 | |
| 63 | /* |
| 64 | * ccn_charbuf_append: append character content |
| 65 | */ |
| 66 | int ccn_charbuf_append(struct ccn_charbuf *c, const void *p, size_t n); |
| 67 | |
| 68 | /* |
| 69 | * ccn_charbuf_append: append n bytes of val |
| 70 | * The n low-order bytes are appended in network byte order (big-endian) |
| 71 | */ |
| 72 | int ccn_charbuf_append_value(struct ccn_charbuf *c, unsigned val, unsigned n); |
| 73 | |
| 74 | |
| 75 | /* |
| 76 | * ccn_charbuf_append_charbuf: append content from another charbuf |
| 77 | */ |
| 78 | int ccn_charbuf_append_charbuf(struct ccn_charbuf *c, const struct ccn_charbuf *i); |
| 79 | |
| 80 | /* |
| 81 | * ccn_charbuf_append: append a string |
| 82 | * Sometimes you have a null-terminated string in hand... |
| 83 | */ |
| 84 | int ccn_charbuf_append_string(struct ccn_charbuf *c, const char *s); |
| 85 | |
| 86 | /* |
| 87 | * ccn_charbuf_putf: formatting output |
| 88 | * Use this in preference to snprintf to simplify bookkeeping. |
| 89 | */ |
| 90 | int ccn_charbuf_putf(struct ccn_charbuf *c, const char *fmt, ...); |
| 91 | |
| 92 | /* |
| 93 | * ccn_charbuf_append_datetime: append a date/time string |
| 94 | * Appends a dateTime string in canonical form according to |
| 95 | * http://www.w3.org/TR/xmlschema-2/ |
| 96 | * Return value is 0, or -1 for error. |
| 97 | * example: 2008-07-22T17:33:14.109Z |
| 98 | */ |
| 99 | int ccn_charbuf_append_datetime(struct ccn_charbuf *c, time_t secs, int nsecs); |
| 100 | |
| 101 | /* |
| 102 | * ccn_charbuf_append_datetime_now: append a date/time string |
| 103 | * Appends a dateTime string representing the current date and time |
| 104 | * in canonical form according to |
| 105 | * http://www.w3.org/TR/xmlschema-2/ |
| 106 | * precision, a non-negative number, indicates the maximum number |
| 107 | * of fractional digits in the seconds. Only values 0..6 have |
| 108 | * any effect, at this times, since the gettimeofday() function |
| 109 | * is defined to return microsecond resolution. |
| 110 | * Return value is 0, or -1 for error. |
| 111 | * example: 2008-07-22T17:33:14.109Z |
| 112 | */ |
| 113 | #define CCN_DATETIME_PRECISION_USEC 6 |
| 114 | #define CCN_DATETIME_PRECISION_MAX 6 |
| 115 | int ccn_charbuf_append_datetime_now(struct ccn_charbuf *c, int precision); |
| 116 | |
| 117 | /* |
| 118 | * ccn_charbuf_as_string: view charbuf contents as a string |
| 119 | * This assures that c->buf has a null termination, and simply |
| 120 | * returns the pointer into the buffer. If the result needs to |
| 121 | * persist beyond the next operation on c, the caller is |
| 122 | * responsible for copying it. |
| 123 | */ |
| 124 | char *ccn_charbuf_as_string(struct ccn_charbuf *c); |
| 125 | |
| 126 | #endif |