xref: /haiku/src/add-ons/kernel/file_systems/udf/Array.h (revision 06b932a49d65e82cdfa7d28a04f48eef6de9ea49)
1 //----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS license.
4 //
5 //  Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
6 //---------------------------------------------------------------------
7 
8 #ifndef _UDF_ARRAY_H
9 #define _UDF_ARRAY_H
10 
11 #include <stdio.h>
12 
13 #include "kernel_cpp.h"
14 
15 #include "SupportDefs.h"
16 
17 namespace Udf {
18 
19 /*! \brief Slightly more typesafe static array type than built-in arrays,
20 	with array length information stored implicitly (i.e. consuming no
21 	physical space in the actual struct) via the \c arrayLength template
22 	parameter.
23 */
24 template<typename DataType, uint32 arrayLength>
25 struct array {
26 public:
27 	void dump() const {
28 		for (uint32 i = 0; i < arrayLength; i++)
29 			data[i].print();
30 	}
31 
32 	uint32 length() const { return arrayLength; }
33 	uint32 size() const { return arrayLength * sizeof(DataType); }
34 
35 	// This doesn't appear to work. I don't know why.
36 	DataType operator[] (int index) const { return data[index]; }
37 
38 	DataType data[arrayLength];
39 };
40 
41 /*! \brief \c uint8 specialization of the \c array template struct.
42 */
43 template<uint32 arrayLength>
44 struct array<uint8, arrayLength> {
45 	void dump() const
46 	{
47 		const uint8 bytesPerRow = 8;
48 		char classname[40];
49 		sprintf(classname, "array<uint8, %ld>", arrayLength);
50 
51 		DUMP_INIT(CF_PUBLIC | CF_DUMP | CF_HIGH_VOLUME, classname);
52 
53 		for (uint32 i = 0; i < arrayLength; i++) {
54 			if (i % bytesPerRow == 0)
55 				PRINT(("[%ld:%ld]: ", i, i+bytesPerRow-1));
56 			SIMPLE_PRINT(("0x%.2x ", data[i]));
57 			if ((i+1) % bytesPerRow == 0 || i+1 == arrayLength)
58 				SIMPLE_PRINT(("\n"));
59 		}
60 	}
61 	uint8 data[arrayLength];
62 };
63 
64 /*! \brief \c char specialization of the \c array template struct.
65 */
66 template<uint32 arrayLength>
67 struct array<char, arrayLength> {
68 	void dump() const
69 	{
70 		const uint8 bytesPerRow = 8;
71 		char classname[40];
72 		sprintf(classname, "array<uint8, %ld>", arrayLength);
73 
74 		DUMP_INIT(CF_PUBLIC | CF_DUMP | CF_HIGH_VOLUME, classname);
75 
76 		for (uint32 i = 0; i < arrayLength; i++) {
77 			if (i % bytesPerRow == 0)
78 				PRINT(("[%ld:%ld]: ", i, i+bytesPerRow-1));
79 			SIMPLE_PRINT(("0x%.2x ", data[i]));
80 			if ((i+1) % bytesPerRow == 0 || i+1 == arrayLength)
81 				SIMPLE_PRINT(("\n"));
82 		}
83 	}
84 	uint8 data[arrayLength];
85 };
86 
87 
88 };	// namespace UDF
89 
90 #endif	// _UDF_ARRAY_H
91