Changeset 84163 in vbox for trunk/src/VBox/Runtime/common/vfs/vfsprintf.cpp
- Timestamp:
- May 6, 2020 3:31:33 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/vfs/vfsprintf.cpp
r84152 r84163 35 35 36 36 37 /*********************************************************************************************************************************38 * Structures and Typedefs *39 *********************************************************************************************************************************/40 typedef struct PRINTFBUF41 {42 RTVFSIOSTREAM hVfsIos;43 int rc;44 size_t offBuf;45 char szBuf[256];46 } PRINTFBUF;47 48 49 37 /** Writes the buffer to the VFS file. */ 50 static void FlushPrintfBuffer(P RINTFBUF *pBuf)38 static void FlushPrintfBuffer(PVFSIOSTRMOUTBUF pBuf) 51 39 { 52 40 if (pBuf->offBuf) … … 61 49 62 50 63 /** @callback_method_impl{FNRTSTROUTPUT} */ 64 static DECLCALLBACK(size_t) MyPrintfOutputter(void *pvArg, const char *pachChars, size_t cbChars) 51 /** 52 * @callback_method_impl{FNRTSTROUTPUT, 53 * For use with VFSIOSTRMOUTBUF.} 54 */ 55 RTDECL(size_t) RTVfsIoStrmStrOutputCallback(void *pvArg, const char *pachChars, size_t cbChars) 65 56 { 66 PRINTFBUF *pBuf = (PRINTFBUF *)pvArg; 57 PVFSIOSTRMOUTBUF pBuf = (PVFSIOSTRMOUTBUF)pvArg; 58 AssertReturn(pBuf->cbSelf == sizeof(*pBuf), 0); 59 67 60 if (cbChars != 0) 68 61 { 69 size_t offSrc = 0; 70 while (offSrc < cbChars) 62 if (cbChars <= sizeof(pBuf->szBuf) * 3 / 2) 71 63 { 72 size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1; 73 if (cbLeft > 0) 64 /* 65 * Small piece of output: Buffer it. 66 */ 67 size_t offSrc = 0; 68 while (offSrc < cbChars) 74 69 { 75 size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft); 76 memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy); 77 pBuf->offBuf += cbToCopy; 78 pBuf->szBuf[pBuf->offBuf] = '\0'; 79 if (cbLeft > cbToCopy) 80 break; 81 offSrc += cbToCopy; 70 size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1; 71 if (cbLeft > 0) 72 { 73 size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft); 74 memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy); 75 pBuf->offBuf += cbToCopy; 76 pBuf->szBuf[pBuf->offBuf] = '\0'; 77 if (cbLeft > cbToCopy) 78 break; 79 offSrc += cbToCopy; 80 } 81 FlushPrintfBuffer(pBuf); 82 82 } 83 } 84 else 85 { 86 /* 87 * Large chunk of output: Output it directly. 88 */ 83 89 FlushPrintfBuffer(pBuf); 90 91 int rc = RTVfsIoStrmWrite(pBuf->hVfsIos, pachChars, cbChars, true /*fBlocking*/, NULL); 92 if (RT_FAILURE(rc)) 93 pBuf->rc = rc; 84 94 } 85 95 } … … 90 100 91 101 92 93 102 RTDECL(ssize_t) RTVfsIoStrmPrintfV(RTVFSIOSTREAM hVfsIos, const char *pszFormat, va_list va) 94 103 { 95 PRINTFBUF Buf; 96 Buf.hVfsIos = hVfsIos; 97 Buf.rc = VINF_SUCCESS; 98 Buf.offBuf = 0; 99 Buf.szBuf[0] = '\0'; 104 VFSIOSTRMOUTBUF Buf; 105 VFSIOSTRMOUTBUF_INIT(&Buf, hVfsIos); 100 106 101 size_t cchRet = RTStrFormatV( MyPrintfOutputter, &Buf, NULL, NULL, pszFormat, va);107 size_t cchRet = RTStrFormatV(RTVfsIoStrmStrOutputCallback, &Buf, NULL, NULL, pszFormat, va); 102 108 if (RT_SUCCESS(Buf.rc)) 103 109 return cchRet;
Note:
See TracChangeset
for help on using the changeset viewer.