/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*\
\                                                                          /
/   Table management                  Copyright (c)  Dmitry A. Kazakov     \
\   [table moving]                                   St.Petersburg         /
/   TabExt, TabCopy                                  Autumn, 1993          \
\                                                                          /
/                                                                          \
\   (C, ANSI C)                       Last revision :  18:48 31 Oct 2001   /
/                                                                          \
\   This library is free software; you can redistribute it and/or modify   /
/   it  under  the  terms  of  the GNU Library General Public License as   \
\   published  by  the Free Software Foundation; either version 2 of the   /
/   License, or (at your option) any later version.                        \
\                                                                          /
/   As a special exception, if other  files  instantiate  generics  from   \
\   this unit, or you link this unit with  other  files  to  produce  an   /
/   executable, this  unit  does  not  by  itself  cause  the  resulting   \
\   executable  to  be  covered  by the GNU General Public License. This   /
/   exception  does  not  however  invalidate  any other reasons why the   \
\   executable file might be covered by the GNU Public License.            /
/                                                                          \
\   This library is distributed in the hope that it will be useful,  but   /
/   WITHOUT   ANY   WARRANTY;  without  even  the  implied  warranty  of   \
\   MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See  the  GNU   /
/   Library General Public License for more details.                       \
\                                                                          /
/   You  should  have  received a copy of the GNU Library General Public   \
\   License  along with this library; if not, write to the Free Software   /
/   Foundation,   Inc.,   59  Temple  Place  -  Suite  330,  Boston,  MA   \
\   02111-1307, USA                                                        /
/                                                                          \
\*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/

#include	"tab.h"		/* Table specific		*/
/*>

   TabCopy -- Copy table to another place

	FromTab	- Points to the table to be copied
	ToTab	- Points to the destination table
	Size	- The number of bytes available for the new table

   This function copies the table. The new table may overlap the old one
   only when ToTab <= FromTab (to  change  the  table  size  use  TabExt
   instead). The parameter Size defines the new table size. So it can be
   changed while copying. 

   Returns :
 
        [0]  New size is too small
        [1]  Successful operation

<*/
#ifndef NON_ANSI
int	TabCopy
(
   const table  FromTab,
   table	ToTab,
   const int	Size
)
#else
int	TabCopy (FromTab, ToTab, Size)
   table  	FromTab;
   table	ToTab;
   int		Size;
#endif	/*> NON_ANSI <*/
{
   int	NameAreaSize;

   if ((FromTab->TotalSize - FromTab->FreeSpace) > Size) return (0);
   MvWords
   (  (char *) ToTab   + sizeof (TabHeader),
      (char *) FromTab + sizeof (TabHeader),
      (FromTab->TOCSize)
   );
   NameAreaSize =
      (  FromTab->TotalSize
      -  FromTab->TOCSize
      -  FromTab->FreeSpace
      -  sizeof (TabHeader)
      );
   MvBytes
   (  (char *) ToTab + Size - NameAreaSize,
      (char *) FromTab + FromTab->TotalSize - NameAreaSize,
      NameAreaSize
   );
   ToTab->TotalSize = Size;
   ToTab->TOCSize   = FromTab->TOCSize;
   ToTab->FreeSpace =
      (  Size
      -  FromTab->TOCSize
      -  NameAreaSize
      - sizeof (TabHeader)
      ); 
   return (1);

}	/*>  TabCopy  <*/
/*>

   TabExt -- Change table size

	Table	- Points to the table
	Size	- The desired table size in bytes

   This  function  changes  the table size. The parameter Size specifies
   the new size. If it is less than the current  table  size  the  table
   will be truncated, otherwise,  enlarged.  In  the  later  case  there
   should be enough free memory space after the end of the table. If the
   new  size  is  less than the used one, the table will be truncated as
   much as possible. 

   Returns :

        New size of the table

<*/
#ifndef NON_ANSI
int	TabExt
(
   table	Table,
   const int	Size
)
#else
int	TabExt (Table, Size)
   table	Table;
   int		Size;
#endif	/*> NON_ANSI <*/
{
   int		NameAreaSize;
   int		NewSize;

   if (Size < (Table->TotalSize - Table->FreeSpace))
   {  /* Truncation						*/
      NewSize = Table-> TotalSize - Table-> FreeSpace;
   }
   else
   {  /* Table is enlarged					*/
      NewSize = Size;
   }
   if (NewSize == Table->TotalSize) return (NewSize);
   NameAreaSize =
      (  Table->TotalSize
      -  Table->TOCSize
      -  Table->FreeSpace
      -  sizeof (TabHeader)
      );
   MvBytes
   (  (char *) Table + NewSize - NameAreaSize,
      (char *) Table + Table->TotalSize - NameAreaSize,
      NameAreaSize
   );
   Table->FreeSpace += NewSize - Table->TotalSize;
   Table->TotalSize  = NewSize;
   return (NewSize);

}	/*>  TableExt  <*/
