
#include <stdio.h>
#include <string.h>
// #include <stdbool.h>
#include "format_utils.h"

int format_float_to_string(uint8_t precision, float value, 
                          char* buffer, int buffer_size)
{
    if (buffer == NULL || buffer_size == 0) {
        return -1;
    }
    
    int len;
    
    switch (precision) {
        case 0:
            len = snprintf(buffer, buffer_size, "%d\r", (int)value);
            break;
        case 1:
            len = snprintf(buffer, buffer_size, "%.1f\r", value);
            break;
        case 2:
            len = snprintf(buffer, buffer_size, "%.2f\r", value);
            break;
        case 3:
            len = snprintf(buffer, buffer_size, "%.3f\r", value);
            break;
        default:
            // 默认使用2位小数
            len = snprintf(buffer, buffer_size, "%.2f\r", value);
            break;
    }
    
    // 检查是否截断
    if (len >= (int)buffer_size) {
        return -2; // 缓冲区不足
    }
    
    return len;
}



void formate_set_value_to_str(const TSETTABLE *fix_val , t_datafixval_cfg *fix_val_cfg , char* buf)
{
    // printf("%s des = %s val = %f\n", __FUNCTION__ , fix_val->szName , fix_val_cfg->f_val_set);
    WORD type = fix_val->wType;
    DWORD wDat;
    uint8_t uc_des_index;
    uint32_t ul_val;
    int32_t l_val;
    int64_t ll_val;
    const SET_LIST *pt_des_list = NULL;
    switch (type) {
        case SETTYPE_LIST: /* 列表选择 */
            pt_des_list = fix_val->list;
            uc_des_index = (uint8_t)fix_val_cfg->f_val_set;
            
            sprintf(buf , "%d:%s" , uc_des_index , pt_des_list[uc_des_index].str);
            break;

        case SETTYPE_F: /* 浮点数 */
            sprintf(buf, "%.02f", fix_val_cfg->f_val_set);

            break;

        case SETTYPE_UINT: /* 无符号整型 */
        case SETTYPE_4INT: /* 4字节整型 */
            ul_val = (uint32_t)fix_val_cfg->f_val_set;
            sprintf(buf, "%d", ul_val);
            break;

        case SETTYPE_5INT: /* 有符号整型 */
            l_val = (int32_t)fix_val_cfg->f_val_set;
            sprintf(buf, "%d", l_val);
            break;

        case SETTYPE_LINT: /* 长整型 */
            ll_val = (int64_t)fix_val_cfg->f_val_set;
            sprintf(buf, "%d", ll_val);
            break;

        case SETTYPE_IP: /* IP地址 */
            sprintf(buf, "%03d.%03d.%03d.%03d",
                    BYTE0(fix_val_cfg->ul_val_set),
                    BYTE1(fix_val_cfg->ul_val_set),
                    BYTE2(fix_val_cfg->ul_val_set),
                    BYTE3(fix_val_cfg->ul_val_set));
            wDat = (DWORD)fix_val_cfg->ul_val_set;
            sprintf(buf, "%d.%d.%d.%d", (u8)(wDat >> 24), (u8)(wDat >> 16), (u8)(wDat >> 8), (u8)(wDat >> 0));
            break;

        case SETTYPE_YMD: /* 年月日 */
            sprintf(buf, "%f", fix_val_cfg->f_val_set);
            break;

        case SETTYPE_HMS: /* 时分秒 */
            sprintf(buf, "%f", fix_val_cfg->f_val_set);
            break;

        case SETTYPE_KG:    /* 控制字 没有后续，直接显示即可 */
        case SETTYPE_LABEL: /* 标签类 实际没有用处 */
        default:
            break;
    }
}