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

« back to all changes in this revision

Viewing changes to src/plugins/contrib/HexEditor/FileContentBuffered.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: FileContentBuffered.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/FileContentBuffered.cpp $
 
21
*/
 
22
 
 
23
#include "FileContentBuffered.h"
 
24
 
 
25
#include <wx/file.h>
 
26
#include <algorithm>
 
27
 
 
28
/** \brief Internal modification class */
 
29
class FileContentBuffered::IntModificationData: public FileContentBuffered::ModificationData
 
30
{
 
31
    public:
 
32
 
 
33
        IntModificationData( std::vector< char >& buffer ): m_Buffer( buffer ) {}
 
34
 
 
35
        enum typeEnum
 
36
        {
 
37
            change,         ///< \brief Some content was changed
 
38
            added,          ///< \brief Some data was inserted
 
39
            removed,        ///< \brief Some data was removed
 
40
        };
 
41
 
 
42
        std::vector< char >& m_Buffer;
 
43
 
 
44
        typeEnum             m_Type;
 
45
        OffsetT              m_Position;
 
46
        std::vector< char >  m_OldData;
 
47
        std::vector< char >  m_NewData;
 
48
 
 
49
        void Apply()
 
50
        {
 
51
            switch ( m_Type )
 
52
            {
 
53
                case added:
 
54
                {
 
55
                    assert( m_Buffer.size() >= m_Position );
 
56
                    m_Buffer.insert( m_Buffer.begin() + m_Position, m_NewData.begin(), m_NewData.end() );
 
57
                    break;
 
58
                }
 
59
 
 
60
                case removed:
 
61
                {
 
62
                    assert( m_Buffer.size() > m_Position );
 
63
                    assert( m_Buffer.size() >= m_Position + m_OldData.size() );
 
64
                    m_Buffer.erase( m_Buffer.begin() + m_Position, m_Buffer.begin() + m_Position + m_OldData.size() );
 
65
                    break;
 
66
                }
 
67
 
 
68
                case change:
 
69
                {
 
70
                    assert( m_Buffer.size() > m_Position );
 
71
                    assert( m_Buffer.size() >= m_Position + m_NewData.size() );
 
72
                    assert( m_OldData.size() == m_NewData.size() );
 
73
 
 
74
                    std::copy( m_NewData.begin(), m_NewData.end(), m_Buffer.begin() + m_Position );
 
75
                    break;
 
76
                }
 
77
            }
 
78
        }
 
79
 
 
80
        void Revert()
 
81
        {
 
82
            switch ( m_Type )
 
83
            {
 
84
                case removed:
 
85
                {
 
86
                    assert( m_Buffer.size() >= m_Position );
 
87
                    m_Buffer.insert( m_Buffer.begin() + m_Position, m_OldData.begin(), m_OldData.end() );
 
88
                    break;
 
89
                }
 
90
 
 
91
                case added:
 
92
                {
 
93
                    assert( m_Buffer.size() > m_Position );
 
94
                    assert( m_Buffer.size() >= m_Position + m_NewData.size() );
 
95
                    m_Buffer.erase( m_Buffer.begin() + m_Position, m_Buffer.begin() + m_Position + m_NewData.size() );
 
96
                    break;
 
97
                }
 
98
 
 
99
                case change:
 
100
                {
 
101
                    assert( m_Buffer.size() > m_Position );
 
102
                    assert( m_Buffer.size() >= m_Position + m_OldData.size() );
 
103
                    assert( m_OldData.size() == m_NewData.size() );
 
104
 
 
105
                    std::copy( m_OldData.begin(), m_OldData.end(), m_Buffer.begin() + m_Position );
 
106
                    break;
 
107
                }
 
108
            }
 
109
        }
 
110
 
 
111
        OffsetT Length()
 
112
        {
 
113
            return m_OldData.empty() ? m_NewData.size() : m_OldData.size();
 
114
        }
 
115
};
 
116
 
 
117
FileContentBuffered::FileContentBuffered()
 
118
{
 
119
}
 
120
 
 
121
FileContentBuffered::~FileContentBuffered()
 
122
{
 
123
}
 
