OSDN Git Service

94d8e4645b62b90c5bf29ba1fdbc368000d5da60
[peercast-im/PeerCastIM.git] / PeerCast.root / PeerCast_1 / core / common / atom.h
1 // ------------------------------------------------
2 // File : atom.h
3 // Date: 1-mar-2004
4 // Author: giles
5 //
6 // (c) 2002-4 peercast.org
7 // ------------------------------------------------
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 // ------------------------------------------------
18
19 #ifndef _ATOM_H
20 #define _ATOM_H
21
22 #include "stream.h"
23
24 #include "id.h"
25
26 // ------------------------------------------------
27 class AtomStream
28 {
29 public:
30         AtomStream(Stream &s) : io(s),numChildren(0),numData(0)
31         {}
32
33         void checkData(int d)
34         {
35                 if (numData != d)
36                         throw StreamException("Bad atom data");
37         }
38
39         void writeParent(ID4 id,int nc)
40         {
41                 io.writeID4(id);
42                 io.writeInt(nc|0x80000000);
43         }
44
45         void writeInt(ID4 id,int d)
46         {
47                 io.writeID4(id);
48                 io.writeInt(4);
49                 io.writeInt(d);
50         }
51         
52         void writeID4(ID4 id,ID4 d)
53         {
54                 io.writeID4(id);
55                 io.writeInt(4);
56                 io.writeID4(d);
57         }
58
59         void writeShort(ID4 id,short d)
60         {
61                 io.writeID4(id);
62                 io.writeInt(2);
63                 io.writeShort(d);
64         }
65
66         void writeChar(ID4 id,char d)
67         {
68                 io.writeID4(id);
69                 io.writeInt(1);
70                 io.writeChar(d);
71         }
72
73         void writeBytes(ID4 id,const void *p,int l)
74         {
75                 io.writeID4(id);
76                 io.writeInt(l);
77                 io.write(p,l);
78         }
79
80
81         int writeStream(ID4 id,Stream &in,int l)
82         {
83                 io.writeID4(id);
84                 io.writeInt(l);
85                 in.writeTo(io,l);
86                 return (sizeof(int)*2)+l;
87
88         }
89
90         void writeString(ID4 id,const char *p)
91         {
92                 writeBytes(id,p,strlen(p)+1);
93         }
94
95         ID4     read(int &numc,int &dlen)
96         {
97                 ID4 id = io.readID4();
98
99                 unsigned int v = io.readInt();
100                 if (v & 0x80000000)
101                 {
102                         numc = v&0x7fffffff;
103                         dlen = 0;
104                 }else
105                 {
106                         numc = 0;
107                         dlen = v;
108                 }
109
110                 numChildren = numc;
111                 numData = dlen;
112
113                 return id;
114         }
115
116         void skip(int c, int d)
117         {
118                 if (d)
119                         io.skip(d);
120
121                 for(int i=0; i<c; i++)
122                 {
123                         int numc,data;
124                         read(numc,data);
125                         skip(numc,data);
126                 }
127
128         }
129
130         int             readInt() 
131         {
132                 checkData(4);
133                 return io.readInt();
134         }
135         int             readID4() 
136         {
137                 checkData(4);
138                 return io.readID4();
139         }
140
141         int             readShort() 
142         {
143                 checkData(2);
144                 return io.readShort();
145         }
146         int             readChar() 
147         {
148                 checkData(1);
149                 return io.readChar();
150         }
151         int             readBytes(void *p,int l) 
152         {
153                 checkData(l);
154                 return io.read(p,l);
155         }
156
157         void    readString(char *s,int max,int dlen)
158         {
159                 checkData(dlen);
160                 readBytes(s,max,dlen);
161                 s[max-1] = 0;
162
163         }
164
165         void    readBytes(void *s,int max,int dlen)
166         {
167                 checkData(dlen);
168                 if (max > dlen)
169                         readBytes(s,dlen);
170                 else
171                 {
172                         readBytes(s,max);
173                         io.skip(dlen-max);
174                 }
175         }
176
177         int             writeAtoms(ID4 id, Stream &in, int cnt, int data)
178         {
179                 int total=0;
180
181                 if (cnt)
182                 {
183                         writeParent(id,cnt);
184                         total+=sizeof(int)*2;
185
186                         for(int i=0; i<cnt; i++)
187                         {
188                                 AtomStream ain(in);
189                                 int c,d;
190                                 ID4 cid = ain.read(c,d);
191                                 total += writeAtoms(cid,in,c,d);
192                         }
193                 }else
194                 {
195                         total += writeStream(id,in,data);
196                 }
197
198                 return total;
199         }
200
201         bool    eof() {return io.eof();}        
202
203
204         int     numChildren,numData;
205         Stream &io;
206 };
207
208 #endif