From 25a2fa0c65934a66aac39d0dd3a0de2e20149dd2 Mon Sep 17 00:00:00 2001 From: John Jones Date: Tue, 21 Mar 2017 12:58:39 -0500 Subject: [PATCH] Testing string tokenizer --- include/ipfs/util/string_tokenizer.h | 10 +++++++ main/Makefile | 3 ++- merkledag/node.c | 9 ++++--- test/Makefile | 1 + util/Makefile | 2 +- util/string_tokenizer.c | 39 ++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 include/ipfs/util/string_tokenizer.h create mode 100644 util/string_tokenizer.c diff --git a/include/ipfs/util/string_tokenizer.h b/include/ipfs/util/string_tokenizer.h new file mode 100644 index 0000000..95e490b --- /dev/null +++ b/include/ipfs/util/string_tokenizer.h @@ -0,0 +1,10 @@ +#pragma once + +/** + * A replacement strtok_r so we can compile with c99 + * @param str the original string + * @param delim the delimiters + * @param nextp used internally to save state + * @returns a pointer to the next element + */ +char* ipfs_utils_strtok_r(char *str, const char *delim, char **nextp); diff --git a/main/Makefile b/main/Makefile index 8bb9111..f3ee38a 100644 --- a/main/Makefile +++ b/main/Makefile @@ -29,7 +29,8 @@ OBJS = main.o \ ../unixfs/unixfs.o \ ../../c-protobuf/protobuf.o ../../c-protobuf/varint.o \ ../util/errs.o \ - ../util/time.o + ../util/time.o \ + ../util/string_tokenizer.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/merkledag/node.c b/merkledag/node.c index 5408010..6e7d1a2 100644 --- a/merkledag/node.c +++ b/merkledag/node.c @@ -13,6 +13,7 @@ #include "ipfs/cid/cid.h" #include "ipfs/merkledag/node.h" #include "ipfs/unixfs/unixfs.h" +#include "ipfs/util/string_tokenizer.h" // for protobuf Node (all fields optional) data (optional bytes) links (repeated node_link) @@ -691,10 +692,10 @@ int Node_Resolve_Max_Size(char * input1) int num = 0; char * tr; char * end; - tr=strtok_r(input,"/",&end); + tr=ipfs_utils_strtok_r(input,"/",&end); for(int i = 0;tr;i++) { - tr=strtok_r(NULL,"/",&end); + tr=ipfs_utils_strtok_r(NULL,"/",&end); num++; } return num; @@ -718,12 +719,12 @@ int Node_Resolve(char ** result, char * input1) strcpy(input, input1); char * tr; char * end; - tr=strtok_r(input,"/",&end); + tr=ipfs_utils_strtok_r(input,"/",&end); for(int i = 0;tr;i++) { result[i] = (char *) malloc(strlen(tr)+1); strcpy(result[i], tr); - tr=strtok_r(NULL,"/",&end); + tr=ipfs_utils_strtok_r(NULL,"/",&end); } return 1; } diff --git a/test/Makefile b/test/Makefile index 776464a..5118ba3 100644 --- a/test/Makefile +++ b/test/Makefile @@ -28,6 +28,7 @@ OBJS = testit.o test_helper.o \ ../routing/supernode.o \ ../thirdparty/ipfsaddr/ipfs_addr.o \ ../unixfs/unixfs.o \ + ../util/string_tokenizer.o \ ../../c-protobuf/protobuf.o ../../c-protobuf/varint.o %.o: %.c $(DEPS) diff --git a/util/Makefile b/util/Makefile index 559e417..a756ff9 100644 --- a/util/Makefile +++ b/util/Makefile @@ -7,7 +7,7 @@ endif LFLAGS = DEPS = -OBJS = errs.o time.o +OBJS = errs.o time.o string_tokenizer.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/util/string_tokenizer.c b/util/string_tokenizer.c new file mode 100644 index 0000000..b8a6d0e --- /dev/null +++ b/util/string_tokenizer.c @@ -0,0 +1,39 @@ +#include +#include + +/** + * A replacement strtok_r so we can compile with c99 + * @param str the original string + * @param delim the delimiters + * @param nextp used internally to save state + * @returns a pointer to the next element + */ +char* ipfs_utils_strtok_r(char *str, const char *delim, char **nextp) +{ + char *ret; + + if (str == NULL) + { + str = *nextp; + } + + str += strspn(str, delim); + + if (*str == '\0') + { + return NULL; + } + + ret = str; + + str += strcspn(str, delim); + + if (*str) + { + *str++ = '\0'; + } + + *nextp = str; + + return ret; +}