| 472 | |
| 473 | |
| 474 | static inline int _json_to_cols(cJSON *Jcols, db_key_t** _c) |
| 475 | { |
| 476 | static db_key_t *cols; |
| 477 | static unsigned int cols_size = 0; |
| 478 | cJSON *col; |
| 479 | str *str_cols; |
| 480 | int nc, i; |
| 481 | |
| 482 | if (Jcols->type != cJSON_Array) { |
| 483 | LM_ERR("bad JSON format, 'cols' must be an array\n"); |
| 484 | goto error; |
| 485 | } |
| 486 | |
| 487 | /* iterate the columns to check and validate */ |
| 488 | for( col=Jcols->child,nc=0 ; col ; col=col->next,nc++ ) { |
| 489 | if (col->type!=cJSON_String) { |
| 490 | LM_ERR("bad JSON format, 'cols' elements must be strings\n"); |
| 491 | goto error; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /* resize the array of cols if we need more */ |
| 496 | if (nc>cols_size) { |
| 497 | /* need a larger set of cols/keys */ |
| 498 | cols = (db_key_t*)pkg_realloc( cols, nc*(sizeof(db_key_t)+sizeof(str)) ); |
| 499 | if (cols==NULL) { |
| 500 | cols_size = 0; |
| 501 | LM_ERR("failed to allocate the needed %d keys/cols\n",nc); |
| 502 | goto error; |
| 503 | } |
| 504 | str_cols = (str*)( cols+nc ); |
| 505 | /* link db_keys to strs */ |
| 506 | for ( i=0 ; i<nc ; i++) |
| 507 | cols[i] = &str_cols[i]; |
| 508 | |
| 509 | cols_size = nc; |
| 510 | } |
| 511 | |
| 512 | /* iterate again to fill in the cols */ |
| 513 | for( col=Jcols->child,i=0 ; col ; col=col->next,i++ ) { |
| 514 | cols[i]->s = col->valuestring; |
| 515 | cols[i]->len = strlen(col->valuestring); |
| 516 | } |
| 517 | |
| 518 | *_c = cols; |
| 519 | |
| 520 | return nc; |
| 521 | error: |
| 522 | *_c = NULL; |
| 523 | return -1; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | static inline int _json_to_filters(cJSON *Jfilter, |