VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/stream.cpp@ 25802

Last change on this file since 25802 was 25685, checked in by vboxsync, 15 years ago

iprt,pdmcritsect: Some more lock validator code, almost there now... :-)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 22.6 KB
Line 
1/* $Id: stream.cpp 25685 2010-01-07 22:03:06Z vboxsync $ */
2/** @file
3 * IPRT - I/O Stream.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include <iprt/stream.h>
37#include "internal/iprt.h"
38
39#include <iprt/asm.h>
40#include <iprt/critsect.h>
41#include <iprt/string.h>
42#include <iprt/assert.h>
43#include <iprt/alloc.h>
44#include <iprt/err.h>
45#include <iprt/param.h>
46#include <iprt/string.h>
47
48#include "internal/alignmentchecks.h"
49#include "internal/magics.h"
50
51#include <stdio.h>
52#include <errno.h>
53
54#if defined(RT_OS_LINUX) /* PORTME: check for the _unlocked functions in stdio.h */
55#define HAVE_FWRITE_UNLOCKED
56#endif
57
58
59/*******************************************************************************
60* Structures and Typedefs *
61*******************************************************************************/
62/**
63 * File stream.
64 */
65typedef struct RTSTREAM
66{
67 /** Magic value used to validate the stream. (RTSTREAM_MAGIC) */
68 uint32_t u32Magic;
69 /** File stream error. */
70 int32_t volatile i32Error;
71 /** Pointer to the LIBC file stream. */
72 FILE *pFile;
73#ifndef HAVE_FWRITE_UNLOCKED
74 /** Critical section for serializing access to the stream. */
75 PRTCRITSECT pCritSect;
76#endif
77} RTSTREAM;
78
79
80/*******************************************************************************
81* Global Variables *
82*******************************************************************************/
83/** The standard input stream. */
84static RTSTREAM g_StdIn =
85{
86 RTSTREAM_MAGIC,
87 0,
88 stdin
89#ifndef HAVE_FWRITE_UNLOCKED
90 , NULL
91#endif
92};
93
94/** The standard error stream. */
95static RTSTREAM g_StdErr =
96{
97 RTSTREAM_MAGIC,
98 0,
99 stderr
100#ifndef HAVE_FWRITE_UNLOCKED
101 , NULL
102#endif
103};
104
105/** The standard output stream. */
106static RTSTREAM g_StdOut =
107{
108 RTSTREAM_MAGIC,
109 0,
110 stdout
111#ifndef HAVE_FWRITE_UNLOCKED
112 , NULL
113#endif
114};
115
116/** Pointer to the standard input stream. */
117RTDATADECL(PRTSTREAM) g_pStdIn = &g_StdIn;
118
119/** Pointer to the standard output stream. */
120RTDATADECL(PRTSTREAM) g_pStdErr = &g_StdErr;
121
122/** Pointer to the standard output stream. */
123RTDATADECL(PRTSTREAM) g_pStdOut = &g_StdOut;
124
125
126#ifndef HAVE_FWRITE_UNLOCKED
127/**
128 * Allocates and acquires the lock for the stream.
129 *
130 * @returns IPRT status.
131 * @param pStream The stream (valid).
132 */
133static int rtStrmAllocLock(PRTSTREAM pStream)
134{
135 Assert(pStream->pCritSect == NULL);
136
137 PRTCRITSECT pCritSect = (PRTCRITSECT)RTMemAlloc(sizeof(*pCritSect));
138 if (!pCritSect)
139 return VERR_NO_MEMORY;
140
141 /* The native stream lock are normally not recursive. */
142 int rc = RTCritSectInitEx(pCritSect, RTCRITSECT_FLAGS_NO_NESTING,
143 NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, "RTSemSpinMutex");
144 if (RT_SUCCESS(rc))
145 {
146 rc = RTCritSectEnter(pCritSect);
147 if (RT_SUCCESS(rc))
148 {
149 if (RT_LIKELY(ASMAtomicCmpXchgPtr((void * volatile *)&pStream->pCritSect, pCritSect, NULL)))
150 return VINF_SUCCESS;
151
152 RTCritSectLeave(pCritSect);
153 }
154 RTCritSectDelete(pCritSect);
155 }
156 RTMemFree(pCritSect);
157
158 /* Handle the lost race case... */
159 pCritSect = (PRTCRITSECT)ASMAtomicReadPtr((void * volatile *)&pStream->pCritSect);
160 if (pCritSect)
161 return RTCritSectEnter(pCritSect);
162
163 return rc;
164}
165#endif /* !HAVE_FWRITE_UNLOCKED */
166
167
168/**
169 * Locks the stream. May have to allocate the lock as well.
170 *
171 * @param pStream The stream (valid).
172 */
173DECLINLINE(void) rtStrmLock(PRTSTREAM pStream)
174{
175#ifdef HAVE_FWRITE_UNLOCKED
176 flockfile(pStream->pFile);
177#else
178 if (RT_LIKELY(pStream->pCritSect))
179 RTCritSectEnter(pStream->pCritSect);
180 else
181 rtStrmAllocLock(pStream);
182#endif
183}
184
185
186/**
187 * Unlocks the stream.
188 *
189 * @param pStream The stream (valid).
190 */
191DECLINLINE(void) rtStrmUnlock(PRTSTREAM pStream)
192{
193#ifdef HAVE_FWRITE_UNLOCKED
194 funlockfile(pStream->pFile);
195#else
196 if (RT_LIKELY(pStream->pCritSect))
197 RTCritSectLeave(pStream->pCritSect);
198#endif
199}
200
201
202/**
203 * Opens a file stream.
204 *
205 * @returns iprt status code.
206 * @param pszFilename Path to the file to open.
207 * @param pszMode The open mode. See fopen() standard.
208 * Format: <a|r|w>[+][b|t]
209 * @param ppStream Where to store the opened stream.
210 */
211RTR3DECL(int) RTStrmOpen(const char *pszFilename, const char *pszMode, PRTSTREAM *ppStream)
212{
213 /*
214 * Validate input.
215 */
216 if (!pszMode || !*pszMode)
217 {
218 AssertMsgFailed(("No pszMode!\n"));
219 return VERR_INVALID_PARAMETER;
220 }
221 if (!pszFilename)
222 {
223 AssertMsgFailed(("No pszFilename!\n"));
224 return VERR_INVALID_PARAMETER;
225 }
226 bool fOk = true;
227 switch (*pszMode)
228 {
229 case 'a':
230 case 'w':
231 case 'r':
232 switch (pszMode[1])
233 {
234 case '\0':
235 break;
236 case '+':
237 switch (pszMode[2])
238 {
239 case '\0':
240 //case 't':
241 case 'b':
242 break;
243 default:
244 fOk = false;
245 break;
246 }
247 break;
248 //case 't':
249 case 'b':
250 break;
251 default:
252 fOk = false;
253 break;
254 }
255 break;
256 default:
257 fOk = false;
258 break;
259 }
260 if (!fOk)
261 {
262 AssertMsgFailed(("Invalid pszMode='%s', '<a|r|w>[+][b|t]'\n", pszMode));
263 return VINF_SUCCESS;
264 }
265
266 /*
267 * Allocate the stream handle and try open it.
268 */
269 PRTSTREAM pStream = (PRTSTREAM)RTMemAlloc(sizeof(*pStream));
270 if (pStream)
271 {
272 pStream->u32Magic = RTSTREAM_MAGIC;
273 pStream->i32Error = VINF_SUCCESS;
274 pStream->pFile = fopen(pszFilename, pszMode);
275 if (pStream->pFile)
276 {
277 *ppStream = pStream;
278 return VINF_SUCCESS;
279 }
280 return RTErrConvertFromErrno(errno);
281 }
282 return VERR_NO_MEMORY;
283}
284
285
286/**
287 * Opens a file stream.
288 *
289 * @returns iprt status code.
290 * @param pszMode The open mode. See fopen() standard.
291 * Format: <a|r|w>[+][b|t]
292 * @param ppStream Where to store the opened stream.
293 * @param pszFilenameFmt Filename path format string.
294 * @param args Arguments to the format string.
295 */
296RTR3DECL(int) RTStrmOpenFV(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt, va_list args)
297{
298 int rc;
299 char szFilename[RTPATH_MAX];
300 size_t cch = RTStrPrintfV(szFilename, sizeof(szFilename), pszFilenameFmt, args);
301 if (cch < sizeof(szFilename))
302 rc = RTStrmOpen(szFilename, pszMode, ppStream);
303 else
304 {
305 AssertMsgFailed(("The filename is too long cch=%d\n", cch));
306 rc = VERR_FILENAME_TOO_LONG;
307 }
308 return rc;
309}
310
311
312/**
313 * Opens a file stream.
314 *
315 * @returns iprt status code.
316 * @param pszMode The open mode. See fopen() standard.
317 * Format: <a|r|w>[+][b|t]
318 * @param ppStream Where to store the opened stream.
319 * @param pszFilenameFmt Filename path format string.
320 * @param ... Arguments to the format string.
321 */
322RTR3DECL(int) RTStrmOpenF(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt, ...)
323{
324 va_list args;
325 va_start(args, pszFilenameFmt);
326 int rc = RTStrmOpenFV(pszMode, ppStream, pszFilenameFmt, args);
327 va_end(args);
328 return rc;
329}
330
331
332/**
333 * Closes the specified stream.
334 *
335 * @returns iprt status code.
336 * @param pStream The stream to close.
337 */
338RTR3DECL(int) RTStrmClose(PRTSTREAM pStream)
339{
340 if (pStream && pStream->u32Magic == RTSTREAM_MAGIC)
341 {
342 if (!fclose(pStream->pFile))
343 {
344 pStream->u32Magic = 0xdeaddead;
345 pStream->pFile = NULL;
346#ifndef HAVE_FWRITE_UNLOCKED
347 if (pStream->pCritSect)
348 {
349 RTCritSectEnter(pStream->pCritSect);
350 RTCritSectLeave(pStream->pCritSect);
351 RTCritSectDelete(pStream->pCritSect);
352 RTMemFree(pStream->pCritSect);
353 pStream->pCritSect = NULL;
354 }
355#endif
356 RTMemFree(pStream);
357 return VINF_SUCCESS;
358 }
359
360 return RTErrConvertFromErrno(errno);
361 }
362 else
363 {
364 AssertMsgFailed(("Invalid stream!\n"));
365 return VERR_INVALID_PARAMETER;
366 }
367}
368
369
370/**
371 * Get the pending error of the stream.
372 *
373 * @returns iprt status code. of the stream.
374 * @param pStream The stream.
375 */
376RTR3DECL(int) RTStrmError(PRTSTREAM pStream)
377{
378 int rc;
379 if (pStream && pStream->u32Magic == RTSTREAM_MAGIC)
380 rc = pStream->i32Error;
381 else
382 {
383 AssertMsgFailed(("Invalid stream!\n"));
384 rc = VERR_INVALID_PARAMETER;
385 }
386 return rc;
387}
388
389
390/**
391 * Clears stream error condition.
392 *
393 * All stream operations save RTStrmClose and this will fail
394 * while an error is asserted on the stream
395 *
396 * @returns iprt status code.
397 * @param pStream The stream.
398 */
399RTR3DECL(int) RTStrmClearError(PRTSTREAM pStream)
400{
401 int rc;
402 if (pStream && pStream->u32Magic == RTSTREAM_MAGIC)
403 {
404 clearerr(pStream->pFile);
405 ASMAtomicXchgS32(&pStream->i32Error, VINF_SUCCESS);
406 rc = VINF_SUCCESS;
407 }
408 else
409 {
410 AssertMsgFailed(("Invalid stream!\n"));
411 rc = VERR_INVALID_PARAMETER;
412 }
413 return rc;
414}
415
416
417/**
418 * Rewinds the stream.
419 *
420 * Stream errors will be reset on success.
421 *
422 * @returns IPRT status code.
423 *
424 * @param pStream The stream.
425 *
426 * @remarks Not all streams are rewindable and that behavior is currently
427 * undefined for those.
428 */
429RTR3DECL(int) RTStrmRewind(PRTSTREAM pStream)
430{
431 AssertPtrReturn(pStream, VERR_INVALID_HANDLE);
432 AssertReturn(pStream->u32Magic == RTSTREAM_MAGIC, VERR_INVALID_HANDLE);
433
434 int rc;
435 clearerr(pStream->pFile);
436 errno = 0;
437 if (!fseek(pStream->pFile, 0, SEEK_SET))
438 {
439 ASMAtomicXchgS32(&pStream->i32Error, VINF_SUCCESS);
440 rc = VINF_SUCCESS;
441 }
442 else
443 {
444 rc = RTErrConvertFromErrno(errno);
445 ASMAtomicXchgS32(&pStream->i32Error, rc);
446 }
447
448 return rc;
449}
450
451
452/**
453 * Reads from a file stream.
454 *
455 * @returns iprt status code.
456 * @param pStream The stream.
457 * @param pvBuf Where to put the read bits.
458 * Must be cbRead bytes or more.
459 * @param cbRead Number of bytes to read.
460 * @param pcbRead Where to store the number of bytes actually read.
461 * If NULL cbRead bytes are read or an error is returned.
462 */
463RTR3DECL(int) RTStrmReadEx(PRTSTREAM pStream, void *pvBuf, size_t cbRead, size_t *pcbRead)
464{
465 int rc;
466 if (pStream && pStream->u32Magic == RTSTREAM_MAGIC)
467 {
468 rc = pStream->i32Error;
469 if (RT_SUCCESS(rc))
470 {
471 if (pcbRead)
472 {
473 /*
474 * Can do with a partial read.
475 */
476 *pcbRead = fread(pvBuf, 1, cbRead, pStream->pFile);
477 if ( *pcbRead == cbRead
478 || !ferror(pStream->pFile))
479 return VINF_SUCCESS;
480 if (feof(pStream->pFile))
481 {
482 if (*pcbRead)
483 return VINF_EOF;
484 rc = VERR_EOF;
485 }
486 else if (ferror(pStream->pFile))
487 rc = VERR_READ_ERROR;
488 else
489 {
490 AssertMsgFailed(("This shouldn't happen\n"));
491 rc = VERR_INTERNAL_ERROR;
492 }
493 }
494 else
495 {
496 /*
497 * Must read it all!
498 */
499 if (fread(pvBuf, cbRead, 1, pStream->pFile) == 1)
500 return VINF_SUCCESS;
501
502 /* possible error/eof. */
503 if (feof(pStream->pFile))
504 rc = VERR_EOF;
505 else if (ferror(pStream->pFile))
506 rc = VERR_READ_ERROR;
507 else
508 {
509 AssertMsgFailed(("This shouldn't happen\n"));
510 rc = VERR_INTERNAL_ERROR;
511 }
512 }
513 ASMAtomicXchgS32(&pStream->i32Error, rc);
514 }
515 }
516 else
517 {
518 AssertMsgFailed(("Invalid stream!\n"));
519 rc = VERR_INVALID_PARAMETER;
520 }
521 return rc;
522}
523
524
525/**
526 * Writes to a file stream.
527 *
528 * @returns iprt status code.
529 * @param pStream The stream.
530 * @param pvBuf Where to get the bits to write from.
531 * @param cbWrite Number of bytes to write.
532 * @param pcbWritten Where to store the number of bytes actually written.
533 * If NULL cbWrite bytes are written or an error is returned.
534 */
535RTR3DECL(int) RTStrmWriteEx(PRTSTREAM pStream, const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
536{
537 int rc;
538 if (pStream && pStream->u32Magic == RTSTREAM_MAGIC)
539 {
540 rc = pStream->i32Error;
541 if (RT_SUCCESS(rc))
542 {
543 if (pcbWritten)
544 {
545 IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc / mempcpy again */
546 *pcbWritten = fwrite(pvBuf, 1, cbWrite, pStream->pFile);
547 IPRT_ALIGNMENT_CHECKS_ENABLE();
548 if ( *pcbWritten == cbWrite
549 || !ferror(pStream->pFile))
550 return VINF_SUCCESS;
551 rc = VERR_WRITE_ERROR;
552 }
553 else
554 {
555 /*
556 * Must write it all!
557 */
558 IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc / mempcpy again */
559 size_t cbWritten = fwrite(pvBuf, cbWrite, 1, pStream->pFile);
560 IPRT_ALIGNMENT_CHECKS_ENABLE();
561 if (cbWritten == 1)
562 return VINF_SUCCESS;
563 if (!ferror(pStream->pFile))
564 return VINF_SUCCESS; /* WEIRD! But anyway... */
565
566 rc = VERR_WRITE_ERROR;
567 }
568 ASMAtomicXchgS32(&pStream->i32Error, rc);
569 }
570 }
571 else
572 {
573 AssertMsgFailed(("Invalid stream!\n"));
574 rc = VERR_INVALID_PARAMETER;
575 }
576 return rc;
577}
578
579
580/**
581 * Reads a character from a file stream.
582 *
583 * @returns The char as an unsigned char cast to int.
584 * @returns -1 on failure.
585 * @param pStream The stream.
586 */
587RTR3DECL(int) RTStrmGetCh(PRTSTREAM pStream)
588{
589 unsigned char ch;
590 int rc = RTStrmReadEx(pStream, &ch, 1, NULL);
591 if (RT_SUCCESS(rc))
592 return ch;
593 return -1;
594}
595
596
597/**
598 * Writes a character to a file stream.
599 *
600 * @returns iprt status code.
601 * @param pStream The stream.
602 * @param ch The char to write.
603 */
604RTR3DECL(int) RTStrmPutCh(PRTSTREAM pStream, int ch)
605{
606 return RTStrmWriteEx(pStream, &ch, 1, NULL);
607}
608
609
610/**
611 * Writes a string to a file stream.
612 *
613 * @returns iprt status code.
614 * @param pStream The stream.
615 * @param pszString The string to write.
616 * No newlines or anything is appended or prepended.
617 * The terminating '\\0' is not written, of course.
618 */
619RTR3DECL(int) RTStrmPutStr(PRTSTREAM pStream, const char *pszString)
620{
621 size_t cch = strlen(pszString);
622 return RTStrmWriteEx(pStream, pszString, cch, NULL);
623}
624
625
626/**
627 * Reads a line from a file stream.
628 * A line ends with a '\\n', '\\0' or the end of the file.
629 *
630 * @returns iprt status code.
631 * @returns VINF_BUFFER_OVERFLOW if the buffer wasn't big enough to read an entire line.
632 * @param pStream The stream.
633 * @param pszString Where to store the line.
634 * The line will *NOT* contain any '\\n'.
635 * @param cchString The size of the string buffer.
636 */
637RTR3DECL(int) RTStrmGetLine(PRTSTREAM pStream, char *pszString, size_t cchString)
638{
639 int rc;
640 if (pStream && pStream->u32Magic == RTSTREAM_MAGIC)
641 {
642 if (pszString && cchString > 1)
643 {
644 rc = pStream->i32Error;
645 if (RT_SUCCESS(rc))
646 {
647 cchString--; /* save space for the terminator. */
648 rtStrmLock(pStream);
649 for (;;)
650 {
651#ifdef HAVE_FWRITE_UNLOCKED /** @todo darwin + freebsd(?) has fgetc_unlocked but not fwrite_unlocked, optimize... */
652 int ch = fgetc_unlocked(pStream->pFile);
653#else
654 int ch = fgetc(pStream->pFile);
655#endif
656 if (ch == EOF)
657 {
658#ifdef HAVE_FWRITE_UNLOCKED
659 if (feof_unlocked(pStream->pFile))
660#else
661 if (feof(pStream->pFile))
662#endif
663 {
664 rc = VERR_EOF;
665 break;
666 }
667#ifdef HAVE_FWRITE_UNLOCKED
668 if (ferror_unlocked(pStream->pFile))
669#else
670 if (ferror(pStream->pFile))
671#endif
672 rc = VERR_READ_ERROR;
673 else
674 {
675 AssertMsgFailed(("This shouldn't happen\n"));
676 rc = VERR_INTERNAL_ERROR;
677 }
678 break;
679 }
680 if (ch == '\0' || ch == '\n' || ch == '\r')
681 break;
682 *pszString++ = ch;
683 if (--cchString <= 0)
684 {
685 rc = VINF_BUFFER_OVERFLOW;
686 break;
687 }
688 }
689 rtStrmUnlock(pStream);
690
691 *pszString = '\0';
692 if (RT_FAILURE(rc))
693 ASMAtomicXchgS32(&pStream->i32Error, rc);
694 }
695 }
696 else
697 {
698 AssertMsgFailed(("no buffer or too small buffer!\n"));
699 rc = VERR_INVALID_PARAMETER;
700 }
701 }
702 else
703 {
704 AssertMsgFailed(("Invalid stream!\n"));
705 rc = VERR_INVALID_PARAMETER;
706 }
707 return rc;
708}
709
710
711/**
712 * Flushes a stream.
713 *
714 * @returns iprt status code.
715 * @param pStream The stream to flush.
716 */
717RTR3DECL(int) RTStrmFlush(PRTSTREAM pStream)
718{
719 if (!fflush(pStream->pFile))
720 return VINF_SUCCESS;
721 return RTErrConvertFromErrno(errno);
722}
723
724
725/**
726 * Output callback.
727 *
728 * @returns number of bytes written.
729 * @param pvArg User argument.
730 * @param pachChars Pointer to an array of utf-8 characters.
731 * @param cchChars Number of bytes in the character array pointed to by pachChars.
732 */
733static DECLCALLBACK(size_t) rtstrmOutput(void *pvArg, const char *pachChars, size_t cchChars)
734{
735 if (cchChars)
736 {
737 PRTSTREAM pStream = (PRTSTREAM)pvArg;
738 int rc = pStream->i32Error;
739 if (RT_SUCCESS(rc))
740 {
741 IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc / mempcpy again */
742#ifdef HAVE_FWRITE_UNLOCKED
743 if (fwrite_unlocked(pachChars, cchChars, 1, pStream->pFile) != 1)
744#else
745 if (fwrite(pachChars, cchChars, 1, pStream->pFile) != 1)
746#endif
747 ASMAtomicXchgS32(&pStream->i32Error, VERR_WRITE_ERROR);
748 IPRT_ALIGNMENT_CHECKS_ENABLE();
749 }
750 }
751 /* else: ignore termination call. */
752 return cchChars;
753}
754
755
756/**
757 * Prints a formatted string to the specified stream.
758 *
759 * @returns Number of bytes printed.
760 * @param pStream The stream to print to.
761 * @param pszFormat IPRT format string.
762 * @param args Arguments specified by pszFormat.
763 */
764RTR3DECL(int) RTStrmPrintfV(PRTSTREAM pStream, const char *pszFormat, va_list args)
765{
766 int rc;
767 if (pStream && pStream->u32Magic == RTSTREAM_MAGIC)
768 {
769 rc = pStream->i32Error;
770 if (RT_SUCCESS(rc))
771 {
772 rtStrmLock(pStream);
773 rc = (int)RTStrFormatV(rtstrmOutput, pStream, NULL, NULL, pszFormat, args);
774 rtStrmUnlock(pStream);
775 Assert(rc >= 0);
776 }
777 else
778 rc = -1;
779 }
780 else
781 {
782 AssertMsgFailed(("Invalid stream!\n"));
783 rc = -1;
784 }
785 return rc;
786}
787
788
789/**
790 * Prints a formatted string to the specified stream.
791 *
792 * @returns Number of bytes printed.
793 * @param pStream The stream to print to.
794 * @param pszFormat IPRT format string.
795 * @param ... Arguments specified by pszFormat.
796 */
797RTR3DECL(int) RTStrmPrintf(PRTSTREAM pStream, const char *pszFormat, ...)
798{
799 va_list args;
800 va_start(args, pszFormat);
801 int rc = RTStrmPrintfV(pStream, pszFormat, args);
802 va_end(args);
803 return rc;
804}
805
806
807/**
808 * Prints a formatted string to the standard output stream (g_pStdOut).
809 *
810 * @returns Number of bytes printed.
811 * @param pszFormat IPRT format string.
812 * @param args Arguments specified by pszFormat.
813 */
814RTR3DECL(int) RTPrintfV(const char *pszFormat, va_list args)
815{
816 return RTStrmPrintfV(g_pStdOut, pszFormat, args);
817}
818
819
820/**
821 * Prints a formatted string to the standard output stream (g_pStdOut).
822 *
823 * @returns Number of bytes printed.
824 * @param pszFormat IPRT format string.
825 * @param ... Arguments specified by pszFormat.
826 */
827RTR3DECL(int) RTPrintf(const char *pszFormat, ...)
828{
829 va_list args;
830 va_start(args, pszFormat);
831 int rc = RTStrmPrintfV(g_pStdOut, pszFormat, args);
832 va_end(args);
833 return rc;
834}
835
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette