"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/astyle/ASStreamIterator.cpp" (12 May 2020, 3195 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 
    2 #include "astyle/ASStreamIterator.h"
    3 
    4 namespace astyle
    5 {
    6 
    7 
    8 ASStreamIterator::ASStreamIterator(istream *in)
    9 {
   10     inStream = in;
   11     buffer.reserve(200);
   12     eolWindows = eolLinux = eolMacOld = 0;
   13     peekStart = 0;
   14     prevLineDeleted = false;
   15     checkForEmptyLine = false;
   16 }
   17 
   18 
   19 ASStreamIterator::~ASStreamIterator()
   20 {
   21 }
   22 
   23 // save the last input line after input has reached EOF
   24 
   25 void ASStreamIterator::saveLastInputLine()
   26 {
   27     assert(inStream->eof());
   28     prevBuffer = buffer;
   29 }
   30 
   31 /**
   32  * read the input stream, delete any end of line characters,
   33  *     and build a string that contains the input line.
   34  *
   35  * @return        string containing the next input line minus any end of line characters
   36  */
   37 //template<typename T>
   38 string ASStreamIterator::nextLine(bool emptyLineWasDeleted)
   39 {
   40     // verify that the current position is correct
   41     assert (peekStart == 0);
   42 
   43     // a deleted line may be replaced if break-blocks is requested
   44     // this sets up the compare to check for a replaced empty line
   45     if (prevLineDeleted)
   46     {
   47         prevLineDeleted = false;
   48         checkForEmptyLine = true;
   49     }
   50     if (!emptyLineWasDeleted)
   51         prevBuffer = buffer;
   52     else
   53         prevLineDeleted = true;
   54 
   55     // read the next record
   56     buffer.clear();
   57     char ch;
   58     inStream->get(ch);
   59 
   60     while (!inStream->eof() && ch != '\n' && ch != '\r')
   61     {
   62         buffer.append(1, ch);
   63         inStream->get(ch);
   64     }
   65 
   66     if (inStream->eof())
   67     {
   68         return buffer;
   69     }
   70 
   71     int peekCh = inStream->peek();
   72 
   73     // find input end-of-line characters
   74     if (!inStream->eof())
   75     {
   76         if (ch == '\r')         // CR+LF is windows otherwise Mac OS 9
   77         {
   78             if (peekCh == '\n')
   79             {
   80                 inStream->get(ch);
   81                 eolWindows++;
   82             }
   83             else
   84                 eolMacOld++;
   85         }
   86         else                    // LF is Linux, allow for improbable LF/CR
   87         {
   88             if (peekCh == '\r')
   89             {
   90                 inStream->get(ch);
   91                 eolWindows++;
   92             }
   93             else
   94                 eolLinux++;
   95         }
   96     }
   97     else
   98     {
   99         inStream->clear();
  100     }
  101 
  102     // set output end of line characters
  103     if (eolWindows >= eolLinux)
  104     {
  105         if (eolWindows >= eolMacOld)
  106             strcpy(outputEOL, "\r\n");  // Windows (CR+LF)
  107         else
  108             strcpy(outputEOL, "\r");    // MacOld (CR)
  109     }
  110     else if (eolLinux >= eolMacOld)
  111         strcpy(outputEOL, "\n");    // Linux (LF)
  112     else
  113         strcpy(outputEOL, "\r");    // MacOld (CR)
  114 
  115     return buffer;
  116 }
  117 
  118 // save the current position and get the next line
  119 // this can be called for multiple reads
  120 // when finished peeking you MUST call peekReset()
  121 // call this function from ASFormatter ONLY
  122 //template<typename T>
  123 string ASStreamIterator::peekNextLine()
  124 {
  125     assert (hasMoreLines());
  126     string nextLine;
  127     char ch;
  128 
  129     if (peekStart == 0)
  130         peekStart = inStream->tellg();
  131 
  132     // read the next record
  133     inStream->get(ch);
  134     while (!inStream->eof() && ch != '\n' && ch != '\r')
  135     {
  136         nextLine.append(1, ch);
  137         inStream->get(ch);
  138     }
  139 
  140     if (inStream->eof())
  141     {
  142         return nextLine;
  143     }
  144 
  145     int peekCh = inStream->peek();
  146 
  147     // remove end-of-line characters
  148     if (!inStream->eof())
  149     {
  150         if ((peekCh == '\n' || peekCh == '\r') && peekCh != ch)  /////////////  changed  //////////
  151             inStream->get(ch);
  152     }
  153 
  154     return nextLine;
  155 }
  156 
  157 // reset current position and EOF for peekNextLine()
  158 //template<typename T>
  159 void ASStreamIterator::peekReset()
  160 {
  161     assert(peekStart != 0);
  162     inStream->clear();
  163     inStream->seekg(peekStart);
  164     peekStart = 0;
  165 }
  166 
  167 }