home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CLIPB52.ZIP / ACKER.ZIP / SPENCE.PRG < prev    next >
Encoding:
Text File  |  1990-05-31  |  18.2 KB  |  656 lines

  1.  FUNCTION bb_view
  2.  
  3.  PARAM t, l, b, r, fname
  4.  
  5.  PRIVATE s_line, save_scr, lkey
  6.  
  7.      * Save and set cursor
  8.      * .
  9.      * .
  10.      * .
  11.      SET CURSOR OFF
  12.  
  13.      IF bb_init(fname, t, l, b, r)
  14.          s_line = 1                      && Start line #
  15.  
  16.          save_scr = savescreen(t, l, b, r)
  17.  
  18.          * If we have at least one line, show it ...
  19.          IF bb_show_win(s_line)
  20.              * Now wait for page up, page down, or ESC
  21.              lkey = inkey(0)
  22.              DO WHILE lkey != ESC
  23.                 DO CASE
  24.                    CASE lkey = PG_UP
  25.                        * Move up by one page full
  26.                        IF s_line != 1
  27.                            s_line = s_line - bb_rows_win
  28.                            bb_show_win(s_line)
  29.                        ENDIF
  30.  
  31.                    CASE lkey = PG_DOWN
  32.                        * Try and move down by one page full
  33.                        s_line = s_line + bb_rows_win
  34.                        IF !bb_show_win(s_line)
  35.                            * There were no more lines. Backtrack
  36.                            * and set last window pointer
  37.                            s_line = s_line - bb_rows_win
  38.                            bb_show_win(s_line)
  39.                        ENDIF
  40.                 ENDCASE
  41.  
  42.                 * Now wait for page up, page down, or ESC
  43.                 lkey = inkey(0)
  44.              ENDDO
  45.          ENDIF                              && Show first window
  46.  
  47.          restscreen(t, l, b, r, save_scr)
  48.          bb_end()
  49.      ENDIF                                  && bb_init ok
  50.  
  51.      * Restore cursor
  52.      * .
  53.      * .
  54.      * .
  55.  RETURN VOID
  56.  
  57.  
  58.     * LOGICAL bb_init(fname, t, l, b, r)
  59.     *
  60.     * NUMERIC fname                        && Name of file to browse
  61.     * NUMERIC t, l, b, r                   && Browse box coordinates
  62.     *
  63.     * Initialize the big browse class.
  64.     * Returns .T. if successfully initialized, .F. otherwise
  65.  
  66.     FUNCTION bb_init
  67.  
  68.     PARAM fname, rows_win
  69.  
  70.     PRIVATE bb_init
  71.  
  72.         PUBLIC bb_fhandle          && File handle of file being viewed
  73.         PUBLIC bb_rows_win         && The number of rows in the window
  74.         PUBLIC bb_cols_win         && The number of columns in the window
  75.         PUBLIC bb_t                && Window coordinates
  76.         PUBLIC bb_l
  77.         PUBLIC bb_b
  78.         PUBLIC bb_r
  79.         PUBLIC bb_f_ptrs           && Character string containing pointers
  80.                                    && into the browsed file, for each page
  81.  
  82.         bb_fhandle = fopen(fname, OPEN_RDWR)
  83.         IF bb_fhandle > 0
  84.             bb_t = t
  85.             bb_l = l
  86.             bb_b = b
  87.             bb_r = r
  88.             bb_rows_win = bb_b - bb_t + 1
  89.             bb_cols_win = bb_r - bb_l + 1
  90.  
  91.             bb_f_ptrs = ""
  92.             bb_mark(1)
  93.             bb_init = .T.
  94.         ELSE
  95.             ? "Cannot open file ", fname
  96.             RELEASE bb_fhandle, bb_rows_win, bb_cols_win, ;
  97.                     bb_t, bb_l, bb_b, bb_r, bb_f_ptrs
  98.             bb_init = .F.
  99.         ENDIF
  100.  
  101.     RETURN bb_init
  102.  
  103.  
  104.     FUNCTION bb_end
  105.  
  106.         fclose(bb_fhandle)
  107.         RELEASE bb_fhandle, bb_rows_win, bb_cols_win, ;
  108.                 bb_t, bb_l, bb_b, bb_r, bb_f_ptrs
  109.  
  110.     RETURN VOID
  111.  
  112.  
  113.  
  114.     * LOGICAL bb_show_win(s_line)
  115.     *
  116.     * NUMERIC s_line                       && Start line to browse from
  117.     *
  118.     * Show a window full from line s_line.
  119.     * Return .T. if at least one line shown, .F. otherwise
  120.  
  121.     FUNCTION bb_show_win
  122.  
  123.     PARAM s_line
  124.  
  125.     PRIVATE line_num, more_lines, line
  126.  
  127.         * Clear window
  128.         scroll(bb_t, bb_l, bb_b, bb_r, 0)
  129.  
  130.         line_num = s_line
  131.         more_lines = bb_gotoln(s_line)
  132.  
  133.         DO WHILE line_num < s_line + bb_rows_win .AND. more_lines
  134.             line = space(MAX_LINE_LEN)
  135.             more_lines = readln(bb_fhandle, @line)
  136.             IF more_lines
  137.                 @ bb_t + line_num - s_line, bb_l SAY pad(line, bb_cols_win)
  138.                 line_num = line_num + 1
  139.             ENDIF
  140.         ENDDO
  141.  
  142.         * if we read a complete window full, we can set next 
  143.         * window pointer ...
  144.         IF more_lines
  145.             bb_mark(line_num)
  146.         ENDIF
  147.  
  148.     RETURN line_num > s_line
  149.  
  150.  
  151.  
  152.     FUNCTION readln
  153.  
  154.     PARAM f_handle, buffer_ptr
  155.  
  156.     PRIVATE where_crlf, num_read
  157.  
  158.         num_read = fread(f_handle, @buffer_ptr, len(buffer_ptr))
  159.         IF num_read > 0
  160.             where_crlf = at(CRLF, buffer_ptr)
  161.             IF where_crlf != 0
  162.                 * Got crlf, position file pointer just after it
  163.                 fseek(f_handle, where_crlf - num_read + 1, SEEK_CUR)
  164.                 buffer_ptr = substr(buffer_ptr, 1, where_crlf - 1)
  165.             ELSE
  166.                 buffer_ptr = trim(buffer_ptr)
  167.             ENDIF
  168.         ENDIF
  169.  
  170.     RETURN num_read > 0
  171.  
  172.  
  173.  
  174.     * VOID bb_mark(line_num)
  175.     *
  176.     * NUMERIC line_num                     && The line number where the file
  177.     *                                      && is currently positioned
  178.     *
  179.     * The file is currently positioned at line_num. Line_num is a multiple
  180.     * of bb_rows_win. If this is the first time we have reached this point,
  181.     * we store a pointer to it in the bb_f_ptrs string.
  182.  
  183.     FUNCTION bb_mark
  184.  
  185.     PARAM line_num
  186.  
  187.     PRIVATE lines_seen
  188.  
  189.         lines_seen = (len(bb_f_ptrs) / F_PTR_LEN) * bb_rows_win
  190.         IF line_num > lines_seen
  191.             bb_f_ptrs = bb_f_ptrs + str(FTELL(bb_fhandle), F_PTR_LEN)
  192.         ENDIF
  193.  
  194.     RETURN VOID
  195.  
  196.  
  197.  
  198.     FUNCTION bb_gotoln
  199.  
  200.     PARAM line_num
  201.  
  202.     PRIVATE ret_val, lines_seen, win_num
  203.  
  204.         * If we have seen this line already, go to it, otherwise error
  205.         lines_seen = (len(bb_f_ptrs) / F_PTR_LEN) * bb_rows_win
  206.         ret_val = line_num <= lines_seen
  207.         IF ret_val
  208.             win_num = (line_num - 1) / bb_rows_win
  209.             fseek(bb_fhandle, val(substr(bb_f_ptrs, ;
  210.                                          win_num * F_PTR_LEN + 1, ;
  211.                                          F_PTR_LEN)))
  212.         ENDIF
  213.  
  214.     RETURN ret_val
  215.  
  216.  
  217.  
  218.  
  219.  * bb.pre
  220.  *
  221.  * Big browse routines. Allow browsing of files of any size
  222.  
  223.  #include "keys.h"
  224.  #include "system.h"
  225.  #include "files.h"
  226.  
  227.  #define F_PTR_LEN 7                    && length of file pointers
  228.  #define MAX_LINE_LEN 255               && we split a line at this length
  229.  
  230.  #define CRLF chr(13) + chr(10)
  231.  
  232.  #define FTELL(f_handle) fseek(f_handle, 0, SEEK_CUR)
  233.  
  234.  * Sample driver, pass file name on comamnd line ...
  235.  
  236.  PARAM fname
  237.  
  238.  CLEAR
  239.  bb_view(0, 0, 24, 79, fname)
  240.  
  241.  
  242.  * VOID bb_view(t, l, b, r, fname)
  243.  *
  244.  * NUMERIC t, l, b, r                   && Window coordinates
  245.  * CHARACTER fname                      && Name of file to browse
  246.  *
  247.  * Browse a file in the window. No restrictions on file size. Page up
  248.  * and down scroll windows, Escape exits
  249.  
  250.  FUNCTION bb_view
  251.  
  252.  PARAM t, l, b, r, fname
  253.  
  254.  PRIVATE s_line, save_scr, lkey
  255.  
  256.      * start_last_win saves line number which start the last viewable
  257.      * window. It is set when we determine we can no longer move,
  258.      * and is used to optimize the viewing of the last window (if they
  259.      * keep hitting page down, nothing happens). Initially set it to
  260.      * invalid line # so we know it has not been set yet ...
  261.  
  262.      * Save and set cursor
  263.      SET CURSOR OFF
  264.      IF bb_init(fname, t, l, b, r)
  265.          s_line = 1                      && Start line #
  266.  
  267.          save_scr = savescreen(t, l, b, r)
  268.  
  269.          * If we have at least one line, show it ...
  270.          IF bb_show_win(s_line)
  271.              * Now wait for page up, page down, or ESC
  272.              lkey = inkey(0)
  273.              DO WHILE lkey != ESC
  274.                  DO CASE
  275.                      CASE lkey = PG_UP
  276.                          * Move up by one page full
  277.                          IF s_line != 1
  278.                              s_line = s_line - bb_rows_win
  279.                              bb_show_win(s_line)
  280.                          ENDIF
  281.  
  282.                      CASE lkey = PG_DOWN
  283.                          * Try and move down by one page full
  284.                          s_line = s_line + bb_rows_win
  285.                          IF !bb_show_win(s_line)
  286.                              * There were no more lines. Backtrack
  287.                              * and set last window pointer
  288.                              s_line = s_line - bb_rows_win
  289.                              bb_show_win(s_line)
  290.                          ENDIF
  291.                  ENDCASE
  292.  
  293.                  * Now wait for page up, page down, or ESC
  294.                  lkey = inkey(0)
  295.              ENDDO
  296.          ENDIF                                    && Show first window
  297.  
  298.          restscreen(t, l, b, r, save_scr)
  299.          bb_end()
  300.      ENDIF                                        && bb_init ok
  301.  
  302.      * Restore cursor
  303.  
  304.  RETURN VOID 
  305.  
  306.  
  307.  * LOGICAL bb_show_win(s_line)
  308.  *
  309.  * NUMERIC s_line                       && Start line to browse from
  310.  *
  311.  * Show a window full from line s_line.
  312.  * Return .T. if at least one line shown, .F. otherwise
  313.  
  314.  FUNCTION bb_show_win
  315.  
  316.  PARAM s_line
  317.  
  318.  PRIVATE line_num, more_lines, line
  319.  
  320.      * Clear window
  321.      scroll(bb_t, bb_l, bb_b, bb_r, 0)
  322.  
  323.      line_num = s_line
  324.      more_lines = bb_gotoln(s_line)
  325.  
  326.      DO WHILE line_num < s_line + bb_rows_win .AND. more_lines
  327.          line = space(MAX_LINE_LEN)
  328.          more_lines = readln(bb_fhandle, @line)
  329.          IF more_lines
  330.              @ bb_t + line_num - s_line, bb_l SAY pad(line, bb_cols_win)
  331.              line_num = line_num + 1
  332.          ENDIF
  333.      ENDDO
  334.  
  335.      * if we read a complete window full, we can set next 
  336.      * window pointer ...
  337.      IF more_lines
  338.          bb_mark(line_num)
  339.      ENDIF
  340.  
  341.  RETURN line_num > s_line
  342.  
  343.  
  344.  * LOGICAL bb_gotoln(line_num)
  345.  *
  346.  * NUMERIC line_num                     && Line number to go to
  347.  *
  348.  * Position file at start of line_num. Return .T. if ok, .F. otherwise
  349.  
  350.  FUNCTION bb_gotoln
  351.  
  352.  PARAM line_num
  353.  
  354.  PRIVATE ret_val, lines_seen, win_num
  355.  
  356.      * If we have seen this line already, go to it, otherwise error
  357.      lines_seen = (len(bb_f_ptrs) / F_PTR_LEN) * bb_rows_win
  358.      ret_val = line_num <= lines_seen
  359.      IF ret_val
  360.          win_num = (line_num - 1) / bb_rows_win
  361.          fseek(bb_fhandle, val(substr(bb_f_ptrs, ;
  362.                                       win_num * F_PTR_LEN + 1, ;
  363.                                       F_PTR_LEN)))
  364.      ENDIF
  365.  
  366.  RETURN ret_val
  367.  
  368.  
  369.  * VOID bb_mark(line_num)
  370.  *
  371.  * NUMERIC line_num                     && The line number where the file
  372.  *                                      && is currently positioned
  373.  *
  374.  * The file is currently positioned at line_num. Line_num is a multiple
  375.  * of bb_rows_win. If this is the first time we have reached this point,
  376.  * we store a pointer to it in the bb_f_ptrs string.
  377.  
  378.  FUNCTION bb_mark
  379.  
  380.  PARAM line_num
  381.  
  382.  PRIVATE lines_seen
  383.  
  384.      lines_seen = (len(bb_f_ptrs) / F_PTR_LEN) * bb_rows_win
  385.      IF line_num > lines_seen
  386.          bb_f_ptrs = bb_f_ptrs + str(FTELL(bb_fhandle), F_PTR_LEN)
  387.      ENDIF
  388.  
  389.  RETURN VOID
  390.  
  391.  
  392.  * LOGICAL bb_init(fname, t, l, b, r)
  393.  *
  394.  * NUMERIC fname                        && Name of file to browse
  395.  * NUMERIC t, l, b, r                   && Browse box coordinates
  396.  *
  397.  * Initialize the big browse class.
  398.  * Returns .T. if successfully initialized, .F. otherwise
  399.  
  400.  FUNCTION bb_init
  401.  
  402.  PARAM fname, rows_win
  403.  
  404.  PRIVATE bb_init
  405.  
  406.      PUBLIC bb_fhandle          && File handle of file being viewed
  407.      PUBLIC bb_rows_win         && The number of rows in the window
  408.      PUBLIC bb_cols_win         && The number of columns in the window
  409.      PUBLIC bb_t                && Window coordinates
  410.      PUBLIC bb_l
  411.      PUBLIC bb_b
  412.      PUBLIC bb_r
  413.      PUBLIC bb_f_ptrs           && Character string containing pointers
  414.                                 && into the browsed file, for each page
  415.  
  416.      bb_fhandle = fopen(fname, OPEN_RDONLY)
  417.      IF bb_fhandle > 0
  418.          bb_t = t
  419.          bb_l = l
  420.          bb_b = b
  421.          bb_r = r
  422.          bb_rows_win = bb_b - bb_t + 1
  423.          bb_cols_win = bb_r - bb_l + 1
  424.  
  425.          bb_f_ptrs = ""
  426.          bb_mark(1)
  427.          bb_init = .T.
  428.      ELSE
  429.          ? "Cannot open file ", fname
  430.          RELEASE bb_fhandle, bb_rows_win, bb_cols_win, ;
  431.                  bb_t, bb_l, bb_b, bb_r, bb_f_ptrs
  432.          bb_init = .F.
  433.      ENDIF
  434.  
  435.  RETURN bb_init
  436.  
  437.  
  438.  FUNCTION bb_end
  439.  
  440.      fclose(bb_fhandle)
  441.      RELEASE bb_fhandle, bb_rows_win, bb_cols_win, ;
  442.              bb_t, bb_l, bb_b, bb_r, bb_f_ptrs
  443.  
  444.  RETURN VOID
  445.  
  446.  
  447.  FUNCTION readln
  448.  
  449.  PARAM f_handle, buffer_ptr
  450.  
  451.  PRIVATE where_crlf, num_read
  452.  
  453.      num_read = fread(f_handle, @buffer_ptr, len(buffer_ptr))
  454.      IF num_read > 0
  455.          where_crlf = at(CRLF, buffer_ptr)
  456.          IF where_crlf != 0
  457.              * Got crlf, position file pointer just after it
  458.              fseek(f_handle, where_crlf - num_read + 1, SEEK_CUR)
  459.              buffer_ptr = substr(buffer_ptr, 1, where_crlf - 1)
  460.          ELSE
  461.              buffer_ptr = trim(buffer_ptr)
  462.          ENDIF
  463.      ENDIF
  464.  
  465.  RETURN num_read > 0
  466.  
  467.  
  468.  
  469.  FUNCTION bb_view
  470.  
  471.  PARAM t, l, b, r, fname
  472.  
  473.  PRIVATE s_line, save_scr, lkey, start_last_win
  474.  
  475.      * start_last_win saves line number which start the last viewable
  476.      * window. It is set when we determine we can no longer move,
  477.      * and is used to optimize the viewing of the last window (if they
  478.      * keep hitting page down, nothing happens). Initially set it to
  479.      * invalid line # so we know it has not been set yet ...
  480.  
  481.      start_last_win = -1
  482.  
  483.      * Save and set cursor
  484.      SET CURSOR OFF
  485.      IF bb_init(fname, t, l, b, r)
  486.          s_line = 1                           && Start line #
  487.  
  488.          save_scr = savescreen(t, l, b, r)
  489.  
  490.          * If we have at least one line, show it ...
  491.          IF bb_show_win(s_line)
  492.              * Now wait for page up, page down, or ESC
  493.              lkey = inkey(0)
  494.              DO WHILE lkey != ESC
  495.                  DO CASE
  496.                      CASE lkey = PG_UP
  497.                          * Move up by one page full
  498.                          IF s_line != 1
  499.                              s_line = s_line - bb_rows_win
  500.                              bb_show_win(s_line)
  501.                          ENDIF
  502.  
  503.                      CASE lkey = PG_DOWN
  504.                          * OPTIMIZATION: If we have already determined
  505.                          * this is the last page, don't try and move
  506.                          IF s_line != start_last_win
  507.                              * Try and move down by one page full
  508.                              s_line = s_line + bb_rows_win
  509.                              IF !bb_show_win(s_line)
  510.                                  * There were no more lines. Backtrack
  511.                                  * and set last window pointer
  512.                                  s_line = s_line - bb_rows_win
  513.                                  start_last_win = s_line
  514.                                  bb_show_win(s_line)
  515.                              ENDIF
  516.                          ENDIF
  517.                  ENDCASE
  518.  
  519.                  * Now wait for page up, page down, or ESC
  520.                  lkey = inkey(0)
  521.              ENDDO
  522.          ENDIF                                && Show first window
  523.  
  524.          restscreen(t, l, b, r, save_scr)
  525.          bb_end()
  526.      ENDIF                                    && bb_init ok
  527.  
  528.      * Restore cursor
  529.  
  530.  RETURN VOID
  531.  
  532.  
  533.  
  534.  * LOGICAL bb_init(fname, t, l, b, r)
  535.  *
  536.  * NUMERIC fname                              && Name of file to browse
  537.  * NUMERIC t, l, b, r                         && Browse box coordinates
  538.  *
  539.  * Initialize the big browse class.
  540.  * Returns .T. if successfully initialized, .F. otherwise
  541.  
  542.  FUNCTION bb_init
  543.  
  544.  PARAM fname, rows_win
  545.  
  546.  PRIVATE bb_init
  547.  
  548.    PUBLIC bb_fhandle       && File handle of file being viewed
  549.    PUBLIC bb_rows_win      && The number of rows in the window
  550.    PUBLIC bb_cols_win      && The number of columns in the window
  551.    PUBLIC bb_t             && Window coordinates
  552.    PUBLIC bb_l
  553.    PUBLIC bb_b
  554.    PUBLIC bb_r
  555.    PUBLIC bb_fptr_handle   && File handle of pointers file
  556.  
  557.    bb_fhandle = fopen(fname, OPEN_RDWR)
  558.    IF bb_fhandle > 0
  559.        bb_t = t
  560.        bb_l = l
  561.        bb_b = b
  562.        bb_r = r
  563.        bb_rows_win = bb_b - bb_t + 1
  564.        bb_cols_win = bb_r - bb_l + 1
  565.  
  566.        bb_fptr_handle = fcreate("_fptrs")
  567.        bb_mark(1)
  568.        bb_init = .T.
  569.    ELSE
  570.        ? "Cannot open file ", fname
  571.        RELEASE bb_fhandle, bb_rows_win, bb_cols_win, ;
  572.                bb_t, bb_l, bb_b, bb_r, bb_fptr_handle
  573.  
  574.        bb_init = .F.
  575.    ENDIF
  576.  
  577.  RETURN bb_init
  578.  
  579.  
  580.  
  581.  FUNCTION bb_end
  582.  
  583.      fclose(bb_fhandle)
  584.      fclose(bb_fptr_handle)
  585.      RELEASE bb_fhandle, bb_rows_win, bb_cols_win, bb_t, bb_l, ;
  586.              bb_b, bb_r, bb_fptr_handle
  587.  
  588.  RETURN VOID
  589.  
  590.  
  591.  
  592.  * VOID bb_mark(line_num)
  593.  *
  594.  * NUMERIC line_num                     && The line number where the file
  595.  *                                      && is currently positioned
  596.  *
  597.  * The file is currently positioned at line_num. Line_num is a multiple
  598.  * of bb_rows_win. If this is the first time we have reached this point,
  599.  * we add a pointer to it at the end of the _fptrs file.
  600.  
  601.  FUNCTION bb_mark
  602.  
  603.  PARAM line_num
  604.  
  605.  PRIVATE lines_seen
  606.  
  607.      lines_seen = (file_len(bb_fptr_handle) / F_PTR_LEN) * bb_rows_win
  608.      IF line_num > lines_seen
  609.          fseek(bb_fptr_handle, 0, SEEK_EOF)
  610.          fwrite(bb_fptr_handle, str(FTELL(bb_fhandle), F_PTR_LEN))
  611.      ENDIF
  612.  
  613.  RETURN VOID
  614.  
  615.  
  616.  
  617.  FUNCTION file_len
  618.  
  619.  PARAM f_handle
  620.  PRIVATE save_fpos, ret_val
  621.  
  622.      save_fpos = FTELL(f_handle)
  623.      ret_val = fseek(f_handle, 0, SEEK_EOF)
  624.      fseek(f_handle, save_fpos)
  625.  
  626.  RETURN ret_val
  627.  
  628.  
  629.  
  630.  
  631.  * LOGICAL bb_gotoln(line_num)
  632.  *
  633.  * NUMERIC line_num                     && Line number to go to
  634.  *
  635.  * Position file at start of line_num. Return .T. if ok, .F. otherwise
  636.  
  637.  FUNCTION bb_gotoln
  638.  
  639.  PARAM line_num
  640.  
  641.  PRIVATE ret_val, lines_seen, win_num
  642.  
  643.      * If we have seen this line already, go to it, otherwise error
  644.      lines_seen = (file_len(bb_fptr_handle) / F_PTR_LEN) * bb_rows_win
  645.      ret_val = line_num <= lines_seen
  646.      IF ret_val
  647.          win_num = (line_num - 1) / bb_rows_win
  648.          fseek(bb_fptr_handle, win_num * F_PTR_LEN + 1)
  649.          fseek(bb_fhandle, val(freadstr(bb_fptr_handle, F_PTR_LEN)))
  650.      ENDIF
  651.  
  652.  RETURN ret_val
  653.  
  654.  
  655.  
  656.