# 读取第一个文件中的文件名，并存储到一个集合中
def read_file_to_set(file_name):
    file_set = set()
    with open(file_name, 'r') as f:
        for line in f:
            line = line.strip()
            # 去掉换行符，存入集合中
            prefix = './data/sv-benchmarks/c/'
            if line.startswith(prefix):
                line = line[len(prefix):]
            line = line.replace('/','-')
            if line.endswith('.i'):
                line = line[:-2] + '.c'
            print(line)
            file_set.add(line)
    return file_set

# 将第二个文件的内容按照是否在第一个文件中，分别存入两个文件
def split_files_based_on_existence(first_file_set, second_file, output_file_in, output_file_out, next_file):
    with open(second_file, 'r') as f_in, open(output_file_in, 'w') as f_out_in, open(output_file_out, 'w') as f_out_out, open(next_file, 'w') as miss_in:
        for line in f_in:
            filename = line.strip()  # 去掉换行符
      #      prefix = './data/sv-benchmarks/c/'
      #      if filename.startswith(prefix):
      ##          filename = filename[len(prefix):]
      ##      filename = filename.replace('/','-')
      #      if filename.endswith('.i'):
      #          filename = filename[:-2] + '.c'
      #      miss_in.write(filename + '\n')
            # 如果文件名在第一个文件中
          #  print(line)
            if filename in first_file_set:
                f_out_in.write(filename + '\n')
            else:
                f_out_out.write(filename + '\n')

# 第一个文件，包含需要判断的文件名
first_file = "new_miss_paths.txt"
# 第二个文件，包含要被分割的文件名
second_file = "not_in.txt"
next_file = "new_miss_ymlnames.txt"
# 输出的两个文件
output_file_in = "in_1.txt"  # 在第一个文件中的文件名
output_file_out = "not_in_1.txt"  # 不在第一个文件中的文件名

# 读取第一个文件的内容到集合中
first_file_set = read_file_to_set(first_file)

# 按照是否在第一个文件中，分割第二个文件的内容
split_files_based_on_existence(first_file_set, second_file, output_file_in, output_file_out,next_file)

print(f"文件已分割：在第一个文件中的存入 {output_file_in}，不在的存入 {output_file_out}")

#def write(first_file, next_file):
#    with open(first_file, 'r') as lines:
#        for line in lines:
#            filename = line.strip()
#            prefix = './data/sv-benchmarks/c'
#            if filename.startswith(prefix):
#                filename = filename[len(prefix):]
#            filename = filename.replace('/', '-')
#            if filename.endswith('.i'):
#                filename = filename[:-2] + '.c'
#
#            with open(next_file, 'w') as f_out_in:
#                f_out_in.write(filename + '\n')
#
#nextfile = "new_miss_ymlnames.txt"
#write(first_file,nextfile)


