Browse Source

EPT parser now has conversion functions for arrays

master
Macoy Madson 6 years ago
parent
commit
84baa0a111
4 changed files with 143 additions and 0 deletions
  1. +93
    -0
      ept/eptParser.cpp
  2. +17
    -0
      ept/eptParser.hpp
  3. +4
    -0
      ept/eptSpecification.txt
  4. +29
    -0
      ept/eptSpecification.txt~

+ 93
- 0
ept/eptParser.cpp View File

@ -315,6 +315,99 @@ bool attrToBool(const std::string& attr)
if (attr=="true") return true;
return false;
}
std::string getArrayValue(const std::string& attr, int index)
{
unsigned int openPos = attr.find("{");
unsigned int closePos = attr.find("}");
std::string inArray = attr.substr(openPos, closePos - openPos);
//getArrayValue won't do bounds checking or see if it's a malformed
//array because it needs to be fast. It will trust that getArrayLength was
//checked first
if (index == 0)
{
//Trim for first element
unsigned int startPos = 0;
unsigned int endPos = inArray.length();
for (unsigned int i = 0; i < inArray.length(); ++i)
{
if (inArray[i] != ' ' && startPos==0) startPos = i;
if (inArray[i] == '}' || (inArray[i] == ',' && inArray[i-1] != '\\'))
{
endPos = i;
break;
}
}
return inArray.substr(startPos, endPos - startPos);
}
int counter = 0;
for (unsigned int i = 0; i < inArray.length(); ++i)
{
if (inArray[i] == ',' && inArray[i - 1] != '\\') //Found nondelimited comma
{
counter++;
if (counter == index) //Found the desired element
{
unsigned int startPos = i + 1;
unsigned int endPos = inArray.length();
for (unsigned int n = i + 1; n < inArray.length(); n++)
{
if (startPos == i + 1 && inArray[n] != ' ') startPos = n;
if (inArray[n]=='}' || (inArray[n] == ',' && inArray[n - 1] != '\\'))
{
endPos = n;
break;
}
}
return inArray.substr(startPos, endPos - i - 2);
}
}
}
//Item doesn't exist (out of bounds, malformed array, etc.)
return "";
}
float attrToArrayFloat(const std::string& attr, int index)
{
return attrToFloat(getArrayValue(attr, index));
}
int attrToArrayInt(const std::string& attr, int index)
{
return attrToInt(getArrayValue(attr, index));
}
bool attrToArrayBool(const std::string& attr, int index)
{
return attrToBool(getArrayValue(attr, index));
}
std::string attrToArrayStr(const std::string& attr, int index)
{
std::string val = getArrayValue(attr, index);
//Parse out comma delimiters
for (unsigned int i = 0; i < val.length(); ++i)
{
if (val[i]=='\\' && val[i + 1]==',') val.erase(i, 1);
}
return val;
}
//Returns 0 if the attribute is not an array
int attrToArrayLength(const std::string& attr)
{
unsigned int openPos = attr.find("{");
unsigned int closePos = attr.find("}");
//Not an array if no curly braces or nothing between the curly braces ( {} )
if (openPos >= attr.length() || closePos >= attr.length() || closePos - openPos == 1)
{
//Malformed or not an array; return 0
return 0;
}
//Length is determined by counting the number of commas
unsigned int length = 1;
for (unsigned int i = openPos + 1; i < closePos; i++)
{
if (attr[i]==',' && attr[i - 1] != '\\') length++;
}
return length;
}
std::string floatToAttr(float in)
{
std::ostringstream s;


+ 17
- 0
ept/eptParser.hpp View File

@ -63,6 +63,23 @@ class eptParser
float attrToFloat(const std::string&);
int attrToInt(const std::string&);
bool attrToBool(const std::string&);
//Array utility functions
//Arrays have the following notation:
//myArray={1, -2.34, false, This is a string\, charles!};
//You MUST put a space after the commas! {arr, arr}, NOT {arr,arr}
//No, arrays cannot be nested. Could they be in the future? yes (TODO)
//They do not have to be homogenous, but you need to call the right functions
//for each respective value. If the value is not an array, attrToArrayLength
//will return a length of 0. All other functions will return default null values
float attrToArrayFloat(const std::string&, int index);
int attrToArrayInt(const std::string&, int index);
bool attrToArrayBool(const std::string&, int index);
std::string attrToArrayStr(const std::string&, int index);
//Returns 0 if the attribute is not an array (or the array is empty; note
//that an empty array MUST be {} - it cannot have spaces etc.
int attrToArrayLength(const std::string&);
//Convert values into attributes
std::string floatToAttr(float);
std::string intToAttr(int);
#endif

+ 4
- 0
ept/eptSpecification.txt View File

@ -13,6 +13,10 @@ groupName:
-Value can be anything. Values are terminated by ";", so no quotation
marks are necessary or needed for strings. To EPT, ints, strings, etc.
are all the same thing as they are stored as mere strings.
-Value conversion functions are provided for ints, floats, strings (no need
for quotation marks), bools ('true' or 'false'), and arrays (surrounded by
brackets ({ and }); empty array is {}, items are delimited by ', ', and homogenity
isn't required)
-Multiline attributes are supported; everything will be extracted until the next semicolon
-Header is space sensitive


+ 29
- 0
ept/eptSpecification.txt~ View File

@ -0,0 +1,29 @@
----------------
EPT SPECIFICATION
----------------
-Format:
<fileName.version>
//Comment (SEE NOTE!)
groupName:
attribute=value;
-WARNING! Attributes are sorted in the map, meaning if you have any
order dependency, you must be conscious of the attribute's name!
-Value can be anything. Values are terminated by ";", so no quotation
marks are necessary or needed for strings. To EPT, ints, strings, etc.
are all the same thing as they are stored as mere strings.
-Multiline attributes are supported; everything will be extracted until the next semicolon
-Header is space sensitive
-Using : is NOT OK in an attribute definition! This is mainly a result of the
precendence I coded for, so it's probably not *that* hard to change if you need to
-NOTE! Comments use "//" and operate on the ENTIRE line! This means that
test=This is a test; //Here is an example attribute
will IGNORE THE ENTIRE LINE!
-It is NOT OK to use "." in a group definition (think about dotted notation). "." is OK
in an attribute name or definition.
-A group definition ends the instant a line contains a ':' (a new group definition)

Loading…
Cancel
Save