Commit d863a8e8 authored by Claes Sjofors's avatar Claes Sjofors

Co, array added

parent 511210c9
/**
* Proview Open Source Process Control.
* Copyright (C) 2005-2016 SSAB EMEA AB.
*
* This file is part of Proview.
*
* This program 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 2 of
* the License, or (at your option) any later version.
*
* This program 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 Proview. If not, see <http://www.gnu.org/licenses/>
*
* Linking Proview statically or dynamically with other modules is
* making a combined work based on Proview. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* In addition, as a special exception, the copyright holders of
* Proview give you permission to, from the build function in the
* Proview Configurator, combine Proview with modules generated by the
* Proview PLC Editor to a PLC program, regardless of the license
* terms of these modules. You may copy and distribute the resulting
* combined work under the terms of your choice, provided that every
* copy of the combined work is accompanied by a complete copy of
* the source code of Proview (the version used to produce the
* combined work), being distributed under the terms of the GNU
* General Public License plus this exception.
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pwr.h"
#include "co_array.h"
int array_Init( array_tCtx *ctx, int elemsize, int alloc_incr)
{
*ctx = calloc( 1, sizeof(array_sCtx));
if ( !*ctx)
return 0;
(*ctx)->elemsize = elemsize;
(*ctx)->alloc_incr = alloc_incr;
(*ctx)->a = calloc( 1, elemsize * alloc_incr);
if ( !(*ctx)->a)
return 0;
(*ctx)->allocated = alloc_incr;
return 1;
}
void array_Close( array_tCtx ctx)
{
free( (char *)ctx->a);
free( (char *)ctx);
}
int array_Add( array_tCtx ctx, void *elem)
{
if ( ctx->allocated <= ctx->a_size) {
void *a_tmp;
ctx->allocated += ctx->alloc_incr;
a_tmp = calloc( ctx->allocated, ctx->elemsize);
if ( !a_tmp)
return 0;
memcpy( a_tmp, ctx->a, ctx->a_size * ctx->elemsize);
free( (char *) ctx->a);
ctx->a = a_tmp;
}
memcpy( (char *)ctx->a + ctx->a_size * ctx->elemsize, elem, ctx->elemsize);
ctx->a_size++;
return 1;
}
int array_MAdd( array_tCtx ctx, void *elem, int elements)
{
if ( ctx->allocated <= ctx->a_size + elements - 1) {
void *a_tmp;
ctx->allocated += ctx->alloc_incr;
if ( ctx->allocated < ctx->a_size + elements)
ctx->allocated = ctx->a_size + elements;
a_tmp = calloc( ctx->allocated, ctx->elemsize);
if ( !a_tmp)
return 0;
memcpy( a_tmp, ctx->a, ctx->a_size * ctx->elemsize);
free( (char *) ctx->a);
ctx->a = a_tmp;
}
memcpy( (char *)ctx->a + ctx->a_size * ctx->elemsize, elem, ctx->elemsize * elements);
ctx->a_size += elements;
return 1;
}
int array_Get( array_tCtx ctx, int idx, void **elem)
{
if ( idx >= ctx->a_size)
return 0;
memcpy( elem, (char *)ctx->a + idx * ctx->elemsize, ctx->elemsize);
return 1;
}
int array_Size( array_tCtx ctx)
{
return ctx->a_size;
}
void *array_Array( array_tCtx ctx)
{
return ctx->a;
}
void *array_CopyArray( array_tCtx ctx)
{
void *buf;
buf = malloc( ctx->elemsize * ctx->a_size);
memcpy( buf, ctx->a, ctx->a_size * ctx->elemsize);
return buf;
}
/**
* Proview Open Source Process Control.
* Copyright (C) 2005-2016 SSAB EMEA AB.
*
* This file is part of Proview.
*
* This program 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 2 of
* the License, or (at your option) any later version.
*
* This program 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 Proview. If not, see <http://www.gnu.org/licenses/>
*
* Linking Proview statically or dynamically with other modules is
* making a combined work based on Proview. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* In addition, as a special exception, the copyright holders of
* Proview give you permission to, from the build function in the
* Proview Configurator, combine Proview with modules generated by the
* Proview PLC Editor to a PLC program, regardless of the license
* terms of these modules. You may copy and distribute the resulting
* combined work under the terms of your choice, provided that every
* copy of the combined work is accompanied by a complete copy of
* the source code of Proview (the version used to produce the
* combined work), being distributed under the terms of the GNU
* General Public License plus this exception.
**/
#ifndef co_array_h
#define co_array_h
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __STDDEF_LOADED
#include <stddef.h>
#endif
typedef struct {
void *a;
int elemsize;
int allocated;
int alloc_incr;
int a_size;
} array_sCtx, *array_tCtx;
int array_Init( array_tCtx *ctx, int elemsize, int alloc_incr);
void array_Close( array_tCtx ctx);
int array_Add( array_tCtx ctx, void *elem);
int array_MAdd( array_tCtx ctx, void *elem, int elements);
int array_Get( array_tCtx ctx, int idx, void **elem);
int array_Size( array_tCtx ctx);
void *array_Array( array_tCtx ctx);
void *array_CopyArray( array_tCtx ctx);
#ifdef __cplusplus
}
#endif
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment