UserInfo.c 4.4 KB
#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;
}