#! /usr/bin/env python3 # # Name: make_enum_tables # # Purpose: This Python script reads the "uldaq.h" include file provided # by Measurement Computing and generates an MX include file # called "i_mccdaq_enum_tables.h" This new include file # provides functions for converting between enum values # such as AI_VOLTAGE and string representation of the # enum such as "AI_VOLTAGE". # # Author: William Lavender # #------------------------------------------------------------------------- # # Copyright 2023 Illinois Institute of Technology # # See the file "LICENSE" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # #------------------------------------------------------------------------- # import sys #-------- # There is a header at the top of each array declaration, but we do not # know what the name of the array is until we reach the end of the enum # declaration in uldaq.h. So we fill up the space with blanks to leave # for the array name being written later. table_header_offset = 0 table_blank_header_bytes = 60 table_blank_header = ( " " * table_blank_header_bytes ) + "\n" #-------- if ( len(sys.argv) != 3 ): print( "" ) print( "Usage: make_enum_tables " ) print( "" ) sys.exit(1) uldaq_header = sys.argv[1] mx_header = sys.argv[2] print( "uldaq_header = '%s'" % ( uldaq_header ) ) print( "mx_header = '%s'" % ( mx_header ) ) uldaq_file = open( uldaq_header, mode='r' ) mx_file = open( mx_header, mode='w' ) in_enum = False while True: input_line = uldaq_file.readline() print( "input_line = '%s'" % ( input_line ) ) # Note that a blank line in the original file still will have a # newline '\n' at the end of the line. If not even the newline # is present, then we have reached the end of the file and # len() will return 0. if ( len( input_line ) == 0 ): break print( "MARKER 0" ) if ( in_enum ): print( "MARKER 1.1") if ( input_line.startswith( '}' ) ): print( "MARKER 2.1") print("ENUM END FOUND") # Print the last line of the array definition. input_line = input_line[1:] print("input_line truncated = '%s'" % ( input_line ) ) input_parameters = input_line.split( ';' ) print( "input_parameters = '%s'" % ( input_parameters ) ) enum_name = input_parameters[0] print( "enum_name = '%s'" % ( enum_name ) ) # Write the last line of the array declaration. write_string = " };\n\n" mx_file.write( write_string ) sys.stdout.write( write_string ) # # We need to go back to the start of the array definition # to write the name of the array there. # mx_file.seek( table_header_offset ) table_header_string = \ ( " MX_FOO_TABLE %s_enum_table[] = \n {" % (enum_name) ) mx_file.write( table_header_string ) sys.stdout.write( table_header_string ) # # Now go back to the _current_ end of the file. # mx_file.seek( 0, 2 ) # #--- in_enum = False print( "Switching to in_enum = False" ) else: print( "MARKER 2.2") input_line = input_line.strip() if ( input_line.startswith( '{' ) ): # There is a line at the beginning of the enum that # starts with {. We leave it out. print( "MARKER 2.2A" ) continue # Print other lines of the array definition. print( "input_line = '%s'" % input_line ) print( "MARKER 2.2B" ) if ( len(input_line) == 0 ): print( "MARKER 2.2.1"); print( "Skipping the { character" ) continue value_list = input_line.split() print( "value_list = '%s'" % (value_list) ) print( "MARKER 2.2C" ) enum_value = value_list[0] if ( enum_value.startswith( "/*" ) ): print( "MARKER 2.2.2" ) continue print( "enum_value = '%s'" % (enum_value) ) print( "MARKER 2.2D" ) if ( enum_value == "*" ): # Skip over '*' lines. print( "MARKER 2.2E" ) continue write_string = \ (" { %s, \"%s\" },\n" % ( enum_value, enum_value )) print( "MARKER 2.3" ) mx_file.write( write_string ) sys.stdout.write( "Write #1 = %s" % (write_string ) ) else: print( "MARKER 1.2") if ( input_line.startswith( "typedef enum" ) ): in_enum = True print( "Switching to in_enum = True" ) table_header_offset = mx_file.tell() mx_file.write( table_blank_header ) print( "MARKER 1.2.1") else: print( "MARKER 1.2.2") print( "MARKER 3" ) mx_file.close() uldaq_file.close() print( "MARKER 999" ) print( "Program complete" )