home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / codebase.zip / LAUNCH_D.C < prev    next >
C/C++ Source or Header  |  1992-12-14  |  5KB  |  206 lines

  1. #include "d4all.h"
  2. #include <fcntl.h>
  3. #include <sys\stat.h>
  4.  
  5. extern _stklen = 10000;
  6.  
  7. REPORT4 *report = NULL;
  8. CODE4   cb;
  9.  
  10. void cleanup( void );
  11. void show_parms( void );
  12. void launch_error( char *, char * );
  13.  
  14. void main( int argc, char **argv)
  15. {
  16.    int out_code = 1, use_codes = 0, p_height = 25, p_width = 80, i;
  17.    char *sort_expr = NULL, *query_expr = NULL, *dest = NULL, *fname = NULL;
  18.    int len;
  19.  
  20.    /* routine must have at least a file name */
  21.    if(argc < 2)
  22.    {
  23.       show_parms();
  24.       return;
  25.    }
  26.  
  27.    /* initialize CODE4 structure */
  28.    d4init(&cb);
  29.    cb.auto_open = 0;
  30.  
  31.  
  32.    if(argv[1][0] == '-')
  33.    {
  34.       launch_error( "The first parameter must be a file name.", NULL );      
  35.       cleanup();
  36.       return;
  37.    }
  38.  
  39.    /* open the specified report file */
  40.    len = strlen(argv[1]);
  41.    fname = (char *) u4alloc_er( &cb, len + 6 );
  42.    u4ncpy( fname, argv[1], len + 1 );
  43.    u4name_ext( fname, len + 6, ".rep", 0 );
  44.    report = report4retrieve( &cb, fname, 1 );
  45.  
  46.    if( report == NULL )
  47.    {
  48.       launch_error("Unable to retrieve report file:",fname);
  49.       cleanup();
  50.       return;
  51.    }
  52.  
  53.    /* parse the command line arguments */
  54.    for(i = 2; i < argc; i++)
  55.    {
  56.       if(argv[i][0] == '-')
  57.       {
  58.          switch( argv[i][1] )
  59.          {
  60.             case 'q':
  61.                i++;
  62.                len = strlen(argv[i]);
  63.                query_expr = (char *)u4alloc_er( &cb, len + 1 );
  64.                if( ! query_expr )
  65.                   break;
  66.                u4ncpy( query_expr, argv[i], len+1 );
  67.                break;
  68.  
  69.             case 's':
  70.                i++;
  71.                len = strlen(argv[i]);
  72.                sort_expr = (char *)u4alloc_er( &cb, len + 1  );
  73.                if( ! sort_expr )
  74.                   break;
  75.                u4ncpy( sort_expr, argv[i], len+1 );
  76.                break;
  77.  
  78.             case 'x':
  79.                p_width = atoi(argv[i]+2);
  80.                break;
  81.  
  82.             case 'y':
  83.                p_height = atoi(argv[i]+2);
  84.                break;
  85.  
  86.             case 'p':
  87.                len = strlen( argv[i] );
  88.                if( len == 2)
  89.                {
  90.                   out_code = 4;
  91.                }
  92.                else
  93.                {
  94.                   dest = u4alloc_er( &cb, len + 1 );
  95.                   u4ncpy( dest, argv[i] + 2, len + 1 );
  96.  
  97.                   out_code = 0;
  98.                   if((out_code = open( dest, O_CREAT | O_BINARY | O_RDWR, S_IWRITE )) == -1)
  99.                   {
  100.                      printf("Unable to open output file %s.\n",dest);
  101.                      out_code = -1;
  102.                      break;
  103.                   }
  104.                }
  105.                break;
  106.  
  107.             case 't':
  108.                use_codes = 1;
  109.                break;
  110.  
  111.  
  112.          }
  113.       }
  114.       else
  115.       {
  116.          launch_error("Unknown argument:",argv[i]);
  117.       }
  118.    }
  119.  
  120.    if(out_code == -1)
  121.    {
  122.       cleanup();
  123.       return;
  124.    }
  125.  
  126.    /* set the new query expression */
  127.    if( query_expr )
  128.       if( relate4query_set( report->relate, query_expr) < 0 )
  129.       {
  130.          cleanup();
  131.          return;
  132.       }
  133.  
  134.    /* set the new sort expression */
  135.    if( sort_expr )
  136.       if( relate4sort_set ( report->relate, sort_expr) < 0 )
  137.       {
  138.          cleanup();
  139.          return;
  140.       }
  141.  
  142.    if(out_code == 1)
  143.       use_codes = 0;
  144.  
  145.    report4output( report, out_code, NULL, use_codes );
  146.  
  147.    report4page_size( report, p_width, p_height );
  148.  
  149.    
  150.    report4do(report);
  151.  
  152.    cleanup();
  153.  
  154. }
  155.  
  156.  
  157. void cleanup( void )
  158. {
  159.    if( report )
  160.       report4free( report, 1, 1 );
  161.  
  162.    d4init_undo( &cb );
  163.  
  164.    return;
  165. }
  166.  
  167.  
  168. void launch_error( char *message, char *parm )
  169. {
  170.    printf("\nLaunch utility error:\n");
  171.    printf("\n\t%s ",message);
  172.  
  173.    ( parm ) ? printf("%s\n",parm) : printf("\n");
  174.  
  175.    return;
  176. }
  177.  
  178.  
  179. void show_parms( void )
  180. {
  181.  
  182.    printf("Usage: launch_d <filename> [-q<expr>][-s<expr>][-x<nn>][-p[dest]][-t]\n");
  183.    printf("\n");
  184.    printf("Options:\n\n");
  185.    printf("-q<expr>  Change the default query expression. Query expressions\n");
  186.    printf("          must be entered in double quotes. String literals within\n");
  187.    printf("          the expression must be entered in single quotes.\n");
  188.    printf("-s<expr>  Change the default sort expression. Sort expressions\n");
  189.    printf("          must be entered in double quotes. String literals within\n");
  190.    printf("          the expression must be entered in single quotes.\n");
  191.    printf("-x<nn>    Change the default horizontal size of the page ( in \n");
  192.    printf("          characters ). The default number of characters is 80.\n");
  193.    printf("-y<nn>    Change the default vertical size of the page (in lines ).\n");
  194.    printf("          The default number of lines is 25.\n");
  195.    printf("-p[dest]  Change the default print destination. Reports by default\n");
  196.    printf("          go to the monitor. Specifying the -p option alone sends\n");
  197.    printf("          output to \'standart print\'. If a character string is\n");
  198.    printf("          specified, output is directed to a file or device with the\n");
  199.    printf("          specified name.\n");
  200.    printf("-t        Use the Non-Windows printer codes stored in the report. By\n");
  201.    printf("          default the printer codes are not used.\n");
  202.    return;
  203. }
  204.  
  205.  
  206.