J
January Weiner
Hello,
I have the following problem.
I have a custom fprintf-like function which is supposed to prepend each
comment output line with a `%' if the output type is a Postscript
document (pseudo-code below). (Postscript does not matter here)
/* ------------------------------------------------------------- */
/* structure for holding options */
typedef struct {
int ps_generate_postscript ;
FILE *out ;
/* other options */
} opt_s ;
/* this is my custom fprintf-like function */
void print_output_line(opt_s *o, char *message, ...) {
va_list vl ;
va_start(vl,message) ;
/* check whether we are generating Postscript */
if(o->ps_generate_postscript) {
print_ps_line(o, message, vl) ;
} else {
vfprintf(o->out, message, vl) ;
fprintf(o->out, "\n") ;
}
}
/* print a Postscript comment */
/* the problem sits here */
void print_ps_line(opt_s *o, char *message, va_list vl) {
/* some PostScript specific operations go in here... */
/* print a `%', which is a comment in Postscript */
fprintf(o->out, "%% ") ;
vfprintf(o->out, message, vl) ;
}
/* ------------------------------------------------------------- */
OK, here is the problem. Given that o->ps_generate_postscript == 1, the
output of
print_output_line(o, "just a test") ;
is
% just a test
Fine. But
print_output_line(o, "just a %s test", "\n") ;
produces
% just a
test
Obviously, I would like to have the second line commented as well, or at
least to replace the newline characters with a space / whatever
(otherwise the Postscript interpreter sees "test" and does not know what
to do with it). A way to do it would be first to call
/* ------------------------------------------------------------- */
char *some_string ;
some_string = malloc(I_HAVE_NO_IDEA_HOW_MUCH * sizeof(some_string)) ;
vsprintf(some_string, message, vl) ;
/* process some_string even one character by one, if necessary */
/* ------------------------------------------------------------- */
However, how can I check how much space should I reserve for
some_string? Or, alternatively: how can I go through va_list vl and
examine it's contents?
Best regards,
January
P.S. Some time ago I have posted a problem in a thread called "Weird
runtime problem" ([email protected]). Obviously, some of
my follow-ups didn't ever got propagated to other news server than
news.uni-muenster.de. Thank you all very much for help; and BTW -- it was
a problem with my hashing function.
I have the following problem.
I have a custom fprintf-like function which is supposed to prepend each
comment output line with a `%' if the output type is a Postscript
document (pseudo-code below). (Postscript does not matter here)
/* ------------------------------------------------------------- */
/* structure for holding options */
typedef struct {
int ps_generate_postscript ;
FILE *out ;
/* other options */
} opt_s ;
/* this is my custom fprintf-like function */
void print_output_line(opt_s *o, char *message, ...) {
va_list vl ;
va_start(vl,message) ;
/* check whether we are generating Postscript */
if(o->ps_generate_postscript) {
print_ps_line(o, message, vl) ;
} else {
vfprintf(o->out, message, vl) ;
fprintf(o->out, "\n") ;
}
}
/* print a Postscript comment */
/* the problem sits here */
void print_ps_line(opt_s *o, char *message, va_list vl) {
/* some PostScript specific operations go in here... */
/* print a `%', which is a comment in Postscript */
fprintf(o->out, "%% ") ;
vfprintf(o->out, message, vl) ;
}
/* ------------------------------------------------------------- */
OK, here is the problem. Given that o->ps_generate_postscript == 1, the
output of
print_output_line(o, "just a test") ;
is
% just a test
Fine. But
print_output_line(o, "just a %s test", "\n") ;
produces
% just a
test
Obviously, I would like to have the second line commented as well, or at
least to replace the newline characters with a space / whatever
(otherwise the Postscript interpreter sees "test" and does not know what
to do with it). A way to do it would be first to call
/* ------------------------------------------------------------- */
char *some_string ;
some_string = malloc(I_HAVE_NO_IDEA_HOW_MUCH * sizeof(some_string)) ;
vsprintf(some_string, message, vl) ;
/* process some_string even one character by one, if necessary */
/* ------------------------------------------------------------- */
However, how can I check how much space should I reserve for
some_string? Or, alternatively: how can I go through va_list vl and
examine it's contents?
Best regards,
January
P.S. Some time ago I have posted a problem in a thread called "Weird
runtime problem" ([email protected]). Obviously, some of
my follow-ups didn't ever got propagated to other news server than
news.uni-muenster.de. Thank you all very much for help; and BTW -- it was
a problem with my hashing function.