Skip to content

Commit

Permalink
Use values instead of identifiers in Strings.g.cpp
Browse files Browse the repository at this point in the history
This allows the code generated on newer OS version to build
on older OS version.
  • Loading branch information
gavv committed Feb 3, 2022
1 parent bd2790c commit 12bcc0c
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 230 deletions.
61 changes: 35 additions & 26 deletions script/generate-strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def selector_is_better(a, b):
scope2code = {}
operation2code = {}
error2code = {}
formatID = set()
formatFlag = set()
fmtid2code = {}
fmtflag2code = {}

# fill selector2code
for m in re.finditer(r'(kAudio\S+Property\S+)\s*=\s*(\'\S+\')', defs):
Expand Down Expand Up @@ -119,14 +119,23 @@ def selector_is_better(a, b):
name, code = m.group(1), m.group(2)
error2code[name] = code

# fill formatID and formatFlag
for m in re.finditer(r'(kAudioFormat\S+)\s*=\s*(\S+)', defs):
# fill fmtid2code and fmtflag2code
for m in re.finditer(r'(kAudioFormat\S+)\s*=\s*([^,}]+)', defs):
name, code = m.group(1), m.group(2)
code = code.strip()

if name == 'kAudioFormatFlagsAreAllClear':
continue

if name == 'kAudioFormatFlagsNativeEndian':
continue

if name.startswith('kAudioFormatFlag'):
if code != '0':
formatFlag.add(name)
if code == '0' or '|' in code:
continue
fmtflag2code[name] = code
else:
formatID.add(name)
fmtid2code[name] = code

env = jinja2.Environment(
trim_blocks=True,
Expand All @@ -150,8 +159,8 @@ def selector_is_better(a, b):
std::string ClassIDToString(AudioClassID classID)
{
switch (classID) {
{% for name in sorted(class2code.keys()) %}
case {{ name }}:
{% for name, code in sorted(class2code.items()) %}
case {{ code }}:
return "{{ name }}";
{% endfor %}
default:
Expand All @@ -162,8 +171,8 @@ def selector_is_better(a, b):
std::string PropertySelectorToString(AudioObjectPropertySelector selector)
{
switch (selector) {
{% for name in sorted(selector2code.keys()) %}
case {{ name }}:
{% for name, code in sorted(selector2code.items()) %}
case {{ code }}:
return "{{ name }}";
{% endfor %}
default:
Expand All @@ -174,8 +183,8 @@ def selector_is_better(a, b):
std::string PropertyScopeToString(AudioObjectPropertyScope scope)
{
switch (scope) {
{% for name in sorted(scope2code.keys()) %}
case {{ name }}:
{% for name, code in sorted(scope2code.items()) %}
case {{ code }}:
return "{{ name }}";
{% endfor %}
default:
Expand All @@ -186,8 +195,8 @@ def selector_is_better(a, b):
std::string OperationIDToString(UInt32 operationID)
{
switch (operationID) {
{% for name in sorted(operation2code.keys()) %}
case {{ name }}:
{% for name, code in sorted(operation2code.items()) %}
case {{ code }}:
return "{{ name }}";
{% endfor %}
default:
Expand All @@ -200,32 +209,32 @@ def selector_is_better(a, b):
switch (status) {
case kAudioHardwareNoError:
return "OK";
{% for name in sorted(error2code.keys()) %}
case {{ name }}:
{% for name, code in sorted(error2code.items()) %}
case {{ code }}:
return "{{ name }}";
{% endfor %}
default:
return CodeToString(UInt32(status));
}
}
std::string FormatIDToString(AudioFormatID formatID)
std::string FormatIDToString(AudioFormatID fmtid2code)
{
switch (formatID) {
{% for name in sorted(formatID) %}
case {{ name }}:
switch (fmtid2code) {
{% for name, code in sorted(fmtid2code.items()) %}
case {{ code }}:
return "{{ name }}";
{% endfor %}
default:
return CodeToString(formatID);
return CodeToString(fmtid2code);
}
}
std::string FormatFlagsToString(AudioFormatFlags formatFlags)
{
std::string ret;
{% for name in sorted(formatFlag) %}
if (formatFlags & {{ name }}) {
{% for name, code in sorted(fmtflag2code.items()) %}
if (formatFlags & {{ code }}) {
if (!ret.empty()) {
ret += "|";
}
Expand All @@ -245,8 +254,8 @@ def selector_is_better(a, b):
scope2code=scope2code,
operation2code=operation2code,
error2code=error2code,
formatID=formatID,
formatFlag=formatFlag,
fmtid2code=fmtid2code,
fmtflag2code=fmtflag2code,
generator_script=os.path.basename(__file__),
generator_input=input_file,
timestamp=datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S %Y UTC"),
Expand Down
Loading

0 comments on commit 12bcc0c

Please sign in to comment.