Changeset 9c5e3a5 in mainline for uspace/lib/sif/src/sif.c


Ignore:
Timestamp:
2018-08-01T11:07:26Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1c398db2
Parents:
b79903b
git-author:
Jiri Svoboda <jiri@…> (2018-07-31 18:06:53)
git-committer:
Jiri Svoboda <jiri@…> (2018-08-01 11:07:26)
Message:

SIF node unmarshalling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/sif/src/sif.c

    rb79903b r9c5e3a5  
    6868
    6969static errno_t sif_export_node(sif_node_t *, FILE *);
     70static errno_t sif_import_node(sif_node_t *, FILE *, sif_node_t **);
    7071
    7172/** Create new SIF node.
     
    169170                return ENOMEM;
    170171
    171         root = sif_node_new(NULL);
    172         if (root == NULL) {
    173                 rc = ENOMEM;
    174                 goto error;
    175         }
    176 
    177         root->ntype = str_dup("sif");
    178         if (root->ntype == NULL) {
    179                 rc = ENOMEM;
    180                 goto error;
    181         }
    182 
    183172        f = fopen(fname, "r+");
    184173        if (f == NULL) {
     
    186175                goto error;
    187176        }
     177
     178        rc = sif_import_node(NULL, f, &root);
     179        if (rc != EOK)
     180                goto error;
     181
     182        if (str_cmp(root->ntype, "sif") != 0) {
     183                rc = EIO;
     184                goto error;
     185        }
     186
     187        sess->root = root;
    188188
    189189        sess->f = f;
     
    534534}
    535535
     536/** Import string from file.
     537 *
     538 * Import string from file (the string in the file must be
     539 * properly bracketed and escaped).
     540 *
     541 * @param f File
     542 * @param rstr Place to store pointer to newly allocated string
     543 * @return EOK on success, EIO on I/O error
     544 */
     545static errno_t sif_import_string(FILE *f, char **rstr)
     546{
     547        char *str;
     548        size_t str_size;
     549        size_t sidx;
     550        char c;
     551        errno_t rc;
     552
     553        str_size = 1;
     554        sidx = 0;
     555        str = malloc(str_size + 1);
     556        if (str == NULL)
     557                return ENOMEM;
     558
     559        c = fgetc(f);
     560        if (c != '[') {
     561                rc = EIO;
     562                goto error;
     563        }
     564
     565        while (true) {
     566                c = fgetc(f);
     567                if (c == EOF) {
     568                        rc = EIO;
     569                        goto error;
     570                }
     571
     572                if (c == ']')
     573                        break;
     574
     575                if (c == '\\') {
     576                        c = fgetc(f);
     577                        if (c == EOF) {
     578                                rc = EIO;
     579                                goto error;
     580                        }
     581                }
     582
     583                if (sidx >= str_size) {
     584                        str_size *= 2;
     585                        str = realloc(str, str_size + 1);
     586                        if (str == NULL) {
     587                                rc = ENOMEM;
     588                                goto error;
     589                        }
     590                }
     591
     592                str[sidx++] = c;
     593        }
     594
     595        str[sidx] = '\0';
     596        *rstr = str;
     597        return EOK;
     598error:
     599        free(str);
     600        return rc;
     601}
     602
    536603/** Export SIF node to file.
    537604 *
     
    567634}
    568635
     636/** Import SIF node from file.
     637 *
     638 * @param parent Parent node
     639 * @param f File
     640 * @param rnode Place to store pointer to imported node
     641 * @return EOK on success, EIO on I/O error
     642 */
     643static errno_t sif_import_node(sif_node_t *parent, FILE *f, sif_node_t **rnode)
     644{
     645        errno_t rc;
     646        sif_node_t *node = NULL;
     647        sif_node_t *child;
     648        char *ntype;
     649        char c;
     650
     651        node = sif_node_new(parent);
     652        if (node == NULL)
     653                return ENOMEM;
     654
     655        rc = sif_import_string(f, &ntype);
     656        if (rc != EOK)
     657                goto error;
     658
     659        node->ntype = ntype;
     660
     661        c = fgetc(f);
     662        if (c != '{') {
     663                rc = EIO;
     664                goto error;
     665        }
     666
     667        c = fgetc(f);
     668        if (c == EOF) {
     669                rc = EIO;
     670                goto error;
     671        }
     672
     673        while (c != '}') {
     674                ungetc(c, f);
     675
     676                rc = sif_import_node(node, f, &child);
     677                if (rc != EOK)
     678                        goto error;
     679
     680                list_append(&child->lparent, &node->children);
     681
     682                c = fgetc(f);
     683                if (c == EOF) {
     684                        rc = EIO;
     685                        goto error;
     686                }
     687        }
     688
     689        *rnode = node;
     690        return EOK;
     691error:
     692        sif_node_delete(node);
     693        return rc;
     694}
     695
    569696/** @}
    570697 */
Note: See TracChangeset for help on using the changeset viewer.