使用正则表达式解析文件并进行修改

时间:2013-10-15 16:24:02

标签: python c regex scripting

我想要一段有一些定义的c代码。我想解析文件做一些更改并创建一个新文件。我希望将定义中的偏移值添加到文件中指示的相应基址。如果有多个基地址和多个偏移量,我希望它能够工作。我在下面显示了一个输入文件和我预期的输出。我怎么能用正则表达式或任何其他方式来做到这一点。

Input.c中

/*******************************************************************
*
* Author: XYZ.
* Version: 1.0
*
*******************************************************************/

#define STDIN_BASEADDRESS 0x40600000
#define STDOUT_BASEADDRESS 0x40600000

/******************************************************************/

/* Definitions for driver UARTLITE */
#define XPAR_XUARTLITE_NUM_INSTANCES 1

/* Definitions for peripheral RS232_UART_1 */
#define XPAR_RS232_UART_1_BASEADDR 0x40600000
#define XPAR_RS232_UART_1_HIGHADDR 0x4060FFFF

/* Definitions for peripherals */
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_BASEADDR 0x76800000
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_HIGHADDR 0x7680FFFF
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_RESET_CNTRS_OFFSET 0x0
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_1_LOW_OFFSET 0xc
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_1_HIGH_OFFSET 0x10
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_3_LOW_OFFSET 0x1c
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_3_HIGH_OFFSET 0x20


#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_BASEADDR 0x74800000
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_HIGHADDR 0x7480FFFF
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_LPM_IP_OFFSET 0x0
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_LPM_IP_MASK_OFFSET 0x4
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_LPM_NEXT_HOP_IP_OFFSET 0x8
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_LPM_OQ_OFFSET 0xc


#define XPAR_MICROBLAZE_0_M15_AXIS_PROTOCOL GENERIC
#define XPAR_MICROBLAZE_0_S15_AXIS_PROTOCOL GENERIC

/******************************************************************/

expected_output.c

/*******************************************************************
*
* Author: XYZ.
* Version: 1.0
*
*******************************************************************/

#define STDIN_BASEADDRESS 0x40600000
#define STDOUT_BASEADDRESS 0x40600000

/******************************************************************/

/* Definitions for driver UARTLITE */
#define XPAR_XUARTLITE_NUM_INSTANCES 1

/* Definitions for peripheral RS232_UART_1 */
#define XPAR_RS232_UART_1_BASEADDR 0x40600000
#define XPAR_RS232_UART_1_HIGHADDR 0x4060FFFF

/* Definitions for peripherals */
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_BASEADDR 0x76800000
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_HIGHADDR 0x7680FFFF
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_RESET_CNTRS_OFFSET 0x76800000
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_1_LOW_OFFSET 0x7680000c
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_1_HIGH_OFFSET 0x76800010
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_3_LOW_OFFSET 0x7680001c
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_3_HIGH_OFFSET 0x76800020


#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_BASEADDR 0x74800000
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_HIGHADDR 0x7480FFFF
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_LPM_IP_OFFSET 0x74800000
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_LPM_IP_MASK_OFFSET 0x74800004
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_LPM_NEXT_HOP_IP_OFFSET 0x74800008
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR1_LPM_OQ_OFFSET 0x7480000c


#define XPAR_MICROBLAZE_0_M15_AXIS_PROTOCOL GENERIC
#define XPAR_MICROBLAZE_0_S15_AXIS_PROTOCOL GENERIC

/******************************************************************/

1 个答案:

答案 0 :(得分:1)

C编译器能够进行数学运算。不需要正则表达式或文件修改。

file.h:

/* Definitions for peripherals */
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_BASEADDR 0x76800000
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_HIGHADDR 0x7680FFFF
//Shorten name to make it easier for asker to read
#define BASEADDR XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_BASEADDR
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_RESET_CNTRS_OFFSET (BASEADDR + 0x0)
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_1_LOW_OFFSET   (BASEADDR + 0xc)
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_1_HIGH_OFFSET  (BASEADDR + 0x10)
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_3_LOW_OFFSET   (BASEADDR + 0x1c)
#define XPAR_NF10_ROUTER_OUTPUT_PORT_LOOKUP_0_BAR0_MAC_3_HIGH_OFFSET  (BASEADDR + 0x20)
#undef BASEADDR

如果您不喜欢这个解决方案,还可以使用伪代码脚本(使用真正的正则表达式)进行所需的转换:

#This script assumes that OFFSET #defines come after
# their associated BASEADDR #defines
input_file = open("path/to/input/file.c", read)
output_file = open("path/to/output/file.c", write)
baseaddr = 0
#In iteration of the loop, "line" will be a string
# containing a single line of the file
foreach line in input_file:
    #Get base address from the _BASEADDR #defines
    if regex_match("#define .*_BASEADDR (0x[0-9]{8})", line):
        baseaddr = hex_string_to_number(regex_groups[0])
        write_line(output_file, line)
    #Create new offset #define by adding offset from source
    # file to base address from source file
    else if regex_match("#define (.*)_OFFSET (0x[0-9]+)", line):
        new_val = hex_string_to_number(regex_groups[1]) + baseaddr
        #Format here is similar to C's sprintf.
        # Whichever language you choose will likely have
        # something that behaves differently
        new_line = format("#define %s_OFFSET %s",
                          regex_groups[0],
                          number_to_hex_string(new_val))
        write_line(output_file, new_line)
    #Pass through all other lines (blanks, HIGHADDRs, comments, etc)
    else:
        write_line(output_file, line)