blob: 3a0ba31ca5b7aacf03f4f3c09d0d4d10a699f3c0 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompson5d89cb92013-06-27 15:57:15 -07005 */
6
7#ifndef NDN_NAME_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_NAME_H
Jeff Thompson5d89cb92013-06-27 15:57:15 -07009
Jeff Thompson25b4e612013-10-10 16:03:24 -070010#include <ndn-cpp/c/common.h>
Jeff Thompson27cae532013-10-08 12:52:41 -070011#include "errors.h"
Jeff Thompson93034532013-10-08 11:52:43 -070012#include "util/blob.h"
13
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070014#ifdef __cplusplus
Jeff Thompson5d89cb92013-06-27 15:57:15 -070015extern "C" {
16#endif
17
Jeff Thompson2934a5b2013-07-03 17:45:53 -070018/**
19 * An ndn_NameComponent holds a pointer to the component value.
20 */
Jeff Thompsonb0fd64f2013-06-27 16:02:36 -070021struct ndn_NameComponent {
Jeff Thompson93034532013-10-08 11:52:43 -070022 struct ndn_Blob value; /**< A Blob with a pointer to the pre-allocated buffer for the component value */
Jeff Thompson5d89cb92013-06-27 15:57:15 -070023};
24
Jeff Thompson2934a5b2013-07-03 17:45:53 -070025/**
26 *
27 * @param self pointer to the ndn_NameComponent struct
28 * @param value the pre-allocated buffer for the component value
29 * @param valueLength the number of bytes in value
30 */
Jeff Thompson97223af2013-09-24 17:01:27 -070031static inline void ndn_NameComponent_initialize(struct ndn_NameComponent *self, uint8_t *value, size_t valueLength)
Jeff Thompsonb0fd64f2013-06-27 16:02:36 -070032{
Jeff Thompson93034532013-10-08 11:52:43 -070033 ndn_Blob_initialize(&self->value, value, valueLength);
Jeff Thompsonb0fd64f2013-06-27 16:02:36 -070034}
Jeff Thompson27cae532013-10-08 12:52:41 -070035
36/**
Jeff Thompson27cae532013-10-08 12:52:41 -070037 * Interpret the name component as a network-ordered number and return an integer.
38 * @param self A pointer to the ndn_NameComponent struct.
39 * @return The integer number.
40 */
41uint64_t ndn_NameComponent_toNumber(struct ndn_NameComponent *self);
42
43/**
Jeff Thompson27cae532013-10-08 12:52:41 -070044 * Interpret the name component as a network-ordered number with a marker and return an integer.
45 * @param self A pointer to the ndn_NameComponent struct.
46 * @param marker The required first byte of the component.
47 * @param result Return the integer number.
48 * @return 0 for success, or an error code if the first byte of the component does not equal the marker.
49 */
50ndn_Error ndn_NameComponent_toNumberWithMarker(struct ndn_NameComponent *self, uint8_t marker, uint64_t *result);
51
Jeff Thompson2934a5b2013-07-03 17:45:53 -070052/**
53 * An ndn_Name holds an array of ndn_NameComponent.
54 */
Jeff Thompson5d89cb92013-06-27 15:57:15 -070055struct ndn_Name {
Jeff Thompson08d56292013-06-27 18:20:44 -070056 struct ndn_NameComponent *components; /**< pointer to the array of components. */
Jeff Thompson97223af2013-09-24 17:01:27 -070057 size_t maxComponents; /**< the number of elements in the allocated components array */
58 size_t nComponents; /**< the number of components in the name */
Jeff Thompson5d89cb92013-06-27 15:57:15 -070059};
60
Jeff Thompson08d56292013-06-27 18:20:44 -070061/**
62 * Initialize an ndn_Name struct with the components array.
63 * @param self pointer to the ndn_Name struct
Jeff Thompson2934a5b2013-07-03 17:45:53 -070064 * @param components the pre-allocated array of ndn_NameComponent
Jeff Thompson08d56292013-06-27 18:20:44 -070065 * @param maxComponents the number of elements in the allocated components array
66 */
Jeff Thompson97223af2013-09-24 17:01:27 -070067static inline void ndn_Name_initialize(struct ndn_Name *self, struct ndn_NameComponent *components, size_t maxComponents)
Jeff Thompsonb0fd64f2013-06-27 16:02:36 -070068{
Jeff Thompson08d56292013-06-27 18:20:44 -070069 self->components = components;
70 self->maxComponents = maxComponents;
Jeff Thompsonb0fd64f2013-06-27 16:02:36 -070071 self->nComponents = 0;
72}
Jeff Thompson5d89cb92013-06-27 15:57:15 -070073
Jeff Thompsonf1ba9bd2013-08-12 17:43:08 -070074/**
75 * Return true if the N components of this name are the same as the first N components of the given name.
76 * @param self A pointer to the ndn_Name struct.
77 * @param name A pointer to the other name to match.
78 * @return 1 if this matches the given name, 0 otherwise. This always returns 1 if this name is empty.
79 */
80int ndn_Name_match(struct ndn_Name *self, struct ndn_Name *name);
81
Jeff Thompsone7ab9442013-12-10 19:34:06 -080082/**
83 * Append a component to this name with the bytes in the given array.
84 * @param self pointer to the ndn_Name struct.
85 * @param value The bytes of the component. This does not copy the bytes.
86 * @param valueLength The number of bytes in value.
87 * @return 0 for success, or an error code if there is no more room in the components array (nComponents is already maxComponents).
88 */
89ndn_Error ndn_Name_appendComponent(struct ndn_Name *self, uint8_t* value, size_t valueLength);
90
91/**
92 * Append a component to this name with the bytes in the given blob.
93 * @param self pointer to the ndn_Name struct.
94 * @param value An ndn_Blob with the bytes of the component. This does not copy the bytes.
95 * @return 0 for success, or an error code if there is no more room in the components array (nComponents is already maxComponents).
96 */
97static inline ndn_Error ndn_Name_appendBlob(struct ndn_Name *self, struct ndn_Blob *value)
98{
99 return ndn_Name_appendComponent(self, value->value, value->length);
100}
101
102/**
103 * Append a component to this name with the bytes in raw string value.
104 * @param self pointer to the ndn_Name struct.
105 * @param value The null-terminated string, treated as a byte array. This does not copy the bytes.
106 * @return 0 for success, or an error code if there is no more room in the components array (nComponents is already maxComponents).
107 */
108ndn_Error ndn_Name_appendString(struct ndn_Name *self, char* value);
109
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700110#ifdef __cplusplus
Jeff Thompson5d89cb92013-06-27 15:57:15 -0700111}
112#endif
113
Jeff Thompson08d56292013-06-27 18:20:44 -0700114#endif
Jeff Thompson5d89cb92013-06-27 15:57:15 -0700115