Previous topic

Memory Allocation Example

Next topic

SDatasource Example

SList Iteration ExampleΒΆ

This code example shows how get an iterator to a container and use it to iterate through the container objects. It can also be found at speect/engine/examples/containers/list_iteration_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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include "speect.h"


int main()
{
    s_erc error = S_SUCCESS;
    SList *list = NULL;
    SIterator *itr = NULL;
    SObject *a = NULL;
    SObject *b = NULL;
    SObject *c = NULL;


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

    /* Create a new list */
    list = S_LIST(S_NEW(SListList, &error));
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to create new list"))
        goto quit;

    /*
     * get iterator to list, should be NULL as there are no objects
     * in the list
     */
    itr = S_ITERATOR_GET(list, &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to get iterator to list"))
        goto quit;

    /* Create some objects and put the into the list */
    a = SObjectSetInt(10, &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to set int object"))
        goto quit;

    b = SObjectSetFloat(3.14, &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to set float object"))
        goto quit;

    c = SObjectSetString("hello world", &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to set string object"))
        goto quit;

    SListPush(list, a, &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to add object to list"))
        goto quit;
    else
        a = NULL; /* object belongs to list now, we don't want to
                   * delete it directly.
                   */

    SListPush(list, b, &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to add object to list"))
        goto quit;
    else
        b = NULL; /* object belongs to list now, we don't want to
                   * delete it directly.
                   */

    SListPush(list, c, &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to add object to list"))
        goto quit;
    else
        c = NULL; /* object belongs to list now, we don't want to
                   * delete it directly.
                   */

    /*
     * get iterator to list, should not be NULL as there are now
     * objects in the list
     */
    itr = S_ITERATOR_GET(list, &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to get iterator to list"))
        goto quit;

    /* iterate through list objects and print them to stdout */
    for (/* NOP */; itr != NULL; itr = SIteratorNext(itr))
    {
        char *buf;
        const SObject *tmp;


        tmp = SIteratorObject(itr, &error);
        if (S_CHK_ERR(&error,  S_CONTERR,
                      "main",
                      "Failed to get list iterator object"))
            goto quit;

        buf = SObjectPrint(tmp, &error);
        if (S_CHK_ERR(&error,  S_CONTERR,
                      "main",
                      "Failed to print  object"))
            goto quit;

        printf("list object = %s\n", buf);
        S_FREE(buf);
    }

quit:
    if (list != NULL)
        S_DELETE(list, "main", &error);

    if (itr != NULL)
        S_DELETE(itr, "main", &error);

    if (a != NULL)
        S_DELETE(a, "main", &error);

    if (b != NULL)
        S_DELETE(b, "main", &error);

    if (c != NULL)
        S_DELETE(c, "main", &error);

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

    return 0;
}