LCOV - code coverage report
Current view: top level - src - schema.c (source / functions) Hit Total Coverage
Test: openitemdef coverage Lines: 47 54 87.0 %
Date: 2025-12-10 02:36:41 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "schema.h"
       2             : #include "steam_types.h"
       3             : 
       4             : #include <jansson.h>
       5             : 
       6             : struct oid_schema_t
       7             : {
       8             :     oid_itemdef_t* item_defs;    /* parsed item definitions */
       9             :     size_t item_defs_capacity;   /* allocated item definitions */
      10             :     size_t item_defs_size;       /* amount of item definitions */
      11             : 
      12             :     json_error_t last_json_err; /* latest jansson parsing error */
      13             : };
      14             : 
      15           9 : oid_schema_t* oid_init_schema(void)
      16             : {
      17           9 :     return calloc(1, sizeof(oid_schema_t));
      18             : }
      19             : 
      20           9 : void oid_free_schema(oid_schema_t* schema)
      21             : {
      22           9 :     if (schema)
      23             :     {
      24           9 :         oid_free_item_defs(schema);
      25           9 :         free(schema);
      26             :     }
      27           9 : }
      28             : 
      29           1 : int oid_parse_json_to_struct(oid_schema_t* sch, json_t* jroot)
      30             : {
      31           1 :     if (!sch || !jroot || !json_is_object(jroot))
      32             :     {
      33           0 :         return 1;
      34             :     }
      35             : 
      36           1 :     oid_free_item_defs(sch);
      37             : 
      38           1 :     return 0;
      39             : }
      40             : 
      41           5 : int oid_load_itemdef_schema(oid_schema_t* sch, const char* file_path)
      42             : {
      43           5 :     if (!sch || !file_path)
      44             :     {
      45           2 :         return 1;
      46             :     }
      47             : 
      48             :     json_error_t json_err;
      49           3 :     json_t* root = json_load_file(file_path, 0, &json_err);
      50           3 :     if(!root)
      51             :     {
      52           2 :         oid_set_json_error(sch, &json_err);
      53           2 :         return 2;
      54             :     }
      55             : 
      56           1 :     int pres = oid_parse_json_to_struct(sch, root);
      57             : 
      58           1 :     json_decref(root);
      59           1 :     return pres;
      60             : }
      61             : 
      62           2 : int oid_alloc_item_defs(oid_schema_t* sch, size_t size)
      63             : {
      64           2 :     if (!sch)
      65             :     {
      66           0 :         return 1;
      67             :     }
      68             : 
      69           2 :     size_t newsz = sch->item_defs_capacity + size;
      70           2 :     oid_itemdef_t* tmp = realloc(sch->item_defs, newsz * sizeof(oid_itemdef_t));
      71           2 :     if (!tmp)
      72             :     {
      73           0 :         return 2;
      74             :     }
      75             :     
      76           2 :     sch->item_defs = tmp;
      77           2 :     sch->item_defs_capacity = newsz;
      78           2 :     return 0;
      79             : }
      80             : 
      81          10 : void oid_free_item_defs(oid_schema_t* sch)
      82             : {
      83          10 :     if (!sch)
      84             :     {
      85           0 :         return;
      86             :     }
      87             : 
      88          10 :     free(sch->item_defs);
      89          10 :     sch->item_defs_capacity = 0;
      90          10 :     sch->item_defs_size = 0;
      91             : }
      92             : 
      93           2 : size_t oid_get_schema_capacity(const oid_schema_t* sch)
      94             : {
      95           2 :     if (!sch)
      96             :     {
      97           0 :         return 0;
      98             :     }
      99             : 
     100           2 :     return sch->item_defs_capacity;
     101             : }
     102             : 
     103           2 : size_t oid_get_schema_size(const oid_schema_t* sch)
     104             : {
     105           2 :     if (!sch)
     106             :     {
     107           0 :         return 0;
     108             :     }
     109             : 
     110           2 :     return sch->item_defs_size;
     111             : }
     112             : 
     113             : /* ERROR HANDLING */
     114             : 
     115           3 : const json_error_t* oid_get_last_json_err(const oid_schema_t* sch)
     116             : {
     117           3 :     if (!sch)
     118             :     {
     119           1 :         return NULL;
     120             :     }
     121             : 
     122           2 :     return &sch->last_json_err;
     123             : }
     124             : 
     125           3 : void oid_set_json_error(oid_schema_t* sch, const json_error_t* err)
     126             : {
     127           3 :     if (!sch || !err)
     128             :     {
     129           0 :         return;
     130             :     }
     131             : 
     132           3 :     sch->last_json_err = *err;
     133             : }

Generated by: LCOV version 1.14