The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EntriesBlock Class Reference

#include <entriesblk.h>

+ Collaboration diagram for EntriesBlock:

Public Member Functions

int addEntry (const char *entry)
 
 EntriesBlock (const char *iBlock, unsigned long size)
 
 EntriesBlock ()
 
int getCount ()
 
const char * getEntry (int entryIndex)
 
unsigned long getEntrySize (int entryIndex)
 
const char * getRawData (unsigned long *size)
 
void removeEntry (int entryIndex)
 
 ~EntriesBlock ()
 

Private Member Functions

void getMetaEntry (int index, unsigned long *offset, unsigned long *size)
 
void setCount (int count)
 
void setMetaEntry (int index, unsigned long offset, unsigned long size)
 

Private Attributes

char * block
 

Static Private Attributes

static const int METAENTRYSIZE = 8
 
static const int METAHEADERSIZE = 4
 

Detailed Description

Definition at line 32 of file entriesblk.h.

Constructor & Destructor Documentation

EntriesBlock::EntriesBlock ( const char *  iBlock,
unsigned long  size 
)

Definition at line 34 of file entriesblk.cpp.

34  {
35  if (size) {
36  block = (char *)calloc(1, size);
37  memcpy(block, iBlock, size);
38  }
39  else {
40  block = (char *)calloc(1, sizeof(SW_u32));
41  }
42 }
char * block
Definition: entriesblk.h:37
int size
Definition: regex.c:5043
unsigned int SW_u32
Definition: sysdata.h:41
EntriesBlock::EntriesBlock ( )

Definition at line 45 of file entriesblk.cpp.

45  {
46  block = (char *)calloc(1, sizeof(SW_u32));
47 }
char * block
Definition: entriesblk.h:37
unsigned int SW_u32
Definition: sysdata.h:41
EntriesBlock::~EntriesBlock ( )

Definition at line 50 of file entriesblk.cpp.

50  {
51  free(block);
52 }
char * block
Definition: entriesblk.h:37
free(preg->fastmap)

Member Function Documentation

int EntriesBlock::addEntry ( const char *  entry)

Definition at line 113 of file entriesblk.cpp.

113  {
114  unsigned long dataSize;
115  getRawData(&dataSize);
116  unsigned long len = strlen(entry);
117  unsigned long offset;
118  unsigned long size;
119  int count = getCount();
120  unsigned long dataStart = METAHEADERSIZE + (count * METAENTRYSIZE);
121  // new meta entry + new data size + 1 because null
122  block = (char *)realloc(block, dataSize + METAENTRYSIZE + len + 1);
123  // shift right to make room for new meta entry
124  memmove(block + dataStart + METAENTRYSIZE, block + dataStart, dataSize - dataStart);
125 
126  for (int loop = 0; loop < count; loop++) {
127  getMetaEntry(loop, &offset, &size);
128  if (offset) { // if not a deleted entry
129  offset += METAENTRYSIZE;
130  setMetaEntry(loop, offset, size);
131  }
132  }
133 
134  offset = dataSize; // original dataSize before realloc
135  size = len + 1;
136  // add our text to the end
137  memcpy(block + offset + METAENTRYSIZE, entry, size);
138  // increment count
139  setCount(count + 1);
140  // add our meta entry
141  setMetaEntry(count, offset + METAENTRYSIZE, size);
142  // return index of our new entry
143  return count;
144 }
static const int METAHEADERSIZE
Definition: entriesblk.h:33
void setCount(int count)
Definition: entriesblk.cpp:55
char * block
Definition: entriesblk.h:37
const char * getRawData(unsigned long *size)
Definition: entriesblk.cpp:99
char * realloc()
void setMetaEntry(int index, unsigned long offset, unsigned long size)
Definition: entriesblk.cpp:86
int size
Definition: regex.c:5043
static const int METAENTRYSIZE
Definition: entriesblk.h:34
void getMetaEntry(int index, unsigned long *offset, unsigned long *size)
Definition: entriesblk.cpp:69
int getCount()
Definition: entriesblk.cpp:61
int EntriesBlock::getCount ( )

Definition at line 61 of file entriesblk.cpp.

61  {
62  SW_u32 count = 0;
63  memcpy(&count, block, sizeof(SW_u32));
64  count = swordtoarch32(count);
65  return count;
66 }
char * block
Definition: entriesblk.h:37
#define swordtoarch32(x)
Definition: sysdata.h:94
unsigned int SW_u32
Definition: sysdata.h:41
const char * EntriesBlock::getEntry ( int  entryIndex)

Definition at line 147 of file entriesblk.cpp.

147  {
148  unsigned long offset;
149  unsigned long size;
150  static const char *empty = "";
151 
152  getMetaEntry(entryIndex, &offset, &size);
153  return (offset) ? block+offset : empty;
154 }
char * block
Definition: entriesblk.h:37
int size
Definition: regex.c:5043
void getMetaEntry(int index, unsigned long *offset, unsigned long *size)
Definition: entriesblk.cpp:69
unsigned long EntriesBlock::getEntrySize ( int  entryIndex)

Definition at line 157 of file entriesblk.cpp.

157  {
158  unsigned long offset;
159  unsigned long size;
160 
161  getMetaEntry(entryIndex, &offset, &size);
162  return (offset) ? size : 0;
163 }
int size
Definition: regex.c:5043
void getMetaEntry(int index, unsigned long *offset, unsigned long *size)
Definition: entriesblk.cpp:69
void EntriesBlock::getMetaEntry ( int  index,
unsigned long *  offset,
unsigned long *  size 
)
private

