~ubuntu-branches/ubuntu/raring/codeblocks/raring-proposed

« back to all changes in this revision

Viewing changes to src/plugins/contrib/HexEditor/FileContentBase.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cosme Domínguez Díaz
  • Date: 2010-08-09 04:38:38 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20100809043838-a59ygguym4eg0jgw
Tags: 10.05-0ubuntu1
* New upstream release. Closes (LP: #322350)
 - Switch to dpkg-source 3.0 (quilt) format
 - Remove unneeded README.source
 - Add debian/get-source-orig script that removes all
   Windows prebuilt binaries
* Bump Standards-Version to 3.9.1
 - Stop shipping *.la files
* debian/control
 - Add cdbs package as Build-Depend
 - Add libbz2-dev and zlib1g-dev packages as
   Build-Depends (needed by libhelp_plugin.so)
 - Remove dpatch package of Build-Depends
 - Add codeblocks-contrib-debug package
 - Split architecture-independent files of codeblocks
   package in codeblocks-common package
* debian/rules
 - Switch to CDBS rules system
 - Add parallel build support
 - Add a call to debian/get-source-orig script
 - Use lzma compression (saves 23,5 MB of free space)
* debian/patches
 - Refresh 01_codeblocks_plugin_path
 - Add 02_no_Makefiles_in_debian_dir to remove any link
   in codeblocks build system to deleted Makefiles of debian directory
 - Drop 02_ftbfs_gcc44 and 03_ftbfs_glib221 (merged in upstream)
* debian/watch
 - Update to use the new host (berlios.de)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* This file is part of HexEditor plugin for Code::Blocks Studio
 
3
* Copyright (C) 2008-2009 Bartlomiej Swiecki
 
4
*
 
5
* HexEditor plugin 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
 
8
* (at your option) any later version.
 
9
*
 
10
* HexEditor pluging 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
 
13
* GNU General Public License for more details.
 
14
*
 
15
* You should have received a copy of the GNU General Public License
 
16
* along with HexEditor. If not, see <http://www.gnu.org/licenses/>.
 
17
*
 
18
* $Revision: 5445 $
 
19
* $Id: FileContentBase.cpp 5445 2009-02-07 00:35:09Z byo $
 
20
* $HeadURL: svn+ssh://jenslody@svn.berlios.de/svnroot/repos/codeblocks/trunk/src/plugins/contrib/HexEditor/FileContentBase.cpp $
 
21
*/
 
22
 
 
23
#include "FileContentBase.h"
 
24
#include "FileContentBuffered.h"
 
25
#include "FileContentDisk.h"
 
26
 
 
27
#include <wx/file.h>
 
28
 
 
29
namespace
 
30
{
 
31
    const FileContentBase::OffsetT maxBufferedSize = 1024L * 1024L * 4;
 
32
    const FileContentBase::OffsetT maxAnySize      = 0x8000000000000000ULL;//1024L * 1024L * 1024L * 2;  // 2 GB limit
 
33
}
 
34
 
 
35
FileContentBase::InvalidModificationData FileContentBase::m_UndoInvalid;
 
36
 
 
37
FileContentBase* FileContentBase::BuildInstance( const wxString& fileName )
 
38
{
 
39
    wxFile fl( fileName );
 
40
    if ( !fl.IsOpened() )
 
41
    {
 
42
        return NULL;
 
43
    }
 
44
 
 
45
    if ( (OffsetT)fl.Length() <= maxBufferedSize )
 
46
    {
 
47
        return new FileContentBuffered();
 
48
    }
 
49
 
 
50
    if ( (OffsetT)fl.Length() <= maxAnySize )
 
51
    {
 
52
        return new FileContentDisk();
 
53
    }
 
54
 
 
55
 
 
56
    return NULL;
 
57
}
 
58
 
 
59
 
 
60
FileContentBase::FileContentBase()
 
61
    : m_UndoBuffer ( 0 )
 
62
    , m_UndoLast   ( 0 )
 
63
    , m_UndoCurrent( 0 )
 
64
    , m_UndoSaved  ( 0 )
 
65
{
 
66
}
 
67
 
 
68
FileContentBase::~FileContentBase()
 
69
{
 
70
    UndoClear();
 
71
}
 
72
 
 
73
void FileContentBase::InsertAndApplyModification( ModificationData* mod )
 
74
{
 
75
    RemoveUndoFrom( m_UndoCurrent );
 
76
 
 
77
    mod->m_Next = 0;
 
78
    mod->m_Prev = m_UndoLast;
 
79
 
 
80
    if ( m_UndoLast )
 
81
    {
 
82
        m_UndoLast->m_Next = mod;
 
83
    }
 
84
    else
 
85
    {
 
86
        m_UndoBuffer = mod;
 
87
    }
 
88
 
 
89
    if ( !m_UndoSaved )
 
90
    {
 
91
        m_UndoSaved = mod;
 
92
    }
 
93
 
 
94
    ApplyModification( mod );
 
95
 
 
96
    m_UndoLast = mod;
 
97
    m_UndoCurrent = 0;
 
98
}
 
99
 
 
100
void FileContentBase::ApplyModification( ModificationData* mod )
 
101
{
 
102
    mod->Apply();
 
103
}
 
104
 
 
105
void FileContentBase::RevertModification( ModificationData* mod )
 
106
{
 
107
    mod->Revert();
 
108
}
 
109
 
 
110
void FileContentBase::RemoveUndoFrom( ModificationData* mod )
 
111
{
 
112
    if ( !mod ) return;
 
113
 
 
114
    m_UndoLast = mod->m_Prev;
 
115
 
 
116
    if ( m_UndoLast )
 
117
    {
 
118
        m_UndoLast->m_Next = 0;
 
119
    }
 
120
    else
 
121
    {
 
122
        m_UndoBuffer = 0;
 
123
    }
 
124
 
 
125
    while ( mod )
 
126
    {
 
127
        if ( mod == m_UndoSaved )
 
128
        {
 
129
            m_UndoSaved = &m_UndoInvalid;
 
130
        }
 
131
 
 
132
        ModificationData* tmp = mod;
 
133
        mod = mod->m_Next;
 
134
        delete tmp;
 
135
    }
 
136
}
 
137
 
 
138
FileContentBase::OffsetT FileContentBase::Remove( const ExtraUndoData& extraUndoData, OffsetT position, OffsetT length )
 
139
{
 
140
    if ( !length ) return 0;
 
141
 
 
142
    ModificationData* mod = BuildRemoveModification( position, length );
 
143
    if ( !mod ) return 0;
 
144
 
 
145
    mod->m_Data = extraUndoData;
 
146
 
 
147
    InsertAndApplyModification( mod );
 
148
 
 
149
    return mod->Length();
 
150
}
 
151
 
 
152
FileContentBase::OffsetT FileContentBase::Add( const ExtraUndoData& extraUndoData, OffsetT position, OffsetT length, void* data)
 
153
{
 
154
    if ( !length ) return 0;
 
155
 
 
156
    ModificationData* mod = BuildAddModification( position, length, data );
 
157
    if ( !mod ) return 0;
 
158
 
 
159
    mod->m_Data = extraUndoData;
 
160
 
 
161
    InsertAndApplyModification( mod );
 
162
 
 
163
    return mod->Length();
 
164
}
 
165
 
 
166
FileContentBase::OffsetT FileContentBase::Write( const ExtraUndoData& extraUndoData, const void* buff, OffsetT position, OffsetT length )
 
167
{
 
168
    if ( !buff ) return 0;
 
169
    if ( !length ) return 0;
 
170
 
 
171
    ModificationData* mod = BuildChangeModification( position, length, buff );
 
172
    if ( !mod ) return 0;
 
173
 
 
174
    mod->m_Data = extraUndoData;
 
175
 
 
176
    InsertAndApplyModification( mod );
 
177
 
 
178
    return mod->Length();
 
179
}
 
180
 
 
181
bool FileContentBase::Modified()
 
182
{
 
183
    return m_UndoSaved != m_UndoCurrent;
 
184
}
 
185
 
 
186
void FileContentBase::SetModified( bool modified )
 
187
{
 
188
    m_UndoSaved = modified ? &m_UndoInvalid : m_UndoCurrent;
 
189
}
 
190
 
 
191
const FileContentBase::ExtraUndoData* FileContentBase::Redo()
 
192
{
 
193
    if ( !m_UndoCurrent ) return 0;
 
194
 
 
195
    ApplyModification( m_UndoCurrent );
 
196
    const ExtraUndoData* ret = &m_UndoCurrent->m_Data;
 
197
 
 
198
    m_UndoCurrent = m_UndoCurrent->m_Next;
 
199
 
 
200
    return ret;
 
201
}
 
202
 
 
203
const FileContentBase::ExtraUndoData* FileContentBase::Undo()
 
204
{
 
205
    if ( m_UndoCurrent == m_UndoBuffer ) return 0;
 
206
 
 
207
    if ( !m_UndoCurrent )
 
208
    {
 
209
        m_UndoCurrent = m_UndoLast;
 
210
        assert ( m_UndoCurrent->m_Next == 0 );
 
211
    }
 
212
    else
 
213
    {
 
214
        assert( m_UndoCurrent->m_Prev != 0 );
 
215
        m_UndoCurrent = m_UndoCurrent->m_Prev;
 
216
    }
 
217
    RevertModification( m_UndoCurrent );
 
218
 
 
219
    return &m_UndoCurrent->m_Data;
 
220
}
 
221
 
 
222
bool FileContentBase::CanRedo()
 
223
{
 
224
    return m_UndoCurrent != 0;
 
225
}
 
226
 
 
227
bool FileContentBase::CanUndo()
 
228
{
 
229
    return m_UndoCurrent != m_UndoBuffer;
 
230
}