Skip to content

Commit

Permalink
Change API around #PLIST_DATA to use uint8_t instead of char arrays
Browse files Browse the repository at this point in the history
This makes it more obvious that it is arbitrary data and not necessarily
a string value.
  • Loading branch information
nikias committed Apr 14, 2024
1 parent 612cdf3 commit a91f574
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
8 changes: 4 additions & 4 deletions cython/plist.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ cdef extern from *:
void plist_get_string_val(plist_t node, char **val)
void plist_set_string_val(plist_t node, char *val)

plist_t plist_new_data(char *val, uint64_t length)
void plist_get_data_val(plist_t node, char **val, uint64_t * length)
void plist_set_data_val(plist_t node, char *val, uint64_t length)
plist_t plist_new_data(uint8_t *val, uint64_t length)
void plist_get_data_val(plist_t node, uint8_t **val, uint64_t * length)
void plist_set_data_val(plist_t node, uint8_t *val, uint64_t length)

plist_t plist_new_null();

Expand Down Expand Up @@ -579,7 +579,7 @@ cdef class Data(Node):

cpdef bytes get_value(self):
cdef:
char* val = NULL
uint8_t* val = NULL
uint64_t length = 0
plist_get_data_val(self._c_node, &val, &length)

Expand Down
6 changes: 3 additions & 3 deletions include/plist/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public :
Data(plist_t node, Node* parent = NULL);
Data(const Data& d);
Data& operator=(const Data& b);
Data(const std::vector<char>& buff);
Data(const std::vector<uint8_t>& buff);
virtual ~Data();

Node* Clone() const;

void SetValue(const std::vector<char>& buff);
std::vector<char> GetValue() const;
void SetValue(const std::vector<uint8_t>& buff);
std::vector<uint8_t> GetValue() const;
};

};
Expand Down
10 changes: 5 additions & 5 deletions include/plist/plist.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ extern "C"
* @return the created item
* @sa #plist_type
*/
PLIST_API plist_t plist_new_data(const char *val, uint64_t length);
PLIST_API plist_t plist_new_data(const uint8_t *val, uint64_t length);

/**
* Create a new plist_t type #PLIST_DATE
Expand Down Expand Up @@ -445,7 +445,7 @@ extern "C"
PLIST_API void plist_dict_get_item_key(plist_t node, char **key);

/**
* Get the nth item in a #PLIST_DICT node.
* Get the item for given key in a #PLIST_DICT node.
*
* @param node the node of type #PLIST_DICT
* @param key the identifier of the item to get.
Expand Down Expand Up @@ -595,7 +595,7 @@ extern "C"
* @param length the length of the buffer
* @note Use plist_mem_free() to free the allocated memory.
*/
PLIST_API void plist_get_data_val(plist_t node, char **val, uint64_t * length);
PLIST_API void plist_get_data_val(plist_t node, uint8_t **val, uint64_t * length);

/**
* Get a pointer to the data buffer of a #PLIST_DATA node.
Expand All @@ -608,7 +608,7 @@ extern "C"
*
* @return Pointer to the buffer
*/
PLIST_API const char* plist_get_data_ptr(plist_t node, uint64_t* length);
PLIST_API const uint8_t* plist_get_data_ptr(plist_t node, uint64_t * length);

/**
* Get the value of a #PLIST_DATE node.
Expand Down Expand Up @@ -700,7 +700,7 @@ extern "C"
* be freed by the node.
* @param length the length of the buffer
*/
PLIST_API void plist_set_data_val(plist_t node, const char *val, uint64_t length);
PLIST_API void plist_set_data_val(plist_t node, const uint8_t *val, uint64_t length);

/**
* Set the value of a node.
Expand Down
12 changes: 6 additions & 6 deletions src/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Data::Data(plist_t node, Node* parent) : Node(node, parent)

Data::Data(const PList::Data& d) : Node(PLIST_DATA)
{
std::vector<char> b = d.GetValue();
std::vector<uint8_t> b = d.GetValue();
plist_set_data_val(_node, &b[0], b.size());
}

Expand All @@ -45,7 +45,7 @@ Data& Data::operator=(const PList::Data& b)
return *this;
}

Data::Data(const std::vector<char>& buff) : Node(PLIST_DATA)
Data::Data(const std::vector<uint8_t>& buff) : Node(PLIST_DATA)
{
plist_set_data_val(_node, &buff[0], buff.size());
}
Expand All @@ -59,17 +59,17 @@ Node* Data::Clone() const
return new Data(*this);
}

void Data::SetValue(const std::vector<char>& buff)
void Data::SetValue(const std::vector<uint8_t>& buff)
{
plist_set_data_val(_node, &buff[0], buff.size());
}

std::vector<char> Data::GetValue() const
std::vector<uint8_t> Data::GetValue() const
{
char* buff = NULL;
uint8_t* buff = NULL;
uint64_t length = 0;
plist_get_data_val(_node, &buff, &length);
std::vector<char> ret(buff, buff + length);
std::vector<uint8_t> ret(buff, buff + length);
delete buff;
return ret;
}
Expand Down
11 changes: 6 additions & 5 deletions src/plist.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <limits.h>
#include <float.h>
#include <ctype.h>
#include <inttypes.h>

#ifdef WIN32
#include <windows.h>
Expand Down Expand Up @@ -490,7 +491,7 @@ plist_t plist_new_real(double val)
return plist_new_node(data);
}

plist_t plist_new_data(const char *val, uint64_t length)
plist_t plist_new_data(const uint8_t *val, uint64_t length)
{
plist_data_t data = plist_new_plist_data();
data->type = PLIST_DATA;
Expand Down Expand Up @@ -1142,7 +1143,7 @@ void plist_get_real_val(plist_t node, double *val)
assert(length == sizeof(double));
}

void plist_get_data_val(plist_t node, char **val, uint64_t * length)
void plist_get_data_val(plist_t node, uint8_t **val, uint64_t * length)
{
if (!node || !val || !length)
return;
Expand All @@ -1152,7 +1153,7 @@ void plist_get_data_val(plist_t node, char **val, uint64_t * length)
plist_get_type_and_value(node, &type, (void *) val, length);
}

const char* plist_get_data_ptr(plist_t node, uint64_t* length)
const uint8_t* plist_get_data_ptr(plist_t node, uint64_t* length)
{
if (!node || !length)
return NULL;
Expand All @@ -1161,7 +1162,7 @@ const char* plist_get_data_ptr(plist_t node, uint64_t* length)
return NULL;
plist_data_t data = plist_get_data(node);
*length = data->length;
return (const char*)data->buff;
return data->buff;
}

void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec)
Expand Down Expand Up @@ -1332,7 +1333,7 @@ void plist_set_real_val(plist_t node, double val)
plist_set_element_val(node, PLIST_REAL, &val, sizeof(double));
}

void plist_set_data_val(plist_t node, const char *val, uint64_t length)
void plist_set_data_val(plist_t node, const uint8_t *val, uint64_t length)
{
plist_set_element_val(node, PLIST_DATA, val, length);
}
Expand Down

0 comments on commit a91f574

Please sign in to comment.