File: clDTL.cpp

package info (click to toggle)
codelite 12.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (220 lines) | stat: -rw-r--r-- 7,795 bytes parent folder | download | duplicates (3)
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : clDTL.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "clDTL.h"
#include "dtl/dtl.hpp"
#include <wx/ffile.h>
#include <wx/tokenzr.h>
#include <wx/utils.h>

clDTL::clDTL()
{
}

clDTL::~clDTL()
{
}

void clDTL::Diff(const wxFileName& fnLeft, const wxFileName& fnRight, DiffMode mode)
{
    wxString leftFile, rightFile;

    {
        wxFFile fp1(fnLeft.GetFullPath(), "rb");
        wxFFile fp2(fnRight.GetFullPath(), "rb");

        if ( !fp1.IsOpened() || !fp2.IsOpened() )
            return;

        // Read the file content
        fp1.ReadAll(&leftFile);
        fp2.ReadAll(&rightFile);
    }

    m_resultLeft.clear();
    m_resultRight.clear();
    m_sequences.clear();

    typedef wxString elem;
    typedef std::pair<elem, dtl::elemInfo> sesElem;

    wxArrayString leftLines = wxStringTokenize(leftFile, "\n", wxTOKEN_RET_DELIMS);
    wxArrayString rightLines = wxStringTokenize(rightFile, "\n", wxTOKEN_RET_DELIMS);

    std::vector<elem> leftLinesVec;
    std::vector<elem> rightLinesVec;
    leftLinesVec.insert(leftLinesVec.end(), leftLines.begin(), leftLines.end());
    rightLinesVec.insert(rightLinesVec.end(), rightLines.begin(), rightLines.end());

    dtl::Diff<elem, std::vector<elem> > diff(leftLinesVec, rightLinesVec);
    diff.onHuge();
    diff.compose();

    if ( 0 == diff.getEditDistance() ) {
        // nothing to be done - files are identical
        return;
    }

    if ( mode & clDTL::kTwoPanes ) {

        ///////////////////////////////////////////////////////////////////
        // Two panes diff
        // designed for displayed on a two panes view where on the left
        // pane all deletions while on the right pane all the new lines
        ///////////////////////////////////////////////////////////////////

        // Loop over the diff and check if it is a whitespace only diff
        std::vector<sesElem> seq = diff.getSes().getSequence();
        m_resultLeft.reserve( seq.size() );
        m_resultRight.reserve( seq.size() );

        const int STATE_NONE   = 0;
        const int STATE_IN_SEQ = 1;

        int state = STATE_NONE;
        int seqStartLine = wxNOT_FOUND;
        size_t seqSize      = 0;

        LineInfoVec_t tmpSeqLeft;
        LineInfoVec_t tmpSeqRight;

        for(size_t i=0; i<seq.size(); ++i) {
            switch(seq.at(i).second.type) {
            case dtl::SES_COMMON: {
                if ( state == STATE_IN_SEQ ) {

                    // set the sequence size
                    seqSize = ::wxMax(tmpSeqLeft.size(), tmpSeqRight.size() );

                    m_sequences.push_back( std::make_pair(seqStartLine, seqStartLine + seqSize) );
                    seqStartLine = wxNOT_FOUND;
                    state = STATE_NONE;

                    // increase the buffer size
                    tmpSeqLeft.resize(seqSize);
                    tmpSeqRight.resize(seqSize);

                    m_resultLeft.insert(m_resultLeft.end(), tmpSeqLeft.begin(), tmpSeqLeft.end());
                    m_resultRight.insert(m_resultRight.end(), tmpSeqRight.begin(), tmpSeqRight.end());

                    tmpSeqLeft.clear();
                    tmpSeqRight.clear();
                    seqSize = 0;
                }
                clDTL::LineInfo line(seq.at(i).first, LINE_COMMON);
                m_resultLeft.push_back( line );
                m_resultRight.push_back( line );
                break;

            }
            case dtl::SES_ADD: {
                clDTL::LineInfo lineRight(seq.at(i).first, LINE_ADDED);
                tmpSeqRight.push_back( lineRight );

                if ( state == STATE_NONE ) {
                    seqStartLine = m_resultLeft.size();
                    state = STATE_IN_SEQ;
                }
                break;

            }
            case dtl::SES_DELETE: {
                clDTL::LineInfo lineLeft(seq.at(i).first, LINE_REMOVED);
                tmpSeqLeft.push_back( lineLeft );

                if ( state == STATE_NONE ) {
                    seqStartLine = m_resultLeft.size();
                    state = STATE_IN_SEQ;
                }
                break;
            }
            }
        }

        if ( state == STATE_IN_SEQ ) {
            // set the sequence size
            seqSize = ::wxMax(tmpSeqLeft.size(), tmpSeqRight.size() );
            if ( seqSize ) {
                m_sequences.push_back( std::make_pair(seqStartLine, seqStartLine + seqSize) );
                seqStartLine = wxNOT_FOUND;
                state = STATE_NONE;

                // increase the buffer size
                tmpSeqLeft.resize(seqSize);
                tmpSeqRight.resize(seqSize);

                m_resultLeft.insert(m_resultLeft.end(), tmpSeqLeft.begin(), tmpSeqLeft.end());
                m_resultRight.insert(m_resultRight.end(), tmpSeqRight.begin(), tmpSeqRight.end());

                tmpSeqLeft.clear();
                tmpSeqRight.clear();
                seqSize = 0;
            }
        }
    } else {
        ///////////////////////////////////////////////////////////////////
        // One pane diff view
        // designed for displayed on a single editor
        ///////////////////////////////////////////////////////////////////
        std::vector<sesElem> seq = diff.getSes().getSequence();
        m_resultLeft.reserve( seq.size() );
        int seqStartLine = wxNOT_FOUND;
        for(size_t i=0; i<seq.size(); ++i) {
            switch(seq.at(i).second.type) {
            case dtl::SES_COMMON: {
                if ( seqStartLine != wxNOT_FOUND ) {
                    m_sequences.push_back( std::make_pair(seqStartLine, m_resultLeft.size()) );
                    seqStartLine = wxNOT_FOUND;
                }
                clDTL::LineInfo line(seq.at(i).first, LINE_COMMON);
                m_resultLeft.push_back( line );
                break;
            }
            case dtl::SES_ADD: {
                if ( seqStartLine == wxNOT_FOUND ) {
                    seqStartLine = m_resultLeft.size();
                }
                clDTL::LineInfo line(seq.at(i).first, LINE_ADDED);
                m_resultLeft.push_back( line );
                break;

            }
            case dtl::SES_DELETE: {
                if ( seqStartLine == wxNOT_FOUND ) {
                    seqStartLine = m_resultLeft.size();
                }
                clDTL::LineInfo line(seq.at(i).first, LINE_REMOVED);
                m_resultLeft.push_back( line );
                break;
            }
            }
        }
        
        if ( seqStartLine != wxNOT_FOUND ) {
            m_sequences.push_back( std::make_pair(seqStartLine, m_resultLeft.size()) );
            seqStartLine = wxNOT_FOUND;
        }
    }
}