Got something working
This commit is contained in:
parent
89426bff98
commit
01348a5adf
|
@ -59,7 +59,7 @@ public class BdfArray implements IBdfType, Iterable<BdfObject>
|
|||
this.elements = new ArrayList<BdfObject>(size);
|
||||
|
||||
for(int i=0;i<size;i++) {
|
||||
this.elements.set(i, new BdfObject(lookupTable));
|
||||
this.elements.add(new BdfObject(lookupTable));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,12 +101,12 @@ public class BdfArray implements IBdfType, Iterable<BdfObject>
|
|||
}
|
||||
|
||||
@Override
|
||||
public int serialize(IBdfDatabase database, int[] locations, int[] map, byte flags)
|
||||
public int serialize(IBdfDatabase database, int[] locations, byte flags)
|
||||
{
|
||||
int pos = 0;
|
||||
|
||||
for(BdfObject o : elements) {
|
||||
pos += o.serialize(database.getPointer(pos), locations, map, (byte)0);
|
||||
pos += o.serialize(database.getPointer(pos), locations, (byte)0);
|
||||
}
|
||||
|
||||
return pos;
|
||||
|
|
|
@ -55,13 +55,19 @@ class BdfLookupTable implements IBdfType
|
|||
}
|
||||
|
||||
@Override
|
||||
public int serialize(IBdfDatabase database, int[] locations, int[] map, byte flags)
|
||||
public int serialize(IBdfDatabase database, int[] locations, byte flags)
|
||||
{
|
||||
int upto = 0;
|
||||
|
||||
for(int i=0;i<map.length;i++)
|
||||
for(int i=0;i<locations.length;i++)
|
||||
{
|
||||
byte[] key = keys.get(map[i]);
|
||||
int loc = locations[i];
|
||||
|
||||
if(loc == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
byte[] key = keys.get(i);
|
||||
|
||||
database.setBytes(key, upto + 1);
|
||||
database.setByte(upto, (byte)key.length);
|
||||
|
@ -78,10 +84,12 @@ class BdfLookupTable implements IBdfType
|
|||
{
|
||||
int size = 0;
|
||||
|
||||
for(int i=0;i<keys.size();i++)
|
||||
for(int i=0;i<locations.length;i++)
|
||||
{
|
||||
// Skip this key if the location is unset (the key has been culled)
|
||||
if(locations[i] == -1) {
|
||||
int loc = locations[i];
|
||||
|
||||
if(loc == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -92,53 +100,9 @@ class BdfLookupTable implements IBdfType
|
|||
return size;
|
||||
}
|
||||
|
||||
// Bubble sort
|
||||
private int[] sortLocations(int[] locations, int[] uses, int[] map)
|
||||
{
|
||||
int[] map_copy = new int[map.length];
|
||||
|
||||
for(int i=0;i<map.length;i++) {
|
||||
map_copy[i] = map[i];
|
||||
}
|
||||
|
||||
for(int i=0; i < map.length; i++)
|
||||
{
|
||||
boolean changed = false;
|
||||
|
||||
for(int j=0; j < map.length - i - 1; j++)
|
||||
{
|
||||
int loc_0 = map[j];
|
||||
int loc_1 = map[j + 1];
|
||||
|
||||
// Swap the index at j+1 and j in locations and uses
|
||||
if(uses[loc_1] > uses[loc_0])
|
||||
{
|
||||
int v_l = locations[loc_0];
|
||||
locations[loc_0] = locations[loc_1];
|
||||
locations[loc_1] = v_l;
|
||||
|
||||
int v_u = uses[loc_0];
|
||||
uses[loc_0] = uses[loc_1];
|
||||
uses[loc_1] = v_u;
|
||||
|
||||
int v_m = map_copy[j];
|
||||
map_copy[j] = map_copy[j+1];
|
||||
map_copy[j+1] = v_m;
|
||||
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(!changed) {
|
||||
return map_copy;
|
||||
}
|
||||
}
|
||||
|
||||
return map_copy;
|
||||
}
|
||||
|
||||
public int[] serializeGetLocations(int[] locations)
|
||||
public int[] serializeGetLocations()
|
||||
{
|
||||
int[] locations = new int[keys.size()];
|
||||
int[] uses = new int[keys.size()];
|
||||
int next = 0;
|
||||
|
||||
|
@ -153,19 +117,8 @@ class BdfLookupTable implements IBdfType
|
|||
locations[i] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
int[] map = new int[next];
|
||||
next = 0;
|
||||
|
||||
for(int i=0;i<locations.length;i++)
|
||||
{
|
||||
if(locations[i] != -1) {
|
||||
map[next] = i;
|
||||
next += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return sortLocations(locations, uses, map);
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -140,13 +140,14 @@ public class BdfNamedList implements IBdfType
|
|||
}
|
||||
|
||||
@Override
|
||||
public int serialize(IBdfDatabase database, int[] locations, int[] map, byte flags)
|
||||
public int serialize(IBdfDatabase database, int[] locations, byte flags)
|
||||
{
|
||||
int pos = 0;
|
||||
|
||||
for(Element o : elements)
|
||||
{
|
||||
int location = locations[o.key];
|
||||
|
||||
byte size_bytes_tag;
|
||||
byte size_bytes;
|
||||
|
||||
|
@ -161,7 +162,7 @@ public class BdfNamedList implements IBdfType
|
|||
size_bytes = 1;
|
||||
}
|
||||
|
||||
int size = o.object.serialize(database.getPointer(pos), locations, map, size_bytes_tag);
|
||||
int size = o.object.serialize(database.getPointer(pos), locations, size_bytes_tag);
|
||||
int offset = pos + size;
|
||||
|
||||
byte[] bytes = DataHelpers.serializeInt(location);
|
||||
|
|
|
@ -419,6 +419,10 @@ public class BdfObject implements IBdfType
|
|||
}
|
||||
}
|
||||
|
||||
public byte getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
private boolean shouldStoreSize(byte b) {
|
||||
return b > 7;
|
||||
}
|
||||
|
@ -509,7 +513,7 @@ public class BdfObject implements IBdfType
|
|||
}
|
||||
|
||||
@Override
|
||||
public int serialize(IBdfDatabase database, int[] locations, int[] map, byte parent_flags)
|
||||
public int serialize(IBdfDatabase database, int[] locations, byte parent_flags)
|
||||
{
|
||||
int size = last_seek;
|
||||
boolean storeSize = shouldStoreSize(type);
|
||||
|
@ -538,11 +542,11 @@ public class BdfObject implements IBdfType
|
|||
switch(type)
|
||||
{
|
||||
case BdfTypes.ARRAY:
|
||||
size = ((BdfArray)object).serialize(database.getPointer(offset), locations, map, (byte)0) + offset;
|
||||
size = ((BdfArray)object).serialize(database.getPointer(offset), locations, (byte)0) + offset;
|
||||
break;
|
||||
|
||||
case BdfTypes.NAMED_LIST:
|
||||
size = ((BdfNamedList)object).serialize(database.getPointer(offset), locations, map, (byte)0) + offset;
|
||||
size = ((BdfNamedList)object).serialize(database.getPointer(offset), locations, (byte)0) + offset;
|
||||
break;
|
||||
|
||||
case BdfTypes.STRING:
|
||||
|
|
|
@ -91,8 +91,7 @@ public class BdfReader
|
|||
|
||||
public BdfDatabase serialize()
|
||||
{
|
||||
int[] locations = new int[lookupTable.size()];
|
||||
int[] map = lookupTable.serializeGetLocations(locations);
|
||||
int[] locations = lookupTable.serializeGetLocations();
|
||||
|
||||
int bdf_size = bdf.serializeSeeker(locations);
|
||||
int lookupTable_size = lookupTable.serializeSeeker(locations);
|
||||
|
@ -115,7 +114,7 @@ public class BdfReader
|
|||
int database_size = bdf_size + lookupTable_size + lookupTable_size_bytes;
|
||||
BdfDatabase database = new BdfDatabase(database_size);
|
||||
|
||||
bdf.serialize(database.getPointer(upto, bdf_size), locations, map, lookupTable_size_tag);
|
||||
bdf.serialize(database.getPointer(upto, bdf_size), locations, lookupTable_size_tag);
|
||||
upto += bdf_size;
|
||||
|
||||
byte[] bytes = DataHelpers.serializeInt(lookupTable_size);
|
||||
|
@ -124,7 +123,7 @@ public class BdfReader
|
|||
database.setByte(i + upto, bytes[i - lookupTable_size_bytes + 4]);
|
||||
}
|
||||
|
||||
lookupTable.serialize(database.getPointer(upto + lookupTable_size_bytes, lookupTable_size), locations, map, (byte)0);
|
||||
lookupTable.serialize(database.getPointer(upto + lookupTable_size_bytes, lookupTable_size), locations, (byte)0);
|
||||
|
||||
return database;
|
||||
}
|
||||
|
@ -167,14 +166,4 @@ public class BdfReader
|
|||
public void serializeHumanReadable(OutputStream stream) throws IOException {
|
||||
serializeHumanReadable(stream, new BdfIndent("", ""));
|
||||
}
|
||||
|
||||
public static BdfReader fromHumanReadable(IBdfDatabase data)
|
||||
{
|
||||
BdfReader reader = new BdfReader();
|
||||
BdfObject bdf = reader.getObject();
|
||||
|
||||
|
||||
|
||||
return reader;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import bdf.data.IBdfDatabase;
|
|||
interface IBdfType
|
||||
{
|
||||
void getLocationUses(int[] locations);
|
||||
int serialize(IBdfDatabase database, int[] locations, int[] map, byte flags);
|
||||
int serialize(IBdfDatabase database, int[] locations, byte flags);
|
||||
int serializeSeeker(int[] locations);
|
||||
|
||||
void serializeHumanReadable(OutputStream stream, BdfIndent indent, int it) throws IOException;
|
||||
|
|
|
@ -1,9 +1,57 @@
|
|||
package tests;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import bdf.classes.IBdfClassManager;
|
||||
import bdf.data.IBdfDatabase;
|
||||
import bdf.file.BdfFileManager;
|
||||
import bdf.types.BdfArray;
|
||||
import bdf.types.BdfIndent;
|
||||
import bdf.types.BdfNamedList;
|
||||
import bdf.types.BdfObject;
|
||||
import bdf.types.BdfReader;
|
||||
import bdf.types.BdfTypes;
|
||||
import bdf.util.FileHelpers;
|
||||
|
||||
public class Tests
|
||||
{
|
||||
private static class Storage implements IBdfClassManager
|
||||
{
|
||||
String name;
|
||||
int type;
|
||||
int age;
|
||||
|
||||
public Storage(String name, int age, int type) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void BdfClassLoad(BdfObject bdf)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void BdfClassSave(BdfObject bdf)
|
||||
{
|
||||
BdfNamedList nl = bdf.getNamedList();
|
||||
|
||||
nl.set("name", bdf.newObject().setString(name));
|
||||
|
||||
if(age != -1) {
|
||||
nl.set("age", bdf.newObject().setAutoInt(age));
|
||||
}
|
||||
|
||||
if(type != -1) {
|
||||
nl.set("type", bdf.newObject().setAutoInt(type));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void displayHex(IBdfDatabase db)
|
||||
{
|
||||
char[] hex_chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
|
@ -23,9 +71,39 @@ public class Tests
|
|||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
public static void main(String[] args) throws IOException
|
||||
{
|
||||
BdfReader reader = new BdfReader();
|
||||
BdfObject bdf = reader.getObject();
|
||||
|
||||
bdf.getKeyLocation("type");
|
||||
bdf.getKeyLocation("qwerty");
|
||||
bdf.getKeyLocation("age");
|
||||
bdf.getKeyLocation("name");
|
||||
|
||||
Storage[] items = {
|
||||
new Storage("test1", -1, 1),
|
||||
new Storage("test2", 69, 2),
|
||||
new Storage("test3", 420, -1),
|
||||
new Storage("test4", 23, 3),
|
||||
new Storage("test5", -1, -1),
|
||||
};
|
||||
|
||||
BdfArray array = bdf.newArray(5);
|
||||
bdf.setArray(array);
|
||||
|
||||
for(int i=0;i<items.length;i++) {
|
||||
items[i].BdfClassSave(array.get(i));
|
||||
}
|
||||
|
||||
reader.serializeHumanReadable(System.out);
|
||||
|
||||
IBdfDatabase db = reader.serialize();
|
||||
displayHex(db);
|
||||
|
||||
reader = new BdfReader(db);
|
||||
|
||||
reader.serializeHumanReadable(System.out);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue