data.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 
30 #ifndef TNT_DATA_H
31 #define TNT_DATA_H
32 
33 #include <iostream>
34 
35 namespace tnt
36 {
37  class DataChunk
38  {
39  const char* data;
40  unsigned len;
41 
42  public:
43  DataChunk(const char* data_, const char* end_)
44  : data(data_),
45  len(end_ - data_)
46  { }
47  DataChunk(const char* data_, unsigned len_)
48  : data(data_),
49  len(len_)
50  { }
51 
52  const char* getData() const { return data; }
53  unsigned getLength() const { return len; }
54  };
55 
57  // interpretes raw data as a data-structure with chunks
58  // unsigned: number of chunks
59  // count X unsigned: offsets
60  // data
61  //
62  class DataChunks
63  {
64  const char* dataObject;
65 
66  const unsigned* udata() const
67  {
68  const char* d = dataObject;
69  return reinterpret_cast<const unsigned*>(d);
70  }
71 
72  public:
73  DataChunks(const char* d = 0)
74  : dataObject(d)
75  { }
76 
77  const char* data() const
78  { return dataObject; }
79 
80  void setData(const char* d)
81  { dataObject = d; }
82 
83  // number of chunks
84  unsigned size() const
85  { return (udata()[0] / sizeof(unsigned)) - 1; }
86 
87  // offset of n-th chunk from start
88  unsigned offset(unsigned n) const
89  { return udata()[n] - udata()[0]; }
90 
91  // size of nth chunk in bytes
92  unsigned size(unsigned n) const
93  { return udata()[n + 1] - udata()[n]; }
94 
95  // pointer to n-th chunk
96  const char* ptr(unsigned n) const
97  {
98  const char* d = dataObject;
99  return d + udata()[n];
100  }
101 
102  DataChunk operator[] (unsigned n) const
103  { return DataChunk(ptr(n), size(n)); }
104  };
105 
106  inline std::ostream& operator << (std::ostream& out, const DataChunk& c)
107  {
108  out.write(c.getData(), c.getLength());
109  return out;
110  }
111 
112 }
113 
114 #endif // TNT_DATA_H
115