stream.cpp revision a969bd45d3108775bc2bb2a077f4f95608f9dd98
/* $Id$ */
/** @file
* IPRT - I/O Stream.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/critsect.h>
#include "internal/alignmentchecks.h"
#include <stdio.h>
#include <errno.h>
#if defined(RT_OS_LINUX) /* PORTME: check for the _unlocked functions in stdio.h */
#define HAVE_FWRITE_UNLOCKED
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* File stream.
*/
typedef struct RTSTREAM
{
/** Magic value used to validate the stream. (RTSTREAM_MAGIC) */
/** File stream error. */
/** Pointer to the LIBC file stream. */
#ifndef HAVE_FWRITE_UNLOCKED
/** Critical section for serializing access to the stream. */
#endif
} RTSTREAM;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The standard input stream. */
{
0,
#ifndef HAVE_FWRITE_UNLOCKED
, NULL
#endif
};
/** The standard error stream. */
{
0,
#ifndef HAVE_FWRITE_UNLOCKED
, NULL
#endif
};
/** The standard output stream. */
{
0,
#ifndef HAVE_FWRITE_UNLOCKED
, NULL
#endif
};
/** Pointer to the standard input stream. */
/** Pointer to the standard output stream. */
/** Pointer to the standard output stream. */
#ifndef HAVE_FWRITE_UNLOCKED
/**
* Allocates and acquires the lock for the stream.
*
* @returns IPRT status.
* @param pStream The stream (valid).
*/
{
if (!pCritSect)
return VERR_NO_MEMORY;
/* The native stream lock are normally not recursive. */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
/* Handle the lost race case... */
if (pCritSect)
return RTCritSectEnter(pCritSect);
return rc;
}
#endif /* !HAVE_FWRITE_UNLOCKED */
/**
* Locks the stream. May have to allocate the lock as well.
*
* @param pStream The stream (valid).
*/
{
#ifdef HAVE_FWRITE_UNLOCKED
#else
else
#endif
}
/**
* Unlocks the stream.
*
* @param pStream The stream (valid).
*/
{
#ifdef HAVE_FWRITE_UNLOCKED
#else
#endif
}
/**
* Opens a file stream.
*
* @returns iprt status code.
* @param pszFilename Path to the file to open.
* @param pszMode The open mode. See fopen() standard.
* Format: <a|r|w>[+][b|t]
* @param ppStream Where to store the opened stream.
*/
{
/*
* Validate input.
*/
{
AssertMsgFailed(("No pszMode!\n"));
return VERR_INVALID_PARAMETER;
}
if (!pszFilename)
{
AssertMsgFailed(("No pszFilename!\n"));
return VERR_INVALID_PARAMETER;
}
bool fOk = true;
switch (*pszMode)
{
case 'a':
case 'w':
case 'r':
switch (pszMode[1])
{
case '\0':
break;
case '+':
switch (pszMode[2])
{
case '\0':
//case 't':
case 'b':
break;
default:
fOk = false;
break;
}
break;
//case 't':
case 'b':
break;
default:
fOk = false;
break;
}
break;
default:
fOk = false;
break;
}
if (!fOk)
{
return VINF_SUCCESS;
}
/*
* Allocate the stream handle and try open it.
*/
if (pStream)
{
#ifndef HAVE_FWRITE_UNLOCKED
#endif /* HAVE_FWRITE_UNLOCKED */
{
return VINF_SUCCESS;
}
return RTErrConvertFromErrno(errno);
}
return VERR_NO_MEMORY;
}
/**
* Opens a file stream.
*
* @returns iprt status code.
* @param pszMode The open mode. See fopen() standard.
* Format: <a|r|w>[+][b|t]
* @param ppStream Where to store the opened stream.
* @param pszFilenameFmt Filename path format string.
* @param args Arguments to the format string.
*/
RTR3DECL(int) RTStrmOpenFV(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt, va_list args)
{
int rc;
char szFilename[RTPATH_MAX];
if (cch < sizeof(szFilename))
else
{
}
return rc;
}
/**
* Opens a file stream.
*
* @returns iprt status code.
* @param pszMode The open mode. See fopen() standard.
* Format: <a|r|w>[+][b|t]
* @param ppStream Where to store the opened stream.
* @param pszFilenameFmt Filename path format string.
* @param ... Arguments to the format string.
*/
RTR3DECL(int) RTStrmOpenF(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt, ...)
{
return rc;
}
/**
* Closes the specified stream.
*
* @returns iprt status code.
* @param pStream The stream to close.
*/
{
{
{
#ifndef HAVE_FWRITE_UNLOCKED
{
}
#endif
return VINF_SUCCESS;
}
return RTErrConvertFromErrno(errno);
}
else
{
AssertMsgFailed(("Invalid stream!\n"));
return VERR_INVALID_PARAMETER;
}
}
/**
* Get the pending error of the stream.
*
* @returns iprt status code. of the stream.
* @param pStream The stream.
*/
{
int rc;
else
{
AssertMsgFailed(("Invalid stream!\n"));
}
return rc;
}
/**
* Clears stream error condition.
*
* All stream operations save RTStrmClose and this will fail
* while an error is asserted on the stream
*
* @returns iprt status code.
* @param pStream The stream.
*/
{
int rc;
{
rc = VINF_SUCCESS;
}
else
{
AssertMsgFailed(("Invalid stream!\n"));
}
return rc;
}
/**
* Rewinds the stream.
*
* Stream errors will be reset on success.
*
* @returns IPRT status code.
*
* @param pStream The stream.
*
* @remarks Not all streams are rewindable and that behavior is currently
* undefined for those.
*/
{
int rc;
errno = 0;
{
rc = VINF_SUCCESS;
}
else
{
}
return rc;
}
/**
* Reads from a file stream.
*
* @returns iprt status code.
* @param pStream The stream.
* @param pvBuf Where to put the read bits.
* Must be cbRead bytes or more.
* @param cbRead Number of bytes to read.
* @param pcbRead Where to store the number of bytes actually read.
* If NULL cbRead bytes are read or an error is returned.
*/
{
int rc;
{
if (RT_SUCCESS(rc))
{
if (pcbRead)
{
/*
* Can do with a partial read.
*/
return VINF_SUCCESS;
{
if (*pcbRead)
return VINF_EOF;
}
else
{
AssertMsgFailed(("This shouldn't happen\n"));
}
}
else
{
/*
* Must read it all!
*/
return VINF_SUCCESS;
else
{
AssertMsgFailed(("This shouldn't happen\n"));
}
}
}
}
else
{
AssertMsgFailed(("Invalid stream!\n"));
}
return rc;
}
/**
* Writes to a file stream.
*
* @returns iprt status code.
* @param pStream The stream.
* @param pvBuf Where to get the bits to write from.
* @param cbWrite Number of bytes to write.
* @param pcbWritten Where to store the number of bytes actually written.
* If NULL cbWrite bytes are written or an error is returned.
*/
RTR3DECL(int) RTStrmWriteEx(PRTSTREAM pStream, const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
{
int rc;
{
if (RT_SUCCESS(rc))
{
if (pcbWritten)
{
IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc / mempcpy again */
if ( *pcbWritten == cbWrite
return VINF_SUCCESS;
}
else
{
/*
* Must write it all!
*/
IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc / mempcpy again */
if (cbWritten == 1)
return VINF_SUCCESS;
return VINF_SUCCESS; /* WEIRD! But anyway... */
}
}
}
else
{
AssertMsgFailed(("Invalid stream!\n"));
}
return rc;
}
/**
* Reads a character from a file stream.
*
* @returns The char as an unsigned char cast to int.
* @returns -1 on failure.
* @param pStream The stream.
*/
{
unsigned char ch;
if (RT_SUCCESS(rc))
return ch;
return -1;
}
/**
* Writes a character to a file stream.
*
* @returns iprt status code.
* @param pStream The stream.
* @param ch The char to write.
*/
{
}
/**
* Writes a string to a file stream.
*
* @returns iprt status code.
* @param pStream The stream.
* @param pszString The string to write.
* No newlines or anything is appended or prepended.
* The terminating '\\0' is not written, of course.
*/
{
}
/**
* Reads a line from a file stream.
* A line ends with a '\\n', '\\0' or the end of the file.
*
* @returns iprt status code.
* @returns VINF_BUFFER_OVERFLOW if the buffer wasn't big enough to read an entire line.
* @param pStream The stream.
* @param pszString Where to store the line.
* The line will *NOT* contain any '\\n'.
* @param cchString The size of the string buffer.
*/
{
int rc;
{
{
if (RT_SUCCESS(rc))
{
cchString--; /* save space for the terminator. */
for (;;)
{
#ifdef HAVE_FWRITE_UNLOCKED /** @todo darwin + freebsd(?) has fgetc_unlocked but not fwrite_unlocked, optimize... */
#else
#endif
{
#ifdef HAVE_FWRITE_UNLOCKED
#else
#endif
{
break;
}
#ifdef HAVE_FWRITE_UNLOCKED
#else
#endif
else
{
AssertMsgFailed(("This shouldn't happen\n"));
}
break;
}
break;
if (--cchString <= 0)
{
break;
}
}
*pszString = '\0';
if (RT_FAILURE(rc))
}
}
else
{
AssertMsgFailed(("no buffer or too small buffer!\n"));
}
}
else
{
AssertMsgFailed(("Invalid stream!\n"));
}
return rc;
}
/**
* Flushes a stream.
*
* @returns iprt status code.
* @param pStream The stream to flush.
*/
{
return VINF_SUCCESS;
return RTErrConvertFromErrno(errno);
}
/**
* Output callback.
*
* @returns number of bytes written.
* @param pvArg User argument.
* @param pachChars Pointer to an array of utf-8 characters.
* @param cchChars Number of bytes in the character array pointed to by pachChars.
*/
{
if (cchChars)
{
if (RT_SUCCESS(rc))
{
IPRT_ALIGNMENT_CHECKS_DISABLE(); /* glibc / mempcpy again */
#ifdef HAVE_FWRITE_UNLOCKED
#else
#endif
}
}
/* else: ignore termination call. */
return cchChars;
}
/**
* Prints a formatted string to the specified stream.
*
* @returns Number of bytes printed.
* @param pStream The stream to print to.
* @param pszFormat IPRT format string.
* @param args Arguments specified by pszFormat.
*/
{
int rc;
{
if (RT_SUCCESS(rc))
{
}
else
rc = -1;
}
else
{
AssertMsgFailed(("Invalid stream!\n"));
rc = -1;
}
return rc;
}
/**
* Prints a formatted string to the specified stream.
*
* @returns Number of bytes printed.
* @param pStream The stream to print to.
* @param pszFormat IPRT format string.
* @param ... Arguments specified by pszFormat.
*/
{
return rc;
}
/**
* Prints a formatted string to the standard output stream (g_pStdOut).
*
* @returns Number of bytes printed.
* @param pszFormat IPRT format string.
* @param args Arguments specified by pszFormat.
*/
{
}
/**
* Prints a formatted string to the standard output stream (g_pStdOut).
*
* @returns Number of bytes printed.
* @param pszFormat IPRT format string.
* @param ... Arguments specified by pszFormat.
*/
{
return rc;
}