Changing ErrorCode - MahadMuhammad/gccrs GitHub Wiki
Change error Code
import os
import re
def convert_error_codes(file_path):
try:
with open(file_path, 'r') as file:
content = file.read()
# Regular expression pattern to match ErrorCode("Exxxx")
pattern = r'ErrorCode\("E\d{4}"\)'
matches = re.findall(pattern, content)
if matches:
# Replace matches with ErrorCode::Exxxx
new_content = re.sub(pattern, lambda x: x.group().replace('ErrorCode("', 'ErrorCode::').replace('")', ''), content)
# Write the updated content back to the file
with open(file_path, 'w') as file:
file.write(new_content)
except Exception as e:
print(f"Error processing file '{file_path}': {e}")
def search_and_convert_error_codes(directory):
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.cc'): # Adjust the extension to match your file types
file_path = os.path.join(root, file)
convert_error_codes(file_path)
if __name__ == "__main__":
target_directory = "/home/mahad/Desktop/mahad/gccrs/gcc/rust/" # Replace with the actual target directory
search_and_convert_error_codes(target_directory)
Corrected Error Code:
import os
import re
def replace_pattern_in_file(file_path):
try:
with open(file_path, 'r') as file:
content = file.read()
# Use regular expression to find the pattern " (Exxxx)"
# where x can be any digit from 0 to 9
pattern = r'\s\("(E\d{4})"\)'
replaced_content = re.sub(pattern, r'::\1', content)
with open(file_path, 'w') as file:
file.write(replaced_content)
print(f"Replaced in file: {file_path}")
except Exception as e:
print(f"Error processing file: {file_path} - {e}")
def recursive_search_and_replace(directory_path):
for root, _, files in os.walk(directory_path):
for file in files:
if file.endswith(".cc"):
file_path = os.path.join(root, file)
replace_pattern_in_file(file_path)
if __name__ == "__main__":
# Replace 'path_to_directory' with the directory where you want to search for files
path_to_directory = "/home/mahad/Desktop/mahad/gccrs/gcc/rust/"
recursive_search_and_replace(path_to_directory)