LCOV - code coverage report
Current view: directory - objdir/dist/include - nsReadLine.h (source / functions) Found Hit Coverage
Test: app.info Lines: 37 36 97.3 %
Date: 2012-06-02 Functions: 4 4 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
       2                 :  *
       3                 :  * ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is mozilla.org code.
      17                 :  *
      18                 :  * The Initial Developer of the Original Code is
      19                 :  * Boris Zbarsky <bzbarsky@mit.edu>.
      20                 :  * Portions created by the Initial Developer are Copyright (C) 2001
      21                 :  * the Initial Developer. All Rights Reserved.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *   Christian Biesinger <cbiesinger@web.de>
      25                 :  *   Mats Palmgren <mats.palmgren@bredband.net>
      26                 :  *
      27                 :  * Alternatively, the contents of this file may be used under the terms of
      28                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      29                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      30                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      31                 :  * of those above. If you wish to allow use of your version of this file only
      32                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      33                 :  * use your version of this file under the terms of the MPL, indicate your
      34                 :  * decision by deleting the provisions above and replace them with the notice
      35                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      36                 :  * the provisions above, a recipient may use your version of this file under
      37                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      38                 :  *
      39                 :  * ***** END LICENSE BLOCK ***** */
      40                 : 
      41                 : #ifndef nsReadLine_h__
      42                 : #define nsReadLine_h__
      43                 : 
      44                 : #include "prmem.h"
      45                 : #include "nsIInputStream.h"
      46                 : 
      47                 : /**
      48                 :  * @file
      49                 :  * Functions to read complete lines from an input stream.
      50                 :  *
      51                 :  * To properly use the helper function in here (NS_ReadLine) the caller
      52                 :  * needs to declare a pointer to an nsLineBuffer, call
      53                 :  * NS_InitLineBuffer on it, and pass it to NS_ReadLine every time it
      54                 :  * wants a line out.
      55                 :  *
      56                 :  * When done, the pointer should be freed using PR_Free.
      57                 :  */
      58                 : 
      59                 : /**
      60                 :  * @internal
      61                 :  * Buffer size. This many bytes will be buffered. If a line is longer than this,
      62                 :  * the partial line will be appended to the out parameter of NS_ReadLine and the
      63                 :  * buffer will be emptied.
      64                 :  * Note: if you change this constant, please update the regression test in
      65                 :  * netwerk/test/unit/test_readline.js accordingly (bug 397850).
      66                 :  */
      67                 : #define kLineBufferSize 4096
      68                 : 
      69                 : /**
      70                 :  * @internal
      71                 :  * Line buffer structure, buffers data from an input stream.
      72                 :  * The buffer is empty when |start| == |end|.
      73                 :  * Invariant: |start| <= |end|
      74                 :  */
      75                 : template<typename CharT>
      76                 : class nsLineBuffer {
      77                 :   public:
      78                 :   CharT buf[kLineBufferSize+1];
      79                 :   CharT* start;
      80                 :   CharT* end;
      81                 : };
      82                 : 
      83                 : /**
      84                 :  * Initialize a line buffer for use with NS_ReadLine.
      85                 :  *
      86                 :  * @param aBufferPtr
      87                 :  *        Pointer to pointer to a line buffer. Upon successful return,
      88                 :  *        *aBufferPtr will contain a valid pointer to a line buffer, for use
      89                 :  *        with NS_ReadLine. Use PR_Free when the buffer is no longer needed.
      90                 :  *
      91                 :  * @retval NS_OK Success.
      92                 :  * @retval NS_ERROR_OUT_OF_MEMORY Not enough memory to allocate the line buffer.
      93                 :  *
      94                 :  * @par Example:
      95                 :  * @code
      96                 :  *    nsLineBuffer* lb;
      97                 :  *    rv = NS_InitLineBuffer(&lb);
      98                 :  *    if (NS_SUCCEEDED(rv)) {
      99                 :  *      // do stuff...
     100                 :  *      PR_Free(lb);
     101                 :  *    }
     102                 :  * @endcode
     103                 :  */
     104                 : template<typename CharT>
     105                 : nsresult
     106             919 : NS_InitLineBuffer (nsLineBuffer<CharT> ** aBufferPtr) {
     107             919 :   *aBufferPtr = PR_NEW(nsLineBuffer<CharT>);
     108             919 :   if (!(*aBufferPtr))
     109               0 :     return NS_ERROR_OUT_OF_MEMORY;
     110                 : 
     111             919 :   (*aBufferPtr)->start = (*aBufferPtr)->end = (*aBufferPtr)->buf;
     112             919 :   return NS_OK;
     113                 : }
     114                 : 
     115                 : /**
     116                 :  * Read a line from an input stream. Lines are separated by '\r' (0x0D) or '\n'
     117                 :  * (0x0A), or "\r\n" or "\n\r".
     118                 :  *
     119                 :  * @param aStream
     120                 :  *        The stream to read from
     121                 :  * @param aBuffer
     122                 :  *        The line buffer to use. Must have been inited with
     123                 :  *        NS_InitLineBuffer before. A single line buffer must not be used with
     124                 :  *        different input streams.
     125                 :  * @param aLine [out]
     126                 :  *        The string where the line will be stored.
     127                 :  * @param more [out]
     128                 :  *        Whether more data is available in the buffer. If true, NS_ReadLine may
     129                 :  *        be called again to read further lines. Otherwise, further calls to
     130                 :  *        NS_ReadLine will return an error.
     131                 :  *
     132                 :  * @retval NS_OK
     133                 :  *         Read successful
     134                 :  * @retval error
     135                 :  *         Input stream returned an error upon read. See
     136                 :  *         nsIInputStream::read.
     137                 :  */
     138                 : template<typename CharT, class StreamType, class StringType>
     139                 : nsresult
     140           56652 : NS_ReadLine (StreamType* aStream, nsLineBuffer<CharT> * aBuffer,
     141                 :              StringType & aLine, bool *more)
     142                 : {
     143           56652 :   CharT eolchar = 0; // the first eol char or 1 after \r\n or \n\r is found
     144                 : 
     145           56652 :   aLine.Truncate();
     146                 : 
     147            1365 :   while (1) { // will be returning out of this loop on eol or eof
     148           58017 :     if (aBuffer->start == aBuffer->end) { // buffer is empty.  Read into it.
     149                 :       PRUint32 bytesRead;
     150            2352 :       nsresult rv = aStream->Read(aBuffer->buf, kLineBufferSize, &bytesRead);
     151            2352 :       if (NS_FAILED(rv) || NS_UNLIKELY(bytesRead == 0)) {
     152             742 :         *more = false;
     153             742 :         return rv;
     154                 :       }
     155            1610 :       aBuffer->start = aBuffer->buf;
     156            1610 :       aBuffer->end = aBuffer->buf + bytesRead;
     157            1610 :       *(aBuffer->end) = '\0';
     158                 :     }
     159                 : 
     160                 :     /*
     161                 :      * Walk the buffer looking for an end-of-line.
     162                 :      * There are 3 cases to consider:
     163                 :      *  1. the eol char is the last char in the buffer
     164                 :      *  2. the eol char + one more char at the end of the buffer
     165                 :      *  3. the eol char + two or more chars at the end of the buffer
     166                 :      * we need at least one char after the first eol char to determine if
     167                 :      * it's a \r\n or \n\r sequence (and skip over it), and we need one
     168                 :      * more char after the end-of-line to set |more| correctly.
     169                 :      */
     170           57275 :     CharT* current = aBuffer->start;
     171           57275 :     if (NS_LIKELY(eolchar == 0)) {
     172         1902046 :       for ( ; current < aBuffer->end; ++current) {
     173         1901114 :         if (*current == '\n' || *current == '\r') {
     174           56325 :           eolchar = *current;
     175           56325 :           *current++ = '\0';
     176           56325 :           aLine.Append(aBuffer->start);
     177           56325 :           break;
     178                 :         }
     179                 :       }
     180                 :     }
     181           57275 :     if (NS_LIKELY(eolchar != 0)) {
     182          113948 :       for ( ; current < aBuffer->end; ++current) {
     183           56541 :         if ((eolchar == '\r' && *current == '\n') ||
     184                 :             (eolchar == '\n' && *current == '\r')) {
     185             631 :           eolchar = 1;
     186             631 :           continue;
     187                 :         }
     188           55910 :         aBuffer->start = current;
     189           55910 :         *more = true;
     190           55910 :         return NS_OK;
     191                 :       }
     192                 :     }
     193                 : 
     194            1365 :     if (eolchar == 0)
     195             932 :       aLine.Append(aBuffer->start);
     196            1365 :     aBuffer->start = aBuffer->end; // mark the buffer empty
     197                 :   }
     198                 : }
     199                 : 
     200                 : #endif // nsReadLine_h__

Generated by: LCOV version 1.7