/*
 * Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2023. All rights reserved.
 * Description: cust config file, common interface can not change
 * Author: Hisilicon
 * Create: 2019-12-12
 */
#include <string>
#include <unistd.h>
#include <log/log.h>
#include <securec.h>
#include "CustUtils.h"

// cfg file prefix of odm
const char* g_odmCfgFiles[] = {
    "/odm",
    "/",
};

// cfg file. default
const unsigned int CUST_TYPE_CONFIG = 0;

static const int ODM_CFG_FILES_COUNT =
    (sizeof(g_odmCfgFiles) / sizeof(g_odmCfgFiles[0]));

// get the highest priority cfg file
char *cust_get_one_cfg_file(const char *path_suffix, int, char buf[MAX_PATH_LEN])
{
    if ((buf == nullptr) || (path_suffix == nullptr)) {
        return nullptr;
    }
    *buf = '\0';
    for (int i = 0; i < ODM_CFG_FILES_COUNT; i++) {
        if (snprintf_s(buf, MAX_PATH_LEN, MAX_PATH_LEN - 1, "%s/%s",
            g_odmCfgFiles[i], path_suffix) == -1) {
            ALOGE("failed to snprintf_s buf\n");
            *buf = '\0';
            continue;
        }

        if (access(buf, F_OK) == 0) {
            break; // find it
        }
        *buf = '\0'; // not found, destroy the path
    }
    return (*buf != '\0') ? buf : nullptr;
}