static bool append_value(bson_t* bson, const char* key, size_t length, object_t* value) {
    switch (value->type) {
        case type_nil:    bson_append_null(bson, key, length);                       break; 
        case type_bool:   bson_append_bool(bson, key, length, value->b);             break; 
        case type_double: bson_append_double(bson, key, length, value->d);           break; 
        case type_str:    bson_append_utf8(bson, key, length, value->str, value->l); break; 

        case type_int:    append_int(bson, key, length, value->i);                   break; 
        case type_uint:   append_int(bson, key, length, (int64_t)value->u);          break;

        case type_map: {
            bson_t child;
            bson_append_document_begin(bson, key, length, &child);
            append_document(&child, value);
            bson_append_document_end(bson, &child);
        } break;

        case type_array: {
            bson_t child;
            bson_append_array_begin(bson, key, length, &child);
            append_array(&child, value);
            bson_append_array_end(bson, &child);
        } break;

        default:
            return false;
    }
    return true;
}
bool run_test(uint32_t* hash_out) {
    bson_t bson = BSON_INITIALIZER;

    bool ok;
    if (root_object->type == type_map)
        ok = append_document(&bson, root_object);
    else
        ok = append_array(&bson, root_object);

    if (!ok) {
        fprintf(stderr, "libbson error writing data!\n");
        bson_destroy(&bson);
        return false;
    }

    *hash_out = hash_str(*hash_out, (const char*)bson_get_data(&bson), bson.len);

    // The documentation says that bson_destroy() should be called
    // regardless of whether the bson_t was initialized via bson_init()
    // bson_new() or BSON_INITIALIZER. This is because it stores a flag
    // to say whether it should be freed when destroyed.
    // This causes a warning under -flto about freeing a stack object
    // even though the bson_t is set for static.
    bson_destroy(&bson);
    return true;
}
Exemple #3
0
/*
 * hsc_write_project_file
 *
 * store all project-data in a string and write it to
 * the file specified with hsc_set_project_filename()
 */
BOOL hsc_project_write_file(HSCPRJ * hp, STRPTR project_fname)
{
    BOOL written = FALSE;

    if (hp && !hp->fatal && project_fname)
    {
        EXPSTR *prjstr = init_estr(256);
        DLNODE *nd = NULL;
        FILE *outfile = NULL;

        DP(fprintf(stderr, DHP "update project file `%s'\n",
                     project_fname));

        /* append header information */
        append_header(prjstr);

        /* append current document to project */
        hsc_project_add_document(hp);

        /*
         * append all old project info of other files
         */
        nd = dll_first(hp->documents);
        while (nd)
        {
            HSCDOC *document = (HSCDOC *) nd->data;

            append_document(prjstr, document);

            nd = dln_next(nd);
        }

        DP(fprintf(stderr, DHP "project file contains:\n%s", estr2str(prjstr)));

        /* write new project file */
        errno = 0;
        outfile = fopen(project_fname, "w");
        if (outfile)
        {
            errno = 0;
            fwrite(estr2str(prjstr), sizeof(char),
                   estrlen(prjstr), outfile);

            if (errno)
            {
                DP(fprintf(stderr, DHP "can't write project file\n"));
                /* TODO: show message "can't open project file" */
            }
            else
                written = TRUE;
        }
        else
        {
            DP(fprintf(stderr, DHP "can't open project file for output\n"));
            /* TODO: show message "can't open project file" */
        }

        del_estr(prjstr);
    }
    else
    {
        D(fprintf(stderr, DHP "no update project\n"));
    }

    return (written);
}