对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)