/*
* tclArithSeries.c --
*
* This file contains the ArithSeries concrete abstract list
* implementation. It implements the inner workings of the lseq command.
*
* Copyright © 2022 Brian S. Griffin.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include <assert.h>
#include "tcl.h"
#include "tclInt.h"
#include "tclArithSeries.h"
/*
* The structure below defines the arithmetic series Tcl Obj Type by means of
* procedures that can be invoked by generic object code.
*
* The arithmetic series object is a oTcl_AbstractList representing an interval
* of an arithmetic series in constant space.
*
* The arithmetic series is internally represented with three integers,
* *start*, *end*, and *step*, Where the length is calculated with
* the following algorithm:
*
* if RANGE == 0 THEN
* ERROR
* if RANGE > 0
* LEN is (((END-START)-1)/STEP) + 1
* else if RANGE < 0
* LEN is (((END-START)-1)/STEP) - 1
*
* And where the list's I-th element is calculated
* as:
*
* LIST[i] = START+(STEP*i)
*
* Zero elements ranges, like in the case of START=10 END=10 STEP=1
* are valid and will be equivalent to the empty list.
*/
static inline double ArithSeriesIndexDbl(ArithSeriesDbl *repPtr, Tcl_Size index)
{
return (repPtr->start + ((double)index * repPtr->step));
}
static inline Tcl_WideInt ArithSeriesIndexInt(ArithSeries *repPtr, Tcl_Size index)
{
return (repPtr->start + (index * repPtr->step));
}
static int TclArithSeriesObjStep(Tcl_Obj *arithSeriesPtr, Tcl_Obj **stepObj);
static int TclArithSeriesObjIndex(Tcl_Interp *interp, Tcl_Obj *arithSeriesPtr,
Tcl_Size index, Tcl_Obj **elemObj);
static Tcl_Size TclArithSeriesObjLength(Tcl_Obj *arithSeriesObj);
static int TclArithSeriesObjRange(Tcl_Interp *interp, Tcl_Obj *arithSeriesPtr,
Tcl_Size fromIdx, Tcl_Size toIdx, Tcl_Obj **newObjPtr);
static int TclArithSeriesObjReverse(Tcl_Interp *interp, Tcl_Obj *arithSeriesPtr, Tcl_Obj **newObjPtr);
static int TclArithSeriesGetElements(Tcl_Interp *interp,
Tcl_Obj *objPtr, Tcl_Size *objcPtr, Tcl_Obj ***objvPtr);
static Tcl_Obj *TclNewArithSeriesInt(Tcl_WideInt start,
Tcl_WideInt end, Tcl_WideInt step,
Tcl_WideInt len);
static Tcl_Obj *TclNewArithSeriesDbl(double start, double end,
double step, Tcl_WideInt len);
static void DupArithSeriesRep(Tcl_Obj *srcPtr, Tcl_Obj *copyPtr);
static void FreeArithSeriesRep(Tcl_Obj *arithSeriesObjPtr);
static void UpdateStringOfArithSeries(Tcl_Obj *arithSeriesObjPtr);
static Tcl_ObjType arithSeriesType = {
"arithseries", /* name */
FreeArithSeriesRep, /* freeIntRepProc */
DupArithSeriesRep, /* dupIntRepProc */
UpdateStringOfArithSeries, /* updateStringProc */
NULL, /* setFromAnyProc */
TCL_OBJTYPE_V1(
TclArithSeriesObjLength,
TclArithSeriesObjIndex,
TclArithSeriesObjRange,
TclArithSeriesObjReverse,
TclArithSeriesGetElements,
NULL, // SetElement
NULL) // Replace
};
/*
*----------------------------------------------------------------------
*
* Arithserieslen --
*
* Compute the length of the equivalent list where
* every element is generated starting from *start*,
* and adding *step* to generate every successive element
* that's < *end* for positive steps, or > *end* for negative
* steps.
*
* Results:
*
* The length of the list generated by the given range,
* that may be zero.
* The function returns -1 if the list is of length infiite.
*
* Side effects:
*
* None.
*
*----------------------------------------------------------------------
*/
static Tcl_WideInt
ArithSeriesLen(Tcl_WideInt start, Tcl_WideInt end, Tcl_WideInt step)
{
Tcl_WideInt len;
if (step == 0) return 0;
len = (step ? (1 + (((end-start))/step)) : 0);
return (len < 0) ? -1 : len;
}
/*
*----------------------------------------------------------------------
*
* DupArithSeriesRep --
*
* Initialize the internal representation of a ArithSeries abstract list
* Tcl_Obj to a copy of the internal representation of an existing
* arithseries object.
*
* Results:
* None.
*
* Side effects:
* We set "copyPtr"s internal rep to a pointer to a
* newly allocated AbstractList structure.
*----------------------------------------------------------------------
*/
static void
DupArithSeriesRep(Tcl_Obj *srcPtr, Tcl_Obj *copyPtr)
{
ArithSeries *srcArithSeries = (ArithSeries*)Tcl_ObjGetConcreteRep(srcPtr);
ArithSeries *copyArithSeries = (ArithSeries *)Tcl_Alloc(sizeof(ArithSeries));
*copyArithSeries = *srcArithSeries;
/* Note: we do not have to be worry about existing internal rep because
copyPtr is supposed to be freshly initialized */
Tcl_ObjSetConcreteRep(copyPtr, copyArithSeries);
copyPtr->typePtr = srcPtr->typePtr;
if (copyArithSeries->len > 0) {
Tcl_InvalidateStringRep(copyPtr);
} else {
Tcl_InitStringRep(copyPtr, NULL, 0);
}
}
/*
*----------------------------------------------------------------------
*
* FreeArithSeriesRep --
*
* Free any allocated memory in the ArithSeries Rep
*
* Results:
* None.
*
* Side effects:
*
*----------------------------------------------------------------------
*/
static void
FreeArithSeriesRep(Tcl_Obj *arithSeriesObjPtr) /* Free any allocated memory */
{
ArithSeries *arithSeriesPtr = (ArithSeries*)Tcl_ObjGetConcreteRep(arithSeriesObjPtr);
if (arithSeriesPtr) {
if (arithSeriesPtr->elements) {
Tcl_WideInt i, len = arithSeriesPtr->len;
for (i=0; i<len; i++) {
Tcl_DecrRefCount(arithSeriesPtr->elements[i]);
}
Tcl_Free((char*)arithSeriesPtr->elements);
arithSeriesPtr->elements = NULL;
}
Tcl_Free((char*)arithSeriesPtr);
}
}
/*
*----------------------------------------------------------------------
*
* TclNewArithSeriesInt --
*
* Creates a new ArithSeries object. The returned object has
* refcount = 0.
*
* Results:
*
* A Tcl_Obj pointer to the created ArithSeries object.
* A NULL pointer of the range is invalid.
*
* Side Effects:
*
* None.
*----------------------------------------------------------------------
*/
Tcl_Obj *
TclNewArithSeriesInt(Tcl_WideInt start, Tcl_WideInt end, Tcl_WideInt step, Tcl_WideInt len)
{
Tcl_WideInt length = (len>=0 ? len : ArithSeriesLen(start, end, step));
Tcl_Obj *arithSeriesObj;
Tcl_ObjInternalRep itr;
ArithSeries *arithSeriesRepPtr;
if (length <= 0) {
TclNewObj(arithSeriesObj);
return arithSeriesObj;
}
arithSeriesRepPtr = (ArithSeries*) Tcl_Alloc(sizeof (ArithSeries));
arithSeriesRepPtr->isDouble = 0;
arithSeriesRepPtr->start = start;
arithSeriesRepPtr->end = end;
arithSeriesRepPtr->step = step;
arithSeriesRepPtr->len = length;
arithSeriesRepPtr->elements = NULL;
TclNewObj(arithSeriesObj);
Tcl_StoreInternalRep(arithSeriesObj, &arithSeriesType, &itr);
Tcl_ObjSetConcreteRep(arithSeriesObj, arithSeriesRepPtr);
if (length > 0)
Tcl_InvalidateStringRep(arithSeriesObj);
return arithSeriesObj;
}
/*
*----------------------------------------------------------------------
*
* TclNewArithSeriesDbl --
*
* Creates a new ArithSeries object with doubles. The returned object has
* refcount = 0.
*
* Results:
*
* A Tcl_Obj pointer to the created ArithSeries object.
* A NULL pointer of the range is invalid.
*
* Side Effects:
*
* None.
*----------------------------------------------------------------------
*/
Tcl_Obj *
TclNewArithSeriesDbl(double start, double end, double step, Tcl_WideInt len)
{
Tcl_WideInt length = (len>=0 ? len : ArithSeriesLen(start, end, step));
Tcl_Obj *arithSeriesObj;
Tcl_ObjInternalRep itr;
ArithSeriesDbl *arithSeriesRepPtr;
if (length <= 0) {
TclNewObj(arithSeriesObj);
return arithSeriesObj;
}
arithSeriesRepPtr = (ArithSeriesDbl*) Tcl_Alloc(sizeof (ArithSeriesDbl));
arithSeriesRepPtr->isDouble = 1;
arithSeriesRepPtr->start = start;
arithSeriesRepPtr->end = end;
arithSeriesRepPtr->step = step;
arithSeriesRepPtr->len = length;
arithSeriesRepPtr->elements = NULL;
TclNewObj(arithSeriesObj);
Tcl_StoreInternalRep(arithSeriesObj, &arithSeriesType, &itr);
Tcl_ObjSetConcreteRep(arithSeriesObj, arithSeriesRepPtr);
if (length > 0)
Tcl_InvalidateStringRep(arithSeriesObj);
return arithSeriesObj;
}
/*
*----------------------------------------------------------------------
*
* assignNumber --
*
* Create the approprite Tcl_Obj value for the given numeric values.
* Used locally only for decoding [lseq] numeric arguments.
* refcount = 0.
*
* Results:
*
* A Tcl_Obj pointer.
* No assignment on error.
*
* Side Effects:
*
* None.
*----------------------------------------------------------------------
*/
static void
assignNumber(
int useDoubles,
Tcl_WideInt *intNumberPtr,
double *dblNumberPtr,
Tcl_Obj *numberObj)
{
union {
double d;
Tcl_WideInt i;
} *number;
int tcl_number_type;
if (Tcl_GetNumberFromObj(NULL, numberObj, (void**)&number, &tcl_number_type) != TCL_OK
|| tcl_number_type == TCL_NUMBER_BIG) {
return;
}
if (useDoubles) {
if (tcl_number_type == TCL_NUMBER_DOUBLE) {
*dblNumberPtr = number->d;
} else {
*dblNumberPtr = (double)number->i;
}
} else {
if (tcl_number_type == TCL_NUMBER_INT) {
*intNumberPtr = number->i;
} else {
*intNumberPtr = (Tcl_WideInt)number->d;
}
}
}
/*
*----------------------------------------------------------------------
*
* TclNewArithSeriesObj --
*
* Creates a new ArithSeries object. Some arguments may be NULL and will
* be computed based on the other given arguments.
* refcount = 0.
*
* Results:
*
* A Tcl_Obj pointer to the created ArithSeries object.
* An empty Tcl_Obj if the range is invalid.
*
* Side Effects:
*
* None.
*----------------------------------------------------------------------
*/
int
TclNewArithSeriesObj(
Tcl_Interp *interp, /* For error reporting */
Tcl_Obj **arithSeriesObj, /* return value */
int useDoubles, /* Promote values to double when true,
* int otherwise */
Tcl_Obj *startObj, /* First value in list */
Tcl_Obj *endObj, /* Upper bound value of list */
Tcl_Obj *stepObj, /* Increment amount */
Tcl_Obj *lenObj) /* Number of elements */
{
double dstart, dend, dstep;
Tcl_WideInt start, end, step;
Tcl_WideInt len;
if (startObj) {
assignNumber(useDoubles, &start, &dstart, startObj);
} else {
start = 0;
dstart = start;
}
if (stepObj) {
assignNumber(useDoubles, &step, &dstep, stepObj);
if (useDoubles) {
step = dstep;
} else {
dstep = step;
}
if (dstep == 0) {
*arithSeriesObj = Tcl_NewObj();
return TCL_OK;
}
}
if (endObj) {
assignNumber(useDoubles, &end, &dend, endObj);
}
if (lenObj) {
if (TCL_OK != Tcl_GetWideIntFromObj(interp, lenObj, &len)) {
return TCL_ERROR;
}
}
if (startObj && endObj) {
if (!stepObj) {
if (useDoubles) {
dstep = (dstart < dend) ? 1.0 : -1.0;
step = dstep;
} else {
step = (start < end) ? 1 : -1;
dstep = step;
}
}
assert(dstep!=0);
if (!lenObj) {
if (useDoubles) {
len = (dend - dstart + dstep)/dstep;
} else {
len = (end - start + step)/step;
}
}
}
if (!endObj) {
if (useDoubles) {
dend = dstart + (dstep * (len-1));
end = dend;
} else {
end = start + (step * (len-1));
dend = end;
}
}
if (TCL_MAJOR_VERSION < 9 && len > ListSizeT_MAX) {
Tcl_SetObjResult(
interp,
Tcl_NewStringObj("max length of a Tcl list exceeded", -1));
Tcl_SetErrorCode(interp, "TCL", "MEMORY", NULL);
return TCL_ERROR;
}
if (arithSeriesObj) {
*arithSeriesObj = (useDoubles)
? TclNewArithSeriesDbl(dstart, dend, dstep, len)
: TclNewArithSeriesInt(start, end, step, len);
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TclArithSeriesObjLength
*
* Returns the length of the arithmentic series.
*
* Results:
*
* The length of the series as Tcl_WideInt.
*
* Side Effects:
*
* None.
*
*----------------------------------------------------------------------
*/
Tcl_Size TclArithSeriesObjLength(Tcl_Obj *arithSeriesObj)
{
ArithSeries *arithSeriesRepPtr = (ArithSeries *)Tcl_ObjGetConcreteRep(arithSeriesObj);
return arithSeriesRepPtr->len;
}
/*
*----------------------------------------------------------------------
*
* TclArithSeriesObjIndex --
*
* Returns the element with the specified index in the list
* represented by the specified Arithmentic Sequence object.
* If the index is out of range, TCL_ERROR is returned,
* otherwise TCL_OK is returned and the integer value of the
* element is stored in *element.
*
* Results:
*
* TCL_OK on succes, TCL_ERROR on index out of range.
*
* Side Effects:
*
* On success, the integer pointed by *element is modified.
*
*----------------------------------------------------------------------
*/
int
TclArithSeriesObjIndex(
Tcl_Interp *interp, /* Used for error reporting if not NULL. */
Tcl_Obj *arithSeriesPtr, /* List obj */
Tcl_Size index, /* index to element of interest */
Tcl_Obj **elemObj) /* Return value */
{
ArithSeries *arithSeriesRepPtr = (ArithSeries *)Tcl_ObjGetConcreteRep(arithSeriesPtr);
(void)interp; // quiet compiler
if (index < arithSeriesRepPtr->len) {
/* List[i] = Start + (Step * index) */
if (arithSeriesRepPtr->isDouble) {
*elemObj = Tcl_NewDoubleObj(ArithSeriesIndexDbl((ArithSeriesDbl*)arithSeriesRepPtr, index));
} else {
*elemObj = Tcl_NewWideIntObj(ArithSeriesIndexInt(arithSeriesRepPtr, index));
}
} else {
TclNewObj(*elemObj); // empty value
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TclArithSeriesObjStep --
*
* Return a Tcl_Obj with the step value from the give ArithSeries Obj.
* refcount = 0.
*
* Results:
*
* A Tcl_Obj pointer to the created ArithSeries object.
* A NULL pointer of the range is invalid.
*
* Side Effects:
*
* None.
*----------------------------------------------------------------------
*/
int
TclArithSeriesObjStep(
Tcl_Obj *arithSeriesPtr,
Tcl_Obj **stepObj)
{
ArithSeries *arithSeriesRepPtr = (ArithSeries *)Tcl_ObjGetConcreteRep(arithSeriesPtr);
if (arithSeriesRepPtr->isDouble) {
*stepObj = Tcl_NewDoubleObj(((ArithSeriesDbl*)(arithSeriesRepPtr))->step);
} else {
*stepObj = Tcl_NewWideIntObj(arithSeriesRepPtr->step);
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TclArithSeriesObjRange --
*
* Makes a slice of an ArithSeries value.
* *arithSeriesPtr must be known to be a valid list.
*
* Results:
* Returns a pointer to the sliced series.
* This may be a new object or the same object if not shared.
*
* Side effects:
* ?The possible conversion of the object referenced by listPtr?
* ?to a list object.?
*
*----------------------------------------------------------------------
*/
int
TclArithSeriesObjRange(
Tcl_Interp *interp, /* for error messages. */
Tcl_Obj *arithSeriesPtr, /* List object to take a range from. */
Tcl_Size fromIdx, /* Index of first element to include. */
Tcl_Size toIdx, /* Index of last element to include. */
Tcl_Obj **newObjPtr) /* return value */
{
ArithSeries *arithSeriesRepPtr;
Tcl_Obj *startObj, *endObj, *stepObj;
(void)interp; /* silence compiler */
arithSeriesRepPtr = (ArithSeries *)Tcl_ObjGetConcreteRep(arithSeriesPtr);
if (fromIdx == TCL_INDEX_NONE) {
fromIdx = 0;
}
if (toIdx >= arithSeriesRepPtr->len) {
toIdx = arithSeriesRepPtr->len-1;
}
if (fromIdx > toIdx ||
fromIdx >= arithSeriesRepPtr->len) {
TclNewObj(*newObjPtr);
return TCL_OK;
}
TclArithSeriesObjIndex(interp, arithSeriesPtr, fromIdx, &startObj);
Tcl_IncrRefCount(startObj);
TclArithSeriesObjIndex(interp, arithSeriesPtr, toIdx, &endObj);
Tcl_IncrRefCount(endObj);
TclArithSeriesObjStep(arithSeriesPtr, &stepObj);
Tcl_IncrRefCount(stepObj);
if (Tcl_IsShared(arithSeriesPtr) ||
((arithSeriesPtr->refCount > 1))) {
int status = TclNewArithSeriesObj(NULL, newObjPtr,
arithSeriesRepPtr->isDouble, startObj, endObj, stepObj, NULL);
Tcl_DecrRefCount(startObj);
Tcl_DecrRefCount(endObj);
Tcl_DecrRefCount(stepObj);
return status;
}
/*
* In-place is possible.
*/
/*
* Even if nothing below cause any changes, we still want the
* string-canonizing effect of [lrange 0 end].
*/
TclInvalidateStringRep(arithSeriesPtr);
if (arithSeriesRepPtr->isDouble) {
ArithSeriesDbl *arithSeriesDblRepPtr = (ArithSeriesDbl*)arithSeriesPtr;
double start, end, step;
Tcl_GetDoubleFromObj(NULL, startObj, &start);
Tcl_GetDoubleFromObj(NULL, endObj, &end);
Tcl_GetDoubleFromObj(NULL, stepObj, &step);
arithSeriesDblRepPtr->start = start;
arithSeriesDblRepPtr->end = end;
arithSeriesDblRepPtr->step = step;
arithSeriesDblRepPtr->len = (end-start+step)/step;
arithSeriesDblRepPtr->elements = NULL;
} else {
Tcl_WideInt start, end, step;
Tcl_GetWideIntFromObj(NULL, startObj, &start);
Tcl_GetWideIntFromObj(NULL, endObj, &end);
Tcl_GetWideIntFromObj(NULL, stepObj, &step);
arithSeriesRepPtr->start = start;
arithSeriesRepPtr->end = end;
arithSeriesRepPtr->step = step;
arithSeriesRepPtr->len = (end-start+step)/step;
arithSeriesRepPtr->elements = NULL;
}
Tcl_DecrRefCount(startObj);
Tcl_DecrRefCount(endObj);
Tcl_DecrRefCount(stepObj);
*newObjPtr = arithSeriesPtr;
return TCL_OK;
}
/*
* Handle ArithSeries special case - don't shimmer a series into a list
* just to reverse it.
*/
int
TclArithSeriesObjReverse(
Tcl_Interp *interp, /* For error messages */
Tcl_Obj *arithSeriesPtr, /* List object to reverse. */
Tcl_Obj **newObjPtr)
{
ArithSeries *arithSeriesRepPtr;
Tcl_Obj *startObj, *endObj, *stepObj;
Tcl_Obj *resultObj;
Tcl_WideInt start, end, step, len;
double dstart, dend, dstep;
int isDouble;
(void)interp;
if (newObjPtr == NULL) {
return TCL_ERROR;
}
arithSeriesRepPtr = (ArithSeries *)Tcl_ObjGetConcreteRep(arithSeriesPtr);
isDouble = arithSeriesRepPtr->isDouble;
len = arithSeriesRepPtr->len;
TclArithSeriesObjIndex(NULL, arithSeriesPtr, (len-1), &startObj);
Tcl_IncrRefCount(startObj);
TclArithSeriesObjIndex(NULL, arithSeriesPtr, 0, &endObj);
Tcl_IncrRefCount(endObj);
TclArithSeriesObjStep(arithSeriesPtr, &stepObj);
Tcl_IncrRefCount(stepObj);
if (isDouble) {
Tcl_GetDoubleFromObj(NULL, startObj, &dstart);
Tcl_GetDoubleFromObj(NULL, endObj, &dend);
Tcl_GetDoubleFromObj(NULL, stepObj, &dstep);
dstep = -dstep;
TclSetDoubleObj(stepObj, dstep);
} else {
Tcl_GetWideIntFromObj(NULL, startObj, &start);
Tcl_GetWideIntFromObj(NULL, endObj, &end);
Tcl_GetWideIntFromObj(NULL, stepObj, &step);
step = -step;
TclSetIntObj(stepObj, step);
}
Tcl_IncrRefCount(startObj);
Tcl_IncrRefCount(endObj);
Tcl_IncrRefCount(stepObj);
if (Tcl_IsShared(arithSeriesPtr) ||
((arithSeriesPtr->refCount > 1))) {
Tcl_Obj *lenObj = Tcl_NewWideIntObj(len);
if (TclNewArithSeriesObj(NULL, &resultObj, isDouble,
startObj, endObj, stepObj, lenObj) != TCL_OK) {
resultObj = NULL;
}
Tcl_DecrRefCount(lenObj);
} else {
/*
* In-place is possible.
*/
TclInvalidateStringRep(arithSeriesPtr);
if (isDouble) {
ArithSeriesDbl *arithSeriesDblRepPtr =
(ArithSeriesDbl*)arithSeriesRepPtr;
arithSeriesDblRepPtr->start = dstart;
arithSeriesDblRepPtr->end = dend;
arithSeriesDblRepPtr->step = dstep;
} else {
arithSeriesRepPtr->start = start;
arithSeriesRepPtr->end = end;
arithSeriesRepPtr->step = step;
}
if (arithSeriesRepPtr->elements) {
Tcl_WideInt i;
for (i=0; i<len; i++) {
Tcl_DecrRefCount(arithSeriesRepPtr->elements[i]);
}
Tcl_Free((char*)arithSeriesRepPtr->elements);
}
arithSeriesRepPtr->elements = NULL;
resultObj = arithSeriesPtr;
}
Tcl_DecrRefCount(startObj);
Tcl_DecrRefCount(endObj);
Tcl_DecrRefCount(stepObj);
*newObjPtr = resultObj;
return TCL_OK;
}
/*
** Handle ArithSeries GetElements call
*/
int
TclArithSeriesGetElements(
Tcl_Interp *interp, /* Used to report errors if not NULL. */
Tcl_Obj *arithSeriesObjPtr, /* ArithSeries object for which an element
* array is to be returned. */
Tcl_Size *objcPtr, /* Where to store the count of objects
* referenced by objv. */
Tcl_Obj ***objvPtr) /* Where to store the pointer to an array of
* pointers to the list's objects. */
{
ArithSeries *arithSeriesRepPtr =
(ArithSeries*)Tcl_ObjGetConcreteRep(arithSeriesObjPtr);
Tcl_Obj **objv;
int i, objc;
objc = arithSeriesRepPtr->len;
if (objvPtr == NULL) {
if (objcPtr) {
*objcPtr = objc;
return TCL_OK;
}
return TCL_ERROR;
}
if (objc && objvPtr && arithSeriesRepPtr->elements) {
objv = arithSeriesRepPtr->elements;
} else if (objc > 0) {
objv = (Tcl_Obj **)Tcl_Alloc(sizeof(Tcl_Obj*) * objc);
if (objv == NULL) {
if (interp) {
Tcl_SetObjResult(
interp,
Tcl_NewStringObj("max length of a Tcl list exceeded", -1));
Tcl_SetErrorCode(interp, "TCL", "MEMORY", NULL);
}
return TCL_ERROR;
}
for (i = 0; i < objc; i++) {
if (TclArithSeriesObjIndex(interp, arithSeriesObjPtr, i, &objv[i]) == TCL_OK) {
Tcl_IncrRefCount(objv[i]);
} else {
// TODO: some cleanup needed here
return TCL_ERROR;
}
}
} else {
objv = NULL;
}
arithSeriesRepPtr->elements = objv;
*objvPtr = objv;
*objcPtr = objc;
return TCL_OK;
}
static void
UpdateStringOfArithSeries(Tcl_Obj *arithSeriesObjPtr)
{
ArithSeries *arithSeriesRepPtr = (ArithSeries*)Tcl_ObjGetConcreteRep(arithSeriesObjPtr);
char *p, *str;
Tcl_Obj *eleObj;
Tcl_WideInt length = 0;
int llen, slen, i;
/*
* Pass 1: estimate space.
*/
llen = arithSeriesRepPtr->len;
if (llen <= 0) {
Tcl_InitStringRep(arithSeriesObjPtr, NULL, 0);
return;
}
for (i = 0; i < llen; i++) {
if (TclArithSeriesObjIndex(NULL, arithSeriesObjPtr, i, &eleObj) == TCL_OK) {
Tcl_GetStringFromObj(eleObj, &slen);
length += slen + 1; /* one more for the space char */
Tcl_DecrRefCount(eleObj);
} else {
// TODO: report error?
}
}
/*
* Pass 2: generate the string repr.
*/
p = Tcl_InitStringRep(arithSeriesObjPtr, NULL, length);
for (i = 0; i < llen; i++) {
if (TclArithSeriesObjIndex(NULL, arithSeriesObjPtr, i, &eleObj) == TCL_OK) {
str = Tcl_GetStringFromObj(eleObj, &slen);
strcpy(p, str);
p[slen] = ' ';
p += slen+1;
Tcl_DecrRefCount(eleObj);
} // else TODO: report error here?
}
if (length > 0) arithSeriesObjPtr->bytes[length-1] = '\0';
arithSeriesObjPtr->length = length-1;
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/