import sys, os, re
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\\GD5F2GQ5UE\\gd5f2gq5ue.c',
    'Drivers\\BSP\\SD2506\\sd2506.c',
    'Drivers\\BSP\\TPAFE5160\\tpafe5160.c',
]

# Generic patterns - these work across comment blocks
generic_fixes = []

# 1. [?] followed by */ -> ) */
generic_fixes.append((rep + r'\? \*/', u'） */'))

# 2. [?] followed by * and 入口参数/函数说明/etc -> ） * xxx
comment_keywords = [
    u'入口参数', u'函数功能', u'函数说明', u'限定条件', u'返回值',
    u'适用平台', u'作者', u'创建日期', u'修改记录', u'模块名称',
    u'模块功能', u'函数说明', u'头文件', u'私有宏', u'私有变量',
    u'私有函数', u'外部变量', u'作者',
]
for kw in comment_keywords:
    # [?] * X -> ） * X
    generic_fixes.append((rep + r'\? \* ' + kw, u'） * ' + kw))

# 3. Various suffix patterns
generic_fixes.append((rep + r'\?\)', u'））'))
generic_fixes.append((u'）' + rep + r'\?', u'））'))

# 4. [?]\n at end of line (before comment continuation)
# This is hard to do with simple string matching, use regex

# 5. Specific contexts that appear across files
generic_fixes.append((rep + r'\?，', u'），'))
generic_fixes.append((rep + r'\?。', u'）。'))

# 6. Numbers or units followed by [?]
# e.g. "范围[?] * 函数" -> "范围）* 函数"
# e.g. "128KB[?] * 入口" -> "128KB）* 入口"

total_fixed = 0
for fname in files:
    path = base + fname
    with open(path, 'rb') as f:
        raw = f.read()
    text = raw.decode('utf-8', errors='replace')
    before = text.count(rep)
    if before == 0:
        print('{}: clean'.format(fname))
        continue
    
    # Apply generic pattern fixes
    for pattern, replacement in generic_fixes:
        text = re.sub(pattern, replacement, text)
    
    # Manual patterns for each file
    if 'ch395f.c' in fname:
        text = text.replace(rep + '?' + u'DMA 接收缓冲区偏移', u'从 DMA 接收缓冲区偏移')
        text = text.replace(u'创建日期' + rep + '?', u'创建日期：')
        text = text.replace(u'要' + rep + '? */', u'要求 */')
        text = text.replace(u'rx_buf[' + rep + '?', u'rx_buf]')
        text = text.replace(u'取反' + rep + '?', u'取反为')
        text = text.replace(u'在前' + rep + '? */', u'在前）*/')
        text = text.replace(u'_ABORT' + rep + '? *', u'_ABORT）*')
        text = text.replace(u'一' + rep + '?SPI', u'一次 SPI')
        text = text.replace(u'缓冲' + rep + '?*/', u'缓冲区）*/')
        text = text.replace(u'~7 + DHCPv6' + rep + '? *', u'~7 + DHCPv6）*')
        text = text.replace(u'100ms)=默认' + rep + '?', u'100ms)=默认值')
        text = text.replace(u'次数据=默认' + rep + '?', u'次数=默认值')
        text = text.replace(u'最' + rep + '?128', u'最大 128')

    if 'ch395f_test.c' in fname:
        text = text.replace(u':8081' + rep + '? *', u':8081）*')
        text = text.replace(u':8082' + rep + '? *', u':8082）*')
        text = text.replace(u'创建' + rep + '?UDP', u'创建 UDP')
        text = text.replace(u'IP ' + rep + '?打印', u'IP，打印')
        text = text.replace(u'(1-byte version) ' + rep + '?read-clear', u'(1-byte version)，read-clear')

    if 'gd5f2gq5ue.c' in fname:
        text = text.replace(u'对齐' + rep + '? *', u'对齐）*')

    if 'sd2506.c' in fname:
        text = text.replace(u'范围' + rep + '? *', u'范围）*')
        text = text.replace(u'始终' + rep + '?])', u'始终为0])')
        text = text.replace(u'(周期' + rep + '? */', u'(周期中断） */')

    if 'tpafe5160.c' in fname:
        text = text.replace(u'16位数据总线' + rep + '?GPIOG', u'16位数据总线（GPIOG')
        text = text.replace(u'驱动' + rep + '?TPAFE5160', u'驱动适配 TPAFE5160')
        text = text.replace(u'滞后' + rep + '?18-24ns', u'滞后约 18-24ns')
        text = text.replace(u'说明' + rep + '?.', u'说明：')
        text = text.replace(u'枚举' + rep + '? tpafe5160_os_t', u'枚举值 tpafe5160_os_t')
        text = text.replace(u'锁存生' + rep + '? */', u'锁存生效 */')
        text = text.replace(u'转换' + rep + '? - 空闲', u'转换中 - 空闲')
        text = text.replace(u'原始化 int16_t', u'原始值 int16_t')
        text = text.replace(u'有符号补' + rep + '? *', u'有符号补码 *')
        text = text.replace(u'65536 ' + rep + '?152.59', u'65536 ≈ 152.59')
        text = text.replace(u'65536 ' + rep + '?305.18', u'65536 ≈ 305.18')
        text = text.replace(u'默' + rep + '?±5V', u'默认 ±5V')
        text = text.replace(u'CS ' + rep + '?RD', u'CS 和 RD')
        text = text.replace(u'写操' + rep + '?~6ns', u'写操作 ~6ns')
        text = text.replace(u'主循环' + rep + '?s_buf_a', u'主循环读 s_buf_a')
        text = text.replace(u'GPIO ' + rep + '?NVIC', u'GPIO 和 NVIC')
        text = text.replace(u'调用' + rep + '? * 入口参数', u'调用）* 入口参数')
    
    after = text.count(rep)
    fixed = before - after
    total_fixed += fixed
    
    with open(path, 'wb') as f:
        f.write(text.encode('utf-8'))
    
    print('{}: fixed {} ({} -> {})'.format(fname, fixed, before, after))

print('\nTotal fixed: {}'.format(total_fixed))

# Show remaining
print('\n=== REMAINING ===')
for fname in files:
    path = base + fname
    with open(path, 'rb') as f:
        text = f.read().decode('utf-8', errors='replace')
    remaining = text.count(rep)
    if remaining > 0:
        print('\n--- {} remaining in {} ---'.format(remaining, fname))
        for i, line in enumerate(text.split(chr(10)), 1):
            if rep in line:
                idx = line.index(rep)
                left = line[max(0,idx-35):idx]
                right = line[idx+2:idx+35]
                ctx_u = left.encode('unicode-escape').decode()
                ctx_v = right.encode('unicode-escape').decode()
                print('L{}: ...{}[?]{}...'.format(i, left, right))
                print('     ...{} [?] {}...'.format(ctx_u, ctx_v))
