home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / gt_compn.c < prev    next >
C/C++ Source or Header  |  1993-10-14  |  4KB  |  143 lines

  1. /*
  2.  * GT CLIPPER STANDARD HEADER
  3.  *
  4.  * File......: gt_compn.c
  5.  * Author....: Brian Dukes
  6.  * BBS.......: THE CELLAR bbs
  7.  * Net/Node..: 013/203
  8.  * User Name.: Brian Dukes
  9.  * Date......: 28/5/93
  10.  * Revision..: 1.0
  11.  *
  12.  * This is an original work by Brian Dukes and is placed in the
  13.  * public domain.
  14.  *
  15.  * Modification history:
  16.  * ---------------------
  17.  *
  18.  * $Log$
  19.  *
  20.  */
  21.  
  22. /*
  23.  *  $DOC$
  24.  *  $FUNCNAME$
  25.  *      GT_COMPN()
  26.  *  $CATEGORY$
  27.  *      General
  28.  *  $ONELINER$
  29.  *      Compress a Clipper Numeric value into a string
  30.  *  $SYNTAX$
  31.  *      <str> := GT_CompN(<numeric>,<size>)
  32.  *  $ARGUMENTS$
  33.  *      <numeric> - Clipper value to compress
  34.  *      <size>    - Number of Bytes to compress to (1/2/4/8)
  35.  *  $RETURNS$
  36.  *      <str>     - Character String Returned
  37.  *  $DESCRIPTION$
  38.  *      This function is surprisingly quite handy.  Its principle use is to
  39.  *      create data storage types similar to C, char,int, long, double.
  40.  *      The use then surpasses that with the fact that these new variables
  41.  *      in clipper are just 1/2/4/8 character strings which can be written
  42.  *      away to databases or even used in an index.
  43.  *
  44.  *      what this all means is that a field that only needs to store values
  45.  *      from 0-255, only need be one character wide in the database
  46.  *      structure.
  47.  *
  48.  *  $EXAMPLES$
  49.  *      #include "gt_compn.h"
  50.  *
  51.  *      function main()
  52.  *
  53.  *         * Setup some Test Values into some local variables
  54.  *
  55.  *         local w   := 10
  56.  *         local x   := 1000
  57.  *         local y   := 1234567
  58.  *         local z   := 1234567.890123
  59.  *
  60.  *         * Reserve Enough Space in Each Storage Variable
  61.  *
  62.  *         local s_w := space( GT_BYTE )
  63.  *         local s_x := space( GT_INT )
  64.  *         local s_y := space( GT_LONG )
  65.  *         local s_z := space( GT_DOUBLE )
  66.  *
  67.  *         * Store The Compressed Numbers in Their Storage Variables
  68.  *
  69.  *         s_w := GT_CompN( w, GT_BYTE)
  70.  *         s_x := GT_CompN( x, GT_INT )
  71.  *         s_y := GT_CompN( y, GT_LONG)
  72.  *         s_z := GT_CompN( z, GT_DOUBLE)
  73.  *
  74.  *  $SEEALSO$
  75.  *      GT_UNCOMPN()
  76.  *  $END$
  77.  */
  78.  
  79. /**** Compiled in MS/C v6.0  as  CL /AL /Gs /Zl -c gt_compn.c        ****/
  80.  
  81. #include <extend.h>
  82. #include "gt_compn.h"
  83.  
  84. CLIPPER GT_CompN()
  85. {
  86.    unsigned int siz = _parni( 2 ) ;    /* Get the SIZE of the Variable */
  87.    int    i_var     = 0 ;
  88.    long   l_var     = 0 ;
  89.    double d_var     = 0.00 ;
  90.    char       out_buf[10] ;
  91.    char far * out   = out_buf ;
  92.  
  93.    switch (siz)
  94.       {
  95.          case GT_BYTE :
  96.             i_var = _parni( 1 ) ;  /* Get the Number initially as an INT */
  97.             out[0] = (unsigned char) i_var ;
  98.             out[ GT_BYTE+1 ] = NULL ;
  99.             break;
  100.  
  101.          case GT_INT :
  102.             i_var = _parni( 1 ) ;  /* Get the Number as an INT */
  103.             _gt_far_copy(out,&i_var,GT_INT) ;
  104.             out[ GT_INT+1 ] = NULL ;
  105.             break;
  106.  
  107.          case GT_LONG :
  108.             l_var = _parnl( 1 ) ;  /* Get the Number as an LONG */
  109.             _gt_far_copy(out,&l_var,GT_LONG) ;
  110.             out[ GT_LONG+1 ] = NULL ;
  111.             break;
  112.  
  113.          case GT_DOUBLE :
  114.             d_var = _parnd( 1 ) ;  /* Get the Number as an DOUBLE */
  115.             _gt_far_copy(out,&d_var,GT_DOUBLE) ;
  116.             out[ GT_DOUBLE+1 ] = NULL ;
  117.             break;
  118.       }
  119.  
  120.    _retclen(out, siz) ;
  121.    return ;
  122. }
  123.  
  124.  
  125. /***************************************************************************
  126.  _gt_far_copy() - Copy Near Memory to Far Memory
  127.  ***************************************************************************/
  128.  
  129. int _gt_far_copy( char far * out , char * in , int len)
  130. {
  131.    int i = 0 ;
  132.    char far * fp = out ;
  133.    char * p = in ;
  134.  
  135.    while (i < len)
  136.       {
  137.          *fp++ = *p++ ;
  138.          ++i ;
  139.       }
  140.  
  141.    return (len) ;
  142. }
  143.