Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
Diff: tcp/tcpbuf.c
- Revision:
- 61:aad055f1b0d1
- Parent:
- 54:84ef2b29cf7e
- Child:
- 79:f50e02fb5c94
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tcp/tcpbuf.c Thu Jan 11 17:38:21 2018 +0000
@@ -0,0 +1,90 @@
+#include <stdbool.h>
+#include <stdio.h>
+
+#include "http.h"
+
+static int currentPositionInMessage;
+static int bufferPositionInMessage;
+static int bufferLength;
+static char* pBuffer;
+static char* p;
+
+static bool currentPositionIsInBuffer()
+{
+ return currentPositionInMessage >= bufferPositionInMessage && currentPositionInMessage < bufferPositionInMessage + bufferLength;
+}
+
+void TcpBufStart(int position, int mss, char *pData)
+{
+ currentPositionInMessage = 0;
+ bufferPositionInMessage = position;
+ bufferLength = mss;
+ pBuffer = pData;
+ p = pData;
+}
+int TcpBufLength()
+{
+ return p - pBuffer;
+}
+
+void TcpBufAddChar(char c)
+{
+ if (currentPositionIsInBuffer()) *p++ = c;
+ currentPositionInMessage++;
+}
+void TcpBufFillChar(char c, int length)
+{
+ while (length > 0)
+ {
+ if (currentPositionIsInBuffer()) *p++ = c;
+ currentPositionInMessage++;
+ length--;
+ }
+}
+int TcpBufAddText(const char* text)
+{
+ const char* start = text;
+ while (*text)
+ {
+ if (currentPositionIsInBuffer()) *p++ = *text;
+ text++;
+ currentPositionInMessage++;
+ }
+ return text - start;
+}
+int TcpBufAddV(char *fmt, va_list argptr)
+{
+ int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required
+ char text[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null
+ vsprintf(text, fmt, argptr); //Fill the new buffer
+ return TcpBufAddText(text); //Add the text
+}
+int TcpBufAddF(char *fmt, ...)
+{
+ va_list argptr;
+ va_start(argptr, fmt);
+ int size = TcpBufAddV(fmt, argptr);
+ va_end(argptr);
+ return size;
+}
+void TcpBufAddData(const char* data, int length)
+{
+ while (length > 0)
+ {
+ if (currentPositionIsInBuffer()) *p++ = *data;
+ data++;
+ currentPositionInMessage++;
+ length--;
+ }
+}
+void TcpBufAddStream(void (*startFunction)(void), int (*enumerateFunction)(void))
+{
+ startFunction();
+ while (true)
+ {
+ int c = enumerateFunction();
+ if (c == EOF) break;
+ if (currentPositionIsInBuffer()) *p++ = c;
+ currentPositionInMessage++;
+ }
+}