473,748 Members | 8,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

variable argument va_arg does not work with icu u_vformatMessag e

Hi Friends,
I was trying a test code to use the icu stuff with variable arguments
and I seem to be heading nowhere.

Can anyone please help.
Given below is the test code and the Output.

Thanks a ton in advance
-braj
#include <stdio.h>
#include <stdarg.h>

#include "unicode/utypes.h"
#include <stdlib.h>
#include <string.h>
#include "unicode/uloc.h"
#include "unicode/umsg.h"
#include "unicode/urename.h"
#include "unicode/ustring.h" /* required for UERROR CODE u_strlen
etc... */

void msgCall(const char *msgid, char *type, ...);

int main()
{
printf("here is goes \n");

msgCall("MSG_VF I_E0332", "HELLO" , "%s: something %s : follows : %s
nothing\n", "ONE", "TWO", "THREE");

return 0;
}

void msgCall(const char *msgid, char *type, ...)
{
char *fmt = NULL, *lfmt = NULL, *lmsg = NULL, *msg = NULL;
va_list ap;
char *finalstr;
UChar *result;
UChar pattern[2048];
int32_t resultlength, resultLengthOut ;
UErrorCode status = U_ZERO_ERROR;
char *message = "CLI Tool {0} could not be {1} found with {2}
ending";
int len = 0;
u_uastrcpy(patt ern, message);

va_start(ap, type);
fmt = va_arg(ap, char *);

resultlength=0;
resultLengthOut =u_vformatMessa ge("en_US", pattern, resultlength,
NULL, resultlength, ap, &status);
if(status == U_BUFFER_OVERFL OW_ERROR) {
status = U_ZERO_ERROR;
resultlength = resultLengthOut +1;
result = (UChar *)malloc(sizeof (UChar) * resultlength);
u_vformatMessag e("en_US", pattern, resultlength, result,
resultlength, ap, &status);
}
len = u_strlen(result );
finalstr = (char*)malloc(s izeof(char) * (len + 1));
u_austrcpy(fina lstr, result);

printf("finalst r = [%s]\n", finalstr);
va_end(ap);
}
/* OUTPUT of the above */
here is goes
finalstr = [CLI Tool ä¹E could not be åOä¹E found with ä¡äEåOä¹E
ending]

Mar 2 '07 #1
1 2354
In article <11************ *********@j27g2 000cwj.googlegr oups.com>
braj <br**********@g mail.comwrote:
>Given below is the test code and the Output.
Most of these are not standard functions, so there is no predicting
what they require or do.

That said, however, if I replace most of your nonstandard code with
two Standard C functions, I see an immediate problem:

[nonstandard code deleted, Standard C added, as marked]
>#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
[stuff removed]
>
void msgCall(const char *msgid, char *type, ...);

int main()
{
printf("here is goes \n");

msgCall("MSG_VF I_E0332", "HELLO" , "%s: something %s : follows : %s \
nothing\n", "ONE", "TWO", "THREE");

return 0;
}

void msgCall(const char *msgid, char *type, ...)
{
char *fmt = NULL, *lfmt = NULL, *lmsg = NULL, *msg = NULL;
va_list ap;
[stuff removed]
va_start(ap, type);
fmt = va_arg(ap, char *);
[insertion of new, Standard C, function call:]

vprintf(fmt, ap);

[stuff removed]
[insertion of new, Standard C, function call, plus skeleton to make
it clearer why there are two calls:]

if (1 /* some condition that decides we should try again */) {
vprintf(fmt, ap);
}

[stuff removed]
va_end(ap);
}
If this new code is compiled and run, the output may (or may not)
be:

ONE: something TWO : follows : THREE nothing
ONE: something TWO : follows : THREE nothing

It seems to me that you are expecting this output. The problem
is that the output may, instead, be something more like:

ONE: something TWO : follows : THREE nothing
keq1: something 8%# : follows : >m,(==_ nothing

(or perhaps a crash when the "%s" directives try to follow invalid
pointers, for instance; in this case, typically the first line will
still get printed though).

The problem is that after calling vprintf() -- or indeed any
function, standard or user-written, that uses va_arg() to access
the various parameters -- the value stored in "ap" is officially
"dead". The only thing you can do is va_end() it. After that,
you can va_start() it again, in this case. (In C99, if you need
to use an "ap" more than once, you can va_copy() it as well.)

So, msgCall() might be better written, e.g.:

void msgCall(const char *msgid, char *type, const char *fmt, ...) {
va_list ap;

va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
if (1 /* replace with appropriate test */) {
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
}

However, note that any time you write a variadic function, you
probably should provide the "v" version of it, for the same reason
that the C library provides not only printf() but also vprintf().
This requires the new C99-specific "va_copy", however:

void msgCall(const char *msgid, char *type, const char *fmt, ...) {
va_list ap;

va_start(ap, fmt);
vmsgCall(msgid, type, fmt, ap);
va_end(ap);
}

void vmsgCall(const char *msgid, char *type, const char *fmt, va_list ap) {
va_list copy;

va_copy(ap, copy); /* make copy before we wreck the original */

vprintf(fmt, ap); /* now use (and wreck) ap */
if (1) {
vprintf(fmt, copy); /* and if we need to repeat, use/wreck copy */
}

va_end(copy); /* in any case, delete the copy we made */
}

If you lack C99 (or at least va_copy), you may be able to get away
with providing only the non-"v" version of msgCall, in the same way
that many programmers get away with using printf() and never needing
vprintf(). (But once you *do* need it, you really need it!)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 3 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7920
by: Suzanne Vogel | last post by:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers of parameters to functions. The example of its use given in the Dinkumware documentation *seems* to imply that the 'va_arg' list should *automatically* be terminated with NULL past the user's last argument: http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=stdarg.html However, I (for some reason) have to pass in the count of the number of arguments, or manually...
3
4902
by: srinivas reddy | last post by:
Hi, I have following questions. 1. Does va_arg allow one to read user defined types. My compiler allows but I am wondering whether it is true for all. 2. I wrote the following code. Pardon my omitting declarations etc.. int main() { func(10, "baaaaa");
7
8320
by: | last post by:
How to call a function with variable argument list from another function again with variable argument list? Example : double average ( int num, ... ); double AFunct1 ( int num, ... ); double AFunct2 ( int num, ... ); double average ( int num, ... )
10
2989
by: Richard Heathfield | last post by:
I was recently asked whether it is legal to start processing a variable argument list a second time, /before/ you've finished with it the first time. (I have no idea why the questioner might want to do this, I'm afraid. Since he's normally fairly bright, I presume he had a good reason for asking.) The following code illustrates the point, I think (but don't use it as a va_arg tutorial!, as it kinda assumes there are at least six args...
3
2797
by: Nimmi Srivastav | last post by:
Consider two functions A and B, both of which accept a variable number of arguments (va_start, va-arg, va_end). Is there an easy way for arguments passed to A to be, in turn, passed to B? (For example, if A is a wrapper function around B). Thanks, Nimmi
3
4340
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call that function (using its pointer) and call it with the same argument list that was passed into the static method.
5
3001
by: shaanxxx | last post by:
i have statements printf("%f",(double)1); /* works fine although i have sent %f for double */ printf("%c",(int)64); /* works fine although i have sent %c for int */ Need your comment on this. As my understanding, when we pass something on variable argument, its type will be promoted . char is promoted to Int . float is promoted to
11
11057
by: Googy | last post by:
Hi friends!! As we know that the input parameters in a function is fixed when the function is defined but how does printf processes variable number of input arguments ? For example: 1. printf("Hey! how are you"); /*No. of arguments = 1*/ 2. printf("Sum of %d and %d is %d",a,b,c"); /* No. of arguments = 4*/
5
4659
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Jun 3, 3:23 am, Jesse Ziser <d...@spam.diewrote: The relevant paragraph from the Standard is: ---- Begin Quote ---- The type declared is va_list which is an object type suitable for holding information needed by the macros
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.