1 //
2 // aegis - project change supervisor
3 // Copyright (C) 2007, 2008, 2010, 2012 Peter Miller
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or (at
8 // your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 
19 #include <common/ac/assert.h>
20 #include <common/ac/ctype.h>
21 
22 #include <libaegis/attribute.h>
23 #include <libaegis/change.h>
24 #include <libaegis/change/branch.h>
25 #include <libaegis/change/file.h>
26 #include <libaegis/input/file_text.h>
27 #include <libaegis/os.h>
28 #include <libaegis/sub.h>
29 
30 #include <aede-policy/validation/files/white_space.h>
31 
32 
~validation_files_white_space()33 validation_files_white_space::~validation_files_white_space()
34 {
35 }
36 
37 
validation_files_white_space()38 validation_files_white_space::validation_files_white_space()
39 {
40 }
41 
42 
43 validation::pointer
create(void)44 validation_files_white_space::create(void)
45 {
46     return pointer(new validation_files_white_space());
47 }
48 
49 
50 bool
check_branches() const51 validation_files_white_space::check_branches()
52     const
53 {
54     return false;
55 }
56 
57 
58 bool
check_downloaded() const59 validation_files_white_space::check_downloaded()
60     const
61 {
62     return false;
63 }
64 
65 
66 bool
check_foreign_copyright() const67 validation_files_white_space::check_foreign_copyright()
68     const
69 {
70     return false;
71 }
72 
73 
74 bool
check_binaries() const75 validation_files_white_space::check_binaries()
76     const
77 {
78     return false;
79 }
80 
81 
82 bool
check(change::pointer cp,fstate_src_ty * src)83 validation_files_white_space::check(change::pointer cp, fstate_src_ty *src)
84 {
85     nstring path(cp->file_path(src));
86     assert(!path.empty());
87     if (path.empty())
88         return true;
89 
90     os_become_orig();
91     int num_blank_lines = 0;
92     bool ok = true;
93     input ip = input_file_text_open(path);
94     int line_number = 0;
95     for (;;)
96     {
97         nstring line;
98         if (!ip->one_line(line))
99             break;
100         ++line_number;
101 
102         //
103         // check for white space on the end of the line
104         //
105         const char *dp = line.c_str();
106         const char *ep = dp + line.size();
107         if (dp < ep && isspace((unsigned char)ep[-1]))
108         {
109             sub_context_ty sc;
110             sc.var_set_format
111             (
112                 "File_Name",
113                 "%s: %d",
114                 src->file_name->str_text,
115                 line_number
116             );
117             change_error
118             (
119                 cp,
120                 &sc,
121                 i18n("$filename: white space at end of line")
122             );
123             ok = false;
124         }
125 
126         //
127         // check for a blank line
128         //
129         while (dp < ep)
130         {
131             if (!isspace((unsigned char)*dp))
132                 break;
133             ++dp;
134         }
135         if (dp == ep)
136             ++num_blank_lines;
137         else
138             num_blank_lines = 0;
139     }
140     ip.close();
141     os_become_undo();
142     if (num_blank_lines)
143     {
144         sub_context_ty sc;
145         sc.var_set_string("File_Name", src->file_name);
146         sc.var_set_long("Number", num_blank_lines);
147         sc.var_optional("Number");
148         change_error(cp, &sc, i18n("$filename: blank lines at end of file"));
149         ok = false;
150     }
151     return ok;
152 }
153 
154 
155 // vim: set ts=8 sw=4 et :
156