对mozjpeg中的函数名进行替换

获取到mozjpeg中的所有函数名

import os
import re

def process_files_in_directory(directory, pattern, output_file, valid_extensions):
    matches = []

    for root, _, files in os.walk(directory):
        for file_name in files:
            # 检查文件后缀
            if not any(file_name.endswith(ext) for ext in valid_extensions):
                continue

            file_path = os.path.join(root, file_name)
            try:
                # 读取文件内容
                with open(file_path, 'r', encoding='utf-8') as file:
                    content = file.read()
                
                # 查找匹配项,提取捕获组中的字符串
                file_matches = re.findall(pattern, content)
                matches.extend(file_matches)
                
                print(f"Processed {file_path}")
                    
            except Exception as e:
                print(f"Error processing {file_path}: {e}")

    # 将所有匹配到的字符串写入到文件中
    if matches:
        with open(output_file, 'w', encoding='utf-8') as out_file:
            for match in matches:
                out_file.write(match + '\n')
    else:
        print("No matches found")

# 示例调用
directory_to_search = r'Z:\\Variablereplace\\mozjpeg-master'
# 修改正则表达式,使用捕获组提取字符串
search_pattern = r"EXTERN\([^)]*\)\s+(\w+)"
output_file_path = 'output_file.txt'
# 指定需要处理的文件扩展名
valid_extensions = ['.c', '.h']

process_files_in_directory(directory_to_search, search_pattern, output_file_path, valid_extensions)

获取到函数列表到指定文件后,删去simd相关的内容。

进行第一次替换,替换所有的函数名称。
第二次替换是替换def目录导出的函数

 import os
import re

def replace_in_files(replace_dict, directory, extensions):
    """
    :param replace_dict: 进行替换的字典
    :param directory: 要搜索的目录
    :param extensions: 需要处理的文件后缀列表,例如 ['.txt', '.md']
    """
    for root, _, files in os.walk(directory):
        for file_name in files:
            if any(file_name.endswith(ext) for ext in extensions):
                file_path = os.path.join(root, file_name)
                try:
                    # 读取文件内容
                    with open(file_path, 'r', encoding='utf-8') as file:
                        content = file.read()
                    
                    # 进行替换
                    for old_string, new_string in replace_dict.items():
                        content = re.sub(r'\b' + re.escape(old_string) + r'\b', new_string, content)
                    
                    # 写回文件
                    with open(file_path, 'w', encoding='utf-8') as file:
                        file.write(content)
                    
                    print(f"Processed {file_path}")
                        
                except Exception as e:
                    print(f"Error processing {file_path}: {e}")

def get_replacement_dict(file_path):
    replace_dict = {}
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            for line in file:
                line = line.strip()
                if line:  # 确保非空行
                    replace_dict[line] = line + '_cjpeg'
    except Exception as e:
        print(f"Error reading replacement file {file_path}: {e}")
    return replace_dict

# 示例调用
replacement_file_path = 'output_file.txt'
# directory_to_search = r'Z:\\Variablereplace\\mozjpeg-master'
# file_extensions = ['.c', '.h']  # 需要处理的文件后缀
directory_to_search = r"Z:\\Variablereplace\\mozjpeg-master\\win"
file_extensions = ['.def']  # 需要处理的文件后缀

replace_dict = get_replacement_dict(replacement_file_path)
replace_in_files(replace_dict, directory_to_search, file_extensions) 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁金金

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值