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