Question

I'm learning how to use ncurses, and wanted to use RAII to automatically init and end window:

class CWindow
{
public:
   CWindow() {initscr();}
   ~CWindow() {endwin();}
};

This is basic idea of CWindow class. It works only for stdscr now, so using functions like addch(), printw(), etc. isn't a problem. However, this class will eventually be used to represent ncurses windows(WINDOW*). It would be intuitive to add member functions like print, so that instead of using

wprintw(win.getWin(), "str %i", someVar);

one could write

win.print("str %i", someVar);

I looked in web, but it seems to me that only cstdio's prinf and similar functions have been wrapped. And cstdio provides an easy way, with function that accepts va_list. However, ncurses doesn't have this set of functions.

To be honest, I'm rather using C++ then C, so my C knowledge isn't excellent. I've also not created any variable argument list functions. I tried naive approach:

void print(const char* str, ...)
{
    va_list args;
    va_start(args, str);
    printw(str, args);
    va_end(args);
}

However, it doesn't work. I also tried to incorporate variadic macros like in this thread, but it doesn't work for me. I may be doing it wrong though.

Anyway, how may I achieve this?

Was it helpful?

Solution 2

You are almost in the right path to do what you want, just change

printw(string, args);

for

WINDOW *win = initscr();
// ...
vwprintw(win, string, args);

OTHER TIPS

The answer by @massa can be improved:

  • while ncurses accepts vwprintw, the preferred form is vw_printw.
  • it is not necessary to use the return value from initscr; the stdscr variable holds the same value.

With those fixes:

void print(const char* str, ...)
{
    va_list args;
    va_start(args, str);
    vw_printw(stdscr, str, args);
    va_end(args);
}

Further reading:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top