From 0fe9ec1499ab9be92c97f5571b76f1fe4349cbc9 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Mon, 29 Aug 2016 13:10:52 +0200 Subject: [PATCH] C Clubby refactoring Complete rewrite of the C Clubby implementation: it is now modeled after the Go implementation, with a slight difference that codec and channel are the same object (something we should probably do in Go as well, as we only have a single type of channel so far, which is used with all types of codecs). This implementation also comes with a new external API, which is hopefully cleaner and easier to use (see mg_clubby.h). In this PR I am not adding any new types of channels, but a UART channel as well as websocket listener channel will be added after this lands. PUBLISHED_FROM=d545d4bb6434e2a02ad159f9e4b64e594a4797e7 --- mongoose.c | 25 +++++++++++++++++++++++++ mongoose.h | 3 +++ 2 files changed, 28 insertions(+) diff --git a/mongoose.c b/mongoose.c index 035b50bc..57d7e6d1 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1064,6 +1064,7 @@ void mbuf_remove(struct mbuf *mb, size_t n) { /* Amalgamated: #include "common/mg_str.h" */ +#include #include int mg_ncasecmp(const char *s1, const char *s2, size_t len); @@ -1096,6 +1097,30 @@ int mg_vcasecmp(const struct mg_str *str1, const char *str2) { } return r; } + +struct mg_str mg_strdup(const struct mg_str s) { + struct mg_str r = {NULL, 0}; + if (s.len > 0 && s.p != NULL) { + r.p = (char *) malloc(s.len); + if (r.p != NULL) { + memcpy((char *) r.p, s.p, s.len); + r.len = s.len; + } + } + return r; +} + +int mg_strcmp(const struct mg_str str1, const struct mg_str str2) { + size_t i = 0; + while (i < str1.len && i < str2.len) { + if (str1.p[i] < str2.p[i]) return -1; + if (str1.p[i] > str2.p[i]) return 1; + i++; + } + if (i < str1.len) return 1; + if (i < str2.len) return -1; + return 0; +} #ifdef MG_MODULE_LINES #line 1 "./src/../../common/sha1.c" #endif diff --git a/mongoose.h b/mongoose.h index 3a7a0935..db35824c 100644 --- a/mongoose.h +++ b/mongoose.h @@ -811,6 +811,9 @@ int mg_vcmp(const struct mg_str *str2, const char *str1); */ int mg_vcasecmp(const struct mg_str *str2, const char *str1); +struct mg_str mg_strdup(const struct mg_str s); +int mg_strcmp(const struct mg_str str1, const struct mg_str str2); + #ifdef __cplusplus } #endif /* __cplusplus */