Files
libucore/include/ucore/string.h
T
2016-03-09 00:10:23 +01:00

130 lines
4.6 KiB
C

#ifndef UC_STRING_H_
#define UC_STRING_H_
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/** Encode to ascii-hex, upper case letters.
*
* Subtract the return value from the passed in @out to find
* the length of the converted data
*
* @param in data to encode
* @param len length of data
* @param out where to store result. Must hold at least len * 2 + 1
* @return returns one past the end of the converted data
*/
char* uc_hex_encode(const uint8_t *restrict in, size_t len, char *restrict out);
/** Decode hex. Will stop at the first non-convertible hex-digit
* spaces(' ','\t,'\r','\n') in input are skipped
* Subtract the return value from the passed in @out to find
* the length of the converted data
*
* @param in data to encode
* @param maxlen max length of @in to convert,
* conversion stops when either maxlen is reached,
* nul terminator of in is found or a non hex digit is found.
* @param out where to store result. Must hold at least maxlen/2 + 1
* @return returns one past the end of the converted data
*/
uint8_t* uc_hex_decode(const char *restrict in, size_t maxlen,uint8_t *restrict out);
/** As uc_hex_encode , but inserts the @delim string between each converted
* byte (each 2 hex chars)
*/
char* uc_hex_encode_delim(const uint8_t *restrict in, size_t inlen,
char *restrict out, int outlen,
const char *restrict delim);
/**
* Convert the string to lowercase, calls tolower() on each char.
* @param s string to lowercase
*/
void uc_str_tolower(char *s);
/**
* Convert the string to uppercase, calls toupper() on each char.
* @param s string to uppercase
*/
void uc_str_toupper(char *s);
/** Macro to calculate the number of bytes required to BCD encode
* the number of ascii digits
*/
#define UC_BCD_LEN(ascii_len) ((ascii_len) / 2 + ((ascii_len) % 2))
/** Macro to calculate the number of bytes required to ASCII encode
* the number of BCD digits
*/
#define UC_ASCII_LEN(bcd_len) ((bcd_len) * 2)
/** Convert the ascii digits to BCD. Digits not in 0..9 are skipped.
*
* @param ascii string to BCD encode.
* @param bcdout resulting BCD data. Must be at least UC_BCD_LEN(strlen(asciii)) big
* @param filler filler byte(not the ascii digit) to write in the last BCD nibble
* if the number of ASCII digits is odd. This would usually be 0x0 or 0xf.
* @return the number of bytes written to bcdout
*/
size_t uc_ascii2bcd(const char *restrict ascii,
unsigned char *restrict bcdout,
unsigned char filler);
/** Convert the BCD digits to ascii.
* The resulting ascii string is 0 terminated.
*
* @param bcd Buffer with BCD data to convert to ascii
* @param bcdlen number of bytes in @bcd to convert
* @param asciiout buffer to place ASCII in, must be UC_ASCII_LEN(bcdlen)+1 big
* @return the number of characters written to bcdout (this will always be UC_ASCII_LEN(bcdlen)
*/
size_t uc_bcd2ascii(const unsigned char *restrict bcd,
size_t bcdlen,
char *restrict asciiout);
/** uc_sprintb for converting the values into ascii bit representation
* e.g. 0x3 -> "00000011". The result must be able to hold the max number
* of bits of the type + 1. Returns 'result'
*/
char* uc_sprintb(char *result, unsigned int value);
char* uc_sprintbc(char *result, unsigned char value);
char* uc_sprintbll(char *result, unsigned long long value);
char* uc_sprintbs(char *result, unsigned short value);
/** Like http://swtch.com/plan9port/man/man3/getfields.html
*/
int uc_getfields(char *str, char **args, int max, int mflag, const char *sep);
int uc_gettokens(char *str, char **args, int max, const char *sep);
//Converts bytes to a human representable test, (i.e. 12kB, 160 MB 1.78 GB etc.)
///result should be at least of size 12,
///returns the result argument
char * uc_human_bytesz_dec(uint64_t bytes, char *result, size_t result_len);
//same as uc_human_bytesz_bin, but Conversion is done in binary
//(base 2 ,1 KiB == 1024 bytes)
char * uc_human_bytesz_bin(uint64_t bytes, char *result, size_t result_len);
/** Copy a C string ensuring dst is nul terminated.
* If @max is 0, no action is done
* src must be a nul terminated string
*
* @param dst destination buffer
* @param src source string
* @maram max max chars (including nul terminator to copy). This should
* is normally the size of the dst buffer)
* return strlen(src) which may be larger than the no. of bytes copied.
*/
size_t uc_strlcpy(char *restrict dst, const char *restrict src, size_t max);
#ifdef __cplusplus
}
#endif
#endif