the equivalent of _vscprintf && _vscwprintf under Linux
来源:互联网 发布:淘宝店手机专享价 编辑:程序博客网 时间:2024/06/11 09:50
Reference Link:
http://msdn.microsoft.com/en-us/library/28d5ce15(v=vs.71).aspx
http://stackoverflow.com/questions/16351523/vscwprintf-on-mac-os-x-linux
Microsoft describes the functions asreturning the number of characters that would be used if the string wereformatted — note that they are documented as not including the null terminator.
int _vscprintf(
const char *format,
va_list argptr
);
int _vscwprintf(
const wchar_t *format,
va_list argptr
);
These functions can, therefore, beemulated with vsprintf() and vswprintf():
int _vscprintf(const char *format,va_list argptr)
{
return(vsnprintf(0, 0, format, argptr));
}
int _vscwprintf(const wchar_t *format,va_list argptr)
{
return(vswprintf(0, 0, format, argptr));
}
It is up to you whether you remove theleading underscore; I would.
vscprintf() and scprintf()
Apologies: I wrote vsprintf() where Ineeded to write vsnprintf() (now fixed in the code above); however, vswprintf()already has the safer interface with the buffer length, so there is novsnwprintf(). There's a reason I prefer to test compile code before (or shortlyafter) posting it — it's been irksome not having the wherewithal to do so for acouple of days.
Here's an SSCCE for vscprintf() (andscprintf()):
#include <stdio.h>
#include <stdarg.h>
extern int vscprintf(const char*format, va_list argptr);
extern int scprintf(const char*format, ...);
int vscprintf(const char *format,va_list argptr)
{
return(vsnprintf(0, 0, format, argptr));
}
int scprintf(const char *format, ...)
{
va_list args;
va_start(args, format);
int rc = vscprintf(format, args);
va_end(args);
return rc;
}
int main(void)
{
int l = scprintf("%-8s %8d\n", "abc", 123);
if (l > 0)
{
char buffer[l+1];
int n = snprintf(buffer, sizeof(buffer), "%-8s %8d\n","abc", 123);
printf("%d = %d: %s", l, n, buffer);
}
return 0;
}
Output:
18 = 18: abc 123
vscwprintf() and scwprintf()
It turns out to be harder to simulate_vscwprintf() because the vswprintf() function is not as helpful as thevsnprintf() function. Specifically, vswprintf() reports an error if theformatted string won't fit in the formatted space, whereas vsnprintf() reportsthe number of characters that would have been needed in the buffer if it wasgoing to fit. Hence, you have to work by trial and error
// the buffer - it just reports anerror when there is not enough
// space. Assume a moderatelylarge machine so kilobytes of wchar_t
// on the stack is not a problem.
int buf_size = 1024;
while (buf_size < 1024 * 1024)
{
va_list args;
va_copy(args, argptr);
wchar_t buffer[buf_size];
int fmt_size = vswprintf(buffer, sizeof(buffer)/sizeof(buffer[0]),format, args);
if (fmt_size >= 0)
return fmt_size;
buf_size *= 2;
}
return -1;
}
int scwprintf(const wchar_t *format,...)
{
va_list args;
va_start(args, format);
int rc = vscwprintf(format, args);
va_end(args);
return rc;
}
int main(void)
{
int l = scwprintf(L"%-8ls %8d\n", L"abc", 123);
if (l > 0)
{
wchar_t buffer[l+1];
int n = swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), L"%-8ls%8d\n", L"abc", 123);
wprintf(L"%d = %d: %ls", l, n, buffer);
}
return 0;
}
When run, this produces the output
18 = 18: abc 123
(the same as before).
Tested on Mac OS X 10.8.3 using GCC4.7.3 (which was built on Mac OS X 10.7.5, but that shouldn't cause anyproblems).
- the equivalent of _vscprintf && _vscwprintf under Linux
- Get the equivalent of GetModuleFileName on Linux
- The usage of grep under LINUX systems.
- Configuring the ECC of Nvidia GPUs under Linux
- The Clojure (or Lisp) Equivalent of a Compound Boolean Test
- CIL Programming: Under the Hood of .NET
- Under the Hood of J2EE Clustering
- Under the Hood of J2EE Clustering
- Under the Hood of J2EE Clustering
- Under the Hood of J2EE Clustering
- Scala: Under The Hood of Hello World
- BASH Shell change the color of my shell prompt under Linux or UNIX
- display the contents of a PEM formatted certificate under Linux, using openssl:
- The role of the server under Ajax (1)
- The role of the server under Ajax (2)
- debugging the linux kernel under ubuntu 11.04
- Set privilege of folder under Apache of Linux
- The order of GCC's options DOES matter under windows.
- 黑马程序员_IO流
- ZOJ3432字符串异或
- ARM的开始
- Android 4.0新增Space及GridLayout初谈
- GUI系统之SurfaceFlinger(6)BufferQueue中的缓冲区分配
- the equivalent of _vscprintf && _vscwprintf under Linux
- cmd下提示“不是内部或外部命令,也不是可运行的程序或批处理文件
- 黑马程序员--注解
- gcc
- 在Flash Builder 4.7的纯AS项目中使用Flex SDK
- RTP协议分析(转载)
- Javascript鼠标事件大全
- 虚拟服务器的路由端口映射
- Windows 2008安装Oracle10g提示操作系统版本检查未通过