Testing string tokenizer

This commit is contained in:
John Jones 2017-03-21 12:58:39 -05:00
parent 618264c709
commit 25a2fa0c65
6 changed files with 58 additions and 6 deletions

View file

@ -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);

View file

@ -29,7 +29,8 @@ OBJS = main.o \
../unixfs/unixfs.o \ ../unixfs/unixfs.o \
../../c-protobuf/protobuf.o ../../c-protobuf/varint.o \ ../../c-protobuf/protobuf.o ../../c-protobuf/varint.o \
../util/errs.o \ ../util/errs.o \
../util/time.o ../util/time.o \
../util/string_tokenizer.o
%.o: %.c $(DEPS) %.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS) $(CC) -c -o $@ $< $(CFLAGS)

View file

@ -13,6 +13,7 @@
#include "ipfs/cid/cid.h" #include "ipfs/cid/cid.h"
#include "ipfs/merkledag/node.h" #include "ipfs/merkledag/node.h"
#include "ipfs/unixfs/unixfs.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) // 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; int num = 0;
char * tr; char * tr;
char * end; char * end;
tr=strtok_r(input,"/",&end); tr=ipfs_utils_strtok_r(input,"/",&end);
for(int i = 0;tr;i++) for(int i = 0;tr;i++)
{ {
tr=strtok_r(NULL,"/",&end); tr=ipfs_utils_strtok_r(NULL,"/",&end);
num++; num++;
} }
return num; return num;
@ -718,12 +719,12 @@ int Node_Resolve(char ** result, char * input1)
strcpy(input, input1); strcpy(input, input1);
char * tr; char * tr;
char * end; char * end;
tr=strtok_r(input,"/",&end); tr=ipfs_utils_strtok_r(input,"/",&end);
for(int i = 0;tr;i++) for(int i = 0;tr;i++)
{ {
result[i] = (char *) malloc(strlen(tr)+1); result[i] = (char *) malloc(strlen(tr)+1);
strcpy(result[i], tr); strcpy(result[i], tr);
tr=strtok_r(NULL,"/",&end); tr=ipfs_utils_strtok_r(NULL,"/",&end);
} }
return 1; return 1;
} }

View file

@ -28,6 +28,7 @@ OBJS = testit.o test_helper.o \
../routing/supernode.o \ ../routing/supernode.o \
../thirdparty/ipfsaddr/ipfs_addr.o \ ../thirdparty/ipfsaddr/ipfs_addr.o \
../unixfs/unixfs.o \ ../unixfs/unixfs.o \
../util/string_tokenizer.o \
../../c-protobuf/protobuf.o ../../c-protobuf/varint.o ../../c-protobuf/protobuf.o ../../c-protobuf/varint.o
%.o: %.c $(DEPS) %.o: %.c $(DEPS)

View file

@ -7,7 +7,7 @@ endif
LFLAGS = LFLAGS =
DEPS = DEPS =
OBJS = errs.o time.o OBJS = errs.o time.o string_tokenizer.o
%.o: %.c $(DEPS) %.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS) $(CC) -c -o $@ $< $(CFLAGS)

39
util/string_tokenizer.c Normal file
View file

@ -0,0 +1,39 @@
#include <stdlib.h>
#include <string.h>
/**
* 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;
}