xref: /haiku/src/apps/diskprobe/DataEditor.h (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 /*
2  * Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT 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 class StateWatcher;
17 
18 class DataEditor : public BLocker {
19 	public:
20 		DataEditor();
21 		DataEditor(entry_ref &ref, const char *attribute = NULL);
22 		DataEditor(BEntry &entry, const char *attribute = NULL);
23 		DataEditor(const DataEditor &editor);
24 		~DataEditor();
25 
26 		status_t SetTo(const char *path, const char *attribute = NULL);
27 		status_t SetTo(entry_ref &ref, const char *attribute = NULL);
28 		status_t SetTo(BEntry &entry, const char *attribute = NULL);
29 
30 		status_t Save();
31 
32 		bool IsReadOnly() const { return fIsReadOnly; }
33 		bool IsDevice() const { return fIsDevice; }
34 		bool IsAttribute() const { return fAttribute != NULL; }
35 		bool IsModified() const { return fLastChange != fFirstChange; }
36 
37 		const char *Attribute() const { return fAttribute; }
38 		type_code Type() const { return fType; }
39 
40 		status_t InitCheck();
41 
42 		status_t Replace(off_t offset, const uint8 *data, size_t length);
43 		status_t Remove(off_t offset, off_t length);
44 		status_t Insert(off_t offset, const uint8 *data, size_t length);
45 
46 		status_t MoveBy(int32 bytes);
47 		status_t MoveTo(off_t offset);
48 
49 		status_t Undo();
50 		status_t Redo();
51 
52 		bool CanUndo() const;
53 		bool CanRedo() const;
54 
55 		status_t SetFileSize(off_t size);
56 		off_t FileSize() const { return fSize; }
57 
58 		status_t SetViewOffset(off_t offset);
59 		off_t ViewOffset() const { return fViewOffset; }
60 
61 		status_t SetViewSize(size_t size);
62 		size_t ViewSize() const { return fViewSize; }
63 
64 		status_t SetBlockSize(size_t size);
65 		size_t BlockSize() const { return fBlockSize; }
66 
67 		status_t UpdateIfNeeded(bool *_updated = NULL);
68 		status_t ForceUpdate();
69 		status_t GetViewBuffer(const uint8 **_buffer);
70 
71 		status_t StartWatching(BMessenger target);
72 		status_t StartWatching(BHandler *handler, BLooper *looper = NULL);
73 		void StopWatching(BMessenger target);
74 		void StopWatching(BHandler *handler, BLooper *looper = NULL);
75 
76 		off_t Find(off_t startPosition, const uint8 *data, size_t dataSize,
77 					bool caseInsensitive, bool cyclic,
78 					BMessenger progressMessenger, volatile bool *stop = NULL);
79 
80 		BFile &File() { return fFile; }
81 		const entry_ref &AttributeRef() const { return fAttributeRef; }
82 		const entry_ref &Ref() const { return fRef; }
83 
84 	private:
85 		friend class StateWatcher;
86 
87 		status_t SetViewOffset(off_t offset, bool sendNotices);
88 		status_t SetViewSize(size_t size, bool sendNotices);
89 		void SendNotices(uint32 what, BMessage *message = NULL);
90 		void SendNotices(DataChange *change);
91 		status_t Update();
92 		void AddChange(DataChange *change);
93 		void ApplyChanges();
94 		void RemoveRedos();
95 
96 		BObjectList<BMessenger> fObservers;
97 
98 		entry_ref	fRef, fAttributeRef;
99 		BFile		fFile;
100 		const char	*fAttribute;
101 		type_code	fType;
102 		bool		fIsDevice, fIsReadOnly;
103 		off_t		fRealSize, fSize;
104 
105 		BObjectList<DataChange>	fChanges;
106 		DataChange				*fFirstChange;
107 		DataChange				*fLastChange;
108 		int32					fChangesFromSaved;
109 
110 		uint8		*fView;
111 		off_t		fRealViewOffset, fViewOffset;
112 		size_t		fRealViewSize, fViewSize;
113 		bool		fNeedsUpdate;
114 
115 		size_t		fBlockSize;
116 };
117 
118 static const uint32 kMsgDataEditorStateChange = 'deSC';
119 static const uint32 kMsgDataEditorUpdate = 'deUp';
120 static const uint32 kMsgDataEditorParameterChange = 'dePC';
121 
122 static const uint32 kMsgDataEditorFindProgress = 'deFP';
123 
124 #endif	/* DATA_EDITOR_H */
125