Definition at line 69 of file entriesblk.cpp.

69  {
70  SW_u32 rawOffset = 0;
71  SW_u32 rawSize = 0;
72  *offset = 0;
73  *size = 0;
74  if (index >= getCount()) // assert index < count
75  return;
76 
77  // first 4 bytes is count, each 6 bytes after is each meta entry
78  memcpy(&rawOffset, block + METAHEADERSIZE + (index * METAENTRYSIZE), sizeof(rawOffset));
79  memcpy(&rawSize, block + METAHEADERSIZE + (index * METAENTRYSIZE) + sizeof(rawOffset), sizeof(rawSize));
80 
81  *offset = (unsigned long)swordtoarch32(rawOffset);
82  *size = (unsigned long)swordtoarch32(rawSize);
83 }
static const int METAHEADERSIZE
Definition: entriesblk.h:33
char * block
Definition: entriesblk.h:37
#define swordtoarch32(x)
Definition: sysdata.h:94
int size
Definition: regex.c:5043
static const int METAENTRYSIZE
Definition: entriesblk.h:34
unsigned int SW_u32
Definition: sysdata.h:41
int getCount()
Definition: entriesblk.cpp:61
const char * EntriesBlock::getRawData ( unsigned long *  size)

Definition at line 99 of file entriesblk.cpp.

99  {
100  unsigned long max = 4;
101  int loop;
102  unsigned long offset;
103  unsigned long size;
104  for (loop = 0; loop < getCount(); loop++) {
105  getMetaEntry(loop, &offset, &size);
106  max = ((offset + size) > max) ? (offset + size) : max;
107  }
108  *retSize = max;
109  return block;
110 }
char * block
Definition: entriesblk.h:37
int size
Definition: regex.c:5043
void getMetaEntry(int index, unsigned long *offset, unsigned long *size)
Definition: entriesblk.cpp:69
int getCount()
Definition: entriesblk.cpp:61
void EntriesBlock::removeEntry ( int  entryIndex)

Definition at line 166 of file entriesblk.cpp.

166  {
167  unsigned long offset;
168  unsigned long size, size2;
169  unsigned long dataSize;
170  getRawData(&dataSize);
171  getMetaEntry(entryIndex, &offset, &size);
172  int count = getCount();
173 
174  if (!offset) // already deleted
175  return;
176 
177  // shift left to retrieve space used for old entry
178  memmove(block + offset, block + offset + size, dataSize - (offset + size));
179 
180  // fix offset for all entries after our entry that were shifted left
181  for (int loop = entryIndex + 1; loop < count; loop++) {
182  getMetaEntry(loop, &offset, &size2);
183  if (offset) { // if not a deleted entry
184  offset -= size;
185  setMetaEntry(loop, offset, size2);
186  }
187  }
188 
189  // zero out our meta entry
190  setMetaEntry(entryIndex, 0L, 0);
191 }
int size2
Definition: regex.c:5079
char * block
Definition: entriesblk.h:37
const char * getRawData(unsigned long *size)
Definition: entriesblk.cpp:99
void setMetaEntry(int index, unsigned long offset, unsigned long size)
Definition: entriesblk.cpp:86
int size
Definition: regex.c:5043
void getMetaEntry(int index, unsigned long *offset, unsigned long *size)
Definition: entriesblk.cpp:69
int getCount()
Definition: entriesblk.cpp:61
void EntriesBlock::setCount ( int  count)
private

Definition at line 55 of file entriesblk.cpp.

55  {
56  SW_u32 rawCount = archtosword32(count);
57  memcpy(block, &rawCount, sizeof(SW_u32));
58 }
#define archtosword32(x)
Definition: sysdata.h:97
char * block
Definition: entriesblk.h:37
unsigned int SW_u32
Definition: sysdata.h:41
void EntriesBlock::setMetaEntry ( int  index,
unsigned long  offset,
unsigned long  size 
)
private

Definition at line 86 of file entriesblk.cpp.

86  {
87  SW_u32 rawOffset = (SW_u32)archtosword32(offset);
88  SW_u32 rawSize = (SW_u32)archtosword32(size);
89 
90  if (index >= getCount()) // assert index < count
91  return;
92 
93  // first 4 bytes is count, each 6 bytes after is each meta entry
94  memcpy(block + METAHEADERSIZE + (index * METAENTRYSIZE), &rawOffset, sizeof(rawOffset));
95  memcpy(block + METAHEADERSIZE + (index * METAENTRYSIZE) + sizeof(rawOffset), &rawSize, sizeof(rawSize));
96 }
static const int METAHEADERSIZE
Definition: entriesblk.h:33
#define archtosword32(x)
Definition: sysdata.h:97
char * block
Definition: entriesblk.h:37
int size
Definition: regex.c:5043
static const int METAENTRYSIZE
Definition: entriesblk.h:34
unsigned int SW_u32
Definition: sysdata.h:41
int getCount()
Definition: entriesblk.cpp:61

Member Data Documentation

char* EntriesBlock::block
private

Definition at line 37 of file entriesblk.h.

const int EntriesBlock::METAENTRYSIZE = 8
staticprivate

Definition at line 34 of file entriesblk.h.

SWORD_NAMESPACE_START const int EntriesBlock::METAHEADERSIZE = 4
staticprivate

Definition at line 33 of file entriesblk.h.


The documentation for this class was generated from the following files: