gdsl  1.8
examples/main_2darray.c

This is an example of how to use gdsl_2darray module.

/*
 * This file is part of the Generic Data Structures Library (GDSL).
 * Copyright (C) 1998-2018 Nicolas Darnis <ndarnis@free.fr>.
 *
 * GDSL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GDSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GDSL.  If not, see <http://www.gnu.org/licenses/>. 
 *
 *
 * GDSL - Generic Data Structures Library
 * $RCSfile: main_2darray.c,v $
 * $Revision: 1.12 $
 * $Date: 2015/02/17 12:33:16 $
 */


#include <config.h>


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


#include "gdsl_2darray.h"
#include "_integers.h"


#define ROWS_NB 4UL
#define COLS_NB 3UL


static void 
my_display_integer (const gdsl_element_t e, FILE* file, gdsl_location_t position, void* user_data)
{
    int* n = (int*) e;

    if (position & GDSL_LOCATION_FIRST_COL)
    {
        if (position & GDSL_LOCATION_FIRST_ROW)
        {
            fprintf (file, "{\n");
        }

        fprintf (file, "\t( ");
    }
    else
    {
        fprintf (file, " ");
    }

    fprintf (file, "%02d", *n);

    if (position & GDSL_LOCATION_LAST_COL)
    {
        fprintf (file, " )\n");

        if (position & GDSL_LOCATION_LAST_ROW)
        {
            fprintf (file, "\t}\n");
        }
    }
}

int main (void)
{
    int i, j, k = 0;
    gdsl_2darray_t m = gdsl_2darray_alloc ("MY ARRAY", ROWS_NB, COLS_NB, alloc_integer, free_integer);

    for (i = 0; i < ROWS_NB; i++)
    {
        for (j = 0; j < COLS_NB; j++)
        {
            int n = ++k;
            gdsl_2darray_set_content (m, i, j, (void*) &n);
        }
    }

    printf ("%s (%ld x %ld) = ", gdsl_2darray_get_name (m),
        gdsl_2darray_get_rows_number (m), 
        gdsl_2darray_get_columns_number (m));

    gdsl_2darray_write (m, my_display_integer, stdout, NULL);
    gdsl_2darray_write_xml (m, my_display_integer, stdout, NULL);
    gdsl_2darray_dump (m, my_display_integer, stdout, NULL);

    gdsl_2darray_free (m);

    exit (EXIT_SUCCESS);
}