xref: /haiku/src/tests/servers/app/code_to_name/code_to_name.cpp (revision c90684742e7361651849be4116d0e5de3a817194)
1 /*
2  * Copyright 2005, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 
9 
10 #include <Entry.h>
11 #include <Path.h>
12 #include <Query.h>
13 #include <Volume.h>
14 
15 #include <fs_info.h>
16 
17 #include <ctype.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 
22 
23 extern const char* __progname;
24 
25 
26 void
27 skip_white_space(char*& line)
28 {
29 	while (isspace(line[0]))
30 		line++;
31 }
32 
33 
34 void
35 get_name(char* line)
36 {
37 	while (isalnum(line[0]) || line[0] == '_') {
38 		line++;
39 	}
40 
41 	line[0] = '\0';
42 }
43 
44 
45 void
46 print_code(BPath& path, int32 code)
47 {
48 	FILE* file = fopen(path.Path(), "r");
49 	if (file == NULL)
50 		return;
51 
52 	int32 lineNumber = 0;
53 	char buffer[4096];
54 
55 	while (fgets(buffer, sizeof(buffer), file) != NULL) {
56 		char* line = buffer;
57 		skip_white_space(line);
58 
59 		if (strncmp(line, "AS_", 3))
60 			continue;
61 
62 		if (++lineNumber != code)
63 			continue;
64 
65 		get_name(line);
66 		printf("code %ld: %s\n", lineNumber, line);
67 		fclose(file);
68 		return;
69 	}
70 
71 	printf("unknown code %ld!\n", code);
72 	fclose(file);
73 }
74 
75 
76 int
77 main(int argc, char** argv)
78 {
79 	if (argc < 2) {
80 		fprintf(stderr, "usage: %s <message-code>\n", __progname);
81 		return -1;
82 	}
83 
84 	int32 number = atol(argv[1]);
85 
86 	BQuery query;
87 	query.SetPredicate("name=ServerProtocol.h");
88 
89 	// search on current volume only
90 	dev_t device = dev_for_path(".");
91 	BVolume volume(device);
92 
93 	query.SetVolume(&volume);
94 	query.Fetch();
95 
96 	status_t status;
97 	BEntry entry;
98 	while ((status = query.GetNextEntry(&entry)) == B_OK) {
99 		BPath path(&entry);
100 		puts(path.Path());
101 		if (strstr(path.Path(), "headers/private/app/ServerProtocol.h") != NULL) {
102 			print_code(path, number);
103 			break;
104 		}
105 	}
106 
107 	if (status != B_OK) {
108 		fprintf(stderr, "%s: could not find ServerProtocol.h", __progname);
109 		return -1;
110 	}
111 	return 0;
112 }
113