VirtualBox

Changeset 41504 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
May 30, 2012 6:40:48 PM (13 years ago)
Author:
vboxsync
Message:

RTStrmGetLine: Deal with correctly with \r\n, current handling is stupid as it returns two lines.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/stream.cpp

    r40304 r41504  
    922922
    923923
    924 /**
    925  * Reads a line from a file stream.
    926  * A line ends with a '\\n', '\\0' or the end of the file.
    927  *
    928  * @returns iprt status code.
    929  * @returns VINF_BUFFER_OVERFLOW if the buffer wasn't big enough to read an entire line.
    930  * @param   pStream         The stream.
    931  * @param   pszString       Where to store the line.
    932  *                          The line will *NOT* contain any '\\n'.
    933  * @param   cchString       The size of the string buffer.
    934  */
    935 RTR3DECL(int) RTStrmGetLine(PRTSTREAM pStream, char *pszString, size_t cchString)
     924RTR3DECL(int) RTStrmGetLine(PRTSTREAM pStream, char *pszString, size_t cbString)
    936925{
    937926    AssertReturn(RT_VALID_PTR(pStream) && pStream->u32Magic == RTSTREAM_MAGIC, VERR_INVALID_PARAMETER);
    938927    int rc;
    939     if (pszString && cchString > 1)
     928    if (pszString && cbString > 1)
    940929    {
    941930        rc = pStream->i32Error;
    942931        if (RT_SUCCESS(rc))
    943932        {
    944             cchString--;            /* save space for the terminator. */
     933            cbString--;            /* save space for the terminator. */
    945934            rtStrmLock(pStream);
    946935            for (;;)
     
    951940                int ch = fgetc(pStream->pFile);
    952941#endif
     942
     943                /* Deal with \r\n sequences here. We'll return lone CR, but
     944                   treat CRLF as LF. */
     945                if (ch == '\r')
     946                {
     947#ifdef HAVE_FWRITE_UNLOCKED /** @todo darwin + freebsd(?) has fgetc_unlocked but not fwrite_unlocked, optimize... */
     948                    ch = fgetc_unlocked(pStream->pFile);
     949#else
     950                    ch = fgetc(pStream->pFile);
     951#endif
     952                    if (ch == '\n')
     953                        break;
     954
     955                    *pszString++ = '\r';
     956                    if (--cbString <= 0)
     957                    {
     958                        /* yeah, this is an error, we dropped a character. */
     959                        rc = VERR_BUFFER_OVERFLOW;
     960                        break;
     961                    }
     962                }
     963
     964                /* Deal with end of file. */
    953965                if (ch == EOF)
    954966                {
     
    975987                    break;
    976988                }
    977                 if (ch == '\0' || ch == '\n' || ch == '\r')
     989
     990                /* Deal with null terminator and (lone) new line. */
     991                if (ch == '\0' || ch == '\n')
    978992                    break;
     993
     994                /* No special character, append it to the return string. */
    979995                *pszString++ = ch;
    980                 if (--cchString <= 0)
     996                if (--cbString <= 0)
    981997                {
    982998                    rc = VINF_BUFFER_OVERFLOW;
Note: See TracChangeset for help on using the changeset viewer.

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