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