Skip to content

Commit

Permalink
added support for 'append-array' using the += operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzmbrzl committed Sep 11, 2018
1 parent d9001ce commit 58b72bd
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 19 deletions.
29 changes: 23 additions & 6 deletions ArmaFiles/src/raven/config/ArrayEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class ArrayEntry extends FieldEntry {
* Indicating whether this is a nested array-entry
*/
protected boolean nested;
/**
* Indicates whether this array appends to an existing one (it was declared
* using the += operator)
*/
protected boolean appends;


/**
Expand All @@ -32,10 +37,11 @@ public class ArrayEntry extends FieldEntry {
* @param content
* The {@linkplain ArrayStruct} representing this array's content
*/
public ArrayEntry(String varName, ArrayStruct content) {
public ArrayEntry(String varName, ArrayStruct content, boolean appends) {
super(varName);
this.content = content;
this.nested = varName == null;
this.appends = appends;
}

@Override
Expand All @@ -52,12 +58,13 @@ public byte getType() {
* The reader to use as a data source
* @param isNested
* Whether the array to be read is nested inside another array (i.e.
* does not have a variable name)
* does not have a variable name) * @param plusEqual Indicates
* whether this array has been declared via the += operator
* @return The created entry
* @throws IOException
* @throws RapificationException
*/
protected static ArrayEntry fromRapified(ByteReader reader, boolean isNested)
protected static ArrayEntry fromRapified(ByteReader reader, boolean isNested, boolean plusEqual)
throws IOException, RapificationException {
String varName = null;

Expand All @@ -71,7 +78,7 @@ protected static ArrayEntry fromRapified(ByteReader reader, boolean isNested)

ArrayStruct content = ArrayStruct.fromRapified(reader);

return new ArrayEntry(varName, content);
return new ArrayEntry(varName, content, plusEqual);
}

/**
Expand All @@ -84,13 +91,15 @@ protected static ArrayEntry fromRapified(ByteReader reader, boolean isNested)
* @param varName
* The variable name this array is being assigned to or
* <code>null</code> if this is a nested array
* @param plusEqual
* Indicates whether this array has been declared via the += operator
* @return The created entry
* @throws IOException
*/
protected static ArrayEntry fromText(TextReader reader, String varName) throws IOException {
protected static ArrayEntry fromText(TextReader reader, String varName, boolean plusEqual) throws IOException {
reader.consumeWhithespace();

return new ArrayEntry(varName, ArrayStruct.fromText(reader));
return new ArrayEntry(varName, ArrayStruct.fromText(reader), plusEqual);
}

/**
Expand Down Expand Up @@ -137,4 +146,12 @@ public String getFieldValueString() {
return content.toText();
}

/**
* Indicates whether this array appends to an existing one (it was declared
* using the += operator)
*/
public boolean isAppending() {
return appends;
}

}
4 changes: 2 additions & 2 deletions ArmaFiles/src/raven/config/ArrayStruct.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected static ArrayStruct fromRapified(ByteReader reader) throws IOException,
content[i] = ValueEntry.fromRapified(reader, true);
break;
case NESTED_ARRAY:
content[i] = ArrayEntry.fromRapified(reader, true);
content[i] = ArrayEntry.fromRapified(reader, true, false);
break;
default:
throw new RapificationException("Unknown or unexpected data type in array struct: " + type);
Expand Down Expand Up @@ -107,7 +107,7 @@ protected static ArrayStruct fromText(TextReader reader) throws IOException {
switch (reader.peek()) {
case '{':
// nested ArrayEntry
content.add(ArrayEntry.fromText(reader, null));
content.add(ArrayEntry.fromText(reader, null, false));
break;
case ',':
// consume comma and all following WS
Expand Down
8 changes: 6 additions & 2 deletions ArmaFiles/src/raven/config/ConfigClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ protected static ConfigClass fromRapified(String className, ByteReader reader)

for (int i = 0; i < entryCount; i++) {
byte entryType = reader.readByte();
boolean plusEqual = false;

switch (entryType) {
case ConfigClassEntry.SUBCLASS:
Expand All @@ -125,8 +126,12 @@ protected static ConfigClass fromRapified(String className, ByteReader reader)
case ConfigClassEntry.ASSIGNMENT:
entries[i] = ValueEntry.fromRapified(reader, false);
break;
case ConfigClassEntry.PLUSEQUAL_ARRAY:
// skip the next four bytes
reader.skip(4);
plusEqual = true;
case ConfigClassEntry.ARRAY:
entries[i] = ArrayEntry.fromRapified(reader, false);
entries[i] = ArrayEntry.fromRapified(reader, false, plusEqual);
break;
case ConfigClassEntry.EXTERN:
entries[i] = new SubclassEntry(new ConfigClass(reader.readString(), "", new ConfigClassEntry[0]));
Expand Down Expand Up @@ -447,5 +452,4 @@ public String toText() {

return builder.toString();
}

}
30 changes: 23 additions & 7 deletions ArmaFiles/src/raven/config/ConfigClassEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public abstract class ConfigClassEntry implements ITextifyable {
* The entry type indicating a delete-statement
*/
public static final byte DELETE = 4;
/**
* The entry type indicating an array that has been declared via the
* "+="-operator
*/
public static final byte PLUSEQUAL_ARRAY = 5;

/**
* Gets the type of this entry
Expand Down Expand Up @@ -69,15 +74,26 @@ protected static ConfigClassEntry fromText(TextReader reader) throws IOException

reader.consumeWhithespace();

if (reader.peek() == (int) '=') {
int c = reader.peek();

if (c == (int) '=' || c == (int) '+') {
// it is an assignment -> Either ArrayEntry or ValueEntry
// consume =
reader.read();

if (c == (int) '+') {
if (!isArray) {
throw new ConfigException("Encountered + after non-array definition (most likely part of +=)!");
} else {
reader.expect('+');
}
}

reader.expect('=');

reader.consumeWhithespace();

if (isArray) {
// it's an array
return ArrayEntry.fromText(reader, id);
return ArrayEntry.fromText(reader, id, c == (int) '+');
} else {
// it's a value
return ValueEntry.fromText(reader, id);
Expand All @@ -90,11 +106,11 @@ protected static ConfigClassEntry fromText(TextReader reader) throws IOException

String name = reader.readWord();
reader.consumeWhithespace();
if(reader.peek() == ';') {

if (reader.peek() == ';') {
return new SubclassEntry(new ConfigClass(name, "", new ConfigClassEntry[0]));
} else {
return SubclassEntry.fromText(reader, name);
return SubclassEntry.fromText(reader, name);
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions ArmaFiles/src/raven/config/FieldEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ public String getVarName() {
public boolean hasVarName() {
return varName != null;
}

@Override
public String toText() {
return (hasVarName() ? getVarName() + " = " : "") + getFieldValueString();
return (hasVarName()
? (getVarName() + " = "
+ ((this instanceof ArrayEntry && ((ArrayEntry) this).isAppending()) ? "+" : ""))
: "") + getFieldValueString();
}

/**
Expand Down
13 changes: 13 additions & 0 deletions ArmaFiles/src/raven/misc/ByteReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,17 @@ public void close() throws IOException {
this.source.close();
}

/**
* Skips the given amount of bytes
*
* @param amount
* The amount of bytes to skip
* @throws IOException
*/
public void skip(int amount) throws IOException {
for (int i = 0; i < amount; i++) {
read();
}
}

}
13 changes: 13 additions & 0 deletions ArmaFiles/src/raven/misc/TextReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,17 @@ public void close() throws IOException {
this.source.close();
}

/**
* Skips the given amount of bytes
*
* @param amount
* The amount of bytes to skip
* @throws IOException
*/
public void skip(int amount) throws IOException {
for (int i = 0; i < amount; i++) {
read();
}
}

}

0 comments on commit 58b72bd

Please sign in to comment.