240118
交易分割报文(指针操作)
/* 交易报文分割 */
uint8_t trade_content_segment(const char *plain, audio_txt_t *dst_content, uint16_t dst_num)
{
uint8_t step = 0;
uint8_t byte = 0;
uint16_t word = 0;
uint8_t cnt = 0;
uint16_t i = 0;
uint16_t len;
uint8_t flag=0;
LOGD("\n");
// 原文长度
len = strlen(plain);
LOGD("\n");
if (len <= 1)
{
return 0;
}
LOGD("plain====%s\n",plain);
for (i = 0; i < len - 1;)
{
byte = plain[i];
memcpy(&word, plain + i, 2);
// 检查到超过了内存则不再继续解析
if (cnt >= dst_num)
{
break;
}
switch (step)
{
case 0:
// 检查到前缀开始
if (!(byte >= '0' && byte <= '9' || byte == '.') && memcmp(&word,",",2)!=0)
{
dst_content[cnt].prefix = plain + i;
step++;
LOGD("dst_content[cnt].prefix=%s\n",dst_content[cnt].prefix);
LOGD("i = %d\n", i);
}
i += 2;
break;
case 1:
// 检查到金额开始
if (byte >= '0' && byte <= '9')
{
dst_content[cnt].prefix_len = plain + i - dst_content[cnt].prefix;
dst_content[cnt].value = plain + i;
LOGD("dst_content[cnt].prefix_len=%d\n",dst_content[cnt].prefix_len);
LOGD("dst_content[cnt].value=%s\n", dst_content[cnt].value);
LOGD("i = %d\n", i);
step++;
}
i += 1;
break;
case 2:
if (memcmp(&word,"元",2)==0 || memcmp(&word,",",2)==0)
{
dst_content[cnt].value_len = plain + i - dst_content[cnt].value;
dst_content[cnt].suffix = plain + i;
flag=1;
cnt++;
step = 0;
i += 2;
LOGD("i = %d\n", i);
}
else
{
i += 1;
}
break;
default:
break;
}
}
// 如果没有检测到金额部分
if (step == 1 && flag==0)
{
dst_content[cnt].prefix_len = plain + i - dst_content[cnt].prefix;
dst_content[cnt].value = NULL;
dst_content[cnt].value_len = 0;
dst_content[cnt].suffix = NULL;
dst_content[cnt].suffix_len = 0;
cnt++;
}
else if (step == 1)
{
LOGD("11111111111111111111111111\n");
dst_content[cnt].value_len = plain + i - dst_content[cnt].value;
dst_content[cnt].suffix = NULL;
dst_content[cnt].suffix_len = 0;
}
return cnt;
}
240426
static修饰的结构体,如何被其他文件引用
当在一个文件中使用static关键字定义的结构体时,该结构体默认具有文件作用域,只能在定义它的文件内访问。但是,如果你希望其他文件也能够访问这个结构体,可以考虑以下几种方法:
不使用static关键字: 最简单的方法是不使用static关键字来定义结构体,这样它就会具有全局作用域,可以被其他文件访问。例如:
复制// 在一个文件中定义结构体,不使用static
typedef struct {
int member1;
char member2;
} MyStruct;
使用头文件: 将结构体的定义放在头文件中,并在需要访问该结构体的文件中包含这个头文件。这样其他文件就可以通过包含同一个头文件来访问该结构体。例如:
在mystruct.h头文件中定义结构体:
复制// mystruct.h
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
typedef struct {
int member1;
char member2;
} MyStruct;
#endif // MYSTRUCT_H
在需要访问结构体的文件中包含头文件:
复制// otherfile.c
#include "mystruct.h"
// 在这里就可以使用MyStruct结构体了
使用外部链接符号: 可以使用extern关键字声明在其他文件中定义的static结构体,从而使得其他文件能够访问它。例如:
在定义结构体的文件中:
复制// structfile.c
static MyStruct myStructInstance;
MyStruct* getMyStructInstance() {
return &myStructInstance;
}
在其他文件中声明并使用该结构体:
复制// otherfile.c
#include "mystruct.h"
extern MyStruct* getMyStructInstance();
void someFunction() {
MyStruct* instance = getMyStructInstance();
// 使用instance指向的结构体
}
这些方法都可以让其他文件访问到原本被static关键字限制在文件作用域的结构体。