逐步获取vsnprintf()输出

时间:2019-07-11 09:42:43

标签: c printf stdio

我有一个大小为N的char数组,如果长度超过了char数组大小减去1(N-1个字节),我需要以不同的方式获取vsnprintf输出。

我想实现类似printf的功能,但要通过UART输出。我不希望N大于100。如果有人要打印一个长于100的字符串,我要分部分进行。我已经使用过vsnprintf,但是我不知道如何部分地获取其输出。也许这不是stdio库中使用的正确函数,我也对vsnprintf_s和_vscprintf进行了研究,但我仍然不知道如何实现我想要的功能。 我不想调用malloc,也不想使用VLA,因为我希望最大缓冲区长度为100,但同时又能够部分输出大于100字节的字符串。 / p>

char char_array[100];
void uart_print(const char *fmt, ...) {
    va_list args;
    va_start(args, fmt);
    vsnprintf(char_array, sizeof(char_array), fmt, args); /* Don't get the result because it is not useful for me */
    uart_output(char_array);
}

实际结果是通过UART输出的字符串切成100个字节。我想要完整的字符串输出。

我想做这样的事情:

char char_array[100];
void uart_print(const char *fmt, ...) {
    va_list args;
    va_start(args, fmt);
    int ret;
    unsigned int start_index = 0;
    size_t max_s = sizeof(char_array);
    do {
        ret = vsnprintf(char_array, max_s, start_index, fmt, args); /* The new parameter is number 3, it would specify from which point from the generated string it starts to save data in char_array */
        uart_output(char_array);
        start_index += max_s;
    } while (max_s <= ret);
}

2 个答案:

答案 0 :(得分:0)

您可以使用fopencookie注入自己的输出函数以进行流输出。

   #define _GNU_SOURCE        
   #include <stdio.h>
   #include <stdarg.h>


   #define CHUNK_SZ 2
   void uart_output(const char *buf, int size )
   {
       fwrite( buf,1,size,stdout );
       putchar(10);
   }

   ssize_t cf_write(void *cookie, const char *buf, size_t size)
   {
       while( size > CHUNK_SZ ) {
         uart_output(buf, CHUNK_SZ );
         size -= CHUNK_SZ;
         buf += CHUNK_SZ;
       }
       if( size ) uart_output(buf, size );
       return size;
   }

   static cookie_io_functions_t cf = { write: cf_write };

   void uart_print(const char *fmt, ...)
   {
     va_list args;
     va_start(args, fmt);
     FILE *fp = fopencookie(NULL, "w", cf );
     vfprintf( fp, fmt, args );
     fclose(fp);
     va_end(args);
   }

 int main(int argc, char **argv)
 {
   uart_print( "hello world %s", "test" );
 }

答案 1 :(得分:0)

您可以将格式字符串(和格式)切成更小的段,每个段包含1个(或更少)%转换。

下面是一个示例:

  • 仍然不完美
  • 也不完整
  • 通常,您会将parse_fmt()函数与main()中的for循环格式合并,从而失去“块”结构数组。
  • 即使缓冲区为100个字符,也有fmtbuff[];并且xxxprintf()函数也将需要此大小的X *。

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>

        //dummie for testing
void uart_output(char *buff) { while(*buff) putc(*buff++,stdout); }

struct chunk    {
        unsigned start;
        unsigned end;   //next%
        char ll;
        char type;
        union   {
                char c;
                int i;
                long int li;
                long long int lli;
                unsigned u;
                unsigned long lu;
                unsigned long long llu;
                long double ld;
                double d;
                char *s;
                void *p;
                } u;
        } chunks[10];

        /* Chop the format string to pieces, with at most one '%' conversion per chunk.
        ** This is still not perfect,
        ** not all conversion types art present yet
        ** and backslash-escapes are not recognised.
        */
#define TINY_BUFFER_SIZE 100

