int32_t UART_vPrintf()

in IndustrialDeviceController/Software/MT3620_IDC_RTApp/lib/Print.c [248:344]


int32_t UART_vPrintf(UART *handle, const char *format, va_list args)
{
    if (!handle) {
        return ERROR_PARAMETER;
    }

    char     tempBuffer[PRINT_TEMP_PRINTF_BUFFER] = {'\0'};
    unsigned tempIndex = 0;
    int32_t  error = ERROR_NONE;

    /* Loop through string and look for format specifier */
    char       *start = NULL, *end = NULL;
    formatSpec  spec;
    char        c;
    while (*format != '\0') {
        if (tempIndex >= (PRINT_TEMP_PRINTF_BUFFER - 1)) {
            error = ERROR_UART_PRINTF_INVALID;
            break;
        }

        if (*format == '%') {
            start = (char*)(format + 1);
            if (*start == '%') {
                // handle %% pseudo-char
                tempBuffer[tempIndex++] = *format;
                format += 2;
                continue;
            }
            spec = parseFormatSpecifier(
                handle, start, &end);
            if (spec.error != ERROR_NONE) {
                error = spec.error;
                break;
            }
            format = end + 1;
            if (tempIndex > 0) {
                /* Write previously globbed tempBuffer */
                tempBuffer[tempIndex] = '\0';
                tempIndex             = 0;
                if ((error = UART_Print(
                    handle, (const char*)tempBuffer)) != ERROR_NONE) {
                    break;
                }
            }
            /* Write formatted arg */
            switch (spec.type) {
            case 'd':
            case 'i':
                error = UART__PrintIntBaseFiller(
                    handle, va_arg(args, int), 10,
                    spec.width, false, spec.filler);
                break;
            case 'u':
                error = UART__PrintUIntBaseFiller(
                    handle, va_arg(args, uint32_t), 10,
                    spec.width, false, spec.filler);
                break;
            case 'x':
                error = UART__PrintUIntBaseFiller(
                    handle, va_arg(args, uint32_t), 16,
                    spec.width, false, spec.filler);
                break;
            case 'o':
                error = UART__PrintUIntBaseFiller(
                    handle, va_arg(args, uint32_t), 8,
                    spec.width, false, spec.filler);
                break;
            case 'f':
                error = UART__PrintFloatFiller(
                    handle, (float)(va_arg(args, double)), spec.sigDigits,
                    spec.width, spec.filler);
                break;
            case 's':
                error = UART_Print(
                    handle, va_arg(args, const char*));
                break;
            case 'c':
                c = (char)va_arg(args, int);
                error = UART_Print(
                    handle, (const char*)(&c));
                break;
            }
        }
        else {
            tempBuffer[tempIndex++] = *format;
            format++;
        }
    }

    if (error == ERROR_NONE) {
        /* Write previously globbed tempBuffer */
        tempBuffer[tempIndex] = '\0';
        error = UART_Print(handle, (const char*)tempBuffer);
    }

    return error;
}