xref: /haiku/src/apps/debuganalyzer/DebugAnalyzer.cpp (revision f60531661beee9c2bcde5df0e6b821dfc0bdfc24)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 
10 #include <new>
11 
12 #include <Application.h>
13 
14 #include <AutoDeleter.h>
15 
16 #include "DataSource.h"
17 #include "MessageCodes.h"
18 
19 #include "main_window/MainWindow.h"
20 
21 
22 static const char* const kSignature = "application/x-vnd.Haiku-DebugAnalyzer";
23 
24 
25 class DebugAnalyzer : public BApplication {
26 public:
27 	DebugAnalyzer()
28 		:
29 		BApplication(kSignature),
30 		fWindowCount(0)
31 	{
32 	}
33 
34 	virtual void ReadyToRun()
35 	{
36 printf("ReadyToRun()\n");
37 		if (fWindowCount == 0 && _CreateWindow(NULL) != B_OK)
38 			PostMessage(B_QUIT_REQUESTED);
39 	}
40 
41 	virtual void ArgvReceived(int32 argc, char** argv)
42 	{
43 		printf("ArgvReceived()\n");
44 		for (int32 i = 0; i < argc; i++)
45 			printf("  arg %" B_PRId32 ": \"%s\"\n", i, argv[i]);
46 
47 		for (int32 i = 1; i < argc; i++) {
48 			PathDataSource* dataSource = new(std::nothrow) PathDataSource;
49 			if (dataSource == NULL) {
50 				// no memory
51 				fprintf(stderr, "DebugAnalyzer::ArgvReceived(): Out of "
52 					"memory!");
53 				return;
54 			}
55 
56 			status_t error = dataSource->Init(argv[i]);
57 			if (error != B_OK) {
58 				fprintf(stderr, "Failed to create data source for path "
59 					"\"%s\": %s\n", argv[i], strerror(error));
60 				// TODO: Alert!
61 				continue;
62 			}
63 
64 			_CreateWindow(dataSource);
65 		}
66 
67 	}
68 
69 	virtual void RefsReceived(BMessage* message)
70 	{
71 printf("RefsReceived()\n");
72 	}
73 
74 private:
75 	status_t _CreateWindow(DataSource* dataSource)
76 	{
77 		ObjectDeleter<DataSource> dataSourceDeleter(dataSource);
78 
79 		MainWindow* window;
80 		try {
81 			window = new MainWindow(dataSource);
82 		} catch (std::bad_alloc&) {
83 			fprintf(stderr, "DebugAnalyzer::_CreateWindow(): Out of memory!\n");
84 			return B_NO_MEMORY;
85 		}
86 
87 		// the data source is owned by the window now
88 		dataSourceDeleter.Detach();
89 
90 		window->Show();
91 
92 		fWindowCount++;
93 
94 		return B_OK;
95 	}
96 
97 	virtual void MessageReceived(BMessage* message)
98 	{
99 		switch (message->what) {
100 			case MSG_WINDOW_QUIT:
101 				if (--fWindowCount == 0)
102 					PostMessage(B_QUIT_REQUESTED);
103 				break;
104 			default:
105 				BApplication::MessageReceived(message);
106 				break;
107 		}
108 	}
109 
110 private:
111 	int32	fWindowCount;
112 };
113 
114 
115 int
116 main(int argc, const char* const* argv)
117 {
118 	DebugAnalyzer app;
119 	app.Run();
120 
121 	return 0;
122 }
123