Skip to content

Commit ce57327

Browse files
kuba2k2Dmitrii Shcherbakov
authored andcommitted
Add Print::printf() method
1 parent 331cdd9 commit ce57327

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

api/Print.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
1818

19+
#include <stdarg.h>
1920
#include <stdlib.h>
2021
#include <stdio.h>
2122
#include <string.h>
@@ -385,3 +386,21 @@ size_t Print::printFloat(double number, int digits)
385386

386387
return n;
387388
}
389+
390+
size_t Print::printf(const char *format, ...)
391+
{
392+
va_list args;
393+
va_list copy;
394+
va_start(args, format);
395+
int len = vsnprintf(NULL, 0, format, args);
396+
if (len <= 0) {
397+
va_end(args);
398+
return 0;
399+
}
400+
char *buf = (char *)malloc(len + 1);
401+
len = vsnprintf(buf, len + 1, format, args);
402+
va_end(args);
403+
len = write(buf, len);
404+
free(buf);
405+
return len;
406+
}

api/Print.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class Print
9090
size_t println(void);
9191

9292
virtual void flush() { /* Empty implementation for backward compatibility */ }
93+
94+
size_t printf(const char *format, ...) __attribute__((format (printf, 2, 3)));
9395
};
9496

9597
}

0 commit comments

Comments
 (0)