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 { 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 off_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 BLocker &Locker() { return fLock; } 67 bool Lock(); 68 void Unlock(); 69 70 status_t StartWatching(BMessenger target); 71 status_t StartWatching(BHandler *handler, BLooper *looper = NULL); 72 void StopWatching(BMessenger target); 73 void StopWatching(BHandler *handler, BLooper *looper = NULL); 74 75 BFile &File() { return fFile; } 76 77 private: 78 void SendNotices(uint32 what, BMessage *message = NULL); 79 status_t Update(); 80 void AddChange(DataChange *change); 81 void ApplyChanges(); 82 void RemoveRedos(); 83 84 BObjectList<BMessenger> fObservers; 85 86 BFile fFile; 87 const char *fAttribute; 88 bool fIsDevice, fIsReadOnly; 89 off_t fRealSize, fSize; 90 91 BObjectList<DataChange> fChanges; 92 DataChange *fFirstChange; 93 DataChange *fLastChange; 94 95 BLocker fLock; 96 uint8 *fView; 97 off_t fRealViewOffset, fViewOffset; 98 size_t fRealViewSize, fViewSize; 99 bool fNeedsUpdate; 100 101 size_t fBlockSize; 102 }; 103 104 static const uint32 kMsgDataEditorUndoState = 'deUS'; 105 static const uint32 kMsgDataEditorRedoState = 'deRS'; 106 static const uint32 kMsgDataEditorModifiedState = 'deMS'; 107 static const uint32 kMsgDataEditorUpdate = 'deUp'; 108 109 #endif /* DATA_EDITOR_H */ 110