Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mimic variadic macro in VC++6.0?

In VS2010, I wrote the following variadic macros to dump out information to files.

#define INDENT(fp, indent) for(size_t __i = 0; __i < (indent); ++__i) fprintf((fp), "  ")
// IND_FP = indented fprintf.
// This macro uses two IMPLICIT parameters.
// 1. (FILE *)fp is a pointer to the output file.
// 2. (size_t)indent specifies the indentation level.
#define IND_FP(format, ...) do{ INDENT(fp, indent); fprintf(fp, format, __VA_ARGS__); }while(0)

These macros frequently occur in my program.

void CMAP::dump_info(FILE *fp, size_t indent){
  IND_FP("<cmap tableVersion=\"0x%08x\" numberOfEncodingTables=\"%d\">\n",
    table_version_number, num_encoding_tables);
  //...
  IND_FP("</cmap>\n");
}

Sadly, now I have to migrate my code to the antediluvian IDE, VC++6.0, which does NOT support variadic macro. I wrote a variadic function instead.

void IND_FP(FILE *fp, size_t indent, char *format, ...){
  INDENT(fp, indent);
  va_list arg_ptr;
  va_start(arg_ptr, format);
  vfprintf(fp, format, arg_ptr);
  va_end(arg_ptr);
}

But I have to change the tens if not hundreds lines of code from IND_FP(format, ...) to IND_FP(fp, indent, format, ...).

Is there any trick of macro that can help me out? Or I'd better use explicit arguments and get used to redundancy?

like image 451
lzl124631x Avatar asked Dec 31 '25 06:12

lzl124631x


1 Answers

#define IND_FP Logger(indent)

class Logger {
  public:
    Logger(int indent);
    int operator()(FILE* fp, char* format, ...);
  private:
    // an exercise for the reader
};
like image 100
n. m. could be an AI Avatar answered Jan 03 '26 10:01

n. m. could be an AI