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_destroy: destroy a charbuf 00037 */ 00038 struct ccn_charbuf *ccn_charbuf_create(void); 00039 void ccn_charbuf_destroy(struct ccn_charbuf **cbp); 00040 00041 /* 00042 * ccn_charbuf_reserve: reserve some space in the buffer 00043 * Grows c->buf if needed and returns a pointer to the new region. 00044 * Does not modify c->length 00045 */ 00046 unsigned char *ccn_charbuf_reserve(struct ccn_charbuf *c, size_t n); 00047 00048 /* 00049 * ccn_charbuf_reset: reset to empty for reuse 00050 * Sets c->length to 0 00051 */ 00052 void ccn_charbuf_reset(struct ccn_charbuf *c); 00053 00054 /* 00055 * ccn_charbuf_append: append character content 00056 */ 00057 int ccn_charbuf_append(struct ccn_charbuf *c, const void *p, size_t n); 00058 00059 /* 00060 * ccn_charbuf_append: append n bytes of val 00061 * The n low-order bytes are appended in network byte order (big-endian) 00062 */ 00063 int ccn_charbuf_append_value(struct ccn_charbuf *c, unsigned val, unsigned n); 00064 00065 00066 /* 00067 * ccn_charbuf_append_charbuf: append content from another charbuf 00068 */ 00069 int ccn_charbuf_append_charbuf(struct ccn_charbuf *c, const struct ccn_charbuf *i); 00070 00071 /* 00072 * ccn_charbuf_append: append a string 00073 * Sometimes you have a null-terminated string in hand... 00074 */ 00075 int ccn_charbuf_append_string(struct ccn_charbuf *c, const char *s); 00076 00077 /* 00078 * ccn_charbuf_putf: formatting output 00079 * Use this in preference to snprintf to simplify bookkeeping. 00080 */ 00081 int ccn_charbuf_putf(struct ccn_charbuf *c, const char *fmt, ...); 00082 00083 /* 00084 * ccn_charbuf_append_datetime: append a date/time string 00085 * Appends a dateTime string in canonical form according to 00086 * http://www.w3.org/TR/xmlschema-2/ 00087 * Return value is 0, or -1 for error. 00088 * example: 2008-07-22T17:33:14.109Z 00089 */ 00090 int ccn_charbuf_append_datetime(struct ccn_charbuf *c, time_t secs, int nsecs); 00091 00092 /* 00093 * ccn_charbuf_append_datetime_now: append a date/time string 00094 * Appends a dateTime string representing the current date and time 00095 * in canonical form according to 00096 * http://www.w3.org/TR/xmlschema-2/ 00097 * precision, a non-negative number, indicates the maximum number 00098 * of fractional digits in the seconds. Only values 0..6 have 00099 * any effect, at this times, since the gettimeofday() function 00100 * is defined to return microsecond resolution. 00101 * Return value is 0, or -1 for error. 00102 * example: 2008-07-22T17:33:14.109Z 00103 */ 00104 #define CCN_DATETIME_PRECISION_USEC 6 00105 #define CCN_DATETIME_PRECISION_MAX 6 00106 int ccn_charbuf_append_datetime_now(struct ccn_charbuf *c, int precision); 00107 00108 /* 00109 * ccn_charbuf_as_string: view charbuf contents as a string 00110 * This assures that c->buf has a null termination, and simply 00111 * returns the pointer into the buffer. If the result needs to 00112 * persist beyond the next operation on c, the caller is 00113 * responsible for copying it. 00114 */ 00115 char *ccn_charbuf_as_string(struct ccn_charbuf *c); 00116 00117 #endif