| [12] | 1 | //  crlf_check implementation  ------------------------------------------------// | 
|---|
 | 2 |  | 
|---|
 | 3 | //  Copyright Beman Dawes 2002. | 
|---|
 | 4 | //  Distributed under the Boost Software License, Version 1.0. | 
|---|
 | 5 | //  (See accompanying file LICENSE_1_0.txt or copy at | 
|---|
 | 6 | //  http://www.boost.org/LICENSE_1_0.txt) | 
|---|
 | 7 |  | 
|---|
 | 8 | //  Contributed by Joerg Walter | 
|---|
 | 9 |  | 
|---|
 | 10 | #include "crlf_check.hpp" | 
|---|
 | 11 |  | 
|---|
 | 12 | namespace boost | 
|---|
 | 13 | { | 
|---|
 | 14 |   namespace inspect | 
|---|
 | 15 |   { | 
|---|
 | 16 |    crlf_check::crlf_check() : m_files_with_errors(0) | 
|---|
 | 17 |    { | 
|---|
 | 18 |    } | 
|---|
 | 19 |  | 
|---|
 | 20 |    void crlf_check::inspect( | 
|---|
 | 21 |       const string & library_name, | 
|---|
 | 22 |       const path & full_path,   // example: c:/foo/boost/filesystem/path.hpp | 
|---|
 | 23 |       const string & contents )     // contents of file to be inspected | 
|---|
 | 24 |     { | 
|---|
 | 25 |       bool failed = false; | 
|---|
 | 26 |       // The understanding on line endings, as I remember it, was that | 
|---|
 | 27 |       // either "\n" or "\r\n" is OK, and they can be mixed, but "\r" alone | 
|---|
 | 28 |       // is not acceptable. Mixed line endings are allowed because Boost files | 
|---|
 | 29 |       // are commonly edited in both Windows and UNIX environments, and editors | 
|---|
 | 30 |       // in those environments generally accept either ending. Even Mac people | 
|---|
 | 31 |       // agreed with this policy. --Beman | 
|---|
 | 32 |        | 
|---|
 | 33 |       // Joerg's original implementation is saved below, | 
|---|
 | 34 |       // in case we change our minds! | 
|---|
 | 35 |  | 
|---|
 | 36 |       for ( std::string::const_iterator itr ( contents.begin() ); | 
|---|
 | 37 |         itr != contents.end(); ++itr ) | 
|---|
 | 38 |       { | 
|---|
 | 39 |         if ( *itr == '\r' && ((itr+1) == contents.end() || *(itr+1) != '\n') ) | 
|---|
 | 40 |         { | 
|---|
 | 41 |           failed = true; | 
|---|
 | 42 |           break; | 
|---|
 | 43 |         } | 
|---|
 | 44 |       } | 
|---|
 | 45 |  | 
|---|
 | 46 |       if (failed && full_path.leaf() != "wrong_line_ends_test.cpp") | 
|---|
 | 47 |       { | 
|---|
 | 48 |         ++m_files_with_errors; | 
|---|
 | 49 |         error( library_name, full_path, desc() ); | 
|---|
 | 50 |       } | 
|---|
 | 51 |  | 
|---|
 | 52 |       if (!failed && full_path.leaf() == "wrong_line_ends_test.cpp") | 
|---|
 | 53 |       { | 
|---|
 | 54 |         ++m_files_with_errors; | 
|---|
 | 55 |         error( library_name, full_path, "should have cr-only line endings" ); | 
|---|
 | 56 |       } | 
|---|
 | 57 |  | 
|---|
 | 58 | /* | 
|---|
 | 59 |       size_t cr_count = 0; | 
|---|
 | 60 |       size_t lf_count = 0; | 
|---|
 | 61 |       size_t crlf_count = 0; | 
|---|
 | 62 |       bool had_cr = false; | 
|---|
 | 63 |       for ( size_t i = 0; i < contents.length(); ++i ) | 
|---|
 | 64 |       { | 
|---|
 | 65 |         switch ( contents[i] ) | 
|---|
 | 66 |         { | 
|---|
 | 67 |           case '\r': | 
|---|
 | 68 |             had_cr = true; | 
|---|
 | 69 |             ++cr_count; | 
|---|
 | 70 |             break; | 
|---|
 | 71 |           case '\n': | 
|---|
 | 72 |             ++lf_count; | 
|---|
 | 73 |             if ( had_cr ) | 
|---|
 | 74 |               ++crlf_count; | 
|---|
 | 75 |             // fallthrough | 
|---|
 | 76 |           default: | 
|---|
 | 77 |             had_cr = false; | 
|---|
 | 78 |             break; | 
|---|
 | 79 |         } | 
|---|
 | 80 |       } | 
|---|
 | 81 |       if ( cr_count > 0 && lf_count != crlf_count ) | 
|---|
 | 82 |       { | 
|---|
 | 83 |         ++m_files_with_errors; | 
|---|
 | 84 |         error( library_name, full_path, desc() ); | 
|---|
 | 85 |       } | 
|---|
 | 86 | */ | 
|---|
 | 87 |     } | 
|---|
 | 88 |   } // namespace inspect | 
|---|
 | 89 | } // namespace boost | 
|---|
 | 90 |  | 
|---|
 | 91 |  | 
|---|