blob: 29c1dea96bbe48a32a1b74079225746e6d8133df (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "internal.h"
#include "dict.h"
/*
* Dict
*/
typedef struct DictElem DictElem;
struct DictElem
{
const char *key;
const char *val;
struct DictElem *next;
};
struct Dict
{
DictElem *root;
int count;
};
Dict *dictCreate()
{
Dict *ret = malloc(sizeof(Dict));
ret->count = 0;
ret->root = 0;
return ret;
}
void dictInsert(Dict* list, const char* key, const char* value)
{
if (!list)
return;
DictElem *node = malloc(sizeof(DictElem));
node->key = key;
node->val = value;
node->next = list->root;
list->root = node;
list->count++;
}
unsigned int dictCount(Dict *list)
{
return (list ? list->count : 0);
}
void dictFree(Dict *list)
{
if (!list)
return;
DictElem *node = list->root;
while (node)
{
DictElem *tmp = node;
node = node->next;
free(tmp);
}
free(list);
}
PyObject *dictToPyDict(Dict *dict)
{
PyObject *pydict = PyDict_New();
DictElem *node = dict->root;
while (node)
{
PyDict_SetItem(pydict, PyBytes_FromString(node->key), PyBytes_FromString(node->val));
node = node->next;
}
return pydict;
}
|