summaryrefslogtreecommitdiffabout
path: root/shared-code/Dynamide.h
blob: 32c93f747d286a6f1037c3473b79f5afcc1c9f1b (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#ifndef __DYNAMIDE_H
#define __DYNAMIDE_H

#include "LRUCache.h"

namespace Klever {

template<int fbSize,int bSize>
class CDynamide : public CObject {
	struct	firstBlock {
		LONG	freeFile;
		BYTE	crap[fbSize-sizeof(LONG)];
	};
	struct	theBlock {
		LONG	next;
		BYTE	data[bSize-sizeof(LONG)];
	};
public:
	static firstBlock FirstBlock;
	static theBlock TheBlock;
private:
	class	CDynaCache : public CLRUCache<DWORD,DWORD,theBlock> {
	public:
		CFile* m_File;
		BOOL m_bAutodelete;
		CDynaCache(CFile* file,BOOL bAutodelete=TRUE) : CLRUCache<DWORD,DWORD,theBlock>(64) {
			m_File=file;
			m_bAutodelete=bAutodelete;
		}
		virtual ~CDynaCache() {
			Flush();
			if(m_bAutodelete){
				m_File->Close();
				delete m_File;
			}
		}
		virtual BOOL _ReadIn(DWORD idx,theBlock* data) {
		LONG p = sizeof(firstBlock)+idx*sizeof(theBlock);
		LONG s = m_File->Seek(p,CFile::begin);
			if(p==s){
			UINT rb = m_File->Read(data,sizeof(*data));
				if(rb==sizeof(*data))
					return TRUE;
				if(rb)
					return FALSE;
				memset(data,0,sizeof(*data));
				data->next=-1;
				try{
					m_File->Write(data,sizeof(*data));
				}catch(CException* e){
					e->Delete();
					return FALSE;
				}
			}else{
			LONG togo = p-s;
				ASSERT(togo>0);
				ASSERT(!(togo%sizeof(theBlock)));
				memset(data,0,sizeof(*data));
				data->next=-1;
				while(togo>=0){
					try{
						m_File->Write(data,sizeof(*data));
					}catch(CException* e){
						e->Delete();
						return FALSE;
					}
				}
			}
			return TRUE;
		}
		virtual BOOL _WriteOut(DWORD idx,theBlock* data) {
		LONG p = sizeof(firstBlock)+idx*sizeof(theBlock);
		LONG s = m_File->Seek(p,CFile::begin);
			if(p!=s)
				return FALSE;
			try{
				m_File->Write(data,sizeof(*data));
			}catch(CException* e){
				e->Delete();
				return FALSE;
			}
			return TRUE;
		}
		DWORD AllocateNode() {
		LONG l = m_File->GetLength();
			ASSERT(!((l-sizeof(firstBlock))%sizeof(theBlock)));
			m_File->SetLength(l+sizeof(theBlock));
			return (l-sizeof(firstBlock))/sizeof(theBlock);
		}
		BOOL Read1stBlock(firstBlock* fb) {
			m_File->SeekToBegin();
		UINT rb = m_File->Read(fb,sizeof(*fb));
			if(rb==sizeof(*fb))
				return TRUE;
			if(rb)
				return FALSE;
			memset(fb,0,sizeof(*fb));
			fb->freeFile = -1;
			try{
				m_File->Write(fb,sizeof(*fb));
			}catch(CException* e){
				e->Delete();
				return FALSE;
			}
			return TRUE;
		}
		BOOL Write1stBlock(firstBlock* fb) {
			m_File->SeekToBegin();
			try{
				m_File->Write(fb,sizeof(*fb));
			}catch(CException* e){
				e->Delete();
				return FALSE;
			}
			return  TRUE;
		}
	};
public:
	class	CDynaFile	: public CFile	{
	public:
		CDynamide<fbSize,bSize>* m_Daddy;
		CArray<LONG,LONG> m_Blocks;
		LONG m_Position;

		CDynaFile(CDynamide<fbSize,bSize>* daddy) : m_Daddy(NULL) { AttachToDaddy(daddy); }
		virtual ~CDynaFile() { Close(); DetachFromDaddy(); }

		void AttachToDaddy(CDynamide<fbSize,bSize>* daddy) {
			ASSERT(!m_Daddy);
			ASSERT(daddy);
			m_Daddy=daddy;
			m_Daddy->AttachDynaFile(this);
		}
		void DetachFromDaddy() {
			ASSERT(m_Daddy);
			ASSERT(!IsOpened());
			m_Daddy->DetachDynaFile(this);
			m_Daddy=NULL;
		}

		BOOL Create() {
			if(IsOpened())
				return FALSE;
			m_Blocks.SetAtGrow(0,m_Daddy->Allocate());
			ASSERT(m_Blocks[0]>=0);
			m_Position=0;
			return TRUE;
		}
		BOOL Open(LONG fb) {
			if(IsOpened())
				return FALSE;
			ASSERT(fb>=0);
		theBlock* b;
			do{
				m_Blocks.Add(fb);
				b = m_Daddy->m_File->GetCached(fb);
				ASSERT(b);
				fb=b->next;
				if(m_Blocks[m_Blocks.GetUpperBound()]==fb)
					return FALSE;
			}while(fb>=0);
			m_Position=0;
			return TRUE;
		}

		LONG GetFile() {
			if(!IsOpened())
				return -1;
			return m_Blocks[0];
		}
		virtual DWORD GetPosition() const {
			if(!IsOpened())
				return 0;
			if(m_Position<0)
				return 0;
			if(m_Position>GetLength())
				return GetLength();
			return m_Position;
		}
		virtual CString GetFileName() {
		CString rv;
			if(IsOpened())
				rv.Format("0x%08lX",m_Blocks[0]);
			else
				rv.Format("None");
			return rv;
		}
		virtual CString GetFileTitle() { return GetFileName(); }
		virtual CString GetFilePath() { return GetFileName(); }
		virtual void SetFilePath(LPCTSTR lpszNewName) { ASSERT(FALSE); }

		virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL) { ASSERT(FALSE); return FALSE; }
        virtual CFile* Duplicate() { ASSERT(FALSE); return NULL; }

        virtual LONG Seek(LONG lOff, UINT nFrom) {
			if(!IsOpened())
				return -1;
			switch(nFrom){
			case CFile::begin:
				m_Position=lOff;
				break;
			case CFile::current:
				m_Position+=lOff;
				break;
			case CFile::end:
				m_Position=GetLength()+lOff;
				break;
			default:
				ASSERT(FALSE);
				return -1;
			}
			if(m_Position<0)
				m_Position=0;
			if(m_Position>GetLength())
				m_Position=GetLength();
			return m_Position;
		}
        virtual void SetLength(DWORD dwNewLen) {
			if(!IsOpened())
				return;
			if(dwNewLen<GetLength()){
				dwNewLen=dwNewLen+sizeof(TheBlock.data);
				dwNewLen-=dwNewLen%sizeof(TheBlock.data);
				while(dwNewLen<GetLength() && m_Blocks.GetSize()>1){
				LONG mb = m_Blocks[m_Blocks.GetUpperBound()];
				LONG mb1 = m_Blocks[m_Blocks.GetUpperBound()-1];
				theBlock* b = m_Daddy->m_File->GetCached(mb1);
					ASSERT(b);
					ASSERT(b->next==mb);
					b->next=-1;
					m_Daddy->m_File->MakeDirty(mb1);
					m_Daddy->Deallocate(mb);
					m_Blocks.SetSize(m_Blocks.GetSize()-1);
				}
			}else{
				while(dwNewLen>GetLength()){
				LONG mb = m_Blocks[m_Blocks.GetUpperBound()];
				LONG newBlock = m_Daddy->Allocate();
					ASSERT(newBlock>=0);
				theBlock* b = m_Daddy->m_File->GetCached(mb);
					ASSERT(b);
					ASSERT(b->next<0);
					b->next=newBlock;
					m_Daddy->m_File->MakeDirty(mb);
					m_Blocks.Add(newBlock);
				}
			}
		}
        virtual DWORD GetLength() const {
			return ((long)m_Blocks.GetSize())*((long)sizeof(TheBlock.data));
		}

        virtual UINT Read(void* lpBuf, UINT nCount) {
		UINT rv = 0;
			ASSERT(m_Position>=0 && m_Position<=GetLength());
		LPBYTE data = (LPBYTE)lpBuf;
			while(nCount && m_Position<GetLength()){
			UINT bn = m_Position/sizeof(TheBlock.data);
			UINT bo = m_Position%sizeof(TheBlock.data);
			theBlock* b = m_Daddy->m_File->GetCached(m_Blocks[bn]);
				ASSERT(b);
			UINT bc = min(nCount,sizeof(TheBlock.data)-bo);
				memmove(data,&b->data[bo],bc);
				nCount-=bc;
				data=&data[bc];
				rv+=bc;
				m_Position+=bc;
			}
			return rv;
		}
        virtual void Write(const void* lpBuf, UINT nCount) {
			ASSERT(m_Position>=0 && m_Position<=GetLength());
		LPBYTE data = (LPBYTE)lpBuf;
			while(nCount){
			UINT bn = m_Position/sizeof(TheBlock.data);
			UINT bo = m_Position%sizeof(TheBlock.data);
				while(bn>=m_Blocks.GetSize()){
				LONG mb = m_Blocks[m_Blocks.GetUpperBound()];
				LONG newBlock = m_Daddy->Allocate();
					ASSERT(newBlock>=0);
				theBlock* b = m_Daddy->m_File->GetCached(mb);
					ASSERT(b);
					ASSERT(b->next<0);
					b->next=newBlock;
					m_Daddy->m_File->MakeDirty(mb);
					m_Blocks.Add(newBlock);
				}
			theBlock* b = m_Daddy->m_File->GetCached(m_Blocks[bn]);
				ASSERT(b);
			UINT bc = min(nCount,sizeof(TheBlock.data)-bo);
				memmove(&b->data[bo],data,bc);
				m_Daddy->m_File->MakeDirty(m_Blocks[bn]);
				nCount-=bc;
				data=&data[bc];
				m_Position+=bc;
			}
		}

        virtual void LockRange(DWORD dwPos, DWORD dwCount) { ASSERT(FALSE); }
        virtual void UnlockRange(DWORD dwPos, DWORD dwCount) { ASSERT(FALSE); }

        virtual void Abort() { ASSERT(FALSE); }
        virtual void Flush() {
			m_Daddy->m_File->Flush();
		}
        virtual void Close() {
			m_Blocks.RemoveAll();
		}

		BOOL IsOpened() const { return m_Blocks.GetSize()!=0; }
	};

	CDynaCache* m_File;
	firstBlock m_FB;

	CDynamide() : m_File(NULL) {}
	~CDynamide() { Close(); }

	BOOL AttachDynaFile(CDynaFile* f) {
	//	ASSERT(!m_Files.Find(f));
	//	m_Files.AddHead(f);
		return TRUE;
	}
	BOOL DetachDynaFile(CDynaFile* f) {
	//POSITION p  = m_Files.Find(f);
	//	ASSERT(p);
	//	m_Files.RemoveAt(p);
		return TRUE;
	}

	BOOL Open(LPCTSTR file,BOOL bRO=FALSE) {
		Close();
		try{
		CFile* f = new CFile(file,CFile::typeBinary|(bRO?CFile::modeRead|CFile::shareDenyWrite:CFile::modeReadWrite|CFile::shareDenyRead));
			return Attach(f,TRUE);
		}catch(CException* e){
			e->Delete();
			return FALSE;
		}
	}
	BOOL Create(LPCTSTR file) {
		Close();
		try{
		CFile* f = new CFile(file,CFile::typeBinary|CFile::modeCreate|CFile::modeReadWrite|CFile::shareDenyRead);
			return Attach(f,TRUE);
		}catch(CException* e){
			e->Delete();
			return FALSE;
		}
	}
	BOOL Attach(CFile* file,BOOL bAutodelete=TRUE) {
		if(IsOpened())
			return FALSE;
		m_File = new CDynaCache(file,bAutodelete);
		if(!m_File->Read1stBlock(&m_FB)){
			memset(&m_FB,0,sizeof(m_FB));
			m_FB.freeFile=-1;
			VERIFY(m_File->Write1stBlock(&m_FB));
		}	
		return IsOpened();
	}
	// CFile* Detach();
	BOOL Close() {
		if(!IsOpened())
			return FALSE;
		m_File->Write1stBlock(&m_FB);
		delete m_File;
		m_File=NULL;
		return TRUE;
	}
	BOOL IsOpened() {
		return m_File != NULL;
	}
	BOOL Write1stBlock(void) {
		if(!IsOpened())
			return FALSE;
		VERIFY(m_File->Write1stBlock(&m_FB));
		return TRUE;
	}

	CDynaFile* CreateFile() {
	CDynaFile* rv = new CDynaFile(this);
		if(rv->Create())
			return rv;
		delete rv;
		return NULL;
	}
	CDynaFile* OpenFile(LONG fb) {
	CDynaFile* rv = new CDynaFile(this);
		if(rv->Open(fb))
			return rv;
		delete rv;
		return NULL;
	}
	BOOL DeleteFile(LONG fb) {
		while(fb>=0){
		theBlock* b = m_File->GetCached(fb);
		LONG nb = b->next;
			Deallocate(fb);
			fb=nb;
		}
		return TRUE;
	}

	LONG Allocate() {
		if(!IsOpened())
			return -1;
		if(m_FB.freeFile<0){
		LONG rv = m_File->AllocateNode();
		theBlock *b = m_File->GetCached(rv);
			b->next=-1;
			m_File->MakeDirty(rv);
			return rv;
		}
	LONG rv = m_FB.freeFile;
	theBlock *b = m_File->GetCached(rv);
		m_FB.freeFile=b->next;
		b->next=-1;
		m_File->MakeDirty(rv);
		m_File->Write1stBlock(&m_FB);
		return rv;
	}
	BOOL Deallocate(LONG bk) {
		if(!IsOpened())
			return FALSE;
	theBlock* b = m_File->GetCached(bk);
		ASSERT(b);
		if(m_FB.freeFile<0){
			b->next=-1;
			m_FB.freeFile=bk;
		}else{
			b->next=m_FB.freeFile;
			m_FB.freeFile=bk;
		}
		m_File->MakeDirty(bk);
		m_File->Write1stBlock(&m_FB);
		return TRUE;
	}
};

};

#endif // __DYNAMIDE_H