/*
 *				f x n e e d . c
 */

/*)LIBRARY
*/

#ifdef	DOCUMENTATION

title	fxneed	Explicitly Grow or Shrink a Flex
index		Explicitly grow or shrink a flex

synopsis
 
	 #ifdef vms
	 #include "c:flex.h"
	 #else
	 #include <flex.h>
	 #endif

	 FLEX *fxneed(fx,cnt)
	 FLEX *fx;
	 unsigned cnt;

description

	Ensure that a flex contains room for some entries.  fxneed(fx,cnt)
	expands the flex, if possible, so that at least cnt items will fit
	with no further expansion.  (If fx is expanded, it is expanded so
	that it will have room for EXACTLY cnt more items.)

	If cnt=0, the flex is reduced in size, if necessary, so that it has
	no empty space in it.  (The extension quantity is not altered; you
	can add to the flex again later.)

	fxneed() ignores the extension quantity (fxext) specified for the
	flex; it will extend exactly as much as it needs to, and will extend
	even when fxext is 0.

	If all goes well, fx is returned; otherwise, NULL is returned and
	the flex is marked damaged.

bugs

author

	Jerry Leichter

#endif

/*
 * )EDITLEVEL=05
 * Edit history
 * 0.0  4-May-81 JSL	Invention
 * 0.1  5-May-81 JSL	Cleanup; add fxneed
 * 0.2  7-May-81 JSL	Split flex.c into separate files; this is one
 * 0.3 20-May-81 JSL	Minor cleanup; changed documentation to new standard
 */

#ifdef	vms
#include "c:flex.h"
#else
#include <flex.h>
#endif
#define NULL 0

extern char *realloc();

FLEX *
fxneed(fx,cnt)
register FLEX *fx;
register unsigned cnt;
{   if (fx != NULL)
    {   if (fx->fxdata == NULL)
	    fx = NULL;
        else
	    if (cnt==0 || (fx->fxdim-fx->fxused) < cnt)
            {   fx->fxdim = fx->fxused+cnt;
                if ((fx->fxdata = realloc(fx->fxdata,fx->fxdim*fx->fxisz))
		     == NULL)
		{   fx->fxdim = 0;
		    fx = NULL;
		}
	     }
     }
     return(fx);
}
