Tcl Source Code

Attachment Details
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to tclconference@googlegroups.com
or submit via the online form by Sep 9.
Overview

Artifact ID: c49b48b5b04c0bb682cd30d27e47c91d31a88f41
Ticket: 1545151fffffffffffffffffffffffffffffffff
Date: 2006-08-23 16:25:25
User: fridolin
Artifact Attached: aff8b76101956daaae41b9ec80782671ef3f1e24
Filename:lreverse.c
Description:
Content Appended
/* #define USE_TCL_STUBS */
#include <tcl.h>

#undef TCL_STORAGE_CLASS
#define TCL_STORAGE_CLASS DLLEXPORT


/*
 *----------------------------------------------------------------
 *
 * Tcl_ListObjReverse
 *
 *     This function reverses a list referenced by listPtr.
 *
 */

int
Tcl_ListObjReverse(Tcl_Interp * interp, /* Used for error reportinh if not NULL. */
                   Tcl_Obj * listPtr,  /* List object to reverse */
                   Tcl_Obj ** resultPtr)
{
  int elemCount;
  Tcl_Obj **elemPtrs, **resObjv;
  int result, i;

  result = Tcl_ListObjGetElements( interp , listPtr, &elemCount, &elemPtrs);
  if( result != TCL_OK )
    return result;

  resObjv = (Tcl_Obj**) ckalloc( sizeof(Tcl_Obj*) * elemCount );

  for( i = 0; i < elemCount; i++ ) {
    resObjv[i] = elemPtrs[elemCount-1-i];
    Tcl_IncrRefCount(resObjv[i]);
  }

  *resultPtr = Tcl_NewListObj( elemCount, resObjv );
  ckfree( (void*) resObjv );

  return TCL_OK;
}

/*
 */

int
Tcl_LreverseObjCmd(ClientData dummy, Tcl_Interp * interp, int objc, Tcl_Obj *CONST objv[])
{
  Tcl_Obj * listPtr, *resultList;
  int result;

  if( objc != 2 ) {
    Tcl_WrongNumArgs(interp, 1, objv, "lreverse");
    return TCL_ERROR;
  }

  listPtr = objv[1];
  result = Tcl_ListObjReverse(interp, listPtr, &resultList);

  if( result != TCL_OK ) {
    return result;
  }

  Tcl_SetObjResult(interp, resultList);
  return TCL_OK;
}

int Lreverse_Init(Tcl_Interp* interp)
{

#if defined(USE_TCL_STUBS)
  if( Tcl_InitStubs( interp, TCL_VERSION, 0 ) == NULL )
    return TCL_ERROR;
#endif

  Tcl_CreateObjCommand(interp, "lreverse", Tcl_LreverseObjCmd, NULL, NULL);

  return TCL_OK;
}