Python parsing C language results

Source: Internet
Author: User

# _*_ coding:utf-8 _*_import restruct_str = "" "struct rncap{UINT16 usTest1; UINT32 UlTest2;}; typedef struct{uint16 USTEST1; UINT32 UlTest2; UINT32 UlTest3; Rncap Rncapstru; UINT16 UsTest6; UINT8 ucTest7; UINT16 UsTest8;} Rncap_pool, "" " stream = "ABCDEF012345678912345" #basic_tpye_list = ["UINT8", "UINT16", "UINT32", "UINT64"]basic_type_dict = {"UINT8": 1,  "UINT16": 2, "UINT32": 4, "UINT64": 8}struct_type_dict = {}class Basictype (object): Def __init__ (self): Self.type =  "" Self.struct_name = "" Self.var_name = "" self.position = 0 self.size = 0 self.value         = 0 Self.sub_basic_type_list = []def struct_2_basic_type (STREAM_STR): If "struct" not in stream_str or '; ' Not in stream_str or ' {' not ' in Stream_str or '} ' not in Stream_str:print ("struct format error        !") return stream_lines = Stream_str.splitlines () type_is_typedef = False type_position = 0 type_size = 0 for L INE in Stream_lines:       If "struct" in Line:basic_type = Basictype () basic_type.position = 0 Type_positio n = 0 type_size = 0 type_is_typedef = False if "typedef" Not in Line:vari Ables = Line.split ("", 1) basic_type.struct_name = Variables[1].strip () else:ty                Pe_is_typedef = True Continue if "}" in line and ";" In Line:if type_is_typedef: m = Re.search ("[a-za-z0-9_]+", line) if m are not None:basic_type.struct_name = m.gr            OUP () Basic_type.size = type_size Basic_type_dict[basic_type.struct_name] = basic_type.size Struct_type_dict[basic_type.struct_name] = Basic_type continue m = Re.search ("[A-za-z0-9_]+[]+[a-za- z0-9_]+; ", line) if M is none:continue variables = M.group () [: -1].split (" ", 1) sub_basic     _type = Basictype ()   Sub_basic_type.struct_name = Variables[0].strip () Sub_basic_type.var_name = Variables[1].strip () Sub_basi C_type.position = type_position sub_size = Basic_type_dict.get (sub_basic_type.struct_name) if Sub_size is No Ne:continue sub_basic_type.size = sub_size type_position + sub_size type_size + = Sub_siz E Struct_type = Struct_type_dict.get (sub_basic_type.struct_name) If Struct_type is not none:sub_ Basic_type.sub_basic_type_list = Struct_type.sub_basic_type_list Basic_type.sub_basic_type_list.append (sub_basic_        Type) return Basic_typedef stream_2_struct (Struct_info, STREAM_STR): if struct_info.size! = Len (STREAM_STR):        Print ("struct size and Len stream is not equal.")            return for sub_struct in Struct_info.sub_basic_type_list:if len (sub_struct.sub_basic_type_list) = = 0: sub_struct.value = Int (stream_str[sub_struct.position:sub_struct.position+sub_struct.sizE], else:stream_2_struct (Sub_struct, stream_str[sub_struct.position:sub_struct.position+sub_struct.    Size]) If __name__ = = "__main__": Basic_type = Struct_2_basic_type (struct_str) stream_2_struct (Basic_type, Stream) Print Basic_type.struct_name Pass

  

Python parsing C language results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.