A pair of possibly useful macros for your toolkit

Started by MrBcx, June 15, 2025, 05:32:31 PM

Previous topic - Next topic

Quin


MrBcx

Hi Robert - yours is a decidedly more illustrative example that showcases the usefulness.


Robert

MrBcx:

What, no refill ?

'*****************************************************
'                  Demonstration
'  Only works for 1D static dimensioned arrays
' Provides an easy way to repeatedly refill an array
'*****************************************************

DIM int_Array[5] AS INT
DIM sng_Array[5] AS SINGLE
DIM dbl_Array[5] AS DOUBLE
DIM str_Array[5] AS STRING

STORE_N (INTEGER, int_Array, 1, 2, 3, 4, 5, EOD_N);
STORE_N (SINGLE, sng_Array, 1.1, 2.2, 3.3, 4.4, 5.5, EOD_N);
STORE_N (DOUBLE, dbl_Array, 10.1, 20.2, 30.3, 40.4, 50.5, EOD_N);
STORE_S (STRING, str_Array, "Keep", "Calm", "And", "Carry", "On.", EOD_S)

FOR_EACH(i, int_Array)
    PRINT int_Array[i]
NEXT_EACH : PRINT

FOR_EACH(i, sng_Array)
    PRINT sng_Array[i]
NEXT_EACH : PRINT

FOR_EACH(i, dbl_Array)
    PRINT dbl_Array[i]
NEXT_EACH : PRINT

FOR_EACH(i, str_Array)
    PRINT str_Array[i]; " ";
NEXT_EACH :

?
PRINT "It's time for a refill !"

PAUSE

STORE_N (INTEGER, int_Array, 6, 7, 8, 9, 10, EOD_N);
STORE_N (SINGLE, sng_Array, 6.6, 7.7, 8.8, 9.9, 10.1, EOD_N);
STORE_N (DOUBLE, dbl_Array, 60.1, 70.2, 80.3, 90.4, 100.5, EOD_N);
STORE_S (STRING, str_Array, "Don't", "Freak !", "Just", "Be", "Cool ....", EOD_S)

FOR_EACH(i, int_Array)
    PRINT int_Array[i]
NEXT_EACH : PRINT

FOR_EACH(i, sng_Array)
    PRINT sng_Array[i]
NEXT_EACH : PRINT

FOR_EACH(i, dbl_Array)
    PRINT dbl_Array[i]
NEXT_EACH : PRINT

FOR_EACH(i, str_Array)
    PRINT str_Array[i]; " ";
NEXT_EACH :

PAUSE

$HEADER       
#define EOD_N  98765432
#define EOD_S  "__END__"

#define STORE_N(datatype, array, ...) \
    do { \
        datatype values[] = { __VA_ARGS__ }; \
        size_t count = sizeof(values) / sizeof(values[0]); \
        size_t i, j = 0; \
        for (i = 0; i < count && values[i] != EOD_N; i++) { \
            array[j++] = values[i]; \
        } \
    } while(0)

#define STORE_S(dummy, array, ...) \
    do { \
        const char* values[] = { __VA_ARGS__ }; \
        size_t count = sizeof(values) / sizeof(values[0]); \
        size_t i, j = 0; \
        for (i = 0; i < count; i++) { \
            if (strcmp(values[i], EOD_S) == 0) break; \
            strcpy(array[j++], values[i]); \
        } \
    } while(0)
$HEADER

dragon57


MrBcx

#1
The BCX SET command provides an easy way to declare and initialize a array with values.


SET Foo[]
  1,2,3,4,5
END SET

FOR_EACH(i,Foo)
  PRINT Foo[i]
NEXT_EACH 
PAUSE

Later in your app, if you want to change the values in Foo[], it
becomes a bit more involved.  You cannot use SET on the same array again.
You might use a loop, you might READ/DATA from DATA statements, or from
a file, or load each cell individually.  Lots of variations.


Today I worked up this little demo that might be useful for some. 
It currently only works with statically dimension arrays and honestly
I don't know that I would ever need to use something like this on a
dynamic array.

This cannot be compiled with LccWin32 - all others compile just fine.

STORE_S () macro is for static string arrays.
STORE_N () macro is for static numeric arrays. (INT, LONG, ULONG, ULONGLONG, LDOUBLE, etc)

If you don't understand this then you don't need it.


'*****************************************************
'                  Demonstration
'  Only works for 1D static dimensioned arrays
' Provides an easy way to repeatedly refill an array
'*****************************************************

DIM int_Array[5] AS INT
DIM sng_Array[5] AS SINGLE
DIM dbl_Array[5] AS DOUBLE
DIM str_Array[5] AS STRING

STORE_N (INTEGER, int_Array, 1, 2, 3, 4, 5, EOD_N);
STORE_N (SINGLE, sng_Array, 1.1, 2.2, 3.3, 4.4, 5.5, EOD_N);
STORE_N (DOUBLE, dbl_Array, 10.1, 20.2, 30.3, 40.4, 50.5, EOD_N);
STORE_S (STRING, str_Array, "Keep", "Calm", "And", "Carry", "On", EOD_S)

FOR_EACH(i, int_Array)
    PRINT int_Array[i]
NEXT_EACH : PRINT

FOR_EACH(i, sng_Array)
    PRINT sng_Array[i]
NEXT_EACH : PRINT

FOR_EACH(i, dbl_Array)
    PRINT dbl_Array[i]
NEXT_EACH : PRINT

FOR_EACH(i, str_Array)
    PRINT str_Array[i]; " ";
NEXT_EACH : PAUSE


$HEADER         
#define EOD_N  98765432
#define EOD_S  "__END__"

#define STORE_N(datatype, array, ...) \
    do { \
        datatype values[] = { __VA_ARGS__ }; \
        size_t count = sizeof(values) / sizeof(values[0]); \
        size_t i, j = 0; \
        for (i = 0; i < count && values[i] != EOD_N; i++) { \
            array[j++] = values[i]; \
        } \
    } while(0)

#define STORE_S(dummy, array, ...) \
    do { \
        const char* values[] = { __VA_ARGS__ }; \
        size_t count = sizeof(values) / sizeof(values[0]); \
        size_t i, j = 0; \
        for (i = 0; i < count; i++) { \
            if (strcmp(values[i], EOD_S) == 0) break; \
            strcpy(array[j++], values[i]); \
        } \
    } while(0)
$HEADER