home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / EXAMPLES / D4SMALL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  2.5 KB  |  125 lines

  1.  
  2. /* (c)Copyright Sequiter Software Inc., 1989. All rights reserved.
  3.  
  4.    This program counts the number of non-blank data bytes in
  5.    a database.
  6.  
  7.    It illustrates how small an EXE file for a Code Base application can be.
  8.  
  9.    This application was compiled with the NOIO, SMALL and switches set.
  10.  
  11.    After compiling and linking with the Turbo C 2.0 Tiny memory model,
  12.    the EXE size was 23.5K.  Refer to file 'd4small.prj'.
  13.  
  14.    After compiling and linking with the Microsoft C 5.1 compiler, with 
  15.    size optimization, the EXE size was 13.5K.  Refer to file 'd4sm.bat'.
  16.    
  17. */
  18.  
  19. #include "d4base.h"
  20. #include "p4misc.h"
  21.  
  22. #ifndef UNIX
  23. #include <io.h>
  24. #endif
  25.  
  26. #include <stdlib.h>
  27. #include <string.h>
  28.  
  29. extern  int    v4cur_base ;
  30. extern  BASE  *v4base ;
  31.  
  32. char  msg[]  = "\nd4small  database_file_name" ;
  33. char  msg2[] = "\nThe number of blank characters in the database: " ;
  34.  
  35. main( int argc, char **argv )
  36. {
  37.    long  i_rec, count ;
  38.    int   rec_width, i ;
  39.    char *ptr, output[40] ;
  40.  
  41.    if ( argc < 2 )
  42.    {
  43.       write( 1, msg, sizeof(msg)-1 ) ;
  44.       exit(1) ;
  45.    }
  46.  
  47.    if ( d4use( argv[1] ) < 0 )  exit(2) ;
  48.  
  49.    ptr =  (char *) f4record() ;
  50.    rec_width =  f4record_width() ;
  51.    count =  0 ;
  52.  
  53.    for ( i_rec = 1; i_rec <= d4reccount(); i_rec++ )
  54.    {
  55.       if ( d4go(i_rec) < 0 )  exit(2) ;
  56.       for ( i=0; i< rec_width; i++ )
  57.      if ( ptr[i] == ' ' )  count++ ;
  58.    }
  59.  
  60.    c4ltoa( count, output, 10 ) ;
  61.    output[10] = '\0' ;
  62.  
  63.    write( 1, msg2, sizeof(msg2)-1 ) ;
  64.    write( 1, output, strlen(output) ) ;
  65.    write( 1, "\n", 1 ) ;
  66.  
  67.    exit(0) ;
  68. }
  69.  
  70.  
  71.  
  72. /*  c4ltoa
  73.  
  74.     Converts a RECNUM to a string.  Fill with '0's rather than blanks if
  75.     'num' is less than zero.
  76.  
  77.     Copy 'c4ltoa' from 'c4.c' so that all of the code in 'c4.c' does
  78.     not get linked in.
  79. */
  80.  
  81. char *c4ltoa( long l_val, char *ptr, int num)
  82. {
  83.    int     n, num_pos ;
  84.    long  i_long ;
  85.  
  86.    i_long =  (l_val>0) ? l_val : -l_val ;
  87.    num_pos =  n =  (num > 0) ? num : -num ;
  88.  
  89.    while (n-- > 0)
  90.    {
  91.       ptr[n] = (char) ('0'+ i_long%10) ;
  92.       i_long = i_long/10 ;
  93.    }
  94.  
  95.    if ( i_long > 0 )
  96.    {
  97.      memset( ptr, (int) '*', (size_t) num_pos ) ;
  98.      return ptr ;
  99.    }
  100.  
  101.    num--;
  102.    for (n=0; n<num; n++)
  103.       if (ptr[n]=='0')
  104.      ptr[n]= ' ';
  105.       else
  106.      break ;
  107.  
  108.    if (l_val < 0)
  109.    {
  110.       if ( ptr[0] != ' ' )
  111.       {
  112.      memset( ptr, (int) '*', (size_t) num_pos ) ;
  113.      return ptr ;
  114.       }
  115.       for (n=num; n>=0; n--)
  116.      if (ptr[n]==' ')
  117.      {
  118.         ptr[n]= '-' ;
  119.         break ;
  120.      }
  121.    }
  122.  
  123.    return(ptr) ;
  124. }
  125.