blob: c6e6c3d74accb39f20aa84fd95a60ac7aeb775db [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#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
vlma6a84d72006-03-16 10:03:35 +00009asn1p_ioc_row_t *
10asn1p_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
43void
44asn1p_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
53struct asn1p_ioc_cell_s *
54asn1p_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
vlmfa67ddc2004-06-03 03:38:44 +000064asn1p_wsyntx_chunk_t *
65asn1p_wsyntx_chunk_new() {
66 asn1p_wsyntx_chunk_t *wc;
67
68 wc = calloc(1, sizeof(*wc));
69
70 return wc;
71}
72
73void
74asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t *wc) {
75 if(wc) {
vlm808411d2006-03-14 16:31:37 +000076 switch(wc->type) {
vlmeeb3c512006-03-16 05:11:14 +000077 case WC_LITERAL:
78 case WC_WHITESPACE:
vlma6a84d72006-03-16 10:03:35 +000079 case WC_FIELD:
vlmeeb3c512006-03-16 05:11:14 +000080 free(wc->content.token); break;
vlm808411d2006-03-14 16:31:37 +000081 case WC_OPTIONALGROUP:
82 asn1p_wsyntx_free(wc->content.syntax);
83 break;
84 }
vlmfa67ddc2004-06-03 03:38:44 +000085 free(wc);
86 }
87}
88
89asn1p_wsyntx_chunk_t *
90asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t *wc) {
91 asn1p_wsyntx_chunk_t *nc;
92
93 nc = asn1p_wsyntx_chunk_new();
94 if(nc) {
vlmeeb3c512006-03-16 05:11:14 +000095 nc->type = wc->type;
vlm808411d2006-03-14 16:31:37 +000096 switch(wc->type) {
97 case WC_LITERAL:
vlmeeb3c512006-03-16 05:11:14 +000098 case WC_WHITESPACE:
vlma6a84d72006-03-16 10:03:35 +000099 case WC_FIELD:
vlm808411d2006-03-14 16:31:37 +0000100 nc->content.token = malloc(strlen(wc->content.token)+1);
101 strcpy(nc->content.token, wc->content.token);
102 break;
vlm808411d2006-03-14 16:31:37 +0000103 case WC_OPTIONALGROUP:
104 nc->content.syntax = asn1p_wsyntx_clone(wc->content.syntax);
105 break;
vlmfa67ddc2004-06-03 03:38:44 +0000106 }
107 }
108
109 return nc;
110}
111
112asn1p_wsyntx_t *
113asn1p_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
124void
125asn1p_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
134asn1p_wsyntx_t *
135asn1p_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
156asn1p_wsyntx_chunk_t *
vlm1fcf7592006-08-18 02:27:55 +0000157asn1p_wsyntx_chunk_fromstring(char *token, int do_copy) {
vlmfa67ddc2004-06-03 03:38:44 +0000158 asn1p_wsyntx_chunk_t *wc;
159
160 if(do_copy) {
161 static asn1p_wsyntx_chunk_t tmp;
vlm808411d2006-03-14 16:31:37 +0000162 tmp.type = WC_LITERAL;
vlm1fcf7592006-08-18 02:27:55 +0000163 tmp.content.token = token;
vlmfa67ddc2004-06-03 03:38:44 +0000164 wc = asn1p_wsyntx_chunk_clone(&tmp);
165 } else {
166 wc = asn1p_wsyntx_chunk_new();
167 if(wc) {
vlm808411d2006-03-14 16:31:37 +0000168 wc->type = WC_LITERAL;
vlm1fcf7592006-08-18 02:27:55 +0000169 wc->content.token = token;
vlmfa67ddc2004-06-03 03:38:44 +0000170 }
171 }
172
173 return wc;
174}
175
vlm808411d2006-03-14 16:31:37 +0000176
177asn1p_wsyntx_chunk_t *
178asn1p_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