1 /*
2 * Copyright (C) 2018-2019 Alexander Borisov
3 *
4 * Author: Alexander Borisov <borisov@lexbor.com>
5 */
6
7 #include "lexbor/core/shs.h"
8
9 #define LEXBOR_STR_RES_MAP_LOWERCASE
10 #include "lexbor/core/str_res.h"
11
12 #include "lexbor/ns/ns.h"
13 #include "lexbor/ns/res.h"
14
15
16 LXB_API const lxb_ns_data_t *
lxb_ns_append(lexbor_hash_t * hash,const lxb_char_t * link,size_t length)17 lxb_ns_append(lexbor_hash_t *hash, const lxb_char_t *link, size_t length)
18 {
19 lxb_ns_data_t *data;
20 const lexbor_shs_entry_t *entry;
21
22 if (link == NULL || length == 0) {
23 return NULL;
24 }
25
26 entry = lexbor_shs_entry_get_lower_static(lxb_ns_res_shs_link_data,
27 link, length);
28 if (entry != NULL) {
29 return entry->value;
30 }
31
32 data = lexbor_hash_insert(hash, lexbor_hash_insert_lower, link, length);
33 if ((lxb_ns_id_t) data <= LXB_NS__LAST_ENTRY) {
34 return NULL;
35 }
36
37 data->ns_id = (lxb_ns_id_t) data;
38
39 return data;
40 }
41
42 const lxb_char_t *
lxb_ns_by_id(lexbor_hash_t * hash,lxb_ns_id_t ns_id,size_t * length)43 lxb_ns_by_id(lexbor_hash_t *hash, lxb_ns_id_t ns_id, size_t *length)
44 {
45 const lxb_ns_data_t *data;
46
47 data = lxb_ns_data_by_id(hash, ns_id);
48 if (data == NULL) {
49 if (length != NULL) {
50 *length = 0;
51 }
52
53 return NULL;
54 }
55
56 if (length != NULL) {
57 *length = data->entry.length;
58 }
59
60 return lexbor_hash_entry_str(&data->entry);
61 }
62
63 const lxb_ns_data_t *
lxb_ns_data_by_id(lexbor_hash_t * hash,lxb_ns_id_t ns_id)64 lxb_ns_data_by_id(lexbor_hash_t *hash, lxb_ns_id_t ns_id)
65 {
66 if (ns_id >= LXB_NS__LAST_ENTRY) {
67 if (ns_id == LXB_NS__LAST_ENTRY) {
68 return NULL;
69 }
70
71 return (const lxb_ns_data_t *) ns_id;
72 }
73
74 return &lxb_ns_res_data[ns_id];
75 }
76
77 const lxb_ns_data_t *
lxb_ns_data_by_link(lexbor_hash_t * hash,const lxb_char_t * link,size_t length)78 lxb_ns_data_by_link(lexbor_hash_t *hash, const lxb_char_t *link, size_t length)
79 {
80 const lexbor_shs_entry_t *entry;
81
82 if (link == NULL || length == 0) {
83 return NULL;
84 }
85
86 entry = lexbor_shs_entry_get_lower_static(lxb_ns_res_shs_link_data,
87 link, length);
88 if (entry != NULL) {
89 return entry->value;
90 }
91
92 return lexbor_hash_search(hash, lexbor_hash_search_lower, link, length);
93 }
94
95 /* Prefix */
96 const lxb_ns_prefix_data_t *
lxb_ns_prefix_append(lexbor_hash_t * hash,const lxb_char_t * prefix,size_t length)97 lxb_ns_prefix_append(lexbor_hash_t *hash,
98 const lxb_char_t *prefix, size_t length)
99 {
100 lxb_ns_prefix_data_t *data;
101 const lexbor_shs_entry_t *entry;
102
103 if (prefix == NULL || length == 0) {
104 return NULL;
105 }
106
107 entry = lexbor_shs_entry_get_lower_static(lxb_ns_res_shs_data,
108 prefix, length);
109 if (entry != NULL) {
110 return entry->value;
111 }
112
113 data = lexbor_hash_insert(hash, lexbor_hash_insert_lower, prefix, length);
114 if ((lxb_ns_prefix_id_t) data <= LXB_NS__LAST_ENTRY) {
115 return NULL;
116 }
117
118 data->prefix_id = (lxb_ns_prefix_id_t) data;
119
120 return data;
121 }
122
123 const lxb_ns_prefix_data_t *
lxb_ns_prefix_data_by_id(lexbor_hash_t * hash,lxb_ns_prefix_id_t prefix_id)124 lxb_ns_prefix_data_by_id(lexbor_hash_t *hash, lxb_ns_prefix_id_t prefix_id)
125 {
126 if (prefix_id >= LXB_NS__LAST_ENTRY) {
127 if (prefix_id == LXB_NS__LAST_ENTRY) {
128 return NULL;
129 }
130
131 return (const lxb_ns_prefix_data_t *) prefix_id;
132 }
133
134 return &lxb_ns_prefix_res_data[prefix_id];
135 }
136
137 const lxb_ns_prefix_data_t *
lxb_ns_prefix_data_by_name(lexbor_hash_t * hash,const lxb_char_t * prefix,size_t length)138 lxb_ns_prefix_data_by_name(lexbor_hash_t *hash,
139 const lxb_char_t *prefix, size_t length)
140 {
141 const lexbor_shs_entry_t *entry;
142
143 if (prefix == NULL || length == 0) {
144 return NULL;
145 }
146
147 entry = lexbor_shs_entry_get_lower_static(lxb_ns_res_shs_data,
148 prefix, length);
149 if (entry != NULL) {
150 return entry->value;
151 }
152
153 return lexbor_hash_search(hash, lexbor_hash_search_lower, prefix, length);
154 }
155