libcnary: Updated typedefs of node_t and node_list_t to contain pointer

This makes the code more readable. Obviously all the code that uses it
is also updated.
This commit is contained in:
Nikias Bassen
2023-02-06 18:28:28 +01:00
parent 52826a6c22
commit d390800634
10 changed files with 143 additions and 134 deletions
+21 -21
View File
@@ -24,42 +24,42 @@
#ifndef NODE_H_
#define NODE_H_
#include "node_list.h"
#include "object.h"
#define NODE_TYPE 1;
struct node_list_t;
// This class implements the abstract iterator class
typedef struct node_t {
typedef struct node* node_t;
struct node {
// Super class
struct node_t* next;
struct node_t* prev;
node_t next;
node_t prev;
unsigned int count;
// Local Members
void *data;
struct node_t* parent;
struct node_list_t* children;
} node_t;
node_t parent;
node_list_t children;
};
void node_destroy(struct node_t* node);
struct node_t* node_create(struct node_t* parent, void* data);
void node_destroy(node_t node);
node_t node_create(node_t parent, void* data);
int node_attach(struct node_t* parent, struct node_t* child);
int node_detach(struct node_t* parent, struct node_t* child);
int node_insert(struct node_t* parent, unsigned int index, struct node_t* child);
int node_attach(node_t parent, node_t child);
int node_detach(node_t parent, node_t child);
int node_insert(node_t parent, unsigned int index, node_t child);
unsigned int node_n_children(struct node_t* node);
node_t* node_nth_child(struct node_t* node, unsigned int n);
node_t* node_first_child(struct node_t* node);
node_t* node_prev_sibling(struct node_t* node);
node_t* node_next_sibling(struct node_t* node);
int node_child_position(struct node_t* parent, node_t* child);
unsigned int node_n_children(node_t node);
node_t node_nth_child(node_t node, unsigned int n);
node_t node_first_child(node_t node);
node_t node_prev_sibling(node_t node);
node_t node_next_sibling(node_t node);
int node_child_position(node_t parent, node_t child);
typedef void* (*copy_func_t)(const void *src);
node_t* node_copy_deep(node_t* node, copy_func_t copy_func);
node_t node_copy_deep(node_t node, copy_func_t copy_func);
void node_debug(struct node_t* node);
void node_debug(node_t node);
#endif /* NODE_H_ */
+13 -10
View File
@@ -24,24 +24,27 @@
#ifndef NODE_LIST_H_
#define NODE_LIST_H_
struct node_t;
#include "node.h"
typedef struct node* node_t;
// This class implements the list_t abstract class
typedef struct node_list_t {
struct node_list {
// list_t members
struct node_t* begin;
struct node_t* end;
node_t begin;
node_t end;
// node_list_t members
unsigned int count;
} node_list_t;
};
typedef struct node_list* node_list_t;
void node_list_destroy(struct node_list_t* list);
struct node_list_t* node_list_create();
void node_list_destroy(node_list_t list);
node_list_t node_list_create();
int node_list_add(node_list_t* list, node_t* node);
int node_list_insert(node_list_t* list, unsigned int index, node_t* node);
int node_list_remove(node_list_t* list, node_t* node);
int node_list_add(node_list_t list, node_t node);
int node_list_insert(node_list_t list, unsigned int index, node_t node);
int node_list_remove(node_list_t list, node_t node);
#endif /* NODE_LIST_H_ */
+27 -22
View File
@@ -27,11 +27,12 @@
#include "node.h"
#include "node_list.h"
void node_destroy(node_t* node) {
void node_destroy(node_t node)
{
if(!node) return;
if (node->children && node->children->count > 0) {
node_t* ch;
node_t ch;
while ((ch = node->children->begin)) {
node_list_remove(node->children, ch);
node_destroy(ch);
@@ -43,10 +44,11 @@ void node_destroy(node_t* node) {
free(node);
}
node_t* node_create(node_t* parent, void* data) {
node_t node_create(node_t parent, void* data)
{
int error = 0;
node_t* node = (node_t*)calloc(1, sizeof(node_t));
node_t node = (node_t)calloc(1, sizeof(struct node));
if (node == NULL) {
return NULL;
}
@@ -73,7 +75,8 @@ node_t* node_create(node_t* parent, void* data) {
return node;
}
int node_attach(node_t* parent, node_t* child) {
int node_attach(node_t parent, node_t child)
{
if (!parent || !child) return -1;
child->parent = parent;
if(!parent->children) {
@@ -86,7 +89,8 @@ int node_attach(node_t* parent, node_t* child) {
return res;
}
int node_detach(node_t* parent, node_t* child) {
int node_detach(node_t parent, node_t child)
{
if (!parent || !child) return -1;
int node_index = node_list_remove(parent->children, child);
if (node_index >= 0) {
@@ -95,7 +99,7 @@ int node_detach(node_t* parent, node_t* child) {
return node_index;
}
int node_insert(node_t* parent, unsigned int node_index, node_t* child)
int node_insert(node_t parent, unsigned int node_index, node_t child)
{
if (!parent || !child) return -1;
child->parent = parent;
@@ -109,9 +113,10 @@ int node_insert(node_t* parent, unsigned int node_index, node_t* child)
return res;
}
static void _node_debug(node_t* node, unsigned int depth) {
static void _node_debug(node_t node, unsigned int depth)
{
unsigned int i = 0;
node_t* current = NULL;
node_t current = NULL;
for(i = 0; i < depth; i++) {
printf("\t");
}
@@ -132,23 +137,23 @@ static void _node_debug(node_t* node, unsigned int depth) {
}
void node_debug(node_t* node)
void node_debug(node_t node)
{
_node_debug(node, 0);
}
unsigned int node_n_children(struct node_t* node)
unsigned int node_n_children(node_t node)
{
if (!node) return 0;
return node->count;
}
node_t* node_nth_child(struct node_t* node, unsigned int n)
node_t node_nth_child(node_t node, unsigned int n)
{
if (!node || !node->children || !node->children->begin) return NULL;
unsigned int node_index = 0;
int found = 0;
node_t *ch;
node_t ch;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
if (node_index++ == n) {
found = 1;
@@ -161,30 +166,30 @@ node_t* node_nth_child(struct node_t* node, unsigned int n)
return ch;
}
node_t* node_first_child(struct node_t* node)
node_t node_first_child(node_t node)
{
if (!node || !node->children) return NULL;
return node->children->begin;
}
node_t* node_prev_sibling(struct node_t* node)
node_t node_prev_sibling(node_t node)
{
if (!node) return NULL;
return node->prev;
}
node_t* node_next_sibling(struct node_t* node)
node_t node_next_sibling(node_t node)
{
if (!node) return NULL;
return node->next;
}
int node_child_position(struct node_t* parent, node_t* child)
int node_child_position(node_t parent, node_t child)
{
if (!parent || !parent->children || !parent->children->begin || !child) return -1;
int node_index = 0;
int found = 0;
node_t *ch;
node_t ch;
for (ch = node_first_child(parent); ch; ch = node_next_sibling(ch)) {
if (ch == child) {
found = 1;
@@ -198,17 +203,17 @@ int node_child_position(struct node_t* parent, node_t* child)
return node_index;
}
node_t* node_copy_deep(node_t* node, copy_func_t copy_func)
node_t node_copy_deep(node_t node, copy_func_t copy_func)
{
if (!node) return NULL;
void *data = NULL;
if (copy_func) {
data = copy_func(node->data);
}
node_t* copy = node_create(NULL, data);
node_t* ch;
node_t copy = node_create(NULL, data);
node_t ch;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
node_t* cc = node_copy_deep(ch, copy_func);
node_t cc = node_copy_deep(ch, copy_func);
node_attach(copy, cc);
}
return copy;
+16 -12
View File
@@ -28,12 +28,14 @@
#include "node.h"
#include "node_list.h"
void node_list_destroy(node_list_t* list) {
void node_list_destroy(node_list_t list)
{
free(list);
}
node_list_t* node_list_create() {
node_list_t* list = (node_list_t*)calloc(1, sizeof(node_list_t));
node_list_t node_list_create()
{
node_list_t list = (node_list_t)calloc(1, sizeof(struct node_list));
if (list == NULL) {
return NULL;
}
@@ -45,11 +47,12 @@ node_list_t* node_list_create() {
return list;
}
int node_list_add(node_list_t* list, node_t* node) {
int node_list_add(node_list_t list, node_t node)
{
if (!list || !node) return -1;
// Find the last element in the list
node_t* last = list->end;
node_t last = list->end;
// Setup our new node as the new last element
node->next = NULL;
@@ -72,17 +75,18 @@ int node_list_add(node_list_t* list, node_t* node) {
return 0;
}
int node_list_insert(node_list_t* list, unsigned int node_index, node_t* node) {
int node_list_insert(node_list_t list, unsigned int node_index, node_t node)
{
if (!list || !node) return -1;
if (node_index >= list->count) {
return node_list_add(list, node);
}
// Get the first element in the list
node_t* cur = list->begin;
node_t cur = list->begin;
unsigned int pos = 0;
node_t* prev = NULL;
node_t prev = NULL;
if (node_index > 0) {
while (pos < node_index) {
@@ -120,15 +124,16 @@ int node_list_insert(node_list_t* list, unsigned int node_index, node_t* node) {
return 0;
}
int node_list_remove(node_list_t* list, node_t* node) {
int node_list_remove(node_list_t list, node_t node)
{
if (!list || !node) return -1;
if (list->count == 0) return -1;
int node_index = 0;
node_t* n;
node_t n;
for (n = list->begin; n; n = n->next) {
if (node == n) {
node_t* newnode = node->next;
node_t newnode = node->next;
if (node->prev) {
node->prev->next = newnode;
if (newnode) {
@@ -153,4 +158,3 @@ int node_list_remove(node_list_t* list, node_t* node) {
}
return -1;
}
+8 -8
View File
@@ -191,7 +191,7 @@ static int uint64_mul_overflow(uint64_t a, uint64_t b, uint64_t *res)
}
#endif
#define NODE_IS_ROOT(x) (((node_t*)(x))->isRoot)
#define NODE_IS_ROOT(x) (((node_t)(x))->isRoot)
struct bplist_data {
const char* data;
@@ -936,7 +936,7 @@ struct serialize_s
hashtable_t* ref_table;
};
static void serialize_plist(node_t* node, void* data)
static void serialize_plist(node_t node, void* data)
{
uint64_t *index_val = NULL;
struct serialize_s *ser = (struct serialize_s *) data;
@@ -959,7 +959,7 @@ static void serialize_plist(node_t* node, void* data)
ptr_array_add(ser->objects, node);
//now recurse on children
node_t *ch;
node_t ch;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
serialize_plist(ch, data);
}
@@ -1111,9 +1111,9 @@ static void write_unicode(bytearray_t * bplist, char *val, uint64_t size)
free(unicodestr);
}
static void write_array(bytearray_t * bplist, node_t* node, hashtable_t* ref_table, uint8_t ref_size)
static void write_array(bytearray_t * bplist, node_t node, hashtable_t* ref_table, uint8_t ref_size)
{
node_t* cur = NULL;
node_t cur = NULL;
uint64_t i = 0;
uint64_t size = node_n_children(node);
@@ -1130,9 +1130,9 @@ static void write_array(bytearray_t * bplist, node_t* node, hashtable_t* ref_tab
}
}
static void write_dict(bytearray_t * bplist, node_t* node, hashtable_t* ref_table, uint8_t ref_size)
static void write_dict(bytearray_t * bplist, node_t node, hashtable_t* ref_table, uint8_t ref_size)
{
node_t* cur = NULL;
node_t cur = NULL;
uint64_t i = 0;
uint64_t size = node_n_children(node) / 2;
@@ -1235,7 +1235,7 @@ PLIST_API plist_err_t plist_to_bin(plist_t plist, char **plist_bin, uint32_t * l
uint64_t req = 0;
for (i = 0; i < num_objects; i++)
{
node_t* node = ptr_array_index(objects, i);
node_t node = ptr_array_index(objects, i);
plist_data_t data = plist_get_data(node);
uint64_t size;
uint8_t bsize;
+5 -6
View File
@@ -34,7 +34,6 @@
#include <limits.h>
#include <node.h>
#include <node_list.h>
#include "plist.h"
#include "strbuf.h"
@@ -101,7 +100,7 @@ static size_t dtostr(char *buf, size_t bufsize, double realval)
return len;
}
static int node_to_json(node_t* node, bytearray_t **outbuf, uint32_t depth, int prettify)
static int node_to_json(node_t node, bytearray_t **outbuf, uint32_t depth, int prettify)
{
plist_data_t node_data = NULL;
@@ -185,7 +184,7 @@ static int node_to_json(node_t* node, bytearray_t **outbuf, uint32_t depth, int
case PLIST_ARRAY: {
str_buf_append(*outbuf, "[", 1);
node_t *ch;
node_t ch;
uint32_t cnt = 0;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
if (cnt > 0) {
@@ -213,7 +212,7 @@ static int node_to_json(node_t* node, bytearray_t **outbuf, uint32_t depth, int
} break;
case PLIST_DICT: {
str_buf_append(*outbuf, "{", 1);
node_t *ch;
node_t ch;
uint32_t cnt = 0;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
if (cnt > 0 && cnt % 2 == 0) {
@@ -302,7 +301,7 @@ static int num_digits_u(uint64_t i)
return n;
}
static int node_estimate_size(node_t *node, uint64_t *size, uint32_t depth, int prettify)
static int node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify)
{
plist_data_t data;
if (!node) {
@@ -310,7 +309,7 @@ static int node_estimate_size(node_t *node, uint64_t *size, uint32_t depth, int
}
data = plist_get_data(node);
if (node->children) {
node_t *ch;
node_t ch;
unsigned int n_children = node_n_children(node);
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
int res = node_estimate_size(ch, size, depth + 1, prettify);
+5 -6
View File
@@ -34,7 +34,6 @@
#include <limits.h>
#include <node.h>
#include <node_list.h>
#include "plist.h"
#include "strbuf.h"
@@ -130,7 +129,7 @@ static int str_needs_quotes(const char* str, size_t len)
return 0;
}
static int node_to_openstep(node_t* node, bytearray_t **outbuf, uint32_t depth, int prettify)
static int node_to_openstep(node_t node, bytearray_t **outbuf, uint32_t depth, int prettify)
{
plist_data_t node_data = NULL;
@@ -209,7 +208,7 @@ static int node_to_openstep(node_t* node, bytearray_t **outbuf, uint32_t depth,
case PLIST_ARRAY: {
str_buf_append(*outbuf, "(", 1);
node_t *ch;
node_t ch;
uint32_t cnt = 0;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
if (cnt > 0) {
@@ -237,7 +236,7 @@ static int node_to_openstep(node_t* node, bytearray_t **outbuf, uint32_t depth,
} break;
case PLIST_DICT: {
str_buf_append(*outbuf, "{", 1);
node_t *ch;
node_t ch;
uint32_t cnt = 0;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
if (cnt > 0 && cnt % 2 == 0) {
@@ -346,7 +345,7 @@ static int num_digits_u(uint64_t i)
return n;
}
static int node_estimate_size(node_t *node, uint64_t *size, uint32_t depth, int prettify)
static int node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify)
{
plist_data_t data;
if (!node) {
@@ -354,7 +353,7 @@ static int node_estimate_size(node_t *node, uint64_t *size, uint32_t depth, int
}
data = plist_get_data(node);
if (node->children) {
node_t *ch;
node_t ch;
unsigned int n_children = node_n_children(node);
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
int res = node_estimate_size(ch, size, depth + 1, prettify);
+41 -41
View File
@@ -266,7 +266,7 @@ plist_data_t plist_get_data(plist_t node)
{
if (!node)
return NULL;
return ((node_t*)node)->data;
return ((node_t)node)->data;
}
plist_data_t plist_new_plist_data(void)
@@ -326,7 +326,7 @@ void plist_free_data(plist_data_t data)
}
}
static int plist_free_node(node_t* node)
static int plist_free_node(node_t node)
{
plist_data_t data = NULL;
int node_index = node_detach(node->parent, node);
@@ -334,9 +334,9 @@ static int plist_free_node(node_t* node)
plist_free_data(data);
node->data = NULL;
node_t *ch;
node_t ch;
for (ch = node_first_child(node); ch; ) {
node_t *next = node_next_sibling(ch);
node_t next = node_next_sibling(ch);
plist_free_node(ch);
ch = next;
}
@@ -468,7 +468,7 @@ PLIST_API void plist_mem_free(void* ptr)
}
}
static plist_t plist_copy_node(node_t *node)
static plist_t plist_copy_node(node_t node)
{
plist_type node_type = PLIST_NONE;
plist_t newnode = NULL;
@@ -509,7 +509,7 @@ static plist_t plist_copy_node(node_t *node)
}
newnode = plist_new_node(newdata);
node_t *ch;
node_t ch;
unsigned int node_index = 0;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
/* copy child node */
@@ -525,7 +525,7 @@ static plist_t plist_copy_node(node_t *node)
break;
case PLIST_DICT:
if (newdata->hashtable && (node_index % 2 != 0)) {
hash_table_insert((hashtable_t*)newdata->hashtable, (node_prev_sibling((node_t*)newch))->data, newch);
hash_table_insert((hashtable_t*)newdata->hashtable, (node_prev_sibling((node_t)newch))->data, newch);
}
break;
default:
@@ -556,7 +556,7 @@ PLIST_API plist_t plist_array_get_item(plist_t node, uint32_t n)
plist_t ret = NULL;
if (node && PLIST_ARRAY == plist_get_node_type(node) && n < INT_MAX)
{
ptrarray_t *pa = ((plist_data_t)((node_t*)node)->data)->hashtable;
ptrarray_t *pa = ((plist_data_t)((node_t)node)->data)->hashtable;
if (pa) {
ret = (plist_t)ptr_array_index(pa, n);
} else {
@@ -578,12 +578,12 @@ PLIST_API uint32_t plist_array_get_item_index(plist_t node)
static void _plist_array_post_insert(plist_t node, plist_t item, long n)
{
ptrarray_t *pa = ((plist_data_t)((node_t*)node)->data)->hashtable;
ptrarray_t *pa = ((plist_data_t)((node_t)node)->data)->hashtable;
if (pa) {
/* store pointer to item in array */
ptr_array_insert(pa, item, n);
} else {
if (((node_t*)node)->count > 100) {
if (((node_t)node)->count > 100) {
/* make new lookup array */
pa = ptr_array_new(128);
plist_t current = NULL;
@@ -593,7 +593,7 @@ static void _plist_array_post_insert(plist_t node, plist_t item, long n)
{
ptr_array_add(pa, current);
}
((plist_data_t)((node_t*)node)->data)->hashtable = pa;
((plist_data_t)((node_t)node)->data)->hashtable = pa;
}
}
}
@@ -611,7 +611,7 @@ PLIST_API void plist_array_set_item(plist_t node, plist_t item, uint32_t n)
return;
}
node_insert(node, idx, item);
ptrarray_t* pa = ((plist_data_t)((node_t*)node)->data)->hashtable;
ptrarray_t* pa = ((plist_data_t)((node_t)node)->data)->hashtable;
if (pa) {
ptr_array_set(pa, item, idx);
}
@@ -644,7 +644,7 @@ PLIST_API void plist_array_remove_item(plist_t node, uint32_t n)
plist_t old_item = plist_array_get_item(node, n);
if (old_item)
{
ptrarray_t* pa = ((plist_data_t)((node_t*)node)->data)->hashtable;
ptrarray_t* pa = ((plist_data_t)((node_t)node)->data)->hashtable;
if (pa) {
ptr_array_remove(pa, n);
}
@@ -660,7 +660,7 @@ PLIST_API void plist_array_item_remove(plist_t node)
{
int n = node_child_position(father, node);
if (n < 0) return;
ptrarray_t* pa = ((plist_data_t)((node_t*)father)->data)->hashtable;
ptrarray_t* pa = ((plist_data_t)((node_t)father)->data)->hashtable;
if (pa) {
ptr_array_remove(pa, n);
}
@@ -672,14 +672,14 @@ PLIST_API void plist_array_new_iter(plist_t node, plist_array_iter *iter)
{
if (iter)
{
*iter = malloc(sizeof(node_t*));
*((node_t**)(*iter)) = node_first_child(node);
*iter = malloc(sizeof(node_t));
*((node_t*)(*iter)) = node_first_child(node);
}
}
PLIST_API void plist_array_next_item(plist_t node, plist_array_iter iter, plist_t *item)
{
node_t** iter_node = (node_t**)iter;
node_t* iter_node = (node_t*)iter;
if (item)
{
@@ -710,14 +710,14 @@ PLIST_API void plist_dict_new_iter(plist_t node, plist_dict_iter *iter)
{
if (iter)
{
*iter = malloc(sizeof(node_t*));
*((node_t**)(*iter)) = node_first_child(node);
*iter = malloc(sizeof(node_t));
*((node_t*)(*iter)) = node_first_child(node);
}
}
PLIST_API void plist_dict_next_item(plist_t node, plist_dict_iter iter, char **key, plist_t *val)
{
node_t** iter_node = (node_t**)iter;
node_t* iter_node = (node_t*)iter;
if (key)
{
@@ -799,7 +799,7 @@ PLIST_API plist_t plist_dict_get_item(plist_t node, const char* key)
PLIST_API void plist_dict_set_item(plist_t node, const char* key, plist_t item)
{
if (node && PLIST_DICT == plist_get_node_type(node)) {
node_t* old_item = plist_dict_get_item(node, key);
node_t old_item = plist_dict_get_item(node, key);
plist_t key_node = NULL;
if (old_item) {
int idx = plist_free_node(old_item);
@@ -815,12 +815,12 @@ PLIST_API void plist_dict_set_item(plist_t node, const char* key, plist_t item)
node_attach(node, item);
}
hashtable_t *ht = ((plist_data_t)((node_t*)node)->data)->hashtable;
hashtable_t *ht = ((plist_data_t)((node_t)node)->data)->hashtable;
if (ht) {
/* store pointer to item in hash table */
hash_table_insert(ht, (plist_data_t)((node_t*)key_node)->data, item);
hash_table_insert(ht, (plist_data_t)((node_t)key_node)->data, item);
} else {
if (((node_t*)node)->count > 500) {
if (((node_t)node)->count > 500) {
/* make new hash table */
ht = hash_table_new(dict_key_hash, dict_key_compare, NULL);
/* calculate the hashes for all entries we have so far */
@@ -829,9 +829,9 @@ PLIST_API void plist_dict_set_item(plist_t node, const char* key, plist_t item)
ht && current;
current = (plist_t)node_next_sibling(node_next_sibling(current)))
{
hash_table_insert(ht, ((node_t*)current)->data, node_next_sibling(current));
hash_table_insert(ht, ((node_t)current)->data, node_next_sibling(current));
}
((plist_data_t)((node_t*)node)->data)->hashtable = ht;
((plist_data_t)((node_t)node)->data)->hashtable = ht;
}
}
}
@@ -850,9 +850,9 @@ PLIST_API void plist_dict_remove_item(plist_t node, const char* key)
if (old_item)
{
plist_t key_node = node_prev_sibling(old_item);
hashtable_t* ht = ((plist_data_t)((node_t*)node)->data)->hashtable;
hashtable_t* ht = ((plist_data_t)((node_t)node)->data)->hashtable;
if (ht) {
hash_table_remove(ht, ((node_t*)key_node)->data);
hash_table_remove(ht, ((node_t)key_node)->data);
}
plist_free(key_node);
plist_free(old_item);
@@ -961,7 +961,7 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu
PLIST_API plist_t plist_get_parent(plist_t node)
{
return node ? (plist_t) ((node_t*) node)->parent : NULL;
return node ? (plist_t) ((node_t) node)->parent : NULL;
}
PLIST_API plist_type plist_get_node_type(plist_t node)
@@ -1119,7 +1119,7 @@ int plist_data_compare(const void *a, const void *b)
if (!a || !b)
return FALSE;
if (!((node_t*) a)->data || !((node_t*) b)->data)
if (!((node_t) a)->data || !((node_t) b)->data)
return FALSE;
val_a = plist_get_data((plist_t) a);
@@ -1509,8 +1509,8 @@ PLIST_API void plist_sort(plist_t plist)
plist_sort(plist_array_get_item(plist, i));
}
} else if (PLIST_IS_DICT(plist)) {
node_t *node = (node_t*)plist;
node_t *ch;
node_t node = (node_t)plist;
node_t ch;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
ch = node_next_sibling(ch);
plist_sort((plist_t)ch);
@@ -1521,22 +1521,22 @@ PLIST_API void plist_sort(plist_t plist)
int swapped = 0;
do {
swapped = 0;
node_t *lptr = NULL;
node_t *cur_key = node_first_child((node_t*)plist);
node_t lptr = NULL;
node_t cur_key = node_first_child((node_t)plist);
while (NEXT_KEY(cur_key) != lptr) {
node_t *next_key = NEXT_KEY(cur_key);
node_t next_key = NEXT_KEY(cur_key);
if (strcmp(KEY_STRVAL(cur_key), KEY_STRVAL(next_key)) > 0) {
node_t *cur_val = cur_key->next;
node_t *next_val = next_key->next;
node_t cur_val = cur_key->next;
node_t next_val = next_key->next;
// we need to swap 2 consecutive nodes with the 2 after them
// a -> b -> [c] -> [d] -> [e] -> [f] -> g -> h
// cur next
// swapped:
// a -> b -> [e] -> [f] -> [c] -> [d] -> g -> h
// next cur
node_t *tmp_prev = cur_key->prev;
node_t *tmp_next = next_val->next;
node_t tmp_prev = cur_key->prev;
node_t tmp_next = next_val->next;
cur_key->prev = next_val;
cur_val->next = tmp_next;
next_val->next = cur_key;
@@ -1544,12 +1544,12 @@ PLIST_API void plist_sort(plist_t plist)
if (tmp_prev) {
tmp_prev->next = next_key;
} else {
((node_t*)plist)->children->begin = next_key;
((node_t)plist)->children->begin = next_key;
}
if (tmp_next) {
tmp_next->prev = cur_val;
} else {
((node_t*)plist)->children->end = cur_val;
((node_t)plist)->children->end = cur_val;
}
cur_key = next_key;
swapped = 1;
+5 -6
View File
@@ -41,7 +41,6 @@
#include <limits.h>
#include <node.h>
#include <node_list.h>
#include "plist.h"
#include "base64.h"
@@ -127,7 +126,7 @@ static size_t dtostr(char *buf, size_t bufsize, double realval)
return len;
}
static int node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth)
static int node_to_xml(node_t node, bytearray_t **outbuf, uint32_t depth)
{
plist_data_t node_data = NULL;
@@ -358,7 +357,7 @@ static int node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth)
if (node_data->type == PLIST_DICT && node->children) {
assert((node->children->count % 2) == 0);
}
node_t *ch;
node_t ch;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
int res = node_to_xml(ch, outbuf, depth+1);
if (res < 0) return res;
@@ -438,7 +437,7 @@ static int num_digits_u(uint64_t i)
return n;
}
static int node_estimate_size(node_t *node, uint64_t *size, uint32_t depth)
static int node_estimate_size(node_t node, uint64_t *size, uint32_t depth)
{
plist_data_t data;
if (!node) {
@@ -446,7 +445,7 @@ static int node_estimate_size(node_t *node, uint64_t *size, uint32_t depth)
}
data = plist_get_data(node);
if (node->children) {
node_t *ch;
node_t ch;
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
node_estimate_size(ch, size, depth + 1);
}
@@ -1413,7 +1412,7 @@ static int node_from_xml(parse_ctx ctx, plist_t *plist)
node_path = node_path->prev;
free(path_item);
parent = ((node_t*)parent)->parent;
parent = ((node_t)parent)->parent;
if (!parent) {
goto err_out;
}
+2 -2
View File
@@ -35,12 +35,12 @@
static plist_t plist_get_first_child(plist_t node)
{
return (plist_t) node_first_child((node_t*) node);
return (plist_t) node_first_child((node_t) node);
}
static plist_t plist_get_next_sibling(plist_t node)
{
return (plist_t) node_next_sibling((node_t*) node);
return (plist_t) node_next_sibling((node_t) node);
}
static char compare_plist(plist_t node_l, plist_t node_r)