import sys, os
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
rep = chr(0xfffd)
base = 'D:\\Code\\DTU 程序\\STM32F4-Base\\'

files = [
    'Drivers\\BSP\\CH395F\\ch395f.c',
    'Drivers\\BSP\\CH395F\\ch395f_test.c',
    'Drivers\\BSP\\NET\\net_select.c',
    'Drivers\\BSP\\GD5F2GQ5UE\\gd5f2gq5ue.c',
    'Drivers\\BSP\\SD2506\\sd2506.c',
    'Drivers\\BSP\\TPAFE5160\\tpafe5160.c',
]

for fname in files:
    path = base + fname
    with open(path, 'rb') as f:
        raw = f.read()
    text = raw.decode('utf-8', errors='replace')
    lines = text.split(chr(10))
    count = text.count(rep)
    if count == 0:
        continue
    print('\\n=== {} ({} remaining) ==='.format(fname, count))
    for i, line in enumerate(lines, 1):
        if rep in line:
            idx = line.index(rep)
            left = line[max(0,idx-30):idx]
            right = line[idx+2:idx+30]
            ctx = left + '[?]' + right
            print('L{}: |{}|'.format(i, ctx))
