import glob
import csv
import os
import json

# 获取所有CSV文件路径
solFiles = glob.glob("./data/*.csv")
results = {}
#yml_names_file = open("./data/yml_names_tongji-expand-15.txt", 'w')
toolName_file = open("./data/toolName-select.txt",'w')
yml_names_list = []
toolName_list=[]
# 遍历每个CSV文件
count=1
for tool in solFiles:
    # 读取CSV文件
    with open(tool, mode='r') as file:
        reader = csv.DictReader(file)
        toolName = os.path.basename(tool).replace(".table.csv","")
        toolName_file.write(toolName+":"+str(count)+"\n")
        count=count+1
        print("正在处理"+toolName+"表格数据----")
        # 遍历每一行
        for row in reader:
            # 提取并处理YML Name，将 .yml 替换为 .c
            yml_name = row['YML Name'].replace('/', '-').replace('.yml', '.c')
            category = row['category']
            status = row['status']

            # 将Score、CPU Time、Memory列转换为float
            try:
                score = float(row['Score'])
                cpu_time = float(row['CPU Time (s)'])
                memory = float(row['Memory (MB)'])
            except ValueError as e:
                print(f"无法转换以下内容为float: {row}")
                print(yml_name)
                print(count)
                continue

            if category == "Overflow" :
                category_id=0
            elif category == "ReachSafety" :
                category_id=1
            elif category == "Termination" :
                category_id=2
            elif category == "MemSafety" :
                category_id=3

            # 构造键名：文件名加类别
            key = f"{yml_name}|||{category_id}"

            # 如果键已经存在，追加新的结果
            if key in results:
           #     results[key].append([score, score - (cpu_time / 900 if cpu_time < 900 else 1) - memory / 15000, score - memory/15000])
                results[key].append([score, cpu_time, memory])
            else:
          #      results[key] = [[score, score - (cpu_time / 900 if cpu_time < 900 else 1) - memory / 15000, score - memory/15000]]
                results[key] = [[score, cpu_time, memory]]

#yml_names_list = []
# 检查是否已经在列表中
#with open(solFiles[0], mode='r') as file:
#    reader = csv.DictReader(file)
#    for row in reader:
#        count=count+1
#        if row['YML Name'].replace('.yml', '.c') not in yml_names_list:
        # 将路径写入文件
#            yml_names_file.write("./data/sv-benchmarks/c/" + row['YML Name'].replace('.yml', '.c') + "\n")
        # 添加到列表中，防止重复
#            yml_names_list.append(row['YML Name'].replace('.yml', '.c'))
# 关闭文件
#yml_names_file.close()
# 将结果字典存为JSON文件
json.dump(results, open("./data/select-Practice.json", 'w'))
print("存储完毕")

