xref: /haiku/src/apps/diskprobe/DiskProbe.cpp (revision 9eb55bc1d104b8fda80898f8b25c94d8000c8255)
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 
6 
7 #include "DataEditor.h"
8 
9 #include <Application.h>
10 #include <Autolock.h>
11 
12 #include <stdio.h>
13 #include <string.h>
14 
15 
16 static const char *kSignature = "application/x-vnd.OpenBeOS-DiskProbe";
17 
18 
19 class DiskProbe : public BApplication {
20 	public:
21 		DiskProbe();
22 		~DiskProbe();
23 };
24 
25 
26 #define DUMPED_BLOCK_SIZE 16
27 
28 void
29 dumpBlock(const uint8 *buffer, int size, const char *prefix)
30 {
31 	int i;
32 
33 	for (i = 0; i < size;) {
34 		int start = i;
35 
36 		printf(prefix);
37 		for (; i < start+DUMPED_BLOCK_SIZE; i++) {
38 			if (!(i % 4))
39 				printf(" ");
40 
41 			if (i >= size)
42 				printf("  ");
43 			else
44 				printf("%02x", *(unsigned char *)(buffer + i));
45 		}
46 		printf("  ");
47 
48 		for (i = start; i < start + DUMPED_BLOCK_SIZE; i++) {
49 			if (i < size) {
50 				char c = buffer[i];
51 
52 				if (c < 30)
53 					printf(".");
54 				else
55 					printf("%c", c);
56 			} else
57 				break;
58 		}
59 		printf("\n");
60 	}
61 }
62 
63 
64 status_t
65 printBlock(DataEditor &editor, const char *text)
66 {
67 	puts(text);
68 
69 	const uint8 *buffer;
70 	status_t status = editor.GetViewBuffer(&buffer);
71 	if (status < B_OK) {
72 		fprintf(stderr, "Could not get block: %s\n", strerror(status));
73 		return status;
74 	}
75 
76 	dumpBlock(buffer, editor.FileSize() > editor.ViewOffset() + editor.ViewSize() ?
77 			editor.ViewSize() : editor.FileSize() - editor.ViewOffset(),
78 		"\t");
79 
80 	return B_OK;
81 }
82 
83 
84 //	#pragma mark -
85 
86 
87 DiskProbe::DiskProbe()
88 	: BApplication(kSignature)
89 {
90 	DataEditor editor;
91 	status_t status = editor.SetTo("/boot/beos/apps/DiskProbe");
92 	if (status < B_OK) {
93 		fprintf(stderr, "Could not set editor: %s\n", strerror(status));
94 		return;
95 	}
96 
97 	editor.SetBlockSize(512);
98 	editor.SetViewOffset(0);
99 	editor.SetViewSize(32);
100 
101 	BAutolock locker(editor.Locker());
102 
103 	if (printBlock(editor, "before (location 0):") < B_OK)
104 		return;
105 
106 	editor.Replace(5, (const uint8 *)"*REPLACED*", 10);
107 	if (printBlock(editor, "after (location 0):") < B_OK)
108 		return;
109 
110 	editor.SetViewOffset(700);
111 	if (printBlock(editor, "before (location 700):") < B_OK)
112 		return;
113 
114 	editor.Replace(713, (const uint8 *)"**REPLACED**", 12);
115 	if (printBlock(editor, "after (location 700):") < B_OK)
116 		return;
117 
118 	status = editor.Undo();
119 	if (status < B_OK)
120 		fprintf(stderr, "Could not undo: %s\n", strerror(status));
121 	if (printBlock(editor, "undo (location 700):") < B_OK)
122 		return;
123 
124 	editor.SetViewOffset(0);
125 	if (printBlock(editor, "after (location 0):") < B_OK)
126 		return;
127 
128 	status = editor.Undo();
129 	if (status < B_OK)
130 		fprintf(stderr, "Could not undo: %s\n", strerror(status));
131 	if (printBlock(editor, "undo (location 0):") < B_OK)
132 		return;
133 
134 	status = editor.Redo();
135 	if (status < B_OK)
136 		fprintf(stderr, "Could not redo: %s\n", strerror(status));
137 	if (printBlock(editor, "redo (location 0):") < B_OK)
138 		return;
139 
140 	status = editor.Redo();
141 	editor.SetViewOffset(700);
142 	if (status < B_OK)
143 		fprintf(stderr, "Could not redo: %s\n", strerror(status));
144 	if (printBlock(editor, "redo (location 700):") < B_OK)
145 		return;
146 
147 	status = editor.Redo();
148 	if (status == B_OK)
149 		fprintf(stderr, "Could apply redo (non-expected behaviour)!\n");
150 
151 	status = editor.Undo();
152 	if (status < B_OK)
153 		fprintf(stderr, "Could not undo: %s\n", strerror(status));
154 	if (printBlock(editor, "undo (location 700):") < B_OK)
155 		return;
156 
157 	PostMessage(B_QUIT_REQUESTED);
158 }
159 
160 
161 DiskProbe::~DiskProbe()
162 {
163 }
164 
165 
166 //	#pragma mark -
167 
168 
169 int
170 main(int argc, char **argv)
171 {
172 	DiskProbe probe;
173 
174 	probe.Run();
175 	return 0;
176 }
177