summaryrefslogtreecommitdiff
path: root/filter.cxx
blob: d6f434b4cd4e8f15c23ce89e5ae6b1e9ee0e7fbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Filter tabs -> spaces.
 *
 * Author: Jan Holesovsky <kendy@suse.cz>
 * License: MIT <http://www.opensource.org/licenses/mit-license.php>
 */

#include "error.hxx"
#include "filter.hxx"

#include <regex.h>

#include <cstring>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

struct Tabs {
    int spaces;
    FilterType type;
    regex_t regex;

    Tabs( int spaces_, FilterType type_ ) : spaces( spaces_ ), type( type_ ) {}
    ~Tabs() { regfree( &regex ); }

    bool matches( const string& fname_ ) { return regexec( &regex, fname_.c_str(), 0, NULL, 0 ) == 0; }
};

static std::vector< Tabs* > tabs_vector;

Filter::Filter( const string& fname_ )
    : spaces( 0 ),
      column( 0 ),
      spaces_to_write( 0 ),
      nonspace_appeared( false ),
      type( NO_FILTER )
{
    data.reserve( 16384 );

    for ( std::vector< Tabs* >::const_iterator it = tabs_vector.begin(); it != tabs_vector.end(); ++it )
    {
        if ( (*it)->matches( fname_.c_str() ) )
        {
            spaces = (*it)->spaces;
            type = (*it)->type;
            break; // 1st wins
        }
    }
}

/// The old way of tabs -> spaces: Just the leading whitespace, tab stop is always the same, regardless of the position
inline void addDataLoopOld( char*& dest, char what, int& column, int& spaces_to_write, bool& nonspace_appeared, int no_spaces )
{
    if ( what == '\t' && !nonspace_appeared )
    {
        column += no_spaces;
        spaces_to_write += no_spaces;
    }
    else if ( what == ' ' )
    {
        ++column;
        ++spaces_to_write;
    }
    else if ( what == '\n' )
    {
        // write out any spaces that we need
        for ( int i = 0; i < spaces_to_write; ++i )
            *dest++ = ' ';

        *dest++ = what;
        column = 0;
        spaces_to_write = 0;
        nonspace_appeared = false;
    }
    else
    {
        nonspace_appeared = true;

        // write out any spaces that we need
        for ( int i = 0; i < spaces_to_write; ++i )
            *dest++ = ' ';

        *dest++ = what;
        ++column;
        spaces_to_write = 0;
    }
}

/// Combine the 'old' way of tabs -> spaces with all (as if the old is applied first, and then the new one on top of that)
inline void addDataLoopCombined( char*& dest, char what, int& column, int& spaces_to_write, bool& nonspace_appeared, int no_spaces )
{
    if ( what == '\t' )
    {
        if ( nonspace_appeared )
        {
            // new behavior
            const int tab_size = no_spaces - ( column % no_spaces );
            column += tab_size;
            spaces_to_write += tab_size;
        }
        else
        {
            // old one
            column += no_spaces;
            spaces_to_write += no_spaces;
        }
    }
    else if ( what == ' ' )
    {
        ++column;
        ++spaces_to_write;
    }
    else if ( what == '\n' )
    {
        *dest++ = what;
        column = 0;
        spaces_to_write = 0;
        nonspace_appeared = false;
    }
    else
    {
        nonspace_appeared = true;

        // write out any spaces that we need
        for ( int i = 0; i < spaces_to_write; ++i )
            *dest++ = ' ';

        *dest++ = what;
        ++column;
        spaces_to_write = 0;
    }
}

/// The best tabs -> spaces: converts all, strips trailing whitespace
inline void addDataLoopTabs( char*& dest, char what, int& column, int& spaces_to_write, bool& nonspace_appeared, int no_spaces )
{
    if ( what == '\t' )
    {
        const int tab_size = no_spaces - ( column % no_spaces );
        column += tab_size;
        spaces_to_write += tab_size;
    }
    else if ( what == ' ' )
    {
        ++column;
        ++spaces_to_write;
    }
    else if ( what == '\n' )
    {
        *dest++ = what;
        column = 0;
        spaces_to_write = 0;
    }
    else
    {
        // write out any spaces that we need
        for ( int i = 0; i < spaces_to_write; ++i )
            *dest++ = ' ';

        *dest++ = what;
        ++column;
        spaces_to_write = 0;
    }
}

/// Just convert Unx line ends to DOS ones
inline void addDataLoopDos( char*& dest, char what, int& column, int& spaces_to_write, bool& nonspace_appeared, int no_spaces )
{
    if ( what == '\n' )
        *dest++ = '\r';

    *dest++ = what;
}

/// Just convert DOS line ends to Unx ones
inline void addDataLoopUnx( char*& dest, char what, int& column, int& spaces_to_write, bool& nonspace_appeared, int no_spaces )
{
    if ( what == '\r' )
        return;

    *dest++ = what;
}

void Filter::addData( const char* data_, size_t len_ )
{
    if ( type == NO_FILTER )
    {
        data.append( data_, len_ );
        return;
    }

    // create big enough buffer
    const int size = ( spaces < 2 )? 2*len_: spaces*len_;
    char *tmp = new char[size];
    char *dest = tmp;

    // convert the tabs to spaces (according to spaces)
    switch ( type )
    {
        case FILTER_OLD:
            for ( const char* it = data_; it < data_ + len_; ++it )
                addDataLoopOld( dest, *it, column, spaces_to_write, nonspace_appeared, spaces );
            break;
        case FILTER_COMBINED:
            for ( const char* it = data_; it < data_ + len_; ++it )
                addDataLoopCombined( dest, *it, column, spaces_to_write, nonspace_appeared, spaces );
            break;
        case FILTER_TABS:
            for ( const char* it = data_; it < data_ + len_; ++it )
                addDataLoopTabs( dest, *it, column, spaces_to_write, nonspace_appeared, spaces );
            break;
        case FILTER_DOS:
            for ( const char* it = data_; it < data_ + len_; ++it )
                addDataLoopDos( dest, *it, column, spaces_to_write, nonspace_appeared, spaces );
            break;
        case FILTER_UNX:
            for ( const char* it = data_; it < data_ + len_; ++it )
                addDataLoopUnx( dest, *it, column, spaces_to_write, nonspace_appeared, spaces );
            break;
        case NO_FILTER:
            // NO_FILTER already handled
            break;
    }

    data.append( tmp, dest - tmp );

    delete[] tmp;
}

void Filter::addData( const string& data_ )
{
    addData( data_.data(), data_.size() );
}

void Filter::write( std::ostream& out_ )
{
    out_ << "data " << data.size() << endl
         << data << endl;
}

void Filter::addTabsToSpaces( int how_many_spaces_, FilterType type_, const std::string& files_regex_ )
{
    Tabs* tabs = new Tabs( how_many_spaces_, type_ );

    int status = regcomp( &tabs->regex, files_regex_.c_str(), REG_EXTENDED | REG_NOSUB );
    if ( status == 0 )
        tabs_vector.push_back( tabs );
    else
    {
        Error::report( "Cannot create regex '" + files_regex_ + "' (for tabs_to_spaces_files)." );
        delete tabs;
    }
}