aboutsummaryrefslogtreecommitdiffstats
path: root/doc/api-documentation/html/entriesblk_8cpp-source.html
blob: 61cc452d840befdd7592e8906b00e1bdbef30be6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>entriesblk.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; </center>
<hr><h1>entriesblk.cpp</h1><div class="fragment"><pre>00001 <font class="preprocessor">#include &lt;entriesblk.h&gt;</font>
00002 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>
00003 <font class="preprocessor">#include &lt;string.h&gt;</font>
00004 
00005 <font class="keyword">const</font> <font class="keywordtype">int</font> EntriesBlock::METAHEADERSIZE = 4;
00006         <font class="comment">// count(4);</font>
00007 <font class="keyword">const</font> <font class="keywordtype">int</font> EntriesBlock::METAENTRYSIZE = 8;
00008         <font class="comment">// offset(4); size(4);</font>
00009 
00010 EntriesBlock::EntriesBlock(<font class="keyword">const</font> <font class="keywordtype">char</font> *iBlock, <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> size) {
00011         block = (<font class="keywordtype">char</font> *)calloc(1, size);
00012         memcpy(block, iBlock, size);
00013 }
00014 
00015 
00016 EntriesBlock::EntriesBlock() {
00017         block = (<font class="keywordtype">char</font> *)calloc(1, <font class="keyword">sizeof</font>(__u32));
00018 }
00019 
00020 
00021 EntriesBlock::~EntriesBlock() {
00022         free(block);
00023 }
00024 
00025 
00026 <font class="keywordtype">void</font> EntriesBlock::setCount(<font class="keywordtype">int</font> count) {
00027         __u32 rawCount = archtosword32(count);
00028         memcpy(block, &amp;rawCount, <font class="keyword">sizeof</font>(__u32));
00029 }
00030 
00031 
00032 <font class="keywordtype">int</font> EntriesBlock::getCount() {
00033         __u32 count = 0;
00034         memcpy(&amp;count, block, <font class="keyword">sizeof</font>(__u32));
00035         count = swordtoarch32(count);
00036         <font class="keywordflow">return</font> count;
00037 }
00038 
00039 
00040 <font class="keywordtype">void</font> EntriesBlock::getMetaEntry(<font class="keywordtype">int</font> index, <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> *offset, <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> *size) {
00041         __u32 rawOffset = 0;
00042         __u32 rawSize = 0;
00043         *offset = 0;
00044         *size = 0;
00045         <font class="keywordflow">if</font> (index &gt;= getCount())        <font class="comment">// assert index &lt; count</font>
00046                 <font class="keywordflow">return</font>;
00047 
00048         <font class="comment">// first 4 bytes is count, each 6 bytes after is each meta entry</font>
00049         memcpy(&amp;rawOffset, block + METAHEADERSIZE + (index * METAENTRYSIZE), <font class="keyword">sizeof</font>(rawOffset));
00050         memcpy(&amp;rawSize, block + METAHEADERSIZE + (index * METAENTRYSIZE) + <font class="keyword">sizeof</font>(rawOffset), <font class="keyword">sizeof</font>(rawSize));
00051 
00052         *offset = (<font class="keywordtype">unsigned</font> <font class="keywordtype">long</font>)swordtoarch32(rawOffset);
00053         *size   = (<font class="keywordtype">unsigned</font> <font class="keywordtype">long</font>)swordtoarch32(rawSize);
00054 }
00055 
00056 
00057 <font class="keywordtype">void</font> EntriesBlock::setMetaEntry(<font class="keywordtype">int</font> index, <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> offset, <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> size) {
00058         __u32 rawOffset = archtosword32(offset);
00059         __u32 rawSize = archtosword32(size);
00060 
00061         <font class="keywordflow">if</font> (index &gt;= getCount())        <font class="comment">// assert index &lt; count</font>
00062                 <font class="keywordflow">return</font>;
00063 
00064         <font class="comment">// first 4 bytes is count, each 6 bytes after is each meta entry</font>
00065         memcpy(block + METAHEADERSIZE + (index * METAENTRYSIZE), &amp;rawOffset, <font class="keyword">sizeof</font>(rawOffset));
00066         memcpy(block + METAHEADERSIZE + (index * METAENTRYSIZE) + <font class="keyword">sizeof</font>(rawOffset), &amp;rawSize, <font class="keyword">sizeof</font>(rawSize));
00067 }
00068 
00069 
00070 <font class="keyword">const</font> <font class="keywordtype">char</font> *EntriesBlock::getRawData(<font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> *retSize) {
00071         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> max = 4;
00072         <font class="keywordtype">int</font> loop;
00073         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> offset;
00074         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> size;
00075         <font class="keywordflow">for</font> (loop = 0; loop &lt; getCount(); loop++) {
00076                 getMetaEntry(loop, &amp;offset, &amp;size);
00077                 max = ((offset + size) &gt; max) ? (offset + size) : max;
00078         }
00079         *retSize = max;
00080         <font class="keywordflow">return</font> block;
00081 }
00082 
00083 
00084 <font class="keywordtype">int</font> EntriesBlock::addEntry(<font class="keyword">const</font> <font class="keywordtype">char</font> *entry) {
00085         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> dataSize;
00086         getRawData(&amp;dataSize);
00087         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font>  len = strlen(entry);
00088         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> offset;
00089         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> size;
00090         <font class="keywordtype">int</font> count = getCount();
00091         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> dataStart = METAHEADERSIZE + (count * METAENTRYSIZE);
00092         <font class="comment">// new meta entry + new data size + 1 because null </font>
00093         block = (<font class="keywordtype">char</font> *)realloc(block, dataSize + METAENTRYSIZE + len + 1);
00094         <font class="comment">// shift right to make room for new meta entry</font>
00095         memmove(block + dataStart + METAENTRYSIZE, block + dataStart, dataSize - dataStart);
00096 
00097         <font class="keywordflow">for</font> (<font class="keywordtype">int</font> loop = 0; loop &lt; count; loop++) {
00098                 getMetaEntry(loop, &amp;offset, &amp;size);
00099                 <font class="keywordflow">if</font> (offset) {   <font class="comment">// if not a deleted entry</font>
00100                         offset += METAENTRYSIZE;
00101                         setMetaEntry(loop, offset, size);
00102                 }
00103         }
00104 
00105         offset = dataSize;      <font class="comment">// original dataSize before realloc</font>
00106         size = len + 1;
00107         <font class="comment">// add our text to the end</font>
00108         memcpy(block + offset + METAENTRYSIZE, entry, size);
00109         <font class="comment">// increment count</font>
00110         setCount(count + 1);
00111         <font class="comment">// add our meta entry</font>
00112         setMetaEntry(count, offset + METAENTRYSIZE, size);
00113         <font class="comment">// return index of our new entry</font>
00114         <font class="keywordflow">return</font> count;
00115 }
00116 
00117 
00118 <font class="keyword">const</font> <font class="keywordtype">char</font> *EntriesBlock::getEntry(<font class="keywordtype">int</font> entryIndex) {
00119         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> offset;
00120         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> size;
00121         <font class="keyword">static</font> <font class="keywordtype">char</font> *empty = <font class="stringliteral">""</font>;
00122 
00123         getMetaEntry(entryIndex, &amp;offset, &amp;size);
00124         <font class="keywordflow">return</font> (offset) ? block+offset : empty;
00125 }
00126 
00127 
00128 <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> EntriesBlock::getEntrySize(<font class="keywordtype">int</font> entryIndex) {
00129         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> offset;
00130         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> size;
00131 
00132         getMetaEntry(entryIndex, &amp;offset, &amp;size);
00133         <font class="keywordflow">return</font> (offset) ? size : 0;
00134 }
00135 
00136 
00137 <font class="keywordtype">void</font> EntriesBlock::removeEntry(<font class="keywordtype">int</font> entryIndex) {
00138         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> offset;
00139         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> size, size2;
00140         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> dataSize;
00141         getRawData(&amp;dataSize);
00142         getMetaEntry(entryIndex, &amp;offset, &amp;size);
00143         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> len = size - 1;
00144         <font class="keywordtype">int</font> count = getCount();
00145         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> dataStart = METAHEADERSIZE + (count * METAENTRYSIZE);
00146 
00147         <font class="keywordflow">if</font> (!offset)    <font class="comment">// already deleted</font>
00148                 <font class="keywordflow">return</font>;
00149 
00150         <font class="comment">// shift left to retrieve space used for old entry</font>
00151         memmove(block + offset, block + offset + size, dataSize - (offset + size));
00152 
00153         <font class="comment">// fix offset for all entries after our entry that were shifted left</font>
00154         <font class="keywordflow">for</font> (<font class="keywordtype">int</font> loop = entryIndex + 1; loop &lt; count; loop++) {
00155                 getMetaEntry(loop, &amp;offset, &amp;size2);
00156                 <font class="keywordflow">if</font> (offset) {   <font class="comment">// if not a deleted entry</font>
00157                         offset -= size;
00158                         setMetaEntry(loop, offset, size2);
00159                 }
00160         }
00161 
00162         <font class="comment">// zero out our meta entry</font>
00163         setMetaEntry(entryIndex, 0L, 0);
00164 }
00165 
00166 
</pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:12:59 2002 for The Sword Project by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>