~ubuntu-branches/ubuntu/lucid/sword/lucid

« back to all changes in this revision

Viewing changes to src/modules/common/entriesblk.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Marsden, Jonathan Marsden, Dmitrijs Ledkovs, Closed Bugs
  • Date: 2009-05-30 11:55:55 UTC
  • mfrom: (1.1.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090530115555-1tdz23ekn0pk1va1
Tags: 1.6.0+dfsg-1
[ Jonathan Marsden ]
* New upstream release. (Closes: #507960) (LP: #320558)
* debian/patches/02_libver.diff:
  - Bump SONAME to 8 -- SWORD 1.6 is not backward compatible with 1.5.11.
* debian/patches/series:
  - Remove 10_diatheke.diff -- included in upstream source.
* debian/patches/:
  - Remove several old unused .diff files.
  - Add 11_regex_only_when_needed.diff to conditionally include regex lib.
  - Add 12_fix_compiler_warnings.diff to remove all compiler warnings.
  - Add 13_fix_osis2mod_compression_default.diff from upstream svn.
  - Add 14_closing_section_not_chapter.diff from upstream svn.
* debian/libsword7.*: 
  - Rename to libsword8.*
  - Change libsword7 to libsword8 within files.
* debian/rules: 
  - SONAME bump to 8.
  - Set library version check to >= 1.6
* debian/control:
  - Change libsword7 to libsword8.
  - Add libsword7 to Conflicts.
  - Fix case of sword to SWORD in package descriptions.
  - Bump Standards-Version to 3.8.1 (no changes needed).
  - Fix section for libsword-dbg to avoid lintian warning.
* debian/rules:
  - Add DFSG get-orig-source target.
* debian/copyright:
  - Fix various mistakes in initial attempt to document copyrights.

[ Dmitrijs Ledkovs ]
* debian/rules: Added utils.mk to use missing-files target and call it on
  each build.
* debian/libsword-dev.install: Added libsword.la, previously missing.
* debian/libsword7.install: Added missing libicu translit files.
* debian/control:
  - Updated all uses of SWORD version to 1.6
  - Added libsword-dbg package
* debian/watch: Fixed a small mistake which was resulting in extra "."
  in final version name.
* debian/rules: simplified manpage processing.
* debian/libsword8.lintian-overrides: added override for module
  installation directory.
* debian/copyright: Updated with information about everyfile.
  Closes: #513448 LP: #322638
* debian/diatheke.examples: moved examples here from the diatheke.install
* debian/rules:
  - enabled shell script based testsuite
  - added commented out cppunit testsuite
* debian/patches/40_missing_includes.diff: 
  - added several missing stdio.h includes to prevent FTBFS of testsuite.

[ Closed Bugs ]
* FTBFS on intrepid (LP: #305172)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
 
3
 *      CrossWire Bible Society
 
4
 *      P. O. Box 2528
 
5
 *      Tempe, AZ  85280-2528
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify it
 
8
 * under the terms of the GNU General Public License as published by the
 
9
 * Free Software Foundation version 2.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * General Public License for more details.
 
15
 *
 
16
 */
 
17
 
 
18
#include <entriesblk.h>
 
19
#include <stdlib.h>
 
20
#include <string.h>
 
21
 
 
22
SWORD_NAMESPACE_START
 
23
 
 
24
const int EntriesBlock::METAHEADERSIZE = 4;
 
25
        // count(4);
 
26
const int EntriesBlock::METAENTRYSIZE = 8;
 
27
        // offset(4); size(4);
 
28
 
 
29
EntriesBlock::EntriesBlock(const char *iBlock, unsigned long size) {
 
30
        if (size) {
 
31
                block = (char *)calloc(1, size);
 
32
                memcpy(block, iBlock, size);
 
33
        }
 
34
        else {
 
35
                block = (char *)calloc(1, sizeof(__u32));
 
36
        }
 
37
}
 
38
 
 
39
 
 
40
EntriesBlock::EntriesBlock() {
 
41
        block = (char *)calloc(1, sizeof(__u32));
 
42
}
 
43
 
 
44
 
 
45
EntriesBlock::~EntriesBlock() {
 
46
        free(block);
 
47
}
 
48
 
 
49
 
 
50
void EntriesBlock::setCount(int count) {
 
51
        __u32 rawCount = archtosword32(count);
 
52
        memcpy(block, &rawCount, sizeof(__u32));
 
53
}
 
54
 
 
55
 
 
56
int EntriesBlock::getCount() {
 
57
        __u32 count = 0;
 
58
        memcpy(&count, block, sizeof(__u32));
 
59
        count = swordtoarch32(count);
 
60
        return count;
 
61
}
 
62
 
 
63
 
 
64
void EntriesBlock::getMetaEntry(int index, unsigned long *offset, unsigned long *size) {
 
65
        __u32 rawOffset = 0;
 
66
        __u32 rawSize = 0;
 
67
        *offset = 0;
 
68
        *size = 0;
 
69
        if (index >= getCount())        // assert index < count
 
70
                return;
 
71
 
 
72
        // first 4 bytes is count, each 6 bytes after is each meta entry
 
73
        memcpy(&rawOffset, block + METAHEADERSIZE + (index * METAENTRYSIZE), sizeof(rawOffset));
 
74
        memcpy(&rawSize, block + METAHEADERSIZE + (index * METAENTRYSIZE) + sizeof(rawOffset), sizeof(rawSize));
 
75
 
 
76
        *offset = (unsigned long)swordtoarch32(rawOffset);
 
77
        *size   = (unsigned long)swordtoarch32(rawSize);
 
78
}
 
79
 
 
80
 
 
81
void EntriesBlock::setMetaEntry(int index, unsigned long offset, unsigned long size) {
 
82
        __u32 rawOffset = archtosword32(offset);
 
83
        __u32 rawSize = archtosword32(size);
 
84
 
 
85
        if (index >= getCount())        // assert index < count
 
86
                return;
 
87
 
 
88
        // first 4 bytes is count, each 6 bytes after is each meta entry
 
89
        memcpy(block + METAHEADERSIZE + (index * METAENTRYSIZE), &rawOffset, sizeof(rawOffset));
 
90
        memcpy(block + METAHEADERSIZE + (index * METAENTRYSIZE) + sizeof(rawOffset), &rawSize, sizeof(rawSize));
 
91
}
 
92
 
 
93
 
 
94
const char *EntriesBlock::getRawData(unsigned long *retSize) {
 
95
        unsigned long max = 4;
 
96
        int loop;
 
97
        unsigned long offset;
 
98
        unsigned long size;
 
99
        for (loop = 0; loop < getCount(); loop++) {
 
100
                getMetaEntry(loop, &offset, &size);
 
101
                max = ((offset + size) > max) ? (offset + size) : max;
 
102
        }
 
103
        *retSize = max;
 
104
        return block;
 
105
}
 
106
 
 
107
 
 
108
int EntriesBlock::addEntry(const char *entry) {
 
109
        unsigned long dataSize;
 
110
        getRawData(&dataSize);
 
111
        unsigned long  len = strlen(entry);
 
112
        unsigned long offset;
 
113
        unsigned long size;
 
114
        int count = getCount();
 
115
        unsigned long dataStart = METAHEADERSIZE + (count * METAENTRYSIZE);
 
116
        // new meta entry + new data size + 1 because null 
 
117
        block = (char *)realloc(block, dataSize + METAENTRYSIZE + len + 1);
 
118
        // shift right to make room for new meta entry
 
119
        memmove(block + dataStart + METAENTRYSIZE, block + dataStart, dataSize - dataStart);
 
120
 
 
121
        for (int loop = 0; loop < count; loop++) {
 
122
                getMetaEntry(loop, &offset, &size);
 
123
                if (offset) {   // if not a deleted entry
 
124
                        offset += METAENTRYSIZE;
 
125
                        setMetaEntry(loop, offset, size);
 
126
                }
 
127
        }
 
128
 
 
129
        offset = dataSize;      // original dataSize before realloc
 
130
        size = len + 1;
 
131
        // add our text to the end
 
132
        memcpy(block + offset + METAENTRYSIZE, entry, size);
 
133
        // increment count
 
134
        setCount(count + 1);
 
135
        // add our meta entry
 
136
        setMetaEntry(count, offset + METAENTRYSIZE, size);
 
137
        // return index of our new entry
 
138
        return count;
 
139
}
 
140
 
 
141
 
 
142
const char *EntriesBlock::getEntry(int entryIndex) {
 
143
        unsigned long offset;
 
144
        unsigned long size;
 
145
        static const char *empty = "";
 
146
 
 
147
        getMetaEntry(entryIndex, &offset, &size);
 
148
        return (offset) ? block+offset : empty;
 
149
}
 
150
 
 
151
 
 
152
unsigned long EntriesBlock::getEntrySize(int entryIndex) {
 
153
        unsigned long offset;
 
154
        unsigned long size;
 
155
 
 
156
        getMetaEntry(entryIndex, &offset, &size);
 
157
        return (offset) ? size : 0;
 
158
}
 
159
 
 
160
 
 
161
void EntriesBlock::removeEntry(int entryIndex) {
 
162
        unsigned long offset;
 
163
        unsigned long size, size2;
 
164
        unsigned long dataSize;
 
165
        getRawData(&dataSize);
 
166
        getMetaEntry(entryIndex, &offset, &size);
 
167
        int count = getCount();
 
168
 
 
169
        if (!offset)    // already deleted
 
170
                return;
 
171
 
 
172
        // shift left to retrieve space used for old entry
 
173
        memmove(block + offset, block + offset + size, dataSize - (offset + size));
 
174
 
 
175
        // fix offset for all entries after our entry that were shifted left
 
176
        for (int loop = entryIndex + 1; loop < count; loop++) {
 
177
                getMetaEntry(loop, &offset, &size2);
 
178
                if (offset) {   // if not a deleted entry
 
179
                        offset -= size;
 
180
                        setMetaEntry(loop, offset, size2);
 
181
                }
 
182
        }
 
183
 
 
184
        // zero out our meta entry
 
185
        setMetaEntry(entryIndex, 0L, 0);
 
186
}
 
187
 
 
188
 
 
189
SWORD_NAMESPACE_END