UserInfo.c
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "JZsdkLib.h"
#include "version_choose.h"
#include "BaseConfig.h"
#include "UserInfo.h"
#define USER_INFO_DIR "/root/JZUserInfo.txt"
static T_JZUserInfo g_UserInfo = {0};
T_JZsdkReturnCode JZsdk_UserInfo_Read(T_JZUserInfo *UserInfo)
{
//如果没有写入过使用者信息
if (g_UserInfo.HaveWrite == JZ_FLAGCODE_OFF)
{
//检查本地是否存在使用者信息
if (JZsdk_check_file_exists(USER_INFO_DIR) != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
{
JZSDK_LOG_ERROR("未存在使用者信息");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
//读取本地使用者信息
FILE* file = fopen(USER_INFO_DIR, "r");
if (file != NULL)
{
char line[1024];
while (fgets(line, sizeof(line), file))
{
// 去除行尾的换行符
line[strcspn(line, "\n")] = 0;
// 查找第一个等号的位置
char* separator = strchr(line, '=');
if (separator != NULL)
{
// 分割键和值
*separator = '\0'; // 在等号位置插入字符串结束符
char* key = line;
char* value = separator + 1; // 值从等号后开始
if (strcmp(key, "appName") == 0)
{
strncpy(g_UserInfo.appName, value, sizeof(g_UserInfo.appName) - 1);
g_UserInfo.appName[sizeof(g_UserInfo.appName) - 1] = '\0';
printf("Read\nappName:%s\n", g_UserInfo.appName);
}
else if (strcmp(key, "appId") == 0)
{
strncpy(g_UserInfo.appId, value, sizeof(g_UserInfo.appId) - 1);
g_UserInfo.appId[sizeof(g_UserInfo.appId) - 1] = '\0';
printf("Read\nappId:%s\n", g_UserInfo.appId);
}
else if (strcmp(key, "appKey") == 0)
{
strncpy(g_UserInfo.appKey, value, sizeof(g_UserInfo.appKey) - 1);
g_UserInfo.appKey[sizeof(g_UserInfo.appKey) - 1] = '\0';
printf("Read\nappKey:%s\n", g_UserInfo.appKey);
}
else if (strcmp(key, "appLicense") == 0)
{
strncpy(g_UserInfo.appLicense, value, sizeof(g_UserInfo.appLicense) - 1);
g_UserInfo.appLicense[sizeof(g_UserInfo.appLicense) - 1] = '\0';
printf("Read\nappLicense:%s\n", g_UserInfo.appLicense);
}
else if (strcmp(key, "developerAccount") == 0)
{
strncpy(g_UserInfo.developerAccount, value, sizeof(g_UserInfo.developerAccount) - 1);
g_UserInfo.developerAccount[sizeof(g_UserInfo.developerAccount) - 1] = '\0';
printf("Read\ndeveloperAccount:%s\n", g_UserInfo.developerAccount);
}
}
}
fclose(file);
sync();
JZSDK_LOG_DEBUG("\n从文件读取并填充APP信息完毕\n");
}
else
{
JZSDK_LOG_ERROR("未存在使用者信息");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
}
memcpy(UserInfo, &g_UserInfo, sizeof(T_JZUserInfo));
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_JZsdkReturnCode JZsdk_UserInfo_Write(T_JZUserInfo *UserInfo)
{
// 写入数据
FILE *file2 = fopen(USER_INFO_DIR, "w");
if (file2 != NULL)
{
// 按照相同的键值对格式写入
fprintf(file2, "appName=%s\n", UserInfo->appName);
fprintf(file2, "appId=%s\n", UserInfo->appId);
fprintf(file2, "appKey=%s\n", UserInfo->appKey);
fprintf(file2, "appLicense=%s\n", UserInfo->appLicense);
fprintf(file2, "developerAccount=%s\n", UserInfo->developerAccount);
fclose(file2);
sync();
JZSDK_LOG_INFO("使用者信息写入成功");
printf("name:%s\n", UserInfo->appName);
printf("id:%s\n", UserInfo->appId);
printf("key:%s\n", UserInfo->appKey);
printf("license:%s\n", UserInfo->appLicense);
printf("account:%s\n", UserInfo->developerAccount);
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
JZSDK_LOG_DEBUG("无法正常写入使用者信息");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}