int parse_fmt(struct chunk *arr, const char *fmt, ...) {
    unsigned idx,pos;
    int ch,state,ll;

    va_list args;
    va_start(args, fmt);

    arr[0].start=0;
    arr[0].type=0;
    state=0;
    ll=0;
    for(idx=pos=0; ch=fmt[pos]; pos++)  {
        // fprintf(stderr,"Idx=%d Pos=%u State=%d Char='%c'\n",idx,pos,state, ch);
        switch(state){
        case 0:
                if(pos-arr[idx].start>TINY_BUFFER_SIZE/2)goto next;
                if(ch =='%') {
                        if (!idx) goto next;
                        else {state++; continue; }
                        }
                continue;
        case 1:
                if(ch =='%'){state=0;continue;}
                if(pos-arr[idx].start>80)goto next;
                state++;
                // falltru
        case 2:
                switch(ch) {
                default: // ignore all modifiers except'l'
                case'h': continue;
                case'z': ll=2;continue;
                case'l': ll++;continue;
                case 'c': arr[idx].type= 'c';
                        arr[idx].u.c = va_arg(args,int);
                         break;
                case 'd': arr[idx].type= 'i';
                        if(ll ==2) arr[idx].u.lli = va_arg(args,long long int);
                        else if(ll ==1) arr[idx].u.li = va_arg(args,long int);
                        else arr[idx].u.i = va_arg(args,int);
                        break;
                case 'X':
                case 'x':
                case 'u': arr[idx].type= 'u';
                        if(ll ==2) arr[idx].u.llu = va_arg(args,long long unsigned int);
                        else if(ll ==1) arr[idx].u.lu = va_arg(args,long unsigned int);
                        else arr[idx].u.u = va_arg(args,unsigned int);
                         break;
                case 'g':
                case 'f': arr[idx].type= 'f';
                        if(ll) arr[idx].u.ld = va_arg(args,long double);
                        else arr[idx].u.d = va_arg(args,double);
                         break;
                case 's': arr[idx].type= 's';
                        arr[idx].u.s = va_arg(args,char*);
                         break;
                case 'p': arr[idx].type= 'p';
                        arr[idx].u.p = va_arg(args,void*);
                         break;
                }
                state++; continue;
        case 3:
                // falltru
        next:
                arr[idx].ll=ll;
                arr[idx].end=pos;
                if(idx++) state=0; else state=1;
                arr[idx].start=pos;
                arr[idx].type=0;
                ll=0;
                break;
                }
        }
if(state)  goto next;   // Haha!
if(idx) arr[idx-1].end=pos;
va_end(args);
return idx;
}

int main(void){
int idx,len,res;
char buff[TINY_BUFFER_SIZE];
char fmtcopy[TINY_BUFFER_SIZE];
char*fmt;

fmt = "Haha! Int=%03d Unsigned=%8u Hex=%08x Char=%c"
"... very long string here... very long string here... very long string here... very long string here... very long string here..."
" Float=%8.7f Double=%g %s OMG the end.\n";

len = parse_fmt(chunks, fmt
        , 666, (unsigned)42, (unsigned)0xaa55, '?' , 3.1415926535, sqrt(314.0),"done!"
        );

for(idx=0;idx<len;idx++)        {
        if(0) fprintf(stderr,"%d:{%u,%u}%d,%c"
        ,idx
        , chunks[idx].start
        , chunks[idx].end
        , chunks[idx].ll
        , chunks[idx].type
        );
        // select the part of the formatstring we are foing to handle
        res= chunks[idx].end-chunks[idx].start;
        memcpy(fmtcopy,fmt+chunks[idx].start, res);
        fmtcopy[res] = 0;
        if(0)fprintf(stderr,"\tfmtcopy='%s'", fmtcopy);

        switch( chunks[idx].type){
        case 0: // no percent sign present 
                res= snprintf(buff, sizeof buff, "%s", fmtcopy);
                break;
        case'p':
                res =snprintf(buff , sizeof buff, fmtcopy, chunks[idx].u.p);
                break;
        case's':
                res =snprintf(buff , sizeof buff, fmtcopy, chunks[idx].u.s);
                break;
        case'f':
                if(chunks[idx].ll>0) res =snprintf(buff, sizeof buff, fmtcopy, chunks[idx].u.ld);
                else res =snprintf(buff , sizeof buff, fmtcopy, chunks[idx].u.d);
                break;
        case'c':
                res =snprintf(buff , sizeof buff, fmtcopy, chunks[idx].u.i);
                break;
        case'i':
                if(chunks[idx].ll>1) res =snprintf(buff, sizeof buff, fmtcopy, chunks[idx].u.lli);
                else if(chunks[idx].ll>0) res =snprintf(buff, sizeof buff, fmtcopy, chunks[idx].u.li);
                else res =snprintf(buff , sizeof buff, fmtcopy, chunks[idx].u.i);
                break;
        case'x':
        case'u':
                if(chunks[idx].ll>1) res =snprintf(buff, sizeof buff, fmtcopy, chunks[idx].u.llu);
                else if(chunks[idx].ll>0) res =snprintf(buff, sizeof buff, fmtcopy, chunks[idx].u.lu);
                else res =snprintf(buff , sizeof buff, fmtcopy, chunks[idx].u.u);
                break;
                }
        if(0) fprintf(stderr,"\tRes =%d Buff='%s'\n",res ,buff);
        uart_output(buff);
        }
return 0;
}