summaryrefslogtreecommitdiffstats
path: root/khexedit/lib/kbigbuffer.cpp
blob: 8505ef74d40d419e8e641bd42e283ed9f13f780a (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
/***************************************************************************
                          kbigbuffer.cpp  -  description
                             -------------------
    begin                : Mit Jun 02 2003
    copyright            : (C) 2003 by Friedrich W. H. Kossebau
    email                : Friedrich.W.H@Kossebau.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License version 2 as published by the Free Software Foundation.       *
 *                                                                         *
 ***************************************************************************/


// c specific
#include <stdlib.h>
// lib specific
#include "kbigbuffer.h"

using namespace KHE;

KBigBuffer::KBigBuffer( int NP, int PS )
 : NoOfUsedPages( NP ),
   NoOfFreePages( NP ),
   PageSize( PS ),
   FirstPage( -1 ),
   LastPage( -1 ),
   Size( 0 )
{
  IsOpen = false;

//   if( !filename.empty() )
//     open(filename);
}


KBigBuffer::~KBigBuffer()
{
  if( File.isOpen() )
    close();
}



bool KBigBuffer::prepareRange( KSection /*Range*/ ) const
{
  return true;
}

const char *KBigBuffer::dataSet( KSection /*Section*/ ) const
{
  return 0;
}


char KBigBuffer::datum( unsigned int DatumOffset ) const
{
//   std::cout << "reading datum " << DatumOffset << std::endl;
  int OffsetInPage = DatumOffset - OffsetOfActualPage;
  // there shouldn't be any need to check l
  if( OffsetInPage >= 0 && OffsetInPage < (int)PageSize )
    return ActualPage[OffsetInPage];

  // load the page
  unsigned int PageIndex = DatumOffset / PageSize;
  ensurePageLoaded( PageIndex );
  return ActualPage[DatumOffset-OffsetOfActualPage];
}




int KBigBuffer::insert( int /*Pos*/, const char*, int /*Length*/ )
{
  return 0;
}

int KBigBuffer::remove( KSection /*Section*/ )
{
  return 0;
}

unsigned int KBigBuffer::replace( KSection /*Section*/, const char*, unsigned int /*Length*/ )
{
  return 0;
}

int KBigBuffer::fill( char /*FillChar*/, int /*Length*/, unsigned int /*Pos*/ )
{
  return 0;
}


int KBigBuffer::move( int /*DestPos*/, KSection /*SourceSection*/ ) { return 0; }
//int KBigBuffer::find( const char*, int /*Length*/, int /*Pos*/ ) const  { return 0; }
int KBigBuffer::find( const char*/*KeyData*/, int /*Length*/, KSection /*Section*/ ) const { return 0; }

int KBigBuffer::rfind( const char*, int /*Length*/, int /*Pos*/ ) const { return 0; }



bool KBigBuffer::open( const QString& FileName )
{
  // clear old data
  if( isOpen() && !close() ) // only occurs if close somehow fails.
    return false;

  File.setName( FileName );
  if( !File.open(IO_ReadOnly|IO_Raw) )
    return false;

//   std::cout << "loading file " << FileName << std::endl;

  int FileSize = File.size();
  Size = FileSize;

  // calculate necessary number of pages
  int NoOfPages = FileSize/PageSize + 1;

  // initialize Page pointers
  Data.resize( NoOfPages );
  for( KPageOfChar::iterator D=Data.begin(); D!=Data.end(); ++D )
    *D = 0;

  FirstPage = LastPage = 0;

  return ensurePageLoaded( 0 );
}


bool KBigBuffer::close()
{
  if( !isOpen() )
    return false;

  File.close();

  if( File.status() == IO_UnspecifiedError )
    return false;

//   std::cout << "closing file " << std::endl;

  // free pages
  for( KPageOfChar::iterator D=Data.begin(); D!=Data.end(); ++D )
    delete [] *D;

  FirstPage = LastPage = -1;
  NoOfFreePages = NoOfUsedPages;

  return true;
}


bool KBigBuffer::ensurePageLoaded( unsigned int PageIndex ) const
{
  if( !isOpen() )
    return false;
  // page loaded?
  if( Data[PageIndex] != 0 )
  {
    ActualPage = Data[PageIndex];
    OffsetOfActualPage = PageIndex * PageSize;
    return true;
  }

  // no page available?
  if( NoOfFreePages < 1 )
  {
    // free the page which is the furthest away from the page we are loading
    if( abs(FirstPage-PageIndex) > abs(LastPage-PageIndex) )
      while( !freePage(FirstPage++) );
    else
      while( !freePage(LastPage--) );
  }

//   std::cout << "loading page " << PageIndex << std::endl;
  // create Page
  Data[PageIndex] = new char[PageSize];
  --NoOfFreePages;

  // jump to position and read the page's data in
  bool Success = File.at( (unsigned long)(PageIndex*PageSize) );
  if( Success )
    Success = File.readBlock( Data[PageIndex], PageSize ) > 0;

  if( Success )
  {
    // correct bounds
    if( (int)PageIndex < FirstPage )
      FirstPage = PageIndex;

    if( (int)PageIndex > LastPage )
      LastPage = PageIndex;

    ActualPage = Data[PageIndex];
    OffsetOfActualPage = PageIndex * PageSize;
  }

  return Success;
}


bool KBigBuffer::freePage( unsigned int PageIndex ) const
{
  // check range and if is loaded at all
  if( (unsigned int)PageIndex >= Data.size() || !Data[PageIndex] )
    return false;
//   std::cout << "freeing page " << PageIndex << std::endl;
  delete [] Data[PageIndex];
  Data[PageIndex] = 0;
  ++NoOfFreePages;
  return true;
}