xref: /haiku/src/bin/listres.cpp (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
1 /*
2  * Copyright 2006, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT license.
4  */
5 
6 
7 #include <File.h>
8 #include <Mime.h>
9 #include <Resources.h>
10 #include <TypeConstants.h>
11 
12 #include <stdio.h>
13 #include <string.h>
14 
15 
16 static const char *
17 get_type(type_code type)
18 {
19 	static char buffer[32];
20 
21 	switch (type) {
22 		case B_MIME_STRING_TYPE:
23 			return "MIME String";
24 		case B_RAW_TYPE:
25 			return "Raw Data";
26 
27 		case B_STRING_TYPE:
28 			return "Text";
29 		case B_INT64_TYPE:
30 			return "Int-64";
31 		case B_UINT64_TYPE:
32 			return "Uint-64";
33 		case B_INT32_TYPE:
34 			return "Int-32";
35 		case B_UINT32_TYPE:
36 			return "Uint-32";
37 		case B_INT16_TYPE:
38 			return "Int-16";
39 		case B_UINT16_TYPE:
40 			return "Uint-16";
41 		case B_INT8_TYPE:
42 			return "Int-8";
43 		case B_UINT8_TYPE:
44 			return "Uint-8";
45 		case B_BOOL_TYPE:
46 			return "Boolean";
47 		case B_FLOAT_TYPE:
48 			return "Float";
49 		case B_DOUBLE_TYPE:
50 			return "Double";
51 
52 		case B_MINI_ICON_TYPE:
53 			return "Mini Icon";
54 		case B_LARGE_ICON_TYPE:
55 			return "Icon";
56 
57 		default:
58 		{
59 			int32 missed = 0, shift = 24;
60 			uint8 value[4];
61 			for (int32 i = 0; i < 4; i++, shift -= 8) {
62 				value[i] = uint8(type >> shift);
63 				if (value[i] < ' ' || value[i] > 127) {
64 					value[i] = '.';
65 					missed++;
66 				}
67 			}
68 
69 			if (missed < 2)
70 				sprintf(buffer, "'%c%c%c%c'", value[0], value[1], value[2], value[3]);
71 			else
72 				sprintf(buffer, "0x%08" B_PRIx32, type);
73 			return buffer;
74 		}
75 	}
76 }
77 
78 
79 int
80 main(int argc, char *argv[])
81 {
82 	const char *program = strrchr(argv[0], '/');
83 	if (program == NULL)
84 		program = argv[0];
85 	else
86 		program++;
87 
88 	if (argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
89 		printf("usage: %s <filename> [<filename> ...]\n", program);
90 		return 1;
91 	}
92 
93 	off_t total = 0;
94 
95 	for (int i = 1; i < argc; ++i) {
96 		BFile file(argv[i], B_READ_ONLY);
97 
98 		status_t status = file.InitCheck();
99 		if (status < B_OK) {
100 			fprintf(stderr, "%s: opening file failed for \"%s\": %s\n",
101 				program, argv[i], strerror(status));
102 			return 1;
103 		}
104 
105 		BResources resources;
106 		status = resources.SetTo(&file);
107 		if (status != B_OK) {
108 			fprintf(stderr, "%s: opening resources failed for \"%s\": %s\n",
109 				program, argv[i], strerror(status));
110 			return 1;
111 		}
112 
113 		printf("File: %s\n", argv[i]);
114 		printf("  Type         ID     Size                 Name\n");
115 		printf("-----------  -----  --------  -------------------------------\n");
116 
117 		int32 index = 0;
118 		const char* name;
119 		type_code type;
120 		size_t size;
121 		int32 id;
122 		while (resources.GetResourceInfo(index++, &type, &id, &name, &size)) {
123 			printf("%11s %6" B_PRId32 " %9ld  \"%s\"\n",
124 				get_type(type), id, size, name);
125 
126 			total += size;
127 		}
128 	}
129 
130 	printf("\n%" B_PRIdOFF " bytes total in resources.\n", total);
131 	return 0;
132 }
133