124
 
 
125
FileContentBuffered::ModificationData* FileContentBuffered::BuildAddModification( OffsetT position, OffsetT length, const void* data )
 
126
{
 
127
    IntModificationData* mod = new IntModificationData( m_Buffer );
 
128
    mod->m_Type     = IntModificationData::added;
 
129
    mod->m_Position = position;
 
130
 
 
131
    mod->m_NewData.resize( length );
 
132
    if ( data )
 
133
    {
 
134
        std::copy( (char*)data, (char*)data + length, mod->m_NewData.begin() );
 
135
    }
 
136
 
 
137
    return mod;
 
138
}
 
139
 
 
140
FileContentBuffered::ModificationData* FileContentBuffered::BuildRemoveModification( OffsetT position, OffsetT length )
 
141
{
 
142
    if ( position > m_Buffer.size() ) return 0;
 
143
 
 
144
    if ( position + length > m_Buffer.size() )
 
145
    {
 
146
        length = m_Buffer.size() - position;
 
147
        if ( !length ) return 0;
 
148
    }
 
149
 
 
150
    IntModificationData* mod = new IntModificationData( m_Buffer );
 
151
    mod->m_Type      = IntModificationData::removed;
 
152
    mod->m_Position  = position;
 
153
    mod->m_OldData.resize( length );
 
154
 
 
155
    std::copy( m_Buffer.begin() + position, m_Buffer.begin() + position + length, mod->m_OldData.begin() );
 
156
 
 
157
    return mod;
 
158
}
 
159
 
 
160
FileContentBuffered::ModificationData* FileContentBuffered::BuildChangeModification( OffsetT position, OffsetT length, const void* data )
 
161
{
 
162
    if ( position > m_Buffer.size() ) return 0;
 
163
    if ( position + length > m_Buffer.size() )
 
164
    {
 
165
        length = m_Buffer.size() - position;
 
166
        if ( !length ) return 0;
 
167
    }
 
168
 
 
169
    IntModificationData* mod = new IntModificationData( m_Buffer );
 
170
    mod->m_Type     = IntModificationData::change;
 
171
    mod->m_Position = position;
 
172
    mod->m_OldData.resize( length );
 
173
    mod->m_NewData.resize( length );
 
174
 
 
175
    std::copy( m_Buffer.begin() + position, m_Buffer.begin() + position + length, mod->m_OldData.begin() );
 
176
    if ( data )
 
177
    {
 
178
        std::copy( (char*)data, (char*)data + length, mod->m_NewData.begin() );
 
179
    }
 
180
 
 
181
    return mod;
 
182
}
 
183
 
 
184
FileContentBuffered::OffsetT FileContentBuffered::Read( void* buff, OffsetT position, OffsetT length)
 
185
{
 
186
    if ( position > m_Buffer.size() ) return 0;
 
187
    if ( position + length > m_Buffer.size() )
 
188
    {
 
189
        length = m_Buffer.size() - position;
 
190
        if ( !length ) return 0;
 
191
    }
 
192
 
 
193
    memcpy( buff, &m_Buffer[ position ], length );
 
194
    return length;
 
195
}
 
196
 
 
197
FileContentBuffered::OffsetT FileContentBuffered::GetSize()
 
198
{
 
199
    return m_Buffer.size();
 
200
}
 
201
 
 
202
bool FileContentBuffered::WriteFile(const wxString& fileName)
 
203
{
 
204
    wxFile fl( fileName, wxFile::write );
 
205
    if ( !fl.IsOpened() ) return false;
 
206
    if ( fl.Write( &m_Buffer[0], m_Buffer.size() ) == m_Buffer.size() )
 
207
    {
 
208
        UndoNotifySaved();
 
209
        return true;
 
210
    }
 
211
    return false;
 
212
}
 
213
 
 
214
bool FileContentBuffered::ReadFile(const wxString& fileName)
 
215
{
 
216
    wxFile fl( fileName, wxFile::read );
 
217
    if ( !fl.IsOpened() ) return false;
 
218
 
 
219
    m_Buffer.resize( fl.Length() );
 
220
 
 
221
    UndoClear();
 
222
 
 
223
    return (size_t)fl.Read( &m_Buffer[0], m_Buffer.size() ) == m_Buffer.size();
 
224
}