簡體   English   中英

逐步獲取vsnprintf()輸出

[英]Get vsnprintf() output part by part

我有一個大小為N的char數組,如果長度超過了char數組大小減去1(N-1個字節),我需要在不同部分獲取vsnprintf輸出。

我想實現類似printf的功能,但要通過UART輸出。 我不希望N大於100。如果有人要打印一個長於100的字符串,我要分部分進行。 我已經使用過vsnprintf,但是我不知道如何部分地獲取其輸出。 也許這不是stdio庫中使用的正確函數,我也對vsnprintf_s和_vscprintf進行了研究,但我仍然不知道如何實現我想要的功能。 我不想調用malloc,也不想使用VLA,因為我希望最大緩沖區長度為100,但同時又能夠部分輸出大於100字節的字符串。

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);
}

您可以使用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(或更少) %轉化。

下面是一個例子:

  • 還不完美
  • 也不完整
  • 通常你會合並parse_fmt()與for循環格式化在功能main() ,失去了`塊結構陣列。
  • 即使緩沖區是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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM