johann vallar
/
GPS
affichage d m s
Fork of GPS by
getline.cpp@1:f24af888e699, 2016-03-11 (annotated)
- Committer:
- pierrick
- Date:
- Fri Mar 11 10:18:09 2016 +0000
- Revision:
- 1:f24af888e699
code simple gps ers4 iut nice
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pierrick | 1:f24af888e699 | 1 | #include "mbed.h" |
pierrick | 1:f24af888e699 | 2 | // receive a line from a stream, allowing backspace editing, |
pierrick | 1:f24af888e699 | 3 | // and checking for buffer overflow. Terminates on either a \n or \r. |
pierrick | 1:f24af888e699 | 4 | size_t getline(Stream &s, char *buf, size_t bufsize) |
pierrick | 1:f24af888e699 | 5 | { |
pierrick | 1:f24af888e699 | 6 | char c; |
pierrick | 1:f24af888e699 | 7 | size_t receivedChars = 0; |
pierrick | 1:f24af888e699 | 8 | for(;;) |
pierrick | 1:f24af888e699 | 9 | { |
pierrick | 1:f24af888e699 | 10 | c = s.getc(); |
pierrick | 1:f24af888e699 | 11 | if (c == '\r' || c == '\n') |
pierrick | 1:f24af888e699 | 12 | break; |
pierrick | 1:f24af888e699 | 13 | s.putc(c); |
pierrick | 1:f24af888e699 | 14 | if (c == '\b') |
pierrick | 1:f24af888e699 | 15 | { |
pierrick | 1:f24af888e699 | 16 | if (receivedChars > 0) |
pierrick | 1:f24af888e699 | 17 | { |
pierrick | 1:f24af888e699 | 18 | buf--; |
pierrick | 1:f24af888e699 | 19 | receivedChars--; |
pierrick | 1:f24af888e699 | 20 | } |
pierrick | 1:f24af888e699 | 21 | } |
pierrick | 1:f24af888e699 | 22 | else if (receivedChars < bufsize - 1) |
pierrick | 1:f24af888e699 | 23 | { |
pierrick | 1:f24af888e699 | 24 | *buf++ = c; |
pierrick | 1:f24af888e699 | 25 | receivedChars++; |
pierrick | 1:f24af888e699 | 26 | } |
pierrick | 1:f24af888e699 | 27 | } |
pierrick | 1:f24af888e699 | 28 | *buf++ = 0; |
pierrick | 1:f24af888e699 | 29 | s.putc('\n'); |
pierrick | 1:f24af888e699 | 30 | s.putc('\r'); |
pierrick | 1:f24af888e699 | 31 | return receivedChars; |
pierrick | 1:f24af888e699 | 32 | } |