这篇教程C++ vsnprintf函数代码示例写得很实用,希望能帮到您。
本文整理汇总了C++中vsnprintf函数的典型用法代码示例。如果您正苦于以下问题:C++ vsnprintf函数的具体用法?C++ vsnprintf怎么用?C++ vsnprintf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。 在下文中一共展示了vsnprintf函数的27个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。 示例1: safe_fprintfvoidsafe_fprintf(FILE *f, const char *fmt, ...){ char fmtbuff_stack[256]; /* Place to format the printf() string. */ char outbuff[256]; /* Buffer for outgoing characters. */ char *fmtbuff_heap; /* If fmtbuff_stack is too small, we use malloc */ char *fmtbuff; /* Pointer to fmtbuff_stack or fmtbuff_heap. */ int fmtbuff_length; int length, n; va_list ap; const char *p; unsigned i; wchar_t wc; char try_wc; /* Use a stack-allocated buffer if we can, for speed and safety. */ fmtbuff_heap = NULL; fmtbuff_length = sizeof(fmtbuff_stack); fmtbuff = fmtbuff_stack; /* Try formatting into the stack buffer. */ va_start(ap, fmt); length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap); va_end(ap); /* If the result was too large, allocate a buffer on the heap. */ while (length < 0 || length >= fmtbuff_length) { if (length >= fmtbuff_length) fmtbuff_length = length+1; else if (fmtbuff_length < 8192) fmtbuff_length *= 2; else if (fmtbuff_length < 1000000) fmtbuff_length += fmtbuff_length / 4; else { length = fmtbuff_length; fmtbuff_heap[length-1] = '/0'; break; } free(fmtbuff_heap); fmtbuff_heap = malloc(fmtbuff_length); /* Reformat the result into the heap buffer if we can. */ if (fmtbuff_heap != NULL) { fmtbuff = fmtbuff_heap; va_start(ap, fmt); length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap); va_end(ap); } else { /* Leave fmtbuff pointing to the truncated * string in fmtbuff_stack. */ length = sizeof(fmtbuff_stack) - 1; break; } } /* Note: mbrtowc() has a cleaner API, but mbtowc() seems a bit * more portable, so we use that here instead. */ if (mbtowc(NULL, NULL, 1) == -1) { /* Reset the shift state. */ /* mbtowc() should never fail in practice, but * handle the theoretical error anyway. */ free(fmtbuff_heap); return; } /* Write data, expanding unprintable characters. */ p = fmtbuff; i = 0; try_wc = 1; while (*p != '/0') { /* Convert to wide char, test if the wide * char is printable in the current locale. */ if (try_wc && (n = mbtowc(&wc, p, length)) != -1) { length -= n; if (iswprint(wc) && wc != L'//') { /* Printable, copy the bytes through. */ while (n-- > 0) outbuff[i++] = *p++; } else { /* Not printable, format the bytes. */ while (n-- > 0) i += (unsigned)bsdtar_expand_char( outbuff, i, *p++); } } else { /* After any conversion failure, don't bother * trying to convert the rest. */ i += (unsigned)bsdtar_expand_char(outbuff, i, *p++); try_wc = 0; } /* If our output buffer is full, dump it and keep going. */ if (i > (sizeof(outbuff) - 20)) { outbuff[i] = '/0'; fprintf(f, "%s", outbuff); i = 0; } } outbuff[i] = '/0'; fprintf(f, "%s", outbuff);//.........这里部分代码省略.........
开发者ID:Bluewind,项目名称:libarchive,代码行数:101,
示例2: vradlog/* * Log the message to the logfile. Include the severity and * a time stamp. */int vradlog(int lvl, const char *fmt, va_list ap){ struct main_config_t *myconfig = &mainconfig; unsigned char *p; char buffer[8192]; int len; /* * NOT debugging, and trying to log debug messages. * * Throw the message away. */ if (!debug_flag && (lvl == L_DBG)) { return 0; } /* * If we don't want any messages, then * throw them away. */ if (myconfig->radlog_dest == RADLOG_NULL) { return 0; } *buffer = '/0'; len = 0; /* * Don't print timestamps to syslog, it does that for us. * Don't print timestamps for low levels of debugging. * * Print timestamps for non-debugging, and for high levels * of debugging. */ if ((myconfig->radlog_dest != RADLOG_SYSLOG) && (debug_flag != 1) && (debug_flag != 2)) { const char *s; time_t timeval; timeval = time(NULL); CTIME_R(&timeval, buffer + len, sizeof(buffer) - len - 1); s = fr_int2str(levels, (lvl & ~L_CONS), ": "); strcat(buffer, s); len = strlen(buffer); } vsnprintf(buffer + len, sizeof(buffer) - len - 1, fmt, ap); /* * Filter out characters not in Latin-1. */ for (p = (unsigned char *)buffer; *p != '/0'; p++) { if (*p == '/r' || *p == '/n') *p = ' '; else if (*p == '/t') continue; else if (*p < 32 || (*p >= 128 && *p <= 160)) *p = '?'; } strcat(buffer, "/n"); switch (myconfig->radlog_dest) {#ifdef HAVE_SYSLOG_H case RADLOG_SYSLOG: switch(lvl & ~L_CONS) { case L_DBG: lvl = LOG_DEBUG; break; case L_AUTH: lvl = LOG_NOTICE; break; case L_PROXY: lvl = LOG_NOTICE; break; case L_ACCT: lvl = LOG_NOTICE; break; case L_INFO: lvl = LOG_INFO; break; case L_ERR: lvl = LOG_ERR; break; } syslog(lvl, "%s", buffer); break;#endif case RADLOG_FILES: case RADLOG_STDOUT: case RADLOG_STDERR: write(myconfig->radlog_fd, buffer, strlen(buffer)); break;//.........这里部分代码省略.........
开发者ID:amne,项目名称:freeradius-server,代码行数:101,
示例3: setproctitlevoidsetproctitle(const char *fmt, ...){ static struct ps_strings *ps_strings; static char *buf = NULL; static char *obuf = NULL; static char **oargv, *kbuf; static int oargc = -1; static char *nargv[2] = { NULL, NULL }; char **nargvp; int nargc; int i; va_list ap; size_t len; unsigned long ul_ps_strings; int oid[4]; if (buf == NULL) { buf = malloc(SPT_BUFSIZE); if (buf == NULL) return; nargv[0] = buf; } if (obuf == NULL ) { obuf = malloc(SPT_BUFSIZE); if (obuf == NULL) return; *obuf = '/0'; } va_start(ap, fmt); if (fmt) { buf[SPT_BUFSIZE - 1] = '/0'; if (fmt[0] == '-') { /* skip program name prefix */ fmt++; len = 0; } else { /* print program name heading for grep */ (void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname()); len = strlen(buf); } /* print the argument string */ (void) vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap); nargvp = nargv; nargc = 1; kbuf = buf; } else if (*obuf != '/0') { /* Idea from NetBSD - reset the title on fmt == NULL */ nargvp = oargv; nargc = oargc; kbuf = obuf; } else /* Nothing to restore */ return; va_end(ap); /* Set the title into the kernel cached command line */ oid[0] = CTL_KERN; oid[1] = KERN_PROC; oid[2] = KERN_PROC_ARGS; oid[3] = getpid(); sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1); if (ps_strings == NULL) { len = sizeof(ul_ps_strings); if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL, 0) == -1) ul_ps_strings = PS_STRINGS; ps_strings = (struct ps_strings *)ul_ps_strings; } /* PS_STRINGS points to zeroed memory on a style #2 kernel */ if (ps_strings->ps_argvstr) { /* style #3 */ if (oargc == -1) { /* Record our original args */ oargc = ps_strings->ps_nargvstr; oargv = ps_strings->ps_argvstr; for (i = len = 0; i < oargc; i++) { /* * The program may have scribbled into its * argv array, e.g., to remove some arguments. * If that has happened, break out before * trying to call strlen on a NULL pointer. */ if (oargv[i] == NULL) { oargc = i; break; } snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s", len ? " " : "", oargv[i]); if (len) len++;//.........这里部分代码省略.........
开发者ID:ele7enxxh,项目名称:dtrace-pf,代码行数:101,
示例4: MtraceintMtrace(char *fmt,...){ static int inMtraceNow; int len; char *eolp; va_list argp; /* Mtrace not configured or disabled, so just return. */ if (!Mip || Mip->off) return(0); /* This may be called from interrupt and/or non-interrupt space of * an application, so we must deal with possible reentrancy here. */ if (inMtraceNow) { Mip->reentered++; return(0); } inMtraceNow = 1; Mip->ptr += snprintf(Mip->ptr,MAXLINSIZE,"/n<%04d> ",Mip->sno++); va_start(argp,fmt); len = vsnprintf(Mip->ptr,MAXLINSIZE,fmt,argp); va_end(argp); /* Strip all CR/LFs from the incoming string. * The incoming string can have CR/LFs in it; however, they are stripped * so that the format of the dump is stable (one line per Mtrace call). * Notice that the top line of this function inserts a newline ahead * of the sequence number; hence, additional CR/LFs in the text would * just confuse the output. */ eolp = Mip->ptr; while(*eolp) { if ((*eolp == '/r') || (*eolp == '/n')) { strcpy(eolp,eolp+1); len--; } else eolp++; } /* If print flag is set, then dump to the console... */ if (Mip->mode & MODE_PRINT) { int i; for(i=0;i<len;i++) putchar(*Mip->ptr++); putchar('/n'); } else Mip->ptr += len; if (Mip->ptr >= Mip->end) { Mip->ptr = Mip->base; if (Mip->mode & MODE_NOWRAP) Mip->off = 1; else Mip->wrap++; } /* Flush the d-cache of the mtrace buffer and Mip structure after each * transfer... * This is important because if this is being accessed from an * application that has d-cache enabled, then the hardware is reset, * there is a chance that the data written was in cache and would be * lost. */ flushDcache((char *)Mip,sizeof(struct mtInfo)); flushDcache((char *)Mip->base,Mip->end - Mip->base); inMtraceNow = 0; return(len);}
开发者ID:Pedersen175,项目名称:DAN-uMon,代码行数:78,
|