1 /*
2 * Copyright (C) 2018-2019 Alexander Borisov
3 *
4 * Author: Alexander Borisov <borisov@lexbor.com>
5 */
6
7 #ifndef LEXBOR_TAG_H
8 #define LEXBOR_TAG_H
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 #include "lexbor/core/hash.h"
15 #include "lexbor/core/shs.h"
16 #include "lexbor/core/dobject.h"
17 #include "lexbor/core/str.h"
18
19 #include "lexbor/tag/const.h"
20
21
22 typedef struct {
23 lexbor_hash_entry_t entry;
24 lxb_tag_id_t tag_id;
25 size_t ref_count;
26 bool read_only;
27 }
28 lxb_tag_data_t;
29
30
31 LXB_API const lxb_tag_data_t *
32 lxb_tag_data_by_id(lexbor_hash_t *hash, lxb_tag_id_t tag_id);
33
34 LXB_API const lxb_tag_data_t *
35 lxb_tag_data_by_name(lexbor_hash_t *hash, const lxb_char_t *name, size_t len);
36
37 LXB_API const lxb_tag_data_t *
38 lxb_tag_data_by_name_upper(lexbor_hash_t *hash,
39 const lxb_char_t *name, size_t len);
40
41 /*
42 * Inline functions
43 */
44 lxb_inline const lxb_char_t *
lxb_tag_name_by_id(lexbor_hash_t * hash,lxb_tag_id_t tag_id,size_t * len)45 lxb_tag_name_by_id(lexbor_hash_t *hash, lxb_tag_id_t tag_id, size_t *len)
46 {
47 const lxb_tag_data_t *data = lxb_tag_data_by_id(hash, tag_id);
48 if (data == NULL) {
49 if (len != NULL) {
50 *len = 0;
51 }
52
53 return NULL;
54 }
55
56 if (len != NULL) {
57 *len = data->entry.length;
58 }
59
60 return lexbor_hash_entry_str(&data->entry);
61 }
62
63 lxb_inline const lxb_char_t *
lxb_tag_name_upper_by_id(lexbor_hash_t * hash,lxb_tag_id_t tag_id,size_t * len)64 lxb_tag_name_upper_by_id(lexbor_hash_t *hash, lxb_tag_id_t tag_id, size_t *len)
65 {
66 const lxb_tag_data_t *data = lxb_tag_data_by_id(hash, tag_id);
67 if (data == NULL) {
68 if (len != NULL) {
69 *len = 0;
70 }
71
72 return NULL;
73 }
74
75 if (len != NULL) {
76 *len = data->entry.length;
77 }
78
79 return lexbor_hash_entry_str(&data->entry);
80 }
81
82 lxb_inline lxb_tag_id_t
lxb_tag_id_by_name(lexbor_hash_t * hash,const lxb_char_t * name,size_t len)83 lxb_tag_id_by_name(lexbor_hash_t *hash, const lxb_char_t *name, size_t len)
84 {
85 const lxb_tag_data_t *data = lxb_tag_data_by_name(hash, name, len);
86 if (data == NULL) {
87 return LXB_TAG__UNDEF;
88 }
89
90 return data->tag_id;
91 }
92
93 lxb_inline lexbor_mraw_t *
lxb_tag_mraw(lexbor_hash_t * hash)94 lxb_tag_mraw(lexbor_hash_t *hash)
95 {
96 return lexbor_hash_mraw(hash);
97 }
98
99
100 /*
101 * No inline functions for ABI.
102 */
103 LXB_API const lxb_char_t *
104 lxb_tag_name_by_id_noi(lexbor_hash_t *hash, lxb_tag_id_t tag_id,
105 size_t *len);
106
107 LXB_API const lxb_char_t *
108 lxb_tag_name_upper_by_id_noi(lexbor_hash_t *hash,
109 lxb_tag_id_t tag_id, size_t *len);
110
111 LXB_API lxb_tag_id_t
112 lxb_tag_id_by_name_noi(lexbor_hash_t *hash,
113 const lxb_char_t *name, size_t len);
114
115 LXB_API lexbor_mraw_t *
116 lxb_tag_mraw_noi(lexbor_hash_t *hash);
117
118
119 #ifdef __cplusplus
120 } /* extern "C" */
121 #endif
122
123 #endif /* LEXBOR_TAG_H */
124