Добавлены интерфейсы и драйверы
This commit is contained in:
commit
7a596739d8
|
|
@ -0,0 +1,33 @@
|
|||
//
|
||||
// Created by xemon on 10.09.22.
|
||||
//
|
||||
|
||||
#ifndef ASCIISTRINGPARSINGUTILS_ASCIISTRINGPARSINGUTILS_H
|
||||
#define ASCIISTRINGPARSINGUTILS_ASCIISTRINGPARSINGUTILS_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include "stdint.h"
|
||||
|
||||
int64_t iAsciiStringParseSignedLongDecimalNumber(char *str, const char *strEnd);
|
||||
|
||||
uint64_t iAsciiStringParseUnsignedLongDecimalNumber(char *str, const char *strEnd);
|
||||
|
||||
double dAsciiStringParseDouble(char *str, const char *strEnd);
|
||||
|
||||
char *xAsciiStringSkipChar(char *str, char const *strEnd, char symbol);
|
||||
|
||||
#define xAsciiStringSkipSpace(STR, STR_END) xAsciiStringSkipChar(STR,STR_END,' ')
|
||||
|
||||
char *xAsciiStringSeekChar(char *str, char const *strEnd, char symbol);
|
||||
|
||||
uint8_t iAsciiStringMoveToNextParsingBlock(char **blockBeginPos, char **blockDividerPos, char *strEnd, char divider);
|
||||
|
||||
uint8_t iAsciiStringParseHexByte(const char *str);
|
||||
|
||||
size_t iAsciiStringParseHexBytes(uint8_t *bytesData, char *str, size_t stringLen);
|
||||
|
||||
#define BIN_FROM_HEX(DATA,LEN,HEX_STRING) \
|
||||
uint8_t DATA[(sizeof(HEX_STRING)-1)/2]; \
|
||||
size_t LEN = iAsciiStringParseHexBytes(DATA, HEX_STRING, sizeof(HEX_STRING) - 1);
|
||||
|
||||
#endif //ASCIISTRINGPARSINGUTILS_ASCIISTRINGPARSINGUTILS_H
|
||||
|
|
@ -0,0 +1,123 @@
|
|||
//
|
||||
// Created by xemon on 10.09.22.
|
||||
//
|
||||
|
||||
#include "AsciiStringParsingUtils.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
uint8_t iAsciiStringParseCharToDecimalInt(char symbol) {
|
||||
return (symbol - '0'); // return overvalue if not a decimal number
|
||||
}
|
||||
|
||||
uint8_t iAsciiStringParseHexCharToInt(char symbol) {
|
||||
if (symbol >= '0' && symbol <= '9') {
|
||||
return iAsciiStringParseCharToDecimalInt(symbol);
|
||||
} else if (symbol >= 'a' && symbol <= 'f') {
|
||||
return 0xA + symbol - 'a';
|
||||
} else if (symbol >= 'A' && symbol <= 'F') {
|
||||
return 0xA + (symbol - 'A');
|
||||
} else {
|
||||
return 0x10; //not a hex number, return overvalue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint8_t iAsciiStringParseHexByte(const char *str) {
|
||||
return iAsciiStringParseHexCharToInt(*str) * 0x10 + iAsciiStringParseHexCharToInt(*(str + 1));
|
||||
}
|
||||
|
||||
size_t iAsciiStringParseHexBytes(uint8_t *bytesData, char *str, size_t stringLen) {
|
||||
char *end = str + stringLen;
|
||||
|
||||
size_t dataLen = stringLen / 2;
|
||||
|
||||
while (end > str) {
|
||||
|
||||
*bytesData = iAsciiStringParseHexByte(str);
|
||||
|
||||
str += 2;
|
||||
++bytesData;
|
||||
}
|
||||
|
||||
return dataLen;
|
||||
}
|
||||
|
||||
uint64_t iAsciiStringParseUIntDecimalNumberWhitEnd(char **str, const char *strEnd) {
|
||||
|
||||
|
||||
uint32_t value = 0;
|
||||
uint8_t digit;
|
||||
|
||||
while ((*str) < strEnd) {
|
||||
digit = iAsciiStringParseCharToDecimalInt(**str);
|
||||
if (digit > 9) {
|
||||
return value;
|
||||
} else {
|
||||
value = (value * 10) + digit;
|
||||
}
|
||||
++(*str);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
int64_t iAsciiStringParseSignedLongDecimalNumber(char *str, const char *strEnd) {
|
||||
int8_t sign = 1;
|
||||
if (*str == '-') {
|
||||
sign = -1;
|
||||
++str;
|
||||
}
|
||||
|
||||
return iAsciiStringParseUIntDecimalNumberWhitEnd(&str, strEnd) * sign;
|
||||
}
|
||||
|
||||
uint64_t iAsciiStringParseUnsignedLongDecimalNumber(char *str, const char *strEnd) {
|
||||
return iAsciiStringParseUIntDecimalNumberWhitEnd(&str, strEnd);
|
||||
}
|
||||
|
||||
|
||||
char *xAsciiStringSkipChar(char *utcString, char const *utcStringEnd, char symbol) {
|
||||
while ((utcString < utcStringEnd) && (*utcString == symbol)) {
|
||||
++utcString;
|
||||
}
|
||||
return utcString;
|
||||
}
|
||||
|
||||
char *xAsciiStringSeekChar(char *utcString, char const *utcStringEnd, char symbol) {
|
||||
while ((utcString < utcStringEnd) && (*utcString != symbol)) {
|
||||
++utcString;
|
||||
}
|
||||
return utcString;
|
||||
}
|
||||
|
||||
double dAsciiStringParseDouble(char *str, const char *strEnd) {
|
||||
double sign = 1;
|
||||
if (str[0] == '-') {
|
||||
sign = -1;
|
||||
++str;
|
||||
}
|
||||
char *dividerSymbol = xAsciiStringSeekChar(str, strEnd, '.');
|
||||
|
||||
double result = iAsciiStringParseUnsignedLongDecimalNumber(str, dividerSymbol);
|
||||
|
||||
if (dividerSymbol < (strEnd - 1)) {
|
||||
++dividerSymbol;
|
||||
char *fractionalStr = dividerSymbol;
|
||||
double fractional = iAsciiStringParseUIntDecimalNumberWhitEnd(&fractionalStr, strEnd);
|
||||
|
||||
for (; dividerSymbol < fractionalStr; ++dividerSymbol) {
|
||||
fractional /= 10;
|
||||
}
|
||||
result += fractional;
|
||||
}
|
||||
|
||||
|
||||
return result * sign;
|
||||
}
|
||||
|
||||
uint8_t iAsciiStringMoveToNextParsingBlock(char **blockBeginPos, char **blockDividerPos, char *strEnd, char divider) {
|
||||
*blockBeginPos = xAsciiStringSkipSpace((*blockDividerPos + 1), strEnd);
|
||||
*blockDividerPos = xAsciiStringSeekChar(*blockBeginPos, strEnd, divider);
|
||||
return *blockDividerPos - *blockBeginPos; //return block length
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
cmake_minimum_required(VERSION 3.17)
|
||||
PROJECT(test)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include(modular.cmake)
|
||||
|
||||
add_executable(test ${SOURCES})
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
//
|
||||
// Created by xemon on 10.09.22.
|
||||
//
|
||||
#include "AsciiStringParsingUtils.h"
|
||||
#include "printf.h"
|
||||
|
||||
int main() {
|
||||
char srt[] = "22341.233adfa";
|
||||
|
||||
|
||||
int32_t si = iAsciiStringParseSignedLongDecimalNumber(srt, srt + 10);
|
||||
double dbl = dAsciiStringParseDouble(srt, srt + 10);
|
||||
|
||||
printf("short unsigned %i %f\n", si, dbl);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"dep": [
|
||||
{
|
||||
"type": "local",
|
||||
"dir": "../"
|
||||
}
|
||||
],
|
||||
"cmake": {
|
||||
"inc_dirs": [
|
||||
"Inc"
|
||||
],
|
||||
"srcs": [
|
||||
"Src/**.c"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"dep": [
|
||||
],
|
||||
"cmake": {
|
||||
"inc_dirs": [
|
||||
"Inc"
|
||||
],
|
||||
"srcs": [
|
||||
"Src/**.c"
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue