home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctech / 1988_02 / bigfile.c < prev    next >
Text File  |  1985-08-12  |  13KB  |  393 lines

  1.  /* vbigfile.c -  400 line sample program */
  2. #include "stdio.h"
  3.  
  4. #include "bigfile1.h"
  5. #include "bigfile2.h"
  6. #include "bigfile3.h"
  7.  
  8.  long top_of_page ;
  9.  FILE *diskfile ;                   /* file pointer for text file */
  10.  char filename[81] ;
  11.  long filesize ;                  /* size of the file in bytes */
  12.  
  13.  main()
  14.  {
  15.     int cmd ;                     /* holds current cmd     */
  16.     long new_pos  ;               /* holds input file position */
  17.                                   /* from move to cmd */
  18.  
  19.     get_filename() ;              /* get file name and open file */
  20.     set_filesize() ;              /* set up file size variable */
  21.     cmd = FIRSTPAGE ;             /* force display of 1st page */
  22.  
  23.     do {                          /* repeat until told to exit */
  24.          exec_cmd(cmd,new_pos);   /* execute the current command */
  25.          cmd = get_cmd(&new_pos); /* get the next command */
  26.        } while( cmd != EXITPGM );
  27.  
  28.     close_data_file() ;
  29.  }
  30.  
  31.  
  32.  /* viewget.c - get_cmd function - get an input command  */
  33.  
  34.  
  35. /* definitions of key values returned by getkey */
  36.  
  37.  int get_cmd(new_pos)      /* get next input command from keyboard */
  38.                            /* returns the command type entered */
  39.  long int *new_pos ;       /* store the new file position here */
  40.  {
  41.     int key ;              /* holds the keyboard input value */
  42.                /* ( see keyio.h ) for values */
  43.     int cmd ;              /* holds the command type value */
  44.     long get_new_pos();    /* declare it so type is known  */
  45.  
  46.                            /* display prompts */
  47.     printf(  "\n         Type one of these Input Commands");
  48.     printf(  "\n HOME  = First Page      ");
  49.     printf(    "  %c   = Previous Line   ",24);
  50.     printf(     "PG UP = Previous Page ");
  51.     printf(  "\n END   = Last  Page      ");
  52.     printf(    "  %c   = Next Line       ",25);
  53.     printf(     "PG DN = Next Page \n");
  54.     printf(  "\n ESC   = Exit Pgm        ");
  55.     printf(     "SPACE = Move to position ");
  56.  
  57.     do
  58.       { key = getkey() ;   /* get next keyboard input */
  59.         switch( key )      /* classify it */
  60.           {
  61.           case PGDNKEY : cmd = NEXTPAGE       ;break;
  62.           case PGUPKEY : cmd = PREVPAGE       ;break;
  63.           case ASCESC  : cmd = EXITPGM        ;break;
  64.           case ' '     : cmd = MOVETOPOS      ;break;
  65.           case HOMEKEY : cmd = FIRSTPAGE      ;break;
  66.           case ENDKEY  : cmd = LASTPAGE       ;break;
  67.           case UPARROW : cmd = PREVLINE       ;break;
  68.           case DOWNARROW : cmd = NEXTLINE     ;break;
  69.           default      : cmd = INVALIDCMD     ;
  70.           }  /* end of switch stmt */
  71.       } while( cmd == INVALIDCMD ) ;
  72.  
  73.     /* the move to position command requires a position as input */
  74.     if (cmd== MOVETOPOS)
  75.        *new_pos = get_new_pos() ;
  76.  
  77.     return(cmd) ;
  78.  }
  79.  
  80.  
  81.  
  82.  long int get_new_pos()  
  83.  {
  84.     char scratch[81] ;     /* use to flush invalid input */
  85.     long new_pos ;
  86.     int  valid_input ;     /* flag for loop to test */
  87.  
  88.     /* get a new position from the keyboard and validate it */
  89.     /* if it is not valid, repeat the process */
  90.     valid_input = 0 ;
  91.     do
  92.       { printf("\n new file position: ");
  93.         if( scanf("%ld",&new_pos) == 0 )
  94.            {                         /* non-numeric input  */
  95.              scanf("%s",scratch) ;   /* flush rest of line */
  96.              printf("\n the file position must be numeric") ;
  97.            }
  98.         /* we've got a number - check its range */
  99.         else if( new_pos < 0L )  /* is it > 0 ? */
  100.            printf("\n the file position must be greater than zero");
  101.         else if( new_pos > filesize ) /* is it within the file */
  102.            { printf("\n the file position must be less than or equal to");
  103.              printf(" %ld",filesize) ;
  104.            }
  105.         else valid_input = 1 ;   /* we can exit the loop now */
  106.       } while( valid_input == 0  ) ;
  107.  
  108.     return(new_pos) ;
  109.  }
  110.  
  111.  
  112.             
  113.  /* viewexec.c file - exec_cmd function - executes one input command  */
  114.  
  115.  int exec_cmd(cmd,new_pos)               /* execute input command */
  116.  int cmd ;                               /* value of command to execute */
  117.  long int new_pos ;                      /* where to position file next */
  118.                              /* ( only used with MOVETO cmd ) */
  119.  {
  120.        switch( cmd )  {
  121.           case PREVPAGE  :               /* move backward to last page */
  122.                move_backward(PAGE_SIZE - LINES_OVERLAP) ;
  123.                display_page() ;
  124.                break;
  125.           case NEXTPAGE  :               /* move forward to next page */
  126.                move_forward(PAGE_SIZE - LINES_OVERLAP);
  127.                display_page() ;
  128.                break;
  129.           case EXITPGM :
  130.                break;
  131.           case MOVETOPOS :
  132.                move_to(new_pos) ;        /* move to specified position */
  133.            set_top_page() ;          /* make it top_of_page for now */
  134.                move_backward(0);         /* move to begin. of current line */
  135.                display_page() ;
  136.                break;
  137.           case FIRSTPAGE  :
  138.                move_to(0L);               /* move to beginning of file */
  139.            set_top_page() ;           /* make it the top of the page */
  140.                display_page() ;
  141.                break;
  142.           case LASTPAGE  :
  143.            move_to(filesize) ;              /* move to end of file */
  144.            set_top_page() ;           /* make it the top of the page */
  145.                move_backward(PAGE_SIZE); /* put the end of file at bottom */
  146.                display_page() ;
  147.                break;
  148.      case  PREVLINE :
  149.            move_backward(1) ;        /* move back one line */
  150.            display_page() ;
  151.            break;
  152.          case  NEXTLINE :
  153.            move_forward(1);          /* move forward one line */
  154.            display_page() ;
  155.                break;
  156.           }
  157.  }
  158.  
  159.  /* viewfind.c file - find module - moves to new positions in the file */
  160.  
  161.  
  162.  int move_forward(nlines)
  163.  int nlines ;               /* number of lines to move */
  164.  {
  165.     int c ;                 /* hold char here */
  166.  
  167.     move_to(top_of_page) ;       /* start at top of page */
  168.     /* move forward until we find (nlines) end of lines or end of file */
  169.     do {
  170.          c = get_next_char() ;
  171.          if( c  == END_LINE ) /* check for end of line */
  172.              nlines = nlines - 1 ; 
  173.        } while( (nlines  > 0) & (c != EOF_MARK) ) ;
  174.  
  175.     set_top_page() ;        /* make this the new top of the display page */
  176.  }
  177.  
  178.  
  179.  int move_backward(nlines)
  180.  int nlines ;               /* number of lines to move */
  181.  {                          /* zero lines means start of current line */
  182.     int c ;                 /* hold char here */
  183.  
  184.     move_to(top_of_page) ;       /* start at top of page */
  185.     nlines = nlines + 1 ;        /* add one for current line */
  186.     /* move backward past (nlines) end of lines or to BOF */
  187.     do {
  188.          c = get_previous_char();
  189.          if(  c  == END_LINE )
  190.              nlines = nlines - 1 ;
  191.        } while( (nlines > 0) & (c != BOF_MARK) ) ;
  192.  
  193.     /* we are now in front of a end of line char ( or at BOF ) */
  194.     /* we must skip over the end of line char  unless we are at BOF */
  195.     if( c != BOF_MARK )
  196.         get_next_char() ;
  197.     set_top_page() ;        /* make this position the new top of page */
  198.  }
  199.  
  200.  
  201. /* viewdisp.c file - display_page function - display current page of file */
  202.  
  203. #define TAB_WIDTH     8
  204. #define PAGE_WIDTH   80
  205. #define ASC_TAB       9
  206.  int  row ;      /* current line of page */
  207.  int  col ;      /* current column of page */
  208.  
  209.  display_page()
  210.  {
  211.     int c ;           /* hold the character here */
  212.     int i ;           /* counter for writing string of dashes */
  213.  
  214.     row = 1 ;         /* set up starting row and column values */
  215.     col = 1 ;
  216.  
  217.     move_to(top_of_page); /* start out at top of the page */
  218.                      /* write a header line */
  219.     printf("\n FILE - %s    POSITION - %ld    FILE SIZE - %ld \n",
  220.     filename,top_of_page,filesize);
  221.  
  222.                       /* write a border line of dashes */
  223.     for( i=1 ; i <= 80 ; i = i + 1 )
  224.        { putchar('-'); }
  225.  
  226.     /* get chars from file until we've written (PAGE_SIZE) lines */
  227.     /* or we've reached the end of the file */
  228.     c = get_next_char() ;
  229.     while( ( row <= PAGE_SIZE ) && ( c != EOF_MARK ) )
  230.       {                             /* write til end of page or file */
  231.        disp_char(c) ;               /* display current character */
  232.        c = get_next_char() ;        /* and get another one */
  233.       }
  234.  
  235.     while( row <= PAGE_SIZE )        /* pad out page if eof reached */
  236.       { putchar('\n'); row = row + 1 ; }
  237.  
  238.                     /* write a border line of dashes */
  239.     for( i=1 ; i <= 80 ; i = i + 1 )
  240.        { putchar('-'); }
  241.  
  242.  }
  243.  
  244.  
  245.  
  246.  int disp_char(c)                   /* display one char */
  247.                     /* and update row and column */
  248.  int c ;                            /* value of char to write */
  249.  {
  250.     /* classify the character and handle accordingly */
  251.  
  252.     if( c >= 32 )                  
  253.       { putchar(c);             /* ASCII graphic character - display it */
  254.         col = col + 1 ;             /* advance column number */
  255.         if( col > PAGE_WIDTH )      /* check for wrap-around */
  256.           { row = row + 1 ;         /*   y - advance row no  */
  257.             col = 1 ;               /*       and set column back to first col*/
  258.           }
  259.       }
  260.     else if( c == END_LINE )
  261.       { putchar('\n') ;             /* end of line char - force a new line */
  262.         row = row + 1 ;             /* advance row no. */
  263.         col = 1 ;                   /* set column back to first col */
  264.       }
  265.     else if( c == ASC_TAB )
  266.       {
  267.     do                          /* tab - expand it */
  268.           { putchar(' ');           /* print spaces    */
  269.         col = col + 1 ;         /* and advance column no. */
  270.             if( col > PAGE_WIDTH )  /* checking for wrap-around */
  271.               { row = row + 1 ;
  272.                 col = 1 ;
  273.           } 
  274.           } while( ( col % TAB_WIDTH ) != 1 ) ; /* until tab stop reached */
  275.       } 
  276.  }
  277.  
  278.  /* viewio.c file - I/O module for VIEW program */
  279.  
  280.  
  281.  /* seek mode definitions */
  282. #define  CURRENT_REL   1 
  283. #define  EOF_REL       2 
  284. #define  BOF_REL       0 
  285.  
  286.  /* control Z character - marks EOF in CP/M convention */
  287. #define  CTL_Z        26
  288.  
  289. /* extern long int fseek() ;*/
  290.  extern long int ftell() ;
  291.  
  292.  int get_filename()               /* get name of file and open it */
  293.  {
  294.     do
  295.       { printf("file name:") ;
  296.         scanf("%s",filename) ;
  297.         diskfile = fopen(filename,"rb") ;
  298.         if (diskfile == 0)
  299.            printf("\n file - %s can not be opened\n",filename);
  300.       } while( diskfile == 0 ) ;
  301.  }
  302.  
  303.  
  304.  int set_filesize()             /* set up file size */
  305.  {
  306.     long current_position ;
  307.     int c ;
  308.     
  309.     filesize = fseek(diskfile,0L,EOF_REL) ;  /* get size of file */
  310.  
  311.     /* check last 128 byte block for a CTL-z */
  312.     fseek(diskfile, (filesize-1) & ~ 0x7f ,BOF_REL);
  313.     do {
  314.          c = getc(diskfile); 
  315.        } while( (c != CTL_Z) & (c != EOF) );
  316.  
  317.     /* set filesize to be in front of the control-Z (if it was found) */
  318.     if( c == CTL_Z )
  319.         filesize = ftell(diskfile) - 1 ;  
  320.  }
  321.  
  322.  
  323.  
  324.  int move_to(new_pos)           /* move to specified position */
  325.  long new_pos ;
  326.  {
  327.     fseek(diskfile,new_pos,BOF_REL);
  328.  }
  329.  
  330.  
  331.  int set_top_page()             /* make the current file position */
  332.  {                              /* new top of page */
  333.    top_of_page = ftell(diskfile) ;
  334.  }
  335.  
  336.  
  337.  
  338.  
  339.  int get_next_char()           /* get char at file position */
  340.  {
  341.     char c ;
  342.  
  343.     if( ftell(diskfile) == filesize )   /* are we at end of file ? */
  344.         return( EOF_MARK ) ;            /*   y - return our end file mark */
  345.     else
  346.       { /* not at end - read the next byte from the file */
  347.         c = getc(diskfile);             /* get a char */
  348.         return( (c & 0x7f) ) ;     /* set parity bit to zero and return it */
  349.       }
  350.  }
  351.  
  352.  
  353.  
  354.  int get_previous_char()         /* read the char in front of */
  355.  {                               /* current file position */
  356.     char c ;
  357.  
  358.     if( ftell(diskfile) != 0L )            /* check for begin. file */
  359.        { fseek(diskfile,-1L,CURRENT_REL);  /* back up one char */
  360.          c = getc(diskfile);               /* read a char*/
  361.      fseek(diskfile,-1L,CURRENT_REL) ; /*  back up in front of char read*/
  362.          return( (c & 0x7f) ) ;          /* set the parity bit to zero */
  363.                          /* and return the char */
  364.        }
  365.     else return(BOF_MARK) ;
  366.  }
  367.  
  368.  
  369.  int close_data_file()
  370.  {
  371.     fclose(diskfile) ;
  372.  }
  373.   
  374.  
  375.  
  376.  int keypress()
  377.  {
  378.    if ((bdos(0xb,0) & 0xff) == 0xff)
  379.        return( 1) ;
  380.       else return(0) ;
  381.  }
  382.  
  383.  int getkey()
  384.  {
  385.     int c ;
  386.  
  387.     c = (bdos(0x8,0) & 0xff) ;
  388.     if (c == 0) c = 0x100 + (bdos(0x8,0) & 0xff) ;
  389.     return(c) ;
  390.  }
  391.  
  392.                         
  393.