This repository has been archived by the owner on Sep 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfpar.c
104 lines (95 loc) · 2.7 KB
/
confpar.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "confpar.h"
#include<stdio.h>
#include<stdlib.h>
#include<libxml/tree.h>
#include<libxml/parser.h>
#include<string.h>
#include "debug.h"
/**
* Function to initialize confpar_item_t to default values
* @param item the confpar_item_t item to be initialised
*/
void confpar_item_init(confpar_item_t * item)
{
if (item !=NULL)
{
item->key = NULL;
item->value = NULL;
item->children = NULL;
item->next = NULL;
LOG("Config Parser item initialised\n");
}
#ifdef DEBUG
else
LOG("Config Parser item to be initialised was NULL\n");
#endif
}
/**
* Function to initialize conpar_t object to default values
* @param confpar confpar_t object to be initialized
*/
void confpar_object_init(confpar_t * confpar)
{
if (confpar != NULL)
{
confpar->filepath = NULL;
confpar->root = NULL;
LOG("Config Parser Object initialised\n");
}
#ifdef DEBUG
else
LOG("Config Parser Object to be initialised was NULL\n");
#endif
}
#ifdef DEBUG
static void print_element_names(xmlNode * a_node, const xmlChar* parent)
{
xmlNode *cur_node = NULL;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE && parent != NULL) {
xmlChar * content = xmlNodeGetContent(cur_node);
printf("Parent : %s Element Name: '%s' contains %s, line %d\n", parent, cur_node->name, content, cur_node->line);
}
if (cur_node->type == XML_TEXT_NODE && cur_node->children == NULL)
{
xmlChar * content = xmlNodeGetContent(cur_node);
printf("Children NULL, value -> %s\n", content);
}
print_element_names(cur_node->children, cur_node->name);
}
}
#endif
/**
* Function that parses the config file and updates key values pairs
* @param confpar confpar_t object which will contain all the info after the file is parsed
* @todo finish implemtntation of this function after doc is defined
*/
int confpar_update(confpar_t * confpar)
{
if (confpar->filepath == NULL)
{
LOG("Error: Filepath of file to be parsed was NULL");
return -1;
}
/*
* check for version mismatch of LIBXML
*/
LIBXML_TEST_VERSION
// read the XML config file
xmlDocPtr doc;
doc = xmlReadFile(confpar->filepath, NULL, 0);
if (doc == NULL)
{
LOG("Error: Failed to open '%s' to read config\n", confpar->filepath);
return -1;
}
#ifdef DEBUG
// print the parsed xml document
xmlNode *xmlRoot;
xmlRoot = xmlDocGetRootElement(doc);
print_element_names(xmlRoot, NULL);
#endif
xmlFreeDoc(doc);
xmlCleanupParser();
return 1;
}