home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / GENCSRC.ZIP / LISTF.C < prev    next >
C/C++ Source or Header  |  1987-11-21  |  7KB  |  159 lines

  1. /* *************************************************************** */
  2. /* This module contains the functions called by the list.c program */
  3. /* program. If this were a program to be used for some specific    */
  4. /* purpose, it would probablly not be wise to break it up into two */
  5. /* separately compiled modules. It is only done here for purposes  */
  6. /* of illustration. It is a useful program.                        */
  7. /* *************************************************************** */
  8.  
  9. #define MAXLINES 54            /* maximum number of lines per page */
  10. #include "stdio.h"                     /* standard I/O header file */
  11. #include "string.h"                    /* prototypes for strings   */
  12.  
  13. void open_file(int no,char *name);
  14. void open_print_file(void);
  15. void print_a_line(void);
  16. void header(void);
  17. void top_of_page(void);
  18.  
  19. extern FILE *file_point;         /* pointer to the file to be read */
  20. extern FILE *print_file_point;           /* pointer to the printer */
  21. extern char oneline[];                 /* input string buffer area */
  22. char filename[15];               /* filename from header or prompt */
  23. int line_number = 0;             /* line number initialized to one */
  24. int page_number = 1;             /* page number initialized to one */
  25. int lines_this_page = 0;              /* lines on this page so far */
  26.  
  27.  
  28. /* ***************************************************** open_file */
  29. /* This function opens the input file named on the command line,   */
  30. /* if there was one defined. Otherwise, it requests a file name to */
  31. /* open and opens the requested file.                              */
  32. /* *************************************************************** */
  33. void open_file(int no,char *name)
  34. {
  35.  
  36.    strcpy(filename,name);         /* copy name for printing header */
  37.    file_point = NULL;           /* if no name was given in command */
  38.    if (no == 2) {              /* 2nd field in command is filename */
  39.       file_point = fopen(name,"r");         /* open requested file */
  40.       if (file_point == NULL)        /* NULL if file doesn't exist */
  41.          printf("Filename on command line doesn't exist!\n");
  42.    }
  43.  
  44.    do {
  45.       if (file_point == NULL) {                 /* no filename yet */
  46.          printf("Enter filename -> ");
  47.          scanf("%s",filename);
  48.          file_point = fopen(filename,"r");            /* open file */
  49.          if (file_point == NULL)          /* NULL if file no exist */
  50.             printf("Filename doesn't exist, try again.\n");
  51.       }
  52.    } while (file_point == NULL);   /* continue until good filename */
  53. }
  54.  
  55.  
  56. /* *********************************************** open_print_file */
  57. /* This function opens the printer file to the standard printer.   */
  58. /* *************************************************************** */
  59. void open_print_file(void)
  60. {
  61.    print_file_point = fopen("PRN","w");       /* open printer file */
  62. }
  63.  
  64.  
  65. /* ************************************************** print_a_line */
  66. /* This routine prints a line of text and checks to see if there   */
  67. /* is room for another line on the page. If not, it starts a new   */
  68. /* page with a new header. This routine calls several other local  */
  69. /* routines.                                                       */
  70. /* *************************************************************** */
  71. void print_a_line(void)
  72. {
  73. int index;
  74.  
  75.    header();
  76.    printf("%5d %s",line_number,oneline);
  77.  
  78.                        /* This prints a line of less than 72 chars */
  79.    if (strlen(oneline) < 72)
  80.       fprintf(print_file_point,"%5d %s",line_number,oneline);
  81.  
  82.                           /* This prints a line of 72 to 143 chars */
  83.    else if (strlen(oneline) < 144) {
  84.       fprintf(print_file_point,"%5d ",line_number);
  85.       for (index = 0;index < 72;index++)
  86.          fprintf(print_file_point,"%c",oneline[index]);
  87.       fprintf(print_file_point,"<\n      ");
  88.       for (index = 72;index < strlen(oneline);index++)
  89.          fprintf(print_file_point,"%c",oneline[index]);
  90.       lines_this_page++;
  91.    }
  92.  
  93.                           /* This prints a line of 144 to 235 chars */
  94.    else if (strlen(oneline) < 235) {
  95.       fprintf(print_file_point,"%5d ",line_number);
  96.       for (index = 0;index < 72;index++)
  97.          fprintf(print_file_point,"%c",oneline[index]);
  98.       fprintf(print_file_point,"<\n      ");
  99.       for (index = 72;index < 144;index++)
  100.          fprintf(print_file_point,"%c",oneline[index]);
  101.       fprintf(print_file_point,"<\n      ");
  102.       for (index = 144;index < strlen(oneline);index++)
  103.          fprintf(print_file_point,"%c",oneline[index]);
  104.       lines_this_page += 2;
  105.    }
  106.          /* the following line outputs a newline if there is none
  107.                                       at the end of the last line */
  108.    if (oneline[strlen(oneline)-1] != '\n')
  109.       fprintf(print_file_point,"%c",'\n');
  110.    line_number++;
  111.    lines_this_page++;
  112. }
  113.  
  114.  
  115. /* ******************************************************** header */
  116. /* This routine checks to see if a header needs to be printed. It  */
  117. /* also checks for the end of a page. and spaces the paper up.     */
  118. /* *************************************************************** */
  119. void header(void)
  120. {
  121. int index;
  122.  
  123.                   /* first see if we are at the bottom of the page */
  124.    if (lines_this_page > MAXLINES) {  /* space paper up for bottom */
  125.    for (index = lines_this_page;index < 61;index++)
  126.       fprintf(print_file_point,"\n");
  127.    lines_this_page = 0;
  128.    }
  129.  
  130.             /* put a monitor header out only at the very beginning */
  131.    if (line_number == 0) {               /* display monitor header */
  132.       printf("        Source file %s\n",filename);
  133.       line_number = 1;
  134.    }
  135.  
  136.          /* check to see if we are at the top of the page either   */
  137.          /* through starting a file, or following a bottom of page */
  138.    if (lines_this_page == 0) {        /* top of every printer page */
  139.       fprintf(print_file_point,"\n\n\n        ");
  140.       fprintf(print_file_point," Source file - %s        ",filename);
  141.       fprintf(print_file_point,"          Page %d\n\n", page_number);
  142.       page_number++;
  143.    }
  144. }
  145.  
  146.  
  147. /* *************************************************** top_of_page */
  148. /* This function spaces the paper to the top of the next page so   */
  149. /* that another call to this function will start correctly. This   */
  150. /* is used only at the end of a complete printout.                 */
  151. /* *************************************************************** */
  152. void top_of_page(void)
  153. {
  154. int index;
  155.  
  156.    for (index = lines_this_page;index < 61;index++)
  157.       fprintf(print_file_point,"\n");
  158. }
  159.