Previous topic

SList Iteration Example

Next topic

Heterogeneous Relation Graphs Example

SDatasource ExampleΒΆ

This code example shows the usage of the SDatasource API. It can also be found at speect/engine/examples/datasources/datasource_example.c, and be compiled with the WANT_EXAMPLES build option.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include "speect.h"


int main()
{
    s_erc error = S_SUCCESS;
    SDatasource *ds;
    const char *text = "hello world";

    /*
     * initialize speect
     */
    error = speect_init(NULL);
    if (error != S_SUCCESS)
    {
        printf("Failed to initialize Speect\n");
        return 1;
    }

    /*
     * open file data source
     */
    ds = SFilesourceOpenFile("hello_word.txt", "w", &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to open data source"))
        goto quit;

    /*
     * Write to data source
     */
    SDatasourceWrite(ds, text, sizeof(char), s_strsize(text, &error), &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to write to data source"))
    {
        S_DELETE(ds, "main", &error);
        goto quit;
    }

    /*
     * Delete data source, will close file as well
     */
    S_DELETE(ds, "main", &error);

quit:
    /*
     * quit speect
     */
    error = speect_quit();
    if (error != S_SUCCESS)
    {
        printf("Call to 'speect_quit' failedn");
        return 1;
    }

    return 0;
}