00001 /** 00002 * @file ccn/charbuf.h 00003 * 00004 * Expandable character buffer for counted sequences of arbitrary octets. 00005 * 00006 * Part of the CCNx C Library. 00007 * 00008 * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc. 00009 * 00010 * This library is free software; you can redistribute it and/or modify it 00011 * under the terms of the GNU Lesser General Public License version 2.1 00012 * as published by the Free Software Foundation. 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. You should have received 00017 * a copy of the GNU Lesser General Public License along with this library; 00018 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, 00019 * Fifth Floor, Boston, MA 02110-1301 USA. 00020 */ 00021 00022 #ifndef CCN_CHARBUF_DEFINED 00023 #define CCN_CHARBUF_DEFINED 00024 00025 #include <stddef.h> 00026 #include <time.h> 00027 00028 struct ccn_charbuf { 00029 size_t length; 00030 size_t limit; 00031 unsigned char *buf; 00032 }; 00033 00034 /* 00035 * ccn_charbuf_create: allocate a new charbuf 00036 * ccn_charbuf_create_n: allocate a new charbuf with a preallocated but 00037 * uninitialized buffer 00038 * ccn_charbuf_destroy: destroy a charbuf 00039 */ 00040 struct ccn_charbuf *ccn_charbuf_create(void); 00041 struct ccn_charbuf *ccn_charbuf_create_n(size_t n); 00042 void ccn_charbuf_destroy(struct ccn_charbuf **cbp); 00043 00044 /* 00045 * ccn_charbuf_reserve: reserve some space in the buffer 00046 * Grows c->buf if needed and returns a pointer to the new region. 00047 * Does not modify c->length 00048 */ 00049 unsigned char *ccn_charbuf_reserve(struct ccn_charbuf *c, size_t n); 00050 00051 /* 00052 * ccn_charbuf_reset: reset to empty for reuse 00053 * Sets c->length to 0 00054 */ 00055 void ccn_charbuf_reset(struct ccn_charbuf *c); 00056 00057 /* 00058 * ccn_charbuf_append: append character content 00059 */ 00060 int ccn_charbuf_append(struct ccn_charbuf *c, const void *p, size_t n); 00061 00062 /* 00063 * ccn_charbuf_append: append n bytes of val 00064 * The n low-order bytes are appended in network byte order (big-endian) 00065 */ 00066 int ccn_charbuf_append_value(struct ccn_charbuf *c, unsigned val, unsigned n); 00067 00068 00069 /* 00070 * ccn_charbuf_append_charbuf: append content from another charbuf 00071 */ 00072 int ccn_charbuf_append_charbuf(struct ccn_charbuf *c, const struct ccn_charbuf *i); 00073 00074 /* 00075 * ccn_charbuf_append: append a string 00076 * Sometimes you have a null-terminated string in hand... 00077 */ 00078 int ccn_charbuf_append_string(struct ccn_charbuf *c, const char *s); 00079 00080 /* 00081 * ccn_charbuf_putf: formatting output 00082 * Use this in preference to snprintf to simplify bookkeeping. 00083 */ 00084 int ccn_charbuf_putf(struct ccn_charbuf *c, const char *fmt, ...); 00085 00086 /* 00087 * ccn_charbuf_append_datetime: append a date/time string 00088 * Appends a dateTime string in canonical form according to 00089 * http://www.w3.org/TR/xmlschema-2/ 00090 * Return value is 0, or -1 for error. 00091 * example: 2008-07-22T17:33:14.109Z 00092 */ 00093 int ccn_charbuf_append_datetime(struct ccn_charbuf *c, time_t secs, int nsecs); 00094 00095 /* 00096 * ccn_charbuf_append_datetime_now: append a date/time string 00097 * Appends a dateTime string representing the current date and time 00098 * in canonical form according to 00099 * http://www.w3.org/TR/xmlschema-2/ 00100 * precision, a non-negative number, indicates the maximum number 00101 * of fractional digits in the seconds. Only values 0..6 have 00102 * any effect, at this times, since the gettimeofday() function 00103 * is defined to return microsecond resolution. 00104 * Return value is 0, or -1 for error. 00105 * example: 2008-07-22T17:33:14.109Z 00106 */ 00107 #define CCN_DATETIME_PRECISION_USEC 6 00108 #define CCN_DATETIME_PRECISION_MAX 6 00109 int ccn_charbuf_append_datetime_now(struct ccn_charbuf *c, int precision); 00110 00111 /* 00112 * ccn_charbuf_as_string: view charbuf contents as a string 00113 * This assures that c->buf has a null termination, and simply 00114 * returns the pointer into the buffer. If the result needs to 00115 * persist beyond the next operation on c, the caller is 00116 * responsible for copying it. 00117 */ 00118 char *ccn_charbuf_as_string(struct ccn_charbuf *c); 00119 00120 #endif