CloverBootloader/rEFIt_UEFI/Platform/plist.h

310 lines
7.4 KiB
C
Raw Normal View History

2020-03-31 17:59:35 +02:00
/*
* plist.h
*
* Created on: 31 Mar 2020
* Author: jief
*/
#ifndef PLATFORM_PLIST_H_
#define PLATFORM_PLIST_H_
/* XML Tags */
#define kXMLTagPList "plist"
#define kXMLTagDict "dict"
#define kXMLTagKey "key"
#define kXMLTagString "string"
#define kXMLTagInteger "integer"
#define kXMLTagData "data"
#define kXMLTagDate "date"
#define kXMLTagFalse "false/"
#define kXMLTagTrue "true/"
#define kXMLTagArray "array"
#define kXMLTagReference "reference"
#define kXMLTagID "ID="
#define kXMLTagIDREF "IDREF="
#define kXMLTagFloat "real"
2020-08-17 21:40:52 +02:00
typedef enum {
kTagTypeNone,
2020-08-18 18:45:44 +02:00
kTagTypeDict, // 1
kTagTypeKey, // 2
kTagTypeString, // 3
kTagTypeInteger,// 4
kTagTypeData, // 5
kTagTypeDate, // 6
kTagTypeFalse, // 7
kTagTypeTrue, // 8
kTagTypeArray, // 9
kTagTypeFloat // 10
2020-08-17 21:40:52 +02:00
} TAG_TYPE;
2020-08-18 18:45:44 +02:00
class TagStruct;
extern XObjArray<TagStruct> gTagsFree;
2020-08-17 21:40:52 +02:00
class TagStruct
{
UINTN type; // type is private. Use is...() functions.
2020-08-18 18:45:44 +02:00
XString8 _string;
INTN _intValue;
float _floatValue;
UINT8 *_data;
UINTN _dataLen;
XObjArray<TagStruct> _dictOrArrayContent;
2020-08-18 18:45:44 +02:00
2020-08-17 21:40:52 +02:00
public:
2020-08-18 18:45:44 +02:00
2020-08-22 15:39:24 +02:00
TagStruct() : type(kTagTypeNone), _string(), _intValue(0), _floatValue(0), _data(0), _dataLen(0), _dictOrArrayContent() {}
2020-08-17 21:40:52 +02:00
TagStruct(const TagStruct& other) = delete; // Can be defined if needed
const TagStruct& operator = ( const TagStruct & ) = delete; // Can be defined if needed
2020-08-22 15:39:24 +02:00
~TagStruct() { delete _data; }
2020-08-18 18:45:44 +02:00
2020-08-22 15:39:24 +02:00
static TagStruct* getEmptyTag();
static TagStruct* getEmptyDictTag();
static TagStruct* getEmptyArrayTag();
2020-08-18 18:45:44 +02:00
void FreeTag();
// Property<XString8> string();
bool isDict() const { return type == kTagTypeDict; }
bool isKey() const { return type == kTagTypeKey; }
bool isString() const { return type == kTagTypeString; }
bool isInt() const { return type == kTagTypeInteger; }
bool isFloat() const { return type == kTagTypeFloat; }
bool isBool() const { return type == kTagTypeTrue || type == kTagTypeFalse; }
bool isData() const { return type == kTagTypeData; }
bool isDate() const { return type == kTagTypeDate; }
bool isArray() const { return type == kTagTypeArray; }
const XString8 getTypeAsXString8() const {
2020-08-18 18:45:44 +02:00
if ( isDict() ) return "Dict"_XS8;
if ( isKey() ) return "Key"_XS8;
if ( isString() ) return "String"_XS8;
if ( isInt() ) return "Int"_XS8;
if ( isFloat() ) return "Float"_XS8;
if ( isBool() ) return "Bool"_XS8;
if ( isData() ) return "Data"_XS8;
if ( isDate() ) return "Date"_XS8;
if ( isArray() ) return "Array"_XS8;
2020-08-18 18:45:44 +02:00
panic("Unknown type %lld : this is bug", type);
}
2020-08-22 15:39:24 +02:00
/*
* getters and setters
*/
/* data */
const UINT8* dataValue() const
{
if ( !isData() ) panic("TagStruct::dataValue() : !isData() ");
return _data;
}
2020-08-18 18:45:44 +02:00
UINT8* dataValue()
{
if ( !isData() ) panic("TagStruct::dataValue() : !isData() ");
return _data;
}
2020-08-22 15:39:24 +02:00
// const XString8& dataStringValue()
// {
// if ( !isData() ) panic("TagStruct::dataStringValue() : !isData() ");
// return _string;
// }
UINTN dataLenValue() const
2020-08-18 18:45:44 +02:00
{
if ( !isData() ) panic("TagStruct::dataLenValue() : !isData() ");
return _dataLen;
}
void setDataValue(UINT8* data, UINTN dataLen)
{
2020-08-19 09:52:07 +02:00
if ( data == NULL ) panic("TagStruct::setDataValue() : _data == NULL ");
2020-08-18 18:45:44 +02:00
_data = data;
_dataLen = dataLen;
type = kTagTypeData;
}
2020-08-22 15:39:24 +02:00
/* date */
2020-08-18 18:45:44 +02:00
const XString8& dateValue()
{
if ( !isDict() ) panic("TagStruct::dictValue() : !isDict() ");
return _string;
}
void setDateValue(const XString8& xstring)
{
if ( xstring.isEmpty() ) panic("TagStruct::setDateValue() : xstring.isEmpty() ");
type = kTagTypeDate;
_string = xstring;
}
2020-08-22 15:39:24 +02:00
/* dict */
const XObjArray<TagStruct>& dictContent() const
2020-08-18 18:45:44 +02:00
{
2020-08-22 15:39:24 +02:00
if ( !isDict() ) panic("TagStruct::dictContent() : !isDict() ");
return _dictOrArrayContent;
2020-08-18 18:45:44 +02:00
}
2020-08-22 15:39:24 +02:00
XObjArray<TagStruct>& dictContent()
2020-08-18 18:45:44 +02:00
{
2020-08-22 15:39:24 +02:00
if ( !isDict() ) panic("TagStruct::dictContent() : !isDict() ");
return _dictOrArrayContent;
2020-08-18 18:45:44 +02:00
}
2020-08-22 15:39:24 +02:00
INTN dictKeyCount() const;
EFI_STATUS dictKeyAndValueAtIndex(INTN id, const TagStruct** key, const TagStruct** value) const;
const TagStruct* dictPropertyForKey(const CHAR8* key ) const;
2020-08-18 18:45:44 +02:00
2020-08-22 15:39:24 +02:00
/* array */
const XObjArray<TagStruct>& arrayContent() const
2020-08-18 18:45:44 +02:00
{
2020-08-22 15:39:24 +02:00
if ( isArray() ) return _dictOrArrayContent;
panic("TagStruct::arrayContent() const : !isArray() ");
2020-08-18 18:45:44 +02:00
}
2020-08-22 15:39:24 +02:00
XObjArray<TagStruct>& arrayContent()
2020-08-18 18:45:44 +02:00
{
2020-08-22 15:39:24 +02:00
if ( isArray() ) return _dictOrArrayContent;
panic("TagStruct::arrayContent() : !isDict() && !isArray() ");
2020-08-18 18:45:44 +02:00
}
2020-08-22 15:39:24 +02:00
/* key */
const XString8& keyStringValue() const
{
2020-08-22 15:39:24 +02:00
if ( !isKey() ) panic("TagStruct::keyStringValue() const : !isKey() ");
return _string;
}
2020-08-22 15:39:24 +02:00
XString8& keyStringValue()
2020-08-18 18:45:44 +02:00
{
2020-08-22 15:39:24 +02:00
if ( !isKey() ) panic("TagStruct::keyStringValue() : !isKey() ");
2020-08-18 18:45:44 +02:00
return _string;
}
2020-08-22 15:39:24 +02:00
void setKeyValue(const XString8& xstring)
2020-08-18 18:45:44 +02:00
{
if ( xstring.isEmpty() ) panic("TagStruct::setKeyValue() : xstring.isEmpty() ");
type = kTagTypeKey;
_string = xstring;
}
2020-08-22 15:39:24 +02:00
/* string */
const XString8& stringValue() const
{
if ( !isString() ) panic("TagStruct::stringValue() : !isString() ");
return _string;
}
XString8& stringValue()
2020-08-18 18:45:44 +02:00
{
if ( !isString() ) panic("TagStruct::stringValue() : !isString() ");
return _string;
}
void setStringValue(const XString8& xstring)
{
// empty string is allowed
//if ( xstring.isEmpty() ) panic("TagStruct::setStringValue() : xstring.isEmpty() ");
type = kTagTypeString;
_string = xstring;
}
2020-08-22 15:39:24 +02:00
/* int */
INTN intValue() const
2020-08-18 18:45:44 +02:00
{
if ( !isInt() ) panic("TagStruct::intValue() : !isInt() ");
return _intValue;
}
void setIntValue(INTN i)
{
type = kTagTypeInteger;
_intValue = i;
}
2020-08-22 15:39:24 +02:00
/* float */
float floatValue() const
2020-08-18 18:45:44 +02:00
{
if ( !isFloat() ) panic("TagStruct::floatValue() : !isFloat() ");
return _floatValue;
}
void setFloatValue(float f)
{
type = kTagTypeFloat;
_floatValue = f;
}
2020-08-22 15:39:24 +02:00
/* bool */
INTN boolValue() const
2020-08-18 18:45:44 +02:00
{
if ( !isBool() ) panic("TagStruct::boolValue() : !isBool() ");
return type == kTagTypeTrue;
}
void setBoolValue(bool b)
{
if ( b ) type = kTagTypeTrue;
else type = kTagTypeFalse;
}
// Convenience method
bool isTrue() const
2020-08-18 18:45:44 +02:00
{
if ( isBool() ) return boolValue();
return false;
}
bool isFalse() const
2020-08-18 18:45:44 +02:00
{
if ( isBool() ) return !boolValue();
return false;
}
bool isTrueOrYy() const
2020-08-18 18:45:44 +02:00
{
if ( isBool() ) return boolValue();
if ( isString() && stringValue().notEmpty() && (stringValue()[0] == 'y' || stringValue()[0] == 'Y') ) return true;
return false;
}
bool isTrueOrYes() const
2020-08-18 18:45:44 +02:00
{
if ( isBool() ) return boolValue();
if ( isString() && stringValue().equal("Yes"_XS8) ) return true;
return false;
}
bool isFalseOrNn() const
2020-08-18 18:45:44 +02:00
{
if ( isBool() ) return !boolValue();
if ( isString() && stringValue().notEmpty() && (stringValue()[0] == 'n' || stringValue()[0] == 'N') ) return true;
return false;
}
2020-08-17 21:40:52 +02:00
};
2020-03-31 17:59:35 +02:00
CHAR8*
XMLDecode (
CHAR8 *src
);
EFI_STATUS
ParseXML(
CONST CHAR8 *buffer,
TagStruct* *dict,
UINT32 bufSize
);
EFI_STATUS
GetNextTag (
UINT8 *buffer,
CHAR8 **tag,
UINT32 *start,
UINT32 *length
);
BOOLEAN
2020-08-22 15:39:24 +02:00
IsPropertyNotNullAndTrue(
const TagStruct* Prop
);
BOOLEAN
2020-08-22 15:39:24 +02:00
IsPropertyNotNullAndFalse(
const TagStruct* Prop
);
INTN
2020-08-22 15:39:24 +02:00
GetPropertyAsInteger(
const TagStruct* Prop,
INTN Default
);
float GetPropertyFloat (const TagStruct* Prop, float Default);
2020-03-31 17:59:35 +02:00
#endif /* PLATFORM_PLIST_H_ */