Handling Incoming Data
Because the
IOManager implements
IFile, writing text to the display is as simple as calling
IOMANAGER_Write:
nWritten = IOMANAGER_Write(pThis->m_pIIO, sz, STRLEN( sz ));
Under the hood, the method is implemented in
IOManager_Write, which must:
- Determine and create sufficient space in the m_szOutput string for the newly written data, removing old data if needed.
- Update the HTML stored in m_szHTML with the new data, converting line feeds to the HTML line break directive <br/>.
- Redraw the screen with the new content by reloading the HTML viewer with the freshly recreated HTML.
Listing 2 shows
IOManager_Write.
The bulk of Listing 2's code is the skullduggery necessary to handle trimming old content in the event that there's more data to put in the buffer than the buffer can holdwhen the if statement at the beginning of the function holds. If it holds, you search the buffer for the first line feed after the requisite number of characters to be written to the buffer, and use MEMMOVE to slide everything remaining in the buffer to the beginning of the buffer to make room for the newly-written content. The content itself is written to the buffer using STRCAT.
Rather than immediately reprocessing the HTML buffer and redrawing the display, defer these activities until the next pass through the event handler using Brew's asynchronous resume facility, so that in the event of many small writes to the IOManager you'll perform this costly operation only once. This is analogous to buffering file system writes and flushing only infrequently, because file system writes are more costly than buffer writes. Do this by scheduling a resumed invocation of the _HandleWrite method, which does the heavy lifting (Listing 3).
This is just more string-munging. Walk the m_szOutput buffer looking for instances of the linefeed character '\n.' When you encounter one, write out the corresponding HTML line break <br/> tag to the HTML buffer m_szHTML. For all other characters, simply copy the character straight across. Once this is done, erase the existing contents of the control on the display, and set the control's data to contain the newly-created HTML.