Skip to content

bankofcanada/DataEcon

Repository files navigation

build_linux build_windows build_macos codecov

DataEcon

This repository contains the C code for a shared library that is used under the hood of TimeSeriesEcon.jl for reading and writing time series data in Julia.

Using DataEcon in Julia

From Julia simply install the TimeSeriesEcon package.

] add TimeSeriesEcon

In TimeSeriesEcon you will find the sub-module DataEcon.

Example:

using TimeSeriesEcon
DE = TimeSeriesEcon.DataEcon

message = DE.opendaec("example.daec") do de
    DE.write_data(de, :message, "Hello World")
    DE.read_data(de, :message)
end

show(message)

Using DataEcon in C/C++

If not using Julia, the rest of this file should help you to get started.

Getting started

Use make to build the library and run the tests.

For example,

bash$ make lib

will build the library bin/libdaec.so.

Similarly,

bash$ make test

will build a test application bin/test and run it.

Alternatively, you can simply download precompiled binaries for your machine from DataEcon_jll.

Example

To build your own executable, compile it with #include "include/daec.h", and link it against lib/libdaec.so library.

For example:

// example.c
#include "daec.h"

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    de_file de;
    int rc;

    rc = de_open("example.daec", &de);
    if (rc != DE_SUCCESS)
    {
        fprintf(stderr, "Failed to open the file.\n");
        return EXIT_FAILURE;
    }

    char message[] = "Hello World";
    rc = de_store_scalar(de, 0, "message", type_string, freq_none,
                         sizeof message + 1, message, NULL);
    if (rc != DE_SUCCESS)
    {
        fprintf(stderr, "Failed to write the message\n");
        de_close(de);
        return EXIT_FAILURE;
    }

    obj_id_t id;
    rc = de_find_fullpath(de, "/message", &id);
    if (rc != DE_SUCCESS)
    {
        fprintf(stderr, "Failed to find the message.\n");
        de_close(de);
        return EXIT_FAILURE;
    }

    scalar_t scalar;
    rc = de_load_scalar(de, id, &scalar);
    if (rc != DE_SUCCESS)
    {
        fprintf(stderr, "Failed to read the message.\n");
        de_close(de);
        return EXIT_FAILURE;
    }

    printf("%s\n", (char *)scalar.value);

    de_close(de);
    return EXIT_SUCCESS;
}
bash$ gcc -I ./include -L lib -Wl,-rpath,$(pwd)/lib -ldaec example.c -o example
bash$ ./example
Hello World