Changeset 96089 in vbox
- Timestamp:
- Aug 7, 2022 2:12:29 AM (3 years ago)
- svn:sync-xref-src-repo-rev:
- 152888
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/mangling.h
r96078 r96089 2290 2290 # define RTStrmOpenFV RT_MANGLER(RTStrmOpenFV) 2291 2291 # define RTStrmOpenFileHandle RT_MANGLER(RTStrmOpenFileHandle) 2292 # define RTStrmQueryFileHandle RT_MANGLER(RTStrmQueryFileHandle) 2292 2293 # define RTStrmPrintf RT_MANGLER(RTStrmPrintf) 2293 2294 # define RTStrmPrintfV RT_MANGLER(RTStrmPrintfV) … … 2299 2300 # define RTStrmReadEx RT_MANGLER(RTStrmReadEx) 2300 2301 # define RTStrmRewind RT_MANGLER(RTStrmRewind) 2302 # define RTStrmSetBufferingMode RT_MANGLER(RTStrmSetBufferingMode) 2301 2303 # define RTStrmSetMode RT_MANGLER(RTStrmSetMode) 2304 # define RTStrmSeek RT_MANGLER(RTStrmSeek) 2305 # define RTStrmTell RT_MANGLER(RTStrmTell) 2302 2306 # define RTStrmWriteEx RT_MANGLER(RTStrmWriteEx) 2303 2307 # define RTStrmIsTerminal RT_MANGLER(RTStrmIsTerminal) -
trunk/include/iprt/stream.h
r96077 r96089 144 144 145 145 /** 146 * Queries the file handle backing the stream. 147 * 148 * @returns iprt status code. 149 * @retval VERR_NOT_AVAILABLE if the stream has no valid handle associated with 150 * it. 151 * 152 * @param pStream The stream. 153 * @param phFile Where to return the file handle. This should not be 154 * closed! 155 */ 156 RTR3DECL(int) RTStrmQueryFileHandle(PRTSTREAM pStream, PRTFILE phFile); 157 158 /** 146 159 * Closes the specified stream. 147 160 * … … 188 201 RTR3DECL(int) RTStrmSetMode(PRTSTREAM pStream, int fBinary, int fCurrentCodeSet); 189 202 203 /** Stream buffering modes. */ 204 typedef enum RTSTRMBUFMODE 205 { 206 RTSTRMBUFMODE_INVALID = 0, 207 RTSTRMBUFMODE_FULL, /**< Full buffering. */ 208 RTSTRMBUFMODE_LINE, /**< Line buffering. On Windows this could be the same as RTSTRMBUFMODE_FULL. */ 209 RTSTRMBUFMODE_UNBUFFERED, /**< No buffering. */ 210 RTSTRMBUFMODE_END, 211 RTSTRMBUFMODE_32BIT_HACK = 0x7fffffff 212 } RTSTRMBUFMODE; 213 214 /** 215 * Changes the stream buffering mode. 216 * 217 * @returns iprt status code. 218 * @param pStream The stream. 219 * @param enmBufMode The new buffering mode. 220 */ 221 RTR3DECL(int) RTStrmSetBufferingMode(PRTSTREAM pStream, RTSTRMBUFMODE enmBufMode); 222 190 223 /** 191 224 * Returns the current echo mode. 225 * 192 226 * This works only for standard input streams. 193 227 * … … 201 235 /** 202 236 * Changes the behavior for echoing inpit characters on the command line. 237 * 203 238 * This works only for standard input streams. 204 239 * … … 242 277 */ 243 278 RTR3DECL(int) RTStrmRewind(PRTSTREAM pStream); 279 280 /** 281 * Changes the file position. 282 * 283 * @returns IPRT status code. 284 * 285 * @param pStream The stream. 286 * @param off The seek offset. 287 * @param uMethod Seek method, i.e. one of the RTFILE_SEEK_* defines. 288 * 289 * @remarks Not all streams are seekable and that behavior is currently 290 * undefined for those. 291 */ 292 RTR3DECL(int) RTStrmSeek(PRTSTREAM pStream, RTFOFF off, uint32_t uMethod); 293 294 /** 295 * Tells the stream position. 296 * 297 * @returns Stream position or IPRT error status. Non-negative numbers are 298 * stream positions, while negative numbers are IPRT error stauses. 299 * 300 * @param pStream The stream. 301 * 302 * @remarks Not all streams have a position and that behavior is currently 303 * undefined for those. 304 */ 305 RTR3DECL(RTFOFF) RTStrmTell(PRTSTREAM pStream); 244 306 245 307 /** -
trunk/src/VBox/Runtime/r3/stream.cpp
r96077 r96089 160 160 /** The stream buffer direction. */ 161 161 RTSTREAMBUFDIR enmBufDir; 162 /** The buffering style (unbuffered, line, full). */ 162 /** The buffering style (unbuffered, line, full). 163 * @todo replace by RTSTRMBUFMODE. */ 163 164 RTSTREAMBUFSTYLE enmBufStyle; 164 165 # ifdef RTSTREAM_WITH_TEXT_MODE 166 /** Bitmap running parallel to each char pchBuf, indicating where a '\\r' 167 * character have been removed during buffer filling. This is used to implement 168 * RTStrmTell in non-binary mode. */ 169 uint32_t *pbmBuf; 165 170 /** Indicates that we've got a CR ('\\r') beyond the end of official buffer 166 171 * and need to check if there is a LF following it. This member is ignored … … 223 228 /* .enmBufStyle = */ RTSTREAMBUFSTYLE_UNBUFFERED, 224 229 # ifdef RTSTREAM_WITH_TEXT_MODE 230 /* .pbmBuf = */ NULL, 225 231 /* .fPendingCr = */ false, 226 232 # endif … … 254 260 /* .enmBufStyle = */ RTSTREAMBUFSTYLE_UNBUFFERED, 255 261 # ifdef RTSTREAM_WITH_TEXT_MODE 262 /* .pbmBuf = */ NULL, 256 263 /* .fPendingCr = */ false, 257 264 # endif … … 285 292 /* .enmBufStyle = */ RTSTREAMBUFSTYLE_LINE, 286 293 # ifdef RTSTREAM_WITH_TEXT_MODE 294 /* .pbmBuf = */ NULL, 287 295 /* .fPendingCr = */ false, 288 296 # endif … … 530 538 pStream->enmBufStyle = RTSTREAMBUFSTYLE_FULL; 531 539 # ifdef RTSTREAM_WITH_TEXT_MODE 540 pStream->pbmBuf = NULL; 532 541 pStream->fPendingCr = false, 533 542 # endif … … 771 780 pStream->offBufFirst = 0; 772 781 pStream->offBufEnd = 0; 782 # ifdef RTSTREAM_WITH_TEXT_MODE 783 RTMemFree(pStream->pbmBuf); 784 pStream->pbmBuf = NULL; 785 # endif 773 786 #else 774 787 pStream->pFile = NULL; … … 838 851 } 839 852 853 854 RTR3DECL(int) RTStrmSetBufferingMode(PRTSTREAM pStream, RTSTRMBUFMODE enmMode) 855 { 856 AssertPtrReturn(pStream, VERR_INVALID_HANDLE); 857 AssertReturn(pStream->u32Magic == RTSTREAM_MAGIC, VERR_INVALID_HANDLE); 858 AssertReturn(enmMode > RTSTRMBUFMODE_INVALID && enmMode < RTSTRMBUFMODE_END, VERR_INVALID_PARAMETER); 859 860 #ifndef RTSTREAM_STANDALONE 861 int iCrtMode = enmMode == RTSTRMBUFMODE_FULL ? _IOFBF : enmMode == RTSTRMBUFMODE_LINE ? _IOLBF : _IONBF; 862 int rc = setvbuf(pStream->pFile, NULL, iCrtMode, 0); 863 if (rc >= 0) 864 return VINF_SUCCESS; 865 return RTErrConvertFromErrno(errno); 866 867 #else 868 rtStrmLock(pStream); 869 pStream->enmBufStyle = enmMode == RTSTRMBUFMODE_FULL ? RTSTREAMBUFSTYLE_FULL 870 : enmMode == RTSTRMBUFMODE_LINE ? RTSTREAMBUFSTYLE_LINE : RTSTREAMBUFSTYLE_UNBUFFERED; 871 rtStrmUnlock(pStream); 872 return VINF_SUCCESS; 873 #endif 874 } 875 876 840 877 #ifdef RTSTREAM_STANDALONE 841 878 … … 892 929 } 893 930 931 894 932 /** 895 933 * For lazily resolving handles for the standard streams. … … 901 939 return hFile; 902 940 return rtStrmGetFileNil(pStream); 941 } 942 943 944 RTR3DECL(int) RTStrmQueryFileHandle(PRTSTREAM pStream, PRTFILE phFile) 945 { 946 AssertPtrReturn(phFile, VERR_INVALID_POINTER); 947 *phFile = NIL_RTFILE; 948 AssertPtrReturn(pStream, VERR_INVALID_POINTER); 949 AssertReturn(pStream->u32Magic == RTSTREAM_MAGIC, VERR_INVALID_MAGIC); 950 951 rtStrmLock(pStream); 952 RTFILE hFile = rtStrmGetFile(pStream); 953 rtStrmUnlock(pStream); 954 if (hFile != NIL_RTFILE) 955 { 956 *phFile = hFile; 957 return VINF_SUCCESS; 958 } 959 return VERR_NOT_AVAILABLE; 903 960 } 904 961 … … 1194 1251 if (RT_LIKELY(pStream->pchBuf)) 1195 1252 { 1196 pStream->cbBufAlloc = cbBuf; 1197 return VINF_SUCCESS; 1253 # ifdef RTSTREAM_WITH_TEXT_MODE 1254 Assert(RT_ALIGN_Z(cbBuf, 64 / 8) == cbBuf); 1255 pStream->pbmBuf = (uint32_t *)RTMemAllocZ(cbBuf / 8); 1256 if (RT_LIKELY(pStream->pbmBuf)) 1257 # endif 1258 { 1259 pStream->cbBufAlloc = cbBuf; 1260 return VINF_SUCCESS; 1261 } 1262 # ifdef RTSTREAM_WITH_TEXT_MODE 1263 RTMemFree(pStream->pchBuf); 1264 pStream->pchBuf = NULL; 1265 # endif 1198 1266 } 1199 1267 cbBuf /= 2; … … 1313 1381 AssertPtr(pStream->pchBuf); 1314 1382 Assert(pStream->cbBufAlloc >= 256); 1383 Assert(RT_ALIGN_Z(pStream->cbBufAlloc, 64) == pStream->cbBufAlloc); 1315 1384 Assert(pStream->offBufFirst <= pStream->cbBufAlloc); 1316 1385 Assert(pStream->offBufEnd <= pStream->cbBufAlloc); 1317 1386 Assert(pStream->offBufFirst <= pStream->offBufEnd); 1318 1387 # ifdef RTSTREAM_WITH_TEXT_MODE 1388 AssertPtr(pStream->pbmBuf); 1389 # endif 1319 1390 /* 1320 1391 * If there is data in the buffer, move it up to the start. … … 1327 1398 cbInBuffer = pStream->offBufEnd - pStream->offBufFirst; 1328 1399 if (cbInBuffer) 1400 { 1329 1401 memmove(pStream->pchBuf, &pStream->pchBuf[pStream->offBufFirst], cbInBuffer); 1402 # ifdef RTSTREAM_WITH_TEXT_MODE 1403 if (!pStream->fBinary) /** @todo this isn't very efficient, must be a better way of shifting a bitmap. */ 1404 for (size_t off = 0; off < pStream->offBufFirst; off++) 1405 if (ASMBitTest(pStream->pbmBuf, (int32_t)off)) 1406 ASMBitSet(pStream->pbmBuf, (int32_t)off); 1407 else 1408 ASMBitClear(pStream->pbmBuf, (int32_t)off); 1409 # endif 1410 } 1330 1411 pStream->offBufFirst = 0; 1331 1412 pStream->offBufEnd = cbInBuffer; … … 1358 1439 if (cbInBuffer != 0) 1359 1440 { 1441 # ifdef RTSTREAM_WITH_TEXT_MODE 1360 1442 if (pStream->fBinary) 1443 # endif 1361 1444 return VINF_SUCCESS; 1362 1445 } … … 1368 1451 } 1369 1452 1453 # ifdef RTSTREAM_WITH_TEXT_MODE 1370 1454 /* 1371 1455 * Do CRLF -> LF conversion in the buffer. 1372 1456 */ 1457 ASMBitClearRange(pStream->pbmBuf, offCrLfConvStart, RT_ALIGN_Z(cbInBuffer, 64)); 1373 1458 char *pchCur = &pStream->pchBuf[offCrLfConvStart]; 1374 1459 size_t cbLeft = cbInBuffer - offCrLfConvStart; … … 1390 1475 do 1391 1476 { 1477 ASMBitSet(pStream->pbmBuf, (int32_t)(pchCur - pStream->pchBuf)); 1392 1478 *pchCur++ = '\n'; /* dst */ 1393 1479 cbLeft -= 2; … … 1416 1502 1417 1503 return VINF_SUCCESS; 1504 # endif 1418 1505 } 1419 1506 … … 1486 1573 pStream->offBufFirst = 0; 1487 1574 pStream->offBufEnd = 0; 1575 # ifdef RTSTREAM_WITH_TEXT_MODE 1576 RTMemFree(pStream->pbmBuf); 1577 pStream->pbmBuf = NULL; 1578 # endif 1488 1579 } 1489 1580 … … 1585 1676 #endif 1586 1677 return rc; 1678 } 1679 1680 1681 /** 1682 * Changes the file position. 1683 * 1684 * @returns IPRT status code. 1685 * 1686 * @param pStream The stream. 1687 * @param off The seek offset. 1688 * @param uMethod Seek method, i.e. one of the RTFILE_SEEK_* defines. 1689 * 1690 * @remarks Not all streams are seekable and that behavior is currently 1691 * undefined for those. 1692 */ 1693 RTR3DECL(int) RTStrmSeek(PRTSTREAM pStream, RTFOFF off, uint32_t uMethod) 1694 { 1695 AssertReturn(uMethod <= RTFILE_SEEK_END, VERR_INVALID_PARAMETER); 1696 #ifdef RTSTREAM_STANDALONE 1697 rtStrmLock(pStream); 1698 int rc = rtStrmBufFlushWriteMaybe(pStream, true /*fInvalidate*/); 1699 if (RT_SUCCESS(rc)) 1700 rc = RTFileSeek(rtStrmGetFile(pStream), off, uMethod, NULL); 1701 if (RT_FAILURE(rc)) 1702 ASMAtomicWriteS32(&pStream->i32Error, rc); 1703 rtStrmUnlock(pStream); 1704 #else 1705 int const iCrtMethod = uMethod == RTFILE_SEEK_BEGIN ? SEEK_SET : uMethod == RTFILE_SEEK_CURRENT ? SEEK_CUR : SEEK_END; 1706 errno = 0; 1707 int rc; 1708 # ifdef _MSC_VER 1709 if (!_fseeki64(pStream->pFile, off, iCrtMethod)) 1710 # else 1711 if (!fseeko(pStream->pFile, off, iCrtMethod)) 1712 # endif 1713 rc = VINF_SUCCESS; 1714 else 1715 rc = RTErrConvertFromErrno(errno); 1716 ASMAtomicWriteS32(&pStream->i32Error, rc); 1717 #endif 1718 return rc; 1719 } 1720 1721 1722 /** 1723 * Tells the stream position. 1724 * 1725 * @returns Stream position or IPRT error status. Non-negative numbers are 1726 * stream positions, while negative numbers are IPRT error stauses. 1727 * 1728 * @param pStream The stream. 1729 * 1730 * @remarks Not all streams have a position and that behavior is currently 1731 * undefined for those. 1732 */ 1733 RTR3DECL(RTFOFF) RTStrmTell(PRTSTREAM pStream) 1734 { 1735 #ifdef RTSTREAM_STANDALONE 1736 uint64_t off = 0; 1737 rtStrmLock(pStream); 1738 int rc = pStream->i32Error; 1739 if (RT_SUCCESS(rc)) 1740 { 1741 rc = RTFileSeek(rtStrmGetFile(pStream), 0, RTFILE_SEEK_CURRENT, &off); 1742 if (RT_SUCCESS(rc)) 1743 { 1744 switch (pStream->enmBufDir) 1745 { 1746 case RTSTREAMBUFDIR_READ: 1747 /* Subtract unconsumed chars and removed '\r' characters. */ 1748 off -= pStream->offBufEnd - pStream->offBufFirst; 1749 if (!pStream->fBinary) 1750 for (size_t offBuf = pStream->offBufFirst; offBuf < pStream->offBufEnd; offBuf++) 1751 off -= ASMBitTest(pStream->pbmBuf, (int32_t)offBuf); 1752 break; 1753 case RTSTREAMBUFDIR_WRITE: 1754 /* Add unwrittend chars in the buffer. */ 1755 off += pStream->offBufEnd - pStream->offBufFirst; 1756 break; 1757 default: 1758 AssertFailed(); 1759 case RTSTREAMBUFDIR_NONE: 1760 break; 1761 } 1762 } 1763 } 1764 if (RT_FAILURE(rc)) 1765 { 1766 ASMAtomicWriteS32(&pStream->i32Error, rc); 1767 off = rc; 1768 } 1769 rtStrmUnlock(pStream); 1770 #else 1771 # ifdef _MSC_VER 1772 RTFOFF off = _ftelli64(pStream->pFile); 1773 # else 1774 RTFOFF off = ftello(pStream->pFile); 1775 # endif 1776 if (off < 0) 1777 { 1778 int rc = RTErrConvertFromErrno(errno); 1779 ASMAtomicWriteS32(&pStream->i32Error, rc); 1780 off = rc; 1781 } 1782 #endif 1783 return off; 1587 1784 } 1588 1785
Note:
See TracChangeset
for help on using the changeset viewer.