prog
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import argparse
|
||||
import os
|
||||
import csv
|
||||
from tools.util.type import detectType, stringToCType, typeToCType
|
||||
|
||||
parser = argparse.ArgumentParser(description="Convert CSV to .h defines")
|
||||
parser.add_argument("--csv", required=True, help="Path to CSV file")
|
||||
@@ -34,6 +35,7 @@ with open(args.csv, newline='') as csvfile:
|
||||
index_col_idx = headers.index(args.index_column)
|
||||
data_col_idx = headers.index(args.data_column)
|
||||
keyValuePairs = {}
|
||||
valType = None
|
||||
for row in reader:
|
||||
key = row[index_col_idx]
|
||||
value = row[data_col_idx]
|
||||
@@ -42,73 +44,19 @@ with open(args.csv, newline='') as csvfile:
|
||||
if key in keyValuePairs:
|
||||
raise ValueError(f"Duplicate key '{key}' found in CSV")
|
||||
keyValuePairs[key] = value
|
||||
|
||||
# Determine type. Can be float, int, bool, or string.
|
||||
isFloat = False
|
||||
isInt = False
|
||||
isBool = False
|
||||
isString = False
|
||||
|
||||
# If there's no entries, assume string type.
|
||||
if len(keyValuePairs) == 0:
|
||||
isString = True
|
||||
else:
|
||||
allInts = True
|
||||
allFloats = True
|
||||
|
||||
# Check if ALL values can be parsed as int or float.
|
||||
for value in keyValuePairs.values():
|
||||
if allInts:
|
||||
try:
|
||||
int(value)
|
||||
except:
|
||||
allInts = False
|
||||
if allFloats:
|
||||
try:
|
||||
float(value)
|
||||
except:
|
||||
allFloats = False
|
||||
|
||||
if not allInts and not allFloats:
|
||||
break
|
||||
|
||||
if allInts:
|
||||
isInt = True
|
||||
elif allFloats:
|
||||
isFloat = True
|
||||
else:
|
||||
# Not all floats/ints, probably string or bool, check first entry
|
||||
firstValue = next(iter(keyValuePairs.values()))
|
||||
if firstValue.lower() in ['true', 'false']:
|
||||
isBool = True
|
||||
else:
|
||||
isString = True
|
||||
|
||||
typeString = ""
|
||||
if isFloat:
|
||||
typeString = "float"
|
||||
elif isInt:
|
||||
typeString = "int"
|
||||
elif isBool:
|
||||
typeString = "bool"
|
||||
elif isString:
|
||||
typeString = "char*"
|
||||
if valType is None:
|
||||
valType = detectType(value)
|
||||
|
||||
if valType is None:
|
||||
valType = 'String'
|
||||
|
||||
# Now begin generating header.
|
||||
outHeader += f"static const {typeString} {args.variable}[] = {{\n"
|
||||
cType = typeToCType(valType)
|
||||
outHeader += f"static const {cType} {args.variable}[] = {{\n"
|
||||
for key in keyValuePairs:
|
||||
outHeader += f" [{args.index_prefix}{key}] = "
|
||||
value = keyValuePairs[key]
|
||||
if isFloat:
|
||||
outHeader += str(float(value))
|
||||
elif isInt:
|
||||
outHeader += str(int(value))
|
||||
elif isBool:
|
||||
asBool = 'true' if value.lower() == 'true' else 'false'
|
||||
outHeader += asBool
|
||||
elif isString:
|
||||
escaped = value.replace('\\', '\\\\').replace('"', '\\"')
|
||||
outHeader += f'"{escaped}"'
|
||||
outHeader += stringToCType(value)
|
||||
outHeader += ",\n"
|
||||
outHeader += "};\n\n"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user