]> source.dussan.org Git - tigervnc.git/commitdiff
Give CharArray a printf style method to ease automatic allocation
authorPierre Ossman <ossman@cendio.se>
Tue, 3 Mar 2015 15:41:29 +0000 (16:41 +0100)
committerPierre Ossman <ossman@cendio.se>
Tue, 3 Mar 2015 15:41:29 +0000 (16:41 +0100)
common/rfb/util.cxx
common/rfb/util.h
win/winvnc/VNCServerService.cxx
win/winvnc/winvnc.cxx

index 2b3c9f40f4efbfb05e6ae470a5aeaa35b1765d7e..aec45f6136da2a85a8bcea68e4240f88a71e9f48 100644 (file)
@@ -34,6 +34,7 @@
 #include <config.h>
 #endif
 
+#include <stdarg.h>
 #include <stdio.h>
 #include <sys/time.h>
 
 
 namespace rfb {
 
+  void CharArray::format(const char *fmt, ...) {
+    va_list ap;
+    size_t len;
+
+    va_start(ap, fmt);
+    len = vsnprintf(NULL, 0, fmt, ap);
+    va_end(ap);
+
+    delete [] buf;
+
+    if (len < 0) {
+      buf = new char[1];
+      buf[0] = '\0';
+      return;
+    }
+
+    buf = new char[len+1];
+
+    va_start(ap, fmt);
+    vsnprintf(buf, len+1, fmt, ap);
+    va_end(ap);
+  }
+
   char* strDup(const char* s) {
     if (!s) return 0;
     int l = strlen(s);
index 98ce64200fe7b5d495dbc85c275267d70cb337cf..9ad177207f79160cb6ba4d290cfd4989b691fd92 100644 (file)
 
 struct timeval;
 
+#ifdef __GNUC__
+#  define __printf_attr(a, b) __attribute__((__format__ (__printf__, a, b)))
+#else
+#  define __printf_attr(a, b)
+#endif // __GNUC__
+
 namespace rfb {
 
   // -=- Class to handle cleanup of arrays of characters
@@ -45,6 +51,7 @@ namespace rfb {
     ~CharArray() {
       delete [] buf;
     }
+    void format(const char *fmt, ...) __printf_attr(2, 3);
     // Get the buffer pointer & clear it (i.e. caller takes ownership)
     char* takeBuf() {char* tmp = buf; buf = 0; return tmp;}
     void replaceBuf(char* b) {delete [] buf; buf = b;}
index 1a2a8e93aaac3aa5bf469d4786ce394e0f8a48eb..481df217182f576b6674d56d7797798cd12a89de 100644 (file)
@@ -97,9 +97,8 @@ HANDLE LaunchProcessWin(DWORD dwSessionId)
     if (GetSessionUserTokenWin(&hToken))
     {
         ModuleFileName filename;
-        static const char cmdLineFmt[] = "\"%s\" -noconsole -service_run";
-        TCharArray cmdLine(_tcslen(filename.buf) + sizeof(cmdLineFmt)/sizeof(cmdLineFmt[0]));
-        _stprintf(cmdLine.buf, cmdLineFmt, filename.buf);
+        TCharArray cmdLine;
+        cmdLine.format("\"%s\" -noconsole -service_run", filename.buf);
         STARTUPINFO si;
         ZeroMemory(&si, sizeof si);
         si.cb = sizeof si;
index c2abd8923df8efdda8c0107eec9458f3253d8ec2..8fba9dcd81db5f408a92ac1ecb7f63330665e00a 100644 (file)
@@ -148,11 +148,11 @@ static void processParams(int argc, char** argv) {
       } else if (strcasecmp(argv[i], "-status") == 0) {
         printf("Querying service status...\n");
         runServer = false;
+        CharArray result;
         DWORD state = rfb::win32::getServiceState(VNCServerService::Name);
-        CharArray stateStr(rfb::win32::serviceStateName(state));
-        const char* stateMsg = "The %s Service is in the %s state.";
-        CharArray result(strlen(stateStr.buf) + _tcslen(VNCServerService::Name) + strlen(stateMsg) + 1);
-        sprintf(result.buf, stateMsg, (const char*)CStr(VNCServerService::Name), stateStr.buf);
+        result.format("The %s Service is in the %s state.",
+                      (const char*)CStr(VNCServerService::Name),
+                      rfb::win32::serviceStateName(state));
         MsgBoxOrLog(result.buf);
       } else if (strcasecmp(argv[i], "-service") == 0) {
         printf("Run in service mode\n");