home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / WNRDBUF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  4.2 KB  |  136 lines

  1. /**
  2. *
  3. * Name        wnrdbuf -- Read series of adjacent characters
  4. *               from the current window
  5. *
  6. * Synopsis    num_read = wnrdbuf(row,col,num_spaces,buffer,option);
  7. *
  8. *        int  num_read    Number of character cells actually read.
  9. *        int  row,col    Row and column (relative to current
  10. *                window's data area) at which to begin
  11. *                reading.
  12. *        int  num_spaces Number of character cells to read.
  13. *        char *buffer    Space in which to put the data
  14. *        int  option    The option value tells WNRDBUF how the
  15. *                buffer is constructed and where to
  16. *                position the cursor after the buffer has
  17. *                been read.  Three different bits are
  18. *                relevant:
  19. *
  20. *              Value        Meaning
  21. *              -----------   ------------------------------------
  22. *              CUR_BEG        Leave cursor at (row,col).
  23. *              CUR_AFTER     Leave cursor after end of string.
  24. *
  25. *              CHARS_ONLY    Buffer contains characters only.
  26. *              CHAR_ATTR     Buffer contains (char,attr) pairs.
  27. *
  28. *              MOVE_CUR        Leave cursor according to
  29. *                      CUR_BEG/CUR_AFTER bit.
  30. *              NO_MOVE_CUR   Preserve cursor location.
  31. *
  32. * Description    This function reads adjacent characters from the current
  33. *        window with or without their corresponding video
  34. *        attributes.  It can leave the window's cursor at the
  35. *        beginning or end of the space read.
  36. *
  37. *        The num_spaces argument specifies the number of
  38. *        "character cells" to read, where "character cells" means
  39. *        the number of physical spaces on the screen.  (If
  40. *        CHAR_ATTR is specified, twice this number of bytes will
  41. *        be returned in the buffer.)  If num_spaces exceeds the
  42. *        number of characters to the end of the row, the read
  43. *        will continue on the following row, and so on to the end
  44. *        of the window.    The read will stop at the end of the
  45. *        window, hence the number of cells actually read may be
  46. *        less than num_spaces.  The actual number is returned as
  47. *        the value of the function.
  48. *
  49. *        An error occurs if no window is designated as current or
  50. *        if row or col exceeds the window's dimensions.
  51. *
  52. *        If the read includes the lower right corner of the
  53. *        window, then the cursor is left at the lower right
  54. *        corner if MOVE_CUR and CUR_AFTER are both specified.
  55. *
  56. *        Be aware that this function does NOT add a trailing NUL
  57. *        ('\0') at the end of the buffer, and that any 8-bit
  58. *        character may be present in the buffer, depending on
  59. *        what is in the window.
  60. *
  61. * Returns    num_read    Number of character cells read
  62. *                (0 if error)
  63. *        *buffer     Characters read from the window
  64. *                (perhaps with their attributes)
  65. *        b_pcurwin->cur_loc   Possibly altered.
  66. *        b_wnerr     Possible values:
  67. *                (No change)     Success.
  68. *                WN_BAD_WIN     b_pcurwin is invalid.
  69. *                WN_ILL_DIM     row or col out of range
  70. *                WN_NULL_PTR     Internal error.
  71. *
  72. * Version    3.0  (C)Copyright Blaise Computing Inc. 1986
  73. *
  74. **/
  75.  
  76. #include <bwindow.h>
  77.  
  78. int wnrdbuf(row,col,num_spaces,buffer,option)
  79. int row,col,num_spaces;
  80. char *buffer;
  81. int  option;
  82. {
  83.     int  h,w,want_attr,i,offset,last;
  84.     CELL *pdata;
  85.  
  86.     if (wnvalwin(b_pcurwin) == NIL)   /* Validate window structure    */
  87.     {
  88.     wnerror(WN_BAD_WIN);
  89.     return 0;
  90.     }
  91.  
  92.     h = b_pcurwin->img.dim.h;
  93.     w = b_pcurwin->img.dim.w;
  94.     if (   utrange(row,0,h - 1)       /* Validate row, col          */
  95.     || utrange(col,0,w - 1))
  96.     {
  97.     wnerror(WN_ILL_DIM);
  98.     return 0;
  99.     }
  100.  
  101.     if (b_pcurwin->img.pdata == NIL)  /* Be sure there's valid data   */
  102.     {
  103.     wnerror(WN_NULL_PTR);
  104.     return 0;
  105.     }
  106.  
  107.     offset = (row * w) + col;            /* Cell at which to start.*/
  108.     pdata = b_pcurwin->img.pdata + offset;  /* Pointer to first cell  */
  109.  
  110.     utuplim(num_spaces,((h * w) - offset))  /* Don't go beyond end of */
  111.                         /* window.              */
  112.  
  113.     want_attr = ((option & CHAR_ATTR) != 0);
  114.  
  115.     for (i = 0; i < num_spaces; i++)
  116.     {
  117.     *buffer++ = pdata[i].ch;      /* Copy from data in memory.    */
  118.     if (want_attr)
  119.         *buffer++ = pdata[i].attr;
  120.     }
  121.  
  122.     if (!(option & NO_MOVE_CUR))
  123.     {
  124.     if (option & CUR_AFTER)
  125.     {                  /* Leave cursor after last read */
  126.         last = offset + num_spaces;
  127.         utuplim(last,(h * w) - 1)      /* Don't go beyond end of window*/
  128.         wncurmov(last / w,last % w);
  129.     }
  130.     else
  131.         wncurmov(row,col);
  132.     }
  133.  
  134.     return num_spaces;
  135. }
  136.