Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <errno.h> |
| 5 | #include <assert.h> |
| 6 | |
| 7 | #include "asn1parser.h" |
| 8 | |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 9 | asn1p_ioc_row_t * |
| 10 | asn1p_ioc_row_new(asn1p_expr_t *oclass) { |
| 11 | asn1p_ioc_row_t *row; |
| 12 | asn1p_expr_t *field; |
| 13 | int columns = 0; |
| 14 | |
| 15 | assert(oclass->expr_type == A1TC_CLASSDEF); |
| 16 | |
| 17 | row = calloc(1, sizeof *row); |
| 18 | if(!row) return NULL; |
| 19 | |
| 20 | TQ_FOR(field, &oclass->members, next) |
| 21 | columns++; |
| 22 | |
| 23 | row->column = calloc(columns, sizeof *row->column); |
| 24 | if(!row->column) { |
| 25 | free(row); |
| 26 | return NULL; |
| 27 | } |
| 28 | row->columns = columns; |
| 29 | |
| 30 | columns = 0; |
| 31 | TQ_FOR(field, &oclass->members, next) { |
| 32 | int fieldIdLen = strlen(field->Identifier); |
| 33 | if(fieldIdLen > row->max_identifier_length) |
| 34 | row->max_identifier_length = fieldIdLen; |
| 35 | row->column[columns].field = field; |
| 36 | row->column[columns].value = NULL; |
| 37 | columns++; |
| 38 | } |
| 39 | |
| 40 | return row; |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | asn1p_ioc_row_delete(asn1p_ioc_row_t *row) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 45 | int i; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 46 | if(row) { |
| 47 | if(row->column) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 48 | for(i = 0; i < row->columns; i++) { |
| 49 | if(!row->column[i].new_ref && row->column[i].value) { |
| 50 | /* |
| 51 | * Field 'reference' comes from asn1fix_cws.c : |
| 52 | * TQ_FIRST(&cell->field->members)->reference |
| 53 | * so it should not be freed here. |
| 54 | */ |
| 55 | row->column[i].value->reference = NULL; |
| 56 | } |
| 57 | asn1p_expr_free(row->column[i].value); |
| 58 | } |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 59 | free(row->column); |
| 60 | } |
| 61 | free(row); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | struct asn1p_ioc_cell_s * |
| 66 | asn1p_ioc_row_cell_fetch(asn1p_ioc_row_t *row, const char *fieldname) { |
| 67 | int i; |
| 68 | for(i = 0; i < row->columns; i++) { |
| 69 | if(strcmp(row->column[i].field->Identifier, fieldname) == 0) |
| 70 | return &row->column[i]; |
| 71 | } |
| 72 | errno = ESRCH; |
| 73 | return NULL; |
| 74 | } |
| 75 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | asn1p_wsyntx_chunk_t * |
| 77 | asn1p_wsyntx_chunk_new() { |
| 78 | asn1p_wsyntx_chunk_t *wc; |
| 79 | |
| 80 | wc = calloc(1, sizeof(*wc)); |
| 81 | |
| 82 | return wc; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t *wc) { |
| 87 | if(wc) { |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 88 | switch(wc->type) { |
Lev Walkin | 57074f1 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 89 | case WC_LITERAL: |
| 90 | case WC_WHITESPACE: |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 91 | case WC_FIELD: |
Lev Walkin | 57074f1 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 92 | free(wc->content.token); break; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 93 | case WC_OPTIONALGROUP: |
| 94 | asn1p_wsyntx_free(wc->content.syntax); |
| 95 | break; |
| 96 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 97 | free(wc); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | asn1p_wsyntx_chunk_t * |
| 102 | asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t *wc) { |
| 103 | asn1p_wsyntx_chunk_t *nc; |
| 104 | |
| 105 | nc = asn1p_wsyntx_chunk_new(); |
| 106 | if(nc) { |
Lev Walkin | 57074f1 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 107 | nc->type = wc->type; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 108 | switch(wc->type) { |
| 109 | case WC_LITERAL: |
Lev Walkin | 57074f1 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 110 | case WC_WHITESPACE: |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 111 | case WC_FIELD: |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 112 | nc->content.token = malloc(strlen(wc->content.token)+1); |
| 113 | strcpy(nc->content.token, wc->content.token); |
| 114 | break; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 115 | case WC_OPTIONALGROUP: |
| 116 | nc->content.syntax = asn1p_wsyntx_clone(wc->content.syntax); |
| 117 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | return nc; |
| 122 | } |
| 123 | |
| 124 | asn1p_wsyntx_t * |
| 125 | asn1p_wsyntx_new() { |
| 126 | asn1p_wsyntx_t *wx; |
| 127 | |
| 128 | wx = calloc(1, sizeof(*wx)); |
| 129 | if(wx) { |
| 130 | TQ_INIT(&(wx->chunks)); |
| 131 | } |
| 132 | |
| 133 | return wx; |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | asn1p_wsyntx_free(asn1p_wsyntx_t *wx) { |
| 138 | if(wx) { |
| 139 | asn1p_wsyntx_chunk_t *wc; |
| 140 | while((wc = TQ_REMOVE(&(wx->chunks), next))) |
| 141 | asn1p_wsyntx_chunk_free(wc); |
| 142 | free(wx); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | asn1p_wsyntx_t * |
| 147 | asn1p_wsyntx_clone(asn1p_wsyntx_t *wx) { |
| 148 | asn1p_wsyntx_t *nw; |
| 149 | |
| 150 | nw = asn1p_wsyntx_new(); |
| 151 | if(nw) { |
| 152 | asn1p_wsyntx_chunk_t *wc; |
| 153 | asn1p_wsyntx_chunk_t *nc; |
| 154 | TQ_FOR(wc, &(wx->chunks), next) { |
| 155 | nc = asn1p_wsyntx_chunk_clone(wc); |
| 156 | if(nc) { |
| 157 | TQ_ADD(&(nw->chunks), nc, next); |
| 158 | } else { |
| 159 | asn1p_wsyntx_free(nw); |
| 160 | return NULL; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return nw; |
| 166 | } |
| 167 | |
| 168 | asn1p_wsyntx_chunk_t * |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 169 | asn1p_wsyntx_chunk_fromstring(char *token, int do_copy) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 170 | asn1p_wsyntx_chunk_t *wc; |
| 171 | |
| 172 | if(do_copy) { |
| 173 | static asn1p_wsyntx_chunk_t tmp; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 174 | tmp.type = WC_LITERAL; |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 175 | tmp.content.token = token; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 176 | wc = asn1p_wsyntx_chunk_clone(&tmp); |
| 177 | } else { |
| 178 | wc = asn1p_wsyntx_chunk_new(); |
| 179 | if(wc) { |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 180 | wc->type = WC_LITERAL; |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 181 | wc->content.token = token; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | return wc; |
| 186 | } |
| 187 | |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 188 | |
| 189 | asn1p_wsyntx_chunk_t * |
| 190 | asn1p_wsyntx_chunk_fromsyntax(asn1p_wsyntx_t *syntax) { |
| 191 | asn1p_wsyntx_chunk_t *wc; |
| 192 | |
| 193 | wc = asn1p_wsyntx_chunk_new(); |
| 194 | if(wc) { |
| 195 | wc->type = WC_OPTIONALGROUP; |
| 196 | wc->content.syntax = syntax; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 197 | syntax->parent = wc; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return wc; |
| 201 | } |
| 202 | |