10 * Construct a new empty reference.
13 asn1p_ref_new(int _lineno, struct asn1p_module_s *mod) {
16 ref = calloc(1, sizeof *ref);
18 asn1p_ref_set_source(ref, mod, _lineno);
24 asn1p_ref_free(asn1p_ref_t *ref) {
27 size_t i = ref->comp_count;
29 free(ref->components[i].name);
30 ref->components[i].name = 0;
32 free(ref->components);
41 asn1p_ref_set_source(asn1p_ref_t *ref, struct asn1p_module_s *module,
45 ref->_lineno = lineno;
49 static enum asn1p_ref_lex_type_e
50 asn1p_ref_name2lextype(const char *name) {
51 enum asn1p_ref_lex_type_e lex_type;
52 int has_lowercase = 0;
55 if(name[1] >= 'A' && name[1] <= 'Z') {
56 lex_type = RLT_AmpUppercase;
58 lex_type = RLT_Amplowercase;
60 } else if(*name >= 'A' && *name <= 'Z') {
63 for(p = name; *p; p++) {
64 if(*p >= 'a' && *p <= 'z') {
71 lex_type = RLT_Uppercase;
73 lex_type = RLT_CAPITALS;
75 } else if(*name == '@') {
77 lex_type = RLT_AtDotlowercase;
79 lex_type = RLT_Atlowercase;
81 lex_type = RLT_lowercase;
88 asn1p_ref_add_component(asn1p_ref_t *ref, const char *name, enum asn1p_ref_lex_type_e lex_type) {
91 || (int)lex_type < RLT_UNKNOWN || lex_type >= RLT_MAX) {
96 if(ref->comp_count == ref->comp_size) {
97 int newsize = ref->comp_size?(ref->comp_size<<2):4;
98 void *p = realloc(ref->components,
99 newsize * sizeof(ref->components[0]));
102 ref->comp_size = newsize;
109 if(lex_type == RLT_UNKNOWN) {
110 lex_type = asn1p_ref_name2lextype(name);
112 assert(lex_type == asn1p_ref_name2lextype(name));
115 ref->components[ref->comp_count].name = strdup(name);
116 ref->components[ref->comp_count].lex_type = lex_type;
117 if(ref->components[ref->comp_count].name) {
126 asn1p_ref_clone(asn1p_ref_t *ref) {
129 newref = asn1p_ref_new(ref->_lineno, ref->module);
131 for(size_t i = 0; i < ref->comp_count; i++) {
132 if(asn1p_ref_add_component(newref,
133 ref->components[i].name,
134 ref->components[i].lex_type
136 asn1p_ref_free(newref);
147 asn1p_ref_compare(const asn1p_ref_t *a, const asn1p_ref_t *b) {
148 if(a->comp_count != b->comp_count)
150 if(a->module != b->module)
153 for(size_t i = 0; i < a->comp_count; i++) {
154 if(a->components[i].lex_type != b->components[i].lex_type
155 || strcmp(a->components[i].name, b->components[i].name) != 0) {
164 asn1p_ref_string(const asn1p_ref_t *ref) {
165 static char static_buf[32];
166 static char *buf = static_buf;
167 static size_t buf_size = sizeof(static_buf);
170 if(!ref) return "<no-ref>";
172 for(size_t i = 0; i < ref->comp_count; i++) {
173 size_t space = buf_size - (p - buf);
175 snprintf(p, space, "%s%s", i ? "." : "", ref->components[i].name);
176 if(ret < 0 || (size_t)ret >= space) {
178 char *tmp = malloc(buf_size * 2 + 1);
180 size_t p_offset = p - buf;
181 memcpy(tmp, buf, (p - buf));
182 if(buf != static_buf) free(buf);