static int register_internal(struct pbc_env * p, void *buffer, int size) { struct pbc_pattern * FIELD_T = _pbcP_new(8); F(0,name,string); F(1,id,int32); F(2,label,int32); F(3,type,int32); F(4,type_name,string); F(5,default_integer,int32); F(6,default_string,string); F(7,default_real,double); struct pbc_pattern * FILE_T = _pbcP_new(10); D(0,name,string); D(1,dependency,string_array); D(2,message_name,string_array); D(3,message_size,int32_array); D(4,message_field,message_array); D(5,enum_name,string_array); D(6,enum_size,int32_array); D(7,enum_string,string_array); D(8,enum_id,int32_array); int ret = 0; struct file_t file; int r = pbc_pattern_unpack(FILE_T, buffer, size, &file); if (r != 0) { ret = 1; goto _return; } _pbcM_sp_insert(p->files , file.name->s.str, NULL); pbc_array queue; _pbcA_open(queue); set_enums(p, &file); set_msgs(FIELD_T, p, &file, queue); _pbcB_register_fields(p, queue); _pbcA_close(queue); pbc_pattern_close_arrays(FILE_T, &file); _return: free(FIELD_T); free(FILE_T); return ret; }
struct pbc_pattern * pbc_pattern_new(struct pbc_env * env , const char * message, const char * format, ... ) { struct _message *m = _pbcP_get_message(env, message); if (m==NULL) { return NULL; } int len = strlen(format); char temp[len+1]; int n = _scan_pattern(format, temp); struct pbc_pattern * pat = _pbcP_new(n); int i; va_list ap; va_start(ap , format); const char *ptr = temp; for (i=0;i<n;i++) { struct _pattern_field * f = &(pat->f[i]); struct _field * field = _pbcM_sp_query(m->name, ptr); if (field == NULL) goto _error; f->id = field->id; f->ptype = field->type; *f->defv = *field->default_v; f->offset = va_arg(ap, int); ptr += strlen(ptr) + 1; f->ctype = _ctype(ptr); if (f->ctype < 0) goto _error; if (f->ctype == CTYPE_ARRAY && field->label == LABEL_PACKED) { f->ctype = CTYPE_PACKED; } if (_check_ctype(field, f)) goto _error; ptr += strlen(ptr) + 1; } va_end(ap); pat->count = n; qsort(pat->f , n , sizeof(struct _pattern_field), _comp_field); return pat; _error: free(pat); return NULL; }