1 /*------------------------------------------------------------------------------
2 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3 *
4 * Distributable under the terms of either the Apache License (Version 2.0) or
5 * the GNU Lesser General Public License, as specified in the COPYING file.
6 ------------------------------------------------------------------------------*/
7 #include "CLucene/StdHeader.h"
8 #include "CLucene/util/StringBuffer.h"
9
10 #ifdef __CL_INCLUDE_TPRINTF
11
CL_NS_USE(util)12 CL_NS_USE(util)
13
14 //print a variable argument to a stream
15 //currently special number formatting is not supported. it is very minimalistic
16 void lucene_vfnwprintf(StringBuffer* buffer, size_t count, const wchar_t * format, va_list& valist){
17 const wchar_t *iter = format;
18 StringBuffer* tmp = NULL;
19 if ( buffer == NULL )
20 tmp = _CLNEW StringBuffer;
21 else
22 tmp = buffer;
23
24 while (*iter)
25 {
26 while (*iter && *iter != '%')
27 {
28 tmp->appendChar(*iter++);
29 }
30 if (*iter == '%')
31 {
32 if (iter[1] == '%')
33 {
34 //just print a %
35 tmp->appendChar('%');
36 iter += 2;
37 continue;
38 }
39
40 iter++;
41 switch (*iter)
42 {
43 case 's':
44 {
45 //todo: this is faulty. it doesn't heed count
46
47 //print a string or null
48 TCHAR *wstr = va_arg(valist, TCHAR *);
49 if ( !wstr )
50 wstr = _T("(null)");
51
52 tmp->append(wstr);
53 iter++;
54 break;
55 }
56
57 case 'c':
58 tmp->appendChar((TCHAR)va_arg(valist, int));
59 iter++;
60 break;
61
62 default:
63 {
64 //todo: this is faulty. it doesn't heed count
65
66 if (*iter == 'p')
67 tmp->appendInt((int32_t)va_arg(valist, long));
68 else
69 {
70 if (*iter == 'a' || *iter == 'A' ||
71 *iter == 'e' || *iter == 'E' ||
72 *iter == 'f' || *iter == 'F' ||
73 *iter == 'g' || *iter == 'G')
74 tmp->appendFloat((qreal)va_arg(valist, double),8);
75 else if (*iter == 'd' || *iter == 'i' ){
76 tmp->appendInt((int32_t)va_arg(valist, int));
77 }else if (*iter == 'l' ){
78 TCHAR b[100];
79 _i64tot((int64_t)va_arg(valist, int64_t),b,10);
80 tmp->append(b);
81 }/*else{
82 TCHAR b[100];
83 _i64tot((int64_t)va_arg(valist, void*),b,10);
84 tmp->append(b);
85 }*/
86 }
87 iter++;
88 break;
89 }
90 }
91 }
92 }
93
94
95 if ( buffer == NULL ){
96 //we are supposed to be writing to the console
97 #ifdef _UCS2
98 TCHAR* pointer = tmp->getBuffer();
99 char ob[MB_LEN_MAX];
100 size_t v;
101 size_t len = tmp->length();
102 for (size_t i=0;i<len;i++){
103 v = wctomb(ob,*pointer);
104 if ( v > 0 ){
105 ob[v]='\0';
106 fputs(ob,stdout);
107 }
108 pointer++;
109 }
110
111
112 #else
113 fputs(tmp->getBuffer(),stdout);
114 #endif
115 _CLDELETE(tmp);
116 }
117 }
118
119 //print a list of arguments to a string
lucene_snwprintf(wchar_t * strbuf,size_t count,const wchar_t * format,...)120 int lucene_snwprintf(wchar_t* strbuf, size_t count, const wchar_t * format, ...){
121 va_list ap;
122 va_start(ap, format);
123 StringBuffer buffer;
124 lucene_vfnwprintf(&buffer,count,format,ap);
125 va_end(ap);
126
127 size_t ret = min(count,(size_t)(buffer.length()+1));
128 _tcsncpy(strbuf,buffer.getBuffer(),ret);
129 return ret;
130 }
131
132 //print a list of arguments to the stdout
lucene_wprintf(const wchar_t * format,...)133 void lucene_wprintf(const wchar_t * format, ...){
134 va_list ap;
135 va_start(ap, format);
136 lucene_vfnwprintf(NULL,LUCENE_INT32_MAX_SHOULDBE,format,ap);
137 va_end(ap);
138 }
139
140 //print a variable argument to a string
lucene_vsnwprintf(wchar_t * strbuf,size_t count,const wchar_t * format,va_list & ap)141 int lucene_vsnwprintf(wchar_t * strbuf, size_t count, const wchar_t * format, va_list& ap){
142 StringBuffer buffer;
143 lucene_vfnwprintf(&buffer,count,format,ap);
144 int ret = min((int32_t)count,buffer.length()+1);
145 _tcsncpy(strbuf,buffer.getBuffer(),ret);
146 return ret;
147 }
148
149 #endif //__CL_INCLUDE_TPRINTF
150