FreeBASIC  0.91.0
array_redim.c
Go to the documentation of this file.
1 /* redim function */
2 
3 #include "fb.h"
4 
6  (
7  FBARRAY *array,
8  size_t element_len,
9  int doclear,
10  FB_DEFCTOR ctor,
11  size_t dimensions,
12  va_list ap
13  )
14 {
15  size_t i, elements, size;
16  ssize_t diff;
17  FBARRAYDIM *dim;
18  ssize_t lbTB[FB_MAXDIMENSIONS];
19  ssize_t ubTB[FB_MAXDIMENSIONS];
20 
21  /* load bounds */
22  dim = &array->dimTB[0];
23  for( i = 0; i < dimensions; i++ )
24  {
25  lbTB[i] = va_arg( ap, ssize_t );
26  ubTB[i] = va_arg( ap, ssize_t );
27 
28  if( lbTB[i] > ubTB[i] )
30 
31  dim->elements = (ubTB[i] - lbTB[i]) + 1;
32  dim->lbound = lbTB[i];
33  dim->ubound = ubTB[i];
34  ++dim;
35  }
36 
37  /* calc size */
38  elements = fb_hArrayCalcElements( dimensions, &lbTB[0], &ubTB[0] );
39  diff = fb_hArrayCalcDiff( dimensions, &lbTB[0], &ubTB[0] ) * element_len;
40  size = elements * element_len;
41 
42  /* alloc new */
43  if( doclear )
44  array->ptr = calloc( size, 1 );
45  else
46  array->ptr = malloc( size );
47 
48  if( array->ptr == NULL )
50 
51  /* set descriptor */
52  FB_ARRAY_SETDESC( array, element_len, dimensions, size, diff );
53 
54  /* call ctor for each element */
55  if( ctor != NULL )
56  {
57  const char *this_ = (const char *)array->ptr;
58  while( elements > 0 )
59  {
60  /* !!!FIXME!!! check exceptions (only if rewritten in C++) */
61  ctor( this_ );
62 
63  this_ += element_len;
64  --elements;
65  }
66  }
67 
68  return fb_ErrorSetNum( FB_RTERROR_OK );
69 }
70 
71 static int hRedim
72  (
73  FBARRAY *array,
74  size_t element_len,
75  int doclear,
76  int isvarlen,
77  size_t dimensions,
78  va_list ap
79  )
80 {
81 
82  /* free old */
83  fb_ArrayErase( array, isvarlen );
84 
85  return fb_hArrayAlloc( array, element_len, doclear, NULL, dimensions, ap );
86 }
87 
89  (
90  FBARRAY *array,
91  size_t element_len,
92  int doclear,
93  int isvarlen,
94  size_t dimensions,
95  ...
96  )
97 {
98  va_list ap;
99  int res;
100 
101  va_start( ap, dimensions );
102  res = hRedim( array, element_len, doclear, isvarlen, dimensions, ap );
103  va_end( ap );
104 
105  return res;
106 }
107 
108 int fb_ArrayRedim
109  (
110  FBARRAY *array,
111  size_t element_len,
112  int isvarlen,
113  size_t dimensions,
114  ...
115  )
116 {
117  va_list ap;
118  int res;
119 
120  va_start( ap, dimensions );
121  res = hRedim( array, element_len, TRUE, isvarlen, dimensions, ap );
122  va_end( ap );
123 
124  return res;
125 }