Enhanced version of Simon Ford's RPC demo program allowing command-line editing and providing interactive help. Includes Stream (Serial, etc.) line editing routine in separate files.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers getline.cpp Source File

getline.cpp

00001 #include "mbed.h"
00002 // receive a line from a stream, allowing backspace editing,
00003 // and checking for buffer overflow. Terminates on either a \n or \r.
00004 size_t getline(Stream &s, char *buf, size_t bufsize)
00005 {
00006     char c;
00007     size_t receivedChars = 0;
00008     for(;;)
00009     {
00010         c = s.getc();
00011         if (c == '\r' || c == '\n')
00012             break;
00013         s.putc(c);
00014         if (c == '\b')
00015         {
00016             if (receivedChars > 0)
00017             {
00018                 buf--;
00019                 receivedChars--;
00020             }
00021         }
00022         else if (receivedChars < bufsize - 1)
00023         {
00024             *buf++ = c;
00025             receivedChars++;
00026         }
00027     }
00028     *buf++ = 0;
00029     s.putc('\n');
00030     s.putc('\r');
00031     return receivedChars;
00032 }