added string tokenizer for c99
This commit is contained in:
parent
e935335964
commit
d09c4a0d76
6 changed files with 56 additions and 3 deletions
2
Makefile
2
Makefile
|
@ -12,7 +12,7 @@ LFLAGS = -lm
|
||||||
DEPS = include/multiaddr/base58.h include/multiaddr/endian.h include/multiaddr/multiaddr.h \
|
DEPS = include/multiaddr/base58.h include/multiaddr/endian.h include/multiaddr/multiaddr.h \
|
||||||
include/multiaddr/protocols.h include/multiaddr/protoutils.h include/multiaddr/varhexutils.h \
|
include/multiaddr/protocols.h include/multiaddr/protoutils.h include/multiaddr/varhexutils.h \
|
||||||
include/multiaddr/varint.h
|
include/multiaddr/varint.h
|
||||||
OBJS = base58.o varint.o varhexutils.o protoutils.o protocols.o multiaddr.o
|
OBJS = base58.o varint.o varhexutils.o protoutils.o protocols.o multiaddr.o string_tokenizer.o
|
||||||
|
|
||||||
%.o: %.c $(DEPS)
|
%.o: %.c $(DEPS)
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
$(CC) -c -o $@ $< $(CFLAGS)
|
||||||
|
|
10
include/multiaddr/string_tokenizer.h
Normal file
10
include/multiaddr/string_tokenizer.h
Normal 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* ma_utils_strtok_r(char *str, const char *delim, char **nextp);
|
|
@ -1,6 +1,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
@ -8,6 +9,7 @@
|
||||||
#include "multiaddr/varhexutils.h"
|
#include "multiaddr/varhexutils.h"
|
||||||
#include "multiaddr/protocols.h"
|
#include "multiaddr/protocols.h"
|
||||||
#include "multiaddr/protoutils.h"
|
#include "multiaddr/protoutils.h"
|
||||||
|
#include "multiaddr/string_tokenizer.h"
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////
|
||||||
char ASCII2bits(char ch) {
|
char ASCII2bits(char ch) {
|
||||||
|
@ -681,7 +683,7 @@ int string_to_bytes(uint8_t** finalbytes, size_t* realbbsize, const char* strx,
|
||||||
//Starting to extract words and process them:
|
//Starting to extract words and process them:
|
||||||
char * wp;
|
char * wp;
|
||||||
char * end;
|
char * end;
|
||||||
wp=strtok_r(pstring,"/",&end);
|
wp=ma_utils_strtok_r(pstring,"/",&end);
|
||||||
struct Protocol * protx;
|
struct Protocol * protx;
|
||||||
while(wp)
|
while(wp)
|
||||||
{
|
{
|
||||||
|
@ -717,7 +719,7 @@ int string_to_bytes(uint8_t** finalbytes, size_t* realbbsize, const char* strx,
|
||||||
protx=NULL;//Since right now it doesn't need that assignment anymore.
|
protx=NULL;//Since right now it doesn't need that assignment anymore.
|
||||||
firstorsecond=1;//Since the next word will be an protocol
|
firstorsecond=1;//Since the next word will be an protocol
|
||||||
}
|
}
|
||||||
wp=strtok_r(NULL,"/",&end);
|
wp=ma_utils_strtok_r(NULL,"/",&end);
|
||||||
}
|
}
|
||||||
protx=NULL;
|
protx=NULL;
|
||||||
unload_protocols(head);
|
unload_protocols(head);
|
||||||
|
|
39
string_tokenizer.c
Normal file
39
string_tokenizer.c
Normal 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* ma_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;
|
||||||
|
}
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include "multiaddr/varint.h"
|
#include "multiaddr/varint.h"
|
||||||
|
|
Loading…
Reference in a new issue