/******************************************************************************* | |
* Author : author | |
* Version : V1.0 | |
* Date : 2021-07-27 | |
* Description : util_string.c | |
********************************************************************************/ | |
/********************************* Include File ********************************/ | |
#include <stdlib.h> | |
#include <string.h> | |
#include "util_string.h" | |
/********************************* Macro Definition ****************************/ | |
/********************************* Type Definition *****************************/ | |
/********************************* Variables ***********************************/ | |
/********************************* Function ************************************/ | |
void util_hex2str(uint8_t *in, uint16_t in_len, char *out) | |
{ | |
char *zEncode = "0123456789ABCDEF"; | |
int i = 0, j = 0; | |
for (i = 0; i < in_len; i++) { | |
out[j++] = zEncode[(in[i] >> 4) & 0xf]; | |
out[j++] = zEncode[(in[i]) & 0xf]; | |
} | |
} | |
char *util_strtok(char *str, const char *wstr) | |
{ | |
int only_delim = 1; | |
static char *pos = NULL; | |
static char *target = NULL; | |
pos = (str == NULL)?(pos):(str); | |
if (pos == NULL || wstr == NULL || | |
strlen(pos) <= strlen(wstr)) { | |
return NULL; | |
} | |
target = pos; | |
while (strlen(pos) >= strlen(wstr)) { | |
if (memcmp(pos,wstr,strlen(wstr)) != 0) { | |
only_delim = 0; | |
pos++; | |
continue; | |
} | |
if (strlen(pos) == strlen(wstr)) { | |
memset(pos,0,strlen(wstr)); | |
if (only_delim) { | |
return NULL; | |
} | |
return target; | |
} | |
if (target == pos) { | |
pos += strlen(wstr); | |
target = pos; | |
}else{ | |
memset(pos,0,strlen(wstr)); | |
pos += strlen(wstr); | |
break; | |
} | |
} | |
return target; | |
} | |