xref: /haiku/src/apps/diskprobe/DataEditor.h (revision 602992140d69561077b9fafbdc49272fa30cdf41)
1 /*
2 ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the OpenBeOS License.
4 */
5 #ifndef DATA_EDITOR_H
6 #define DATA_EDITOR_H
7 
8 
9 #include <File.h>
10 #include <Entry.h>
11 #include <Locker.h>
12 #include <ObjectList.h>
13 
14 
15 class DataChange;
16 
17 class DataEditor : public BLocker {
18 	public:
19 		DataEditor();
20 		DataEditor(entry_ref &ref, const char *attribute = NULL);
21 		DataEditor(BEntry &entry, const char *attribute = NULL);
22 		DataEditor(const DataEditor &editor);
23 		~DataEditor();
24 
25 		status_t SetTo(const char *path, const char *attribute = NULL);
26 		status_t SetTo(entry_ref &ref, const char *attribute = NULL);
27 		status_t SetTo(BEntry &entry, const char *attribute = NULL);
28 		status_t SetToAttribute(const char *attribute);
29 
30 		bool IsReadOnly() const { return fIsReadOnly; }
31 		bool IsDevice() const { return fIsDevice; }
32 		bool IsAttribute() const { return fAttribute != NULL; }
33 		//bool IsModified() const { return fIsModified; }
34 
35 		const char *Attribute() const { return fAttribute; }
36 
37 		status_t InitCheck();
38 
39 		status_t Replace(off_t offset, const uint8 *data, size_t length);
40 		status_t Remove(off_t offset, off_t length);
41 		status_t Insert(off_t offset, const uint8 *data, size_t length);
42 
43 		status_t MoveBy(int32 bytes);
44 		status_t MoveTo(off_t offset);
45 
46 		status_t Undo();
47 		status_t Redo();
48 
49 		bool CanUndo() const;
50 		bool CanRedo() const;
51 
52 		status_t SetFileSize(off_t size);
53 		off_t FileSize() const { return fSize; }
54 
55 		status_t SetViewOffset(off_t offset);
56 		off_t ViewOffset() const { return fViewOffset; }
57 
58 		status_t SetViewSize(size_t size);
59 		size_t ViewSize() const { return fViewSize; }
60 
61 		void SetBlockSize(size_t size);
62 		size_t BlockSize() const { return fBlockSize; }
63 
64 		status_t GetViewBuffer(const uint8 **_buffer);
65 
66 		status_t StartWatching(BMessenger target);
67 		status_t StartWatching(BHandler *handler, BLooper *looper = NULL);
68 		void StopWatching(BMessenger target);
69 		void StopWatching(BHandler *handler, BLooper *looper = NULL);
70 
71 		BFile &File() { return fFile; }
72 
73 	private:
74 		void SendNotices(uint32 what, BMessage *message = NULL);
75 		status_t Update();
76 		void AddChange(DataChange *change);
77 		void ApplyChanges();
78 		void RemoveRedos();
79 
80 		BObjectList<BMessenger> fObservers;
81 
82 		BFile		fFile;
83 		const char	*fAttribute;
84 		bool		fIsDevice, fIsReadOnly;
85 		off_t		fRealSize, fSize;
86 
87 		BObjectList<DataChange>	fChanges;
88 		DataChange				*fFirstChange;
89 		DataChange				*fLastChange;
90 
91 		uint8		*fView;
92 		off_t		fRealViewOffset, fViewOffset;
93 		size_t		fRealViewSize, fViewSize;
94 		bool		fNeedsUpdate;
95 
96 		size_t		fBlockSize;
97 };
98 
99 static const uint32 kDataEditorStateChange = 'deSC';
100 static const uint32 kDataEditorUpdate = 'deUp';
101 
102 #endif	/* DATA_EDITOR_H */
103