1 Star 0 Fork 8

Anderson/vtor_elec_module

forked from vtor3478/vtor_elec_module 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vtor_string.c 6.46 KB
一键复制 编辑 原始数据 按行查看 历史
#include "vtor_string.h"
#ifdef __VTOR_STRING__
#ifdef __VTOR_STRING_CONFIG_PRINT_RESERVE__
uint16_t VtorString_Print(int8_t* str)
{
}
#endif // __VTOR_STRING_CONFIG_PRINT_RESERVE__
void VtorString_AppendChar(int8_t* str, char ch)
{
uint16_t len = VtorString_Length(str);
str[len++] = ch;
if(len > (128-2))
{
len=0;//接收数据即将溢出,重新开始接收
}
str[len] = '\0'; // 清空下一字符,方便计算字符串长度
if(len > 2) //已经可以实现长度校验时,进行校验
{
if(str[len - 2] == 0x0d && str[len - 1] == 0x0a)
{
str[len - 2] = '\0';
// 启动回调函数设置标志位,主程序中处理字符串
VtorString_AppendCharCpltCallback(str);
//VtorTimer_Start(VtorShell_Task, 0);
}
}
}
void VtorString_AppendFloat(int8_t* str, float num)
{
int d = 6;
// 整数部分按int方法
while (*str)
{
str++;
}
if (num < 0)
{
num = -num;
*str++ = '-';
}
VtorString_AppendInt(str, (int32_t)num);
float fractionNum = num - (int32_t)num; // 小数
while (*str)
{
str++;
}
*str++ = '.';
while (d--)
{
fractionNum *= 10;
*str++ = (int32_t)fractionNum % 10 + '0';
}
*str++ = '\0';
}
void VtorString_AppendInt(int8_t* str, int32_t num)
{
while (*str)
{
str++;
}
if (num < 0)
{
*str++ = '-';
num = -num;
}
if (0 == num)
{
*str++ = '0';
*str++ = '\0';
return;
}
int8_t* startStr = str;
while (num)
{
int singleNum = num % 10;
*str++ = singleNum + '0';
num /= 10;
}
*str = '\0';
str--;
while (startStr < str)
{
int8_t ch = *str;
*str = *startStr;
*startStr = ch;
startStr++;
str--;
}
}
void VtorString_AppendHex(int8_t* str, int32_t num)
{
while (*str)
{
str++;
}
*str++ = '0';
*str++ = 'x';
if (0 == num)
{
*str++ = '0';
*str++ = '\0';
return;
}
int8_t* startStr = str;
while (num)
{
int singleNum = num & 0x0f;
int8_t singleNumChar
= (singleNum < 10)
? (singleNum + '0')
: (singleNum + 'a' - 10);
*str++ = singleNumChar;
num >>= 4;
}
*str = '\0';
str--;
while (startStr < str)
{
int8_t ch = *str;
*str = *startStr;
*startStr = ch;
startStr++;
str--;
}
}
void VtorString_AppendMemory(int8_t* str, void* mem, int16_t cnt, int8_t width)
{
int8_t* byteMem = (int8_t*)mem;
#ifdef __VTOR_STRING_CONFIG_LITTLE_ENDIAN__
byteMem += width;
#endif
while (*str)
{
str++;
}
while(cnt--)
{
int i;
*str++ = '0';
*str++ = 'x';
for(i = 0; i < width; i++)
{
#ifdef __VTOR_STRING_CONFIG_LITTLE_ENDIAN__
uint8_t byteValue = *--byteMem;
#else
uint8_t byteValue = *byteMem++;
#endif
*str++ = (byteValue >> 4) < 10
? ((byteValue >> 4) + '0')
: ((byteValue >> 4) - 10 + 'a');
*str++ = (byteValue & 0x0f) < 10
? ((byteValue & 0x0f) + '0')
: ((byteValue & 0x0f) - 10 + 'a');
}
#ifdef __VTOR_STRING_CONFIG_LITTLE_ENDIAN__
byteMem += (width << 1);
#endif
*str++ = ' ';
}
*(str - 1) = '\0';
}
int8_t VtorString_GetDataType(int8_t* str)
{
int8_t dataType = DATA_TYPE_VOID;
while (*str)
{
if ('\'' == *str)
{
dataType = DATA_TYPE_CHAR;
break;
}
if ('"' == *str)
{
dataType = DATA_TYPE_STRING;
break;
}
if ('0' == *str
&&('x' == *(str + 1) || ('X' == *(str + 1))))
{
dataType = DATA_TYPE_INT;
break;
}
if ('0' <= *str && *str <= '9')
{
dataType = DATA_TYPE_INT;
}
if (dataType == DATA_TYPE_INT)
{
if ('.' == *str)
{
dataType = DATA_TYPE_FLOAT;
break;
}
// 整形数据结束了,认为是整形属性
if (*str < '0' || '9' < *str)
{
break;
}
}
str++;
}
return dataType;
}
void VtorString_AppendString(int8_t* str, const int8_t* str2)
{
while (*str)
{
str++;
}
while (*str2)
{
*str++ = *str2++;
}
*str++ = '\0';
}
int8_t VtorString_CmpString(const int8_t* str1, const int8_t* str2)
{
while (*str1 && *str2)
{
if (*str1 > *str2)
return 1;
else if (*str1 < *str2)
return -1;
str1++;
str2++;
}
return 0;
}
int8_t* VtorString_FindFloat(int8_t* str, float* num)
{
int32_t signBit = 1;
int32_t base = 10;
int32_t wholeNum = 0; // 整数
float fractionNum = 0.0f; // 小数
float fractionNumFactor = 1.0f;
// 找到第一个有效数字0~9,或者+-
while (*str)
{
if ('0' <= *str && *str <= '9')
{
break;
}
if (*str == '-')
{
signBit = -1;
str++;
break;
}
str++;
}
// 找出整数部分(十进制)
while (1)
{
int singleNum = 0;
if ('0' <= *str && *str <= '9')
{
singleNum = *str - '0';
}
else
{
break;
}
wholeNum *= base;
wholeNum += singleNum;
str++;
}
// 如果有小数点,挑出小数部分
if ('.' == *str)
{
str++;
while (1)
{
int singleNum = 0;
if ('0' <= *str && *str <= '9')
{
singleNum = *str - '0';
}
else
{
break;
}
fractionNumFactor *= base;
fractionNum *= base;
fractionNum += singleNum;
str++;
}
fractionNum /= fractionNumFactor;
}
*num = wholeNum + fractionNum;
*num *= signBit;
return str;
}
int8_t* VtorString_FindInt(int8_t* str, int32_t* num)
{
int32_t signBit = 1;
int32_t base = 10;
*num = 0;
// 找到第一个有效数字0~9,或者+-
while(*str)
{
if('0' <= *str && *str <= '9')
{
break;
}
if(*str == '-')
{
signBit = -1;
str++;
break;
}
str++;
}
if('0' == *str && ('x' == *(str+1) || 'X' == *(str+1)))
{
base = 16;
str += 2;
}
while(1)
{
int singleNum = 0;
if(10 == base)
{
if('0' <= *str && *str <= '9')
{
singleNum = *str - '0';
}
else
{
break;
}
}
if(16 == base)
{
if('0' <= *str && *str <= '9')
{
singleNum = *str - '0';
}
else if('a' <= *str && *str <= 'f')
{
singleNum = *str - 'a' + 10;
}
else if('A' <= *str && *str <= 'F')
{
singleNum = *str - 'A' + 10;
}
else
{
break;
}
}
*num *= base;
*num += singleNum;
str++;
}
*num *= signBit;
return str;
}
void VtorString_Clear(int8_t* str)
{
*str = '\0';
}
int32_t VtorString_Length(const int8_t* str)
{
const int8_t *ptr=str;
while(*str++!='\0');
return str-ptr-1;
}
// 预处理,截断注释
int8_t* VtorString_Preprocess(int8_t* str)
{
while(*str)
{
if('/' == *str
&& '/' == *(str+1))
{
*str = '\0';
break;
}
str++;
}
return str;
}
int8_t* VtorString_FindString(int8_t* str, int8_t* dstStr)
{
uint16_t pos;
while (*str)
{
if (*str != *dstStr)
{
str++;
continue;
}
for (pos = 0; *(dstStr + pos); pos++)
{
if (*(str + pos) != *(dstStr + pos))
{
break;
}
}
if ('\0' == *(dstStr + pos))
{
break;
}
str++;
}
return str;
}
#endif // __VTOR_STRING__
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Thomas_Ti/vtor_elec_module.git
git@gitee.com:Thomas_Ti/vtor_elec_module.git
Thomas_Ti
vtor_elec_module
vtor_elec_module
main

搜索帮助

0d507c66 1850385 C8b1a773 1850385