get_ints_from_objs

GPS: I wanted to fetch integers from an array of Tcl_Obj. I didn't want to have a massive number of Tcl_GetIntFromObj polluting the flow of code. So, I came up with this solution:

/* REVISION 231 */
#include <stdarg.h>
#ifndef _GET_INTS_FROM_OBJS_C
#define _GET_INTS_FROM_OBJS_C
int
get_ints_from_objs(Tcl_Interp *interp, Tcl_Obj *CONST objv[], int i, int end, ...) {
    va_list va;
    va_start(va, end);
    for (; i < end; ++i) {
        int *i_ptr = va_arg (va, int *);
        if (TCL_OK != Tcl_GetIntFromObj(interp, objv[i], i_ptr)) {
            va_end(va);
            return TCL_ERROR;
        }
    }
    va_end(va);
    return TCL_OK;
}
#endif /* _GET_INTS_FROM_OBJS_C */

Example usage:

int x1, y1, x2, y2;

if (TCL_OK != get_ints_from_objs(interp, objv, 3, 7, &x1, &y1, &x2, &y2)) {
    return TCL_ERROR;
}

DKF: Neat. Here are some ideas to take what you've done and make it even cooler (but not much; you've got nice work there.)

  • You might want to add some extra information to the ErrorInfo so as to state the index that failed.
  • You might want to do a variant that pulls from a List object and which does bounds-checking.
  • You might want to allow a value to be skipped by having that result pointer being NULL.