150276Speter/****************************************************************************
2174993Srafan * Copyright (c) 1998-2004,2007 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
30166124Srafan *   Author:  Juergen Pfeifer, 1995,1997                                    *
3150276Speter ****************************************************************************/
3250276Speter
3350276Speter#include "form.priv.h"
3450276Speter
35174993SrafanMODULE_ID("$Id: fld_dup.c,v 1.12 2007/10/13 19:30:21 tom Exp $")
3650276Speter
3750276Speter/*---------------------------------------------------------------------------
38166124Srafan|   Facility      :  libnform
3950276Speter|   Function      :  FIELD *dup_field(FIELD *field, int frow, int fcol)
40166124Srafan|
4150276Speter|   Description   :  Duplicates the field at the specified position. All
4250276Speter|                    field attributes and the buffers are copied.
4350276Speter|                    If an error occurs, errno is set to
44166124Srafan|
4550276Speter|                    E_BAD_ARGUMENT - invalid argument
4650276Speter|                    E_SYSTEM_ERROR - system error
4750276Speter|
4850276Speter|   Return Values :  Pointer to the new field or NULL if failure
4950276Speter+--------------------------------------------------------------------------*/
5076726SpeterNCURSES_EXPORT(FIELD *)
51166124Srafandup_field(FIELD *field, int frow, int fcol)
5250276Speter{
5350276Speter  FIELD *New_Field = (FIELD *)0;
5450276Speter  int err = E_BAD_ARGUMENT;
5550276Speter
56166124Srafan  T((T_CALLED("dup_field(%p,%d,%d)"), field, frow, fcol));
57166124Srafan  if (field && (frow >= 0) && (fcol >= 0) &&
58166124Srafan      ((err = E_SYSTEM_ERROR) != 0) &&	/* trick : this resets the default error */
59174993Srafan      (New_Field = typeMalloc(FIELD, 1)))
6050276Speter    {
61174993Srafan      T((T_CREATE("field %p"), New_Field));
62166124Srafan      *New_Field = *_nc_Default_Field;
63166124Srafan      New_Field->frow = frow;
64166124Srafan      New_Field->fcol = fcol;
65166124Srafan      New_Field->link = New_Field;
66166124Srafan      New_Field->rows = field->rows;
67166124Srafan      New_Field->cols = field->cols;
68166124Srafan      New_Field->nrow = field->nrow;
69166124Srafan      New_Field->drows = field->drows;
70166124Srafan      New_Field->dcols = field->dcols;
7150276Speter      New_Field->maxgrow = field->maxgrow;
72166124Srafan      New_Field->nbuf = field->nbuf;
73166124Srafan      New_Field->just = field->just;
74166124Srafan      New_Field->fore = field->fore;
75166124Srafan      New_Field->back = field->back;
76166124Srafan      New_Field->pad = field->pad;
77166124Srafan      New_Field->opts = field->opts;
78166124Srafan      New_Field->usrptr = field->usrptr;
7950276Speter
80166124Srafan      if (_nc_Copy_Type(New_Field, field))
8150276Speter	{
82166124Srafan	  size_t i, len;
8350276Speter
8450276Speter	  len = Total_Buffer_Size(New_Field);
85166124Srafan	  if ((New_Field->buf = (FIELD_CELL *)malloc(len)))
8650276Speter	    {
87166124Srafan	      for (i = 0; i < len; ++i)
88166124Srafan		New_Field->buf[i] = field->buf[i];
89166124Srafan	      returnField(New_Field);
9050276Speter	    }
9150276Speter	}
9250276Speter    }
9350276Speter
94166124Srafan  if (New_Field)
9550276Speter    free_field(New_Field);
9650276Speter
9750276Speter  SET_ERROR(err);
98166124Srafan  returnField((FIELD *)0);
9950276Speter}
10050276Speter
10150276Speter/* fld_dup.c ends here */
102