vlm | fa67ddc | 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 | |
vlm | a6a84d7 | 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) { |
| 45 | if(row) { |
| 46 | if(row->column) { |
| 47 | free(row->column); |
| 48 | } |
| 49 | free(row); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | struct asn1p_ioc_cell_s * |
| 54 | asn1p_ioc_row_cell_fetch(asn1p_ioc_row_t *row, const char *fieldname) { |
| 55 | int i; |
| 56 | for(i = 0; i < row->columns; i++) { |
| 57 | if(strcmp(row->column[i].field->Identifier, fieldname) == 0) |
| 58 | return &row->column[i]; |
| 59 | } |
| 60 | errno = ESRCH; |
| 61 | return NULL; |
| 62 | } |
| 63 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 64 | asn1p_wsyntx_chunk_t * |
| 65 | asn1p_wsyntx_chunk_new() { |
| 66 | asn1p_wsyntx_chunk_t *wc; |
| 67 | |
| 68 | wc = calloc(1, sizeof(*wc)); |
| 69 | |
| 70 | return wc; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t *wc) { |
| 75 | if(wc) { |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 76 | switch(wc->type) { |
vlm | eeb3c51 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 77 | case WC_LITERAL: |
| 78 | case WC_WHITESPACE: |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 79 | case WC_FIELD: |
vlm | eeb3c51 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 80 | free(wc->content.token); break; |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 81 | case WC_OPTIONALGROUP: |
| 82 | asn1p_wsyntx_free(wc->content.syntax); |
| 83 | break; |
| 84 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 85 | free(wc); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | asn1p_wsyntx_chunk_t * |
| 90 | asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t *wc) { |
| 91 | asn1p_wsyntx_chunk_t *nc; |
| 92 | |
| 93 | nc = asn1p_wsyntx_chunk_new(); |
| 94 | if(nc) { |
vlm | eeb3c51 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 95 | nc->type = wc->type; |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 96 | switch(wc->type) { |
| 97 | case WC_LITERAL: |
vlm | eeb3c51 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 98 | case WC_WHITESPACE: |
vlm | a6a84d7 | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 99 | case WC_FIELD: |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 100 | nc->content.token = malloc(strlen(wc->content.token)+1); |
| 101 | strcpy(nc->content.token, wc->content.token); |
| 102 | break; |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 103 | case WC_OPTIONALGROUP: |
| 104 | nc->content.syntax = asn1p_wsyntx_clone(wc->content.syntax); |
| 105 | break; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | return nc; |
| 110 | } |
| 111 | |
| 112 | asn1p_wsyntx_t * |
| 113 | asn1p_wsyntx_new() { |
| 114 | asn1p_wsyntx_t *wx; |
| 115 | |
| 116 | wx = calloc(1, sizeof(*wx)); |
| 117 | if(wx) { |
| 118 | TQ_INIT(&(wx->chunks)); |
| 119 | } |
| 120 | |
| 121 | return wx; |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | asn1p_wsyntx_free(asn1p_wsyntx_t *wx) { |
| 126 | if(wx) { |
| 127 | asn1p_wsyntx_chunk_t *wc; |
| 128 | while((wc = TQ_REMOVE(&(wx->chunks), next))) |
| 129 | asn1p_wsyntx_chunk_free(wc); |
| 130 | free(wx); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | asn1p_wsyntx_t * |
| 135 | asn1p_wsyntx_clone(asn1p_wsyntx_t *wx) { |
| 136 | asn1p_wsyntx_t *nw; |
| 137 | |
| 138 | nw = asn1p_wsyntx_new(); |
| 139 | if(nw) { |
| 140 | asn1p_wsyntx_chunk_t *wc; |
| 141 | asn1p_wsyntx_chunk_t *nc; |
| 142 | TQ_FOR(wc, &(wx->chunks), next) { |
| 143 | nc = asn1p_wsyntx_chunk_clone(wc); |
| 144 | if(nc) { |
| 145 | TQ_ADD(&(nw->chunks), nc, next); |
| 146 | } else { |
| 147 | asn1p_wsyntx_free(nw); |
| 148 | return NULL; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return nw; |
| 154 | } |
| 155 | |
| 156 | asn1p_wsyntx_chunk_t * |
vlm | 1fcf759 | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 157 | asn1p_wsyntx_chunk_fromstring(char *token, int do_copy) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 158 | asn1p_wsyntx_chunk_t *wc; |
| 159 | |
| 160 | if(do_copy) { |
| 161 | static asn1p_wsyntx_chunk_t tmp; |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 162 | tmp.type = WC_LITERAL; |
vlm | 1fcf759 | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 163 | tmp.content.token = token; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 164 | wc = asn1p_wsyntx_chunk_clone(&tmp); |
| 165 | } else { |
| 166 | wc = asn1p_wsyntx_chunk_new(); |
| 167 | if(wc) { |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 168 | wc->type = WC_LITERAL; |
vlm | 1fcf759 | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 169 | wc->content.token = token; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | return wc; |
| 174 | } |
| 175 | |
vlm | 808411d | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 176 | |
| 177 | asn1p_wsyntx_chunk_t * |
| 178 | asn1p_wsyntx_chunk_fromsyntax(asn1p_wsyntx_t *syntax) { |
| 179 | asn1p_wsyntx_chunk_t *wc; |
| 180 | |
| 181 | wc = asn1p_wsyntx_chunk_new(); |
| 182 | if(wc) { |
| 183 | wc->type = WC_OPTIONALGROUP; |
| 184 | wc->content.syntax = syntax; |
| 185 | } |
| 186 | |
| 187 | return wc; |
| 188 | } |
| 189 | |