From d0e1d16ae899addf544c765ec8d50d8e095455df Mon Sep 17 00:00:00 2001 From: Greg Ercolano Date: Sun, 8 Oct 2017 20:38:36 +0000 Subject: Added printf() and vprintf() to Fl_Text_Buffer git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12483 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- src/Fl_Text_Buffer.cxx | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/Fl_Text_Buffer.cxx') diff --git a/src/Fl_Text_Buffer.cxx b/src/Fl_Text_Buffer.cxx index d63da183f..6b46dd497 100644 --- a/src/Fl_Text_Buffer.cxx +++ b/src/Fl_Text_Buffer.cxx @@ -292,6 +292,50 @@ void Fl_Text_Buffer::insert(int pos, const char *text) } +/** + Can be used by subclasses that need their own printf() style functionality. + e.g. Fl_Simple_Terminal::printf() would wrap around this method. + \note The expanded string is currently limited to 1024 characters. + \param[in] fmt is a printf format string for the message text. + \param[in] ap is a va_list created by va_start() and closed with va_end(), + which the caller is responsible for handling. +*/ +void Fl_Text_Buffer::vprintf(const char *fmt, va_list ap) { + char buffer[1024]; // XXX: 1024 should be user configurable + ::vsnprintf(buffer, 1024, fmt, ap); + buffer[1024-1] = 0; // XXX: MICROSOFT + append(buffer); +} + + +/** + Appends printf formatted messages to the end of the buffer. + Example: + \code + #include + int main(..) { + : + // Create a text display widget and assign it a text buffer + Fl_Text_Display *tdsp = new Fl_Text_Display(..); + Fl_Text_Buffer *tbuf = new Fl_Text_Buffer(); + tdsp->buffer(tbuf); + : + // Append three lines of formatted text to the buffer + tbuf->printf("The current date is: %s.\nThe time is: %s\n", date_str, time_str); + tbuf->printf("The current PID is %ld.\n", (long)getpid()); + : + \endcode + \note The expanded string is currently limited to 1024 characters. + \param[in] fmt is a printf format string for the message text. +*/ +void Fl_Text_Buffer::printf(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + Fl_Text_Buffer::vprintf(fmt,ap); + va_end(ap); +} + + /* Replace a range of text with new text. Start and end must be at a character boundary. -- cgit v1.2.3