home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name wnrdbuf -- Read series of adjacent characters
- * from the current window
- *
- * Synopsis num_read = wnrdbuf(row,col,num_spaces,buffer,option);
- *
- * int num_read Number of character cells actually read.
- * int row,col Row and column (relative to current
- * window's data area) at which to begin
- * reading.
- * int num_spaces Number of character cells to read.
- * char *buffer Space in which to put the data
- * int option The option value tells WNRDBUF how the
- * buffer is constructed and where to
- * position the cursor after the buffer has
- * been read. Three different bits are
- * relevant:
- *
- * Value Meaning
- * ----------- ------------------------------------
- * CUR_BEG Leave cursor at (row,col).
- * CUR_AFTER Leave cursor after end of string.
- *
- * CHARS_ONLY Buffer contains characters only.
- * CHAR_ATTR Buffer contains (char,attr) pairs.
- *
- * MOVE_CUR Leave cursor according to
- * CUR_BEG/CUR_AFTER bit.
- * NO_MOVE_CUR Preserve cursor location.
- *
- * Description This function reads adjacent characters from the current
- * window with or without their corresponding video
- * attributes. It can leave the window's cursor at the
- * beginning or end of the space read.
- *
- * The num_spaces argument specifies the number of
- * "character cells" to read, where "character cells" means
- * the number of physical spaces on the screen. (If
- * CHAR_ATTR is specified, twice this number of bytes will
- * be returned in the buffer.) If num_spaces exceeds the
- * number of characters to the end of the row, the read
- * will continue on the following row, and so on to the end
- * of the window. The read will stop at the end of the
- * window, hence the number of cells actually read may be
- * less than num_spaces. The actual number is returned as
- * the value of the function.
- *
- * An error occurs if no window is designated as current or
- * if row or col exceeds the window's dimensions.
- *
- * If the read includes the lower right corner of the
- * window, then the cursor is left at the lower right
- * corner if MOVE_CUR and CUR_AFTER are both specified.
- *
- * Be aware that this function does NOT add a trailing NUL
- * ('\0') at the end of the buffer, and that any 8-bit
- * character may be present in the buffer, depending on
- * what is in the window.
- *
- * Returns num_read Number of character cells read
- * (0 if error)
- * *buffer Characters read from the window
- * (perhaps with their attributes)
- * b_pcurwin->cur_loc Possibly altered.
- * b_wnerr Possible values:
- * (No change) Success.
- * WN_BAD_WIN b_pcurwin is invalid.
- * WN_ILL_DIM row or col out of range
- * WN_NULL_PTR Internal error.
- *
- * Version 3.0 (C)Copyright Blaise Computing Inc. 1986
- *
- **/
-
- #include <bwindow.h>
-
- int wnrdbuf(row,col,num_spaces,buffer,option)
- int row,col,num_spaces;
- char *buffer;
- int option;
- {
- int h,w,want_attr,i,offset,last;
- CELL *pdata;
-
- if (wnvalwin(b_pcurwin) == NIL) /* Validate window structure */
- {
- wnerror(WN_BAD_WIN);
- return 0;
- }
-
- h = b_pcurwin->img.dim.h;
- w = b_pcurwin->img.dim.w;
- if ( utrange(row,0,h - 1) /* Validate row, col */
- || utrange(col,0,w - 1))
- {
- wnerror(WN_ILL_DIM);
- return 0;
- }
-
- if (b_pcurwin->img.pdata == NIL) /* Be sure there's valid data */
- {
- wnerror(WN_NULL_PTR);
- return 0;
- }
-
- offset = (row * w) + col; /* Cell at which to start.*/
- pdata = b_pcurwin->img.pdata + offset; /* Pointer to first cell */
-
- utuplim(num_spaces,((h * w) - offset)) /* Don't go beyond end of */
- /* window. */
-
- want_attr = ((option & CHAR_ATTR) != 0);
-
- for (i = 0; i < num_spaces; i++)
- {
- *buffer++ = pdata[i].ch; /* Copy from data in memory. */
- if (want_attr)
- *buffer++ = pdata[i].attr;
- }
-
- if (!(option & NO_MOVE_CUR))
- {
- if (option & CUR_AFTER)
- { /* Leave cursor after last read */
- last = offset + num_spaces;
- utuplim(last,(h * w) - 1) /* Don't go beyond end of window*/
- wncurmov(last / w,last % w);
- }
- else
- wncurmov(row,col);
- }
-
- return num_spaces;
- }