unit parserch ; {** @explan(说明)c头文件解析对外 接口 %% **} interface function pcstruct(s,f); function parserctokens(str); function chtotslclass(); function dealcppcomment(strs); function csstotslarray(csstr,tname); function csstotslarray2(csstr,iv); function csstotslarray3(ret,iv); function cstructtotslclass(data,name,fs); (* //str := data(); //echo tostn(str22array(str)[:,3]); //for := 0 to length(str) do //return echo tostn(getgdihdata()); s := " typedef struct _GdiplusStartupInput { unsigned int GdiplusVersion; unsigned int DebugEventCallback; BOOL SuppressBackgroundThread; BOOL SuppressExternalCodecs; }GdiplusStartupInput; "; return echo tostn(parsercstruct(parserctokens(s))); rt := parsergdifunction(parserctokens(getgdihdata())); echo tostn(rt); *) Implementation function chtotslclass(); begin parsergdifunction(parserctokens(getgdihdata())); end function pcstruct(s,f); begin {** @explan(说明) 解析c结构体字符串到tsl结构化数据 %% @param(s)(string) c结构体字符串 %% @param(f)(bool) true 转换为小写 %% @return(array) tag字段为名称 field 为字段信息 n : 名称 t : 类型 一个数组如果 array(unsigned,int) l : 如果为数组 数组的长度 p : 如果为指针,指针的*个数 nick 别名 **} return parsercstruct(parserctokens(f?lowercase(s):s)); end function addtable(d,n); begin tbs := ""; for i := 1 to n do tbs += "\t"; r := ""; for i,v in str2array(d,"\r\n") do if v then r += tbs+v+"\r\n"; return r; end function cstructtotslclass(data,name,fz); begin {** @explan(说明)将数组结构转换为对象 %% **} s := format("type %s = class(tslcstructureobj)\r\n",name); s += "uses cstructurelib;\r\n"; s += "private\r\n\tstatic SSTRUCT;\r\n"; fs := ""; fp := ""; sf := ""; gsf := ""; intf := format("\tclass function getstruct()\r\n\tbegin\r\n\t\tif not SSTRUCT then SSTRUCT := MemoryAlignmentCalculate(\r\n%s\r\n\t\treturn SSTRUCT;\r\n\tend\r\n",addtable(tostn(data)+");",3)); if fz then cf2 := "public\r\nclass function memsize();\r\nbegin\r\n\tif not SSTRUCT then getstruct();\r\n\tif SSTRUCT then \r\n\tbegin\r\n\t\tldata := length(SSTRUCT)-1;\r\n\t\treturn SSTRUCT[ldata,3]+SSTRUCT[ldata,4]-SSTRUCT[0,3];\r\n\tend \r\n\treturn 0;\r\nend"; else cf2 := ""; cf := "\tpublic\r\n\tfunction create(ptr)\r\n\tbegin\r\n\t\tinherited create(getstruct(),ptr); \r\n\tend\r\n"; for i,v in data do begin v0 := v[0]; //fs += "\tF"+ v[0]+";\r\n"; fp += format('\tproperty %s index "%s" read _getvalue_ write _setvalue_;\r\n',v0,v0); {sf += format("\tfunction Set%s(v)\r\n\tbegin\r\n",v0); sf += format("\t\t_setvalue_('%s',v);\r\n",v0,v0,v0); sf +="\tend\r\n"; gsf += format("\tfunction Get%s()\r\n\tbegin\r\n",v0); gsf += format("\t\treturn _getvalue_('%s');\r\n",v0); gsf +="\tend\r\n";} end s += gsf; s += intf; s += sf; s += addtable(cf2,1); s += cf; s += fp; s += "end"; return s; end //**********c结构体转换到tsl对象********************************* function csstotslarray3(ret,iv); begin {** @explan(说明)对c结构定义,初始值 %% @param(ret)(array) 结构定义 %% @param(iv)(array) 初始值 %% @return(array) 结构定义 **} r := array(); iv := array(); for i,v in ret do begin n := ret[i,0]; r[i]["n"]:= n; r[i]["t"]:= ret[i,1]; iv[n]:= ret[i,2]; end return r; end function csstotslarray2(csstr1,iv); begin {** @explan(说明)将c的结构体字符串形式转换为tsl数组 %% @param(csstr1)(string) c结构体字符串 %% @param(iv)(array) 初始值 %% @return(array) 结构定义 **} (** @example(转换cstruct结构到tslarray-2) s := " typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; } SYSTEMTIME, *PSYSTEMTIME; "; return csstotslarray2(s); **) ret := csstotslarray(csstr1); return csstotslarray3(ret,iv); end function csstotslarray(csstr1,tname); begin {** @explan(说明)将c的结构体字符串形式转换为tsl数组 %% @param(csstr1)(string) c结构体字符串 %% @param(tname)(string) 名称,返回值 %% @return(array) 结构定义%% **} (** @example(转换cstruct结构到tslarray-1) s := " typedef struct tagWINDOWPOS { HWND hwnd; HWND hwndInsertAfter; int x; int y; int cx; int cy; UINT flags; } WINDOWPOS, *LPWINDOWPOS, *PWINDOWPOS; "; return csstotslarray(s); **) typecp := array("int":"int", "float":"float", "uint":"int", "lpstr":"char*", "char":"char", "char*":"char*", "dword":"int", "long":"int", "byte":"byte", "short":"short", "word":"short", //byte[2] "rect":"int[4]", "size":"int[2]", "point":"int[2]", "double":"double", "lpcstr":"char*", "bool":"int", "colorref":"int", "nmhdr":"nmhdr", "guid":"guid", "cef_string_t":"cef_string_t" ); //类型对应初始值表 typecv := array( "float":0, "int":0, "char*":100, "short":0, "byte":0, "int[4]":array(0,0,0,0), "int[2]":array(0,0), "byte[2]":array(0,0), "double":0, "intptr":0, "nmhdr":array( ("hwndfrom","intptr",0), ("idfrom","intptr",0), ("code","int",0)), "guid":( ("data1","int",0), ("data2","short",0), ("data3","short",0), ("data4","char[8]","") ) ); //返回值 r := pcstruct(csstr1,1); tslarray := array(); j := 0; if ifarray(r)then tname := r["tname"]; for i,v in r["field"] do begin tps := v["t"]; tp := tps[length(tps)-1]; if v["p"]then begin if tp="char" and v["p"]=1 then tp := "char*"; end ctyp := typecp[tp]?: "intptr"; //设置默认类型为指针 len := v["l"]; if len then ctyp := ctyp+format("[%s]",len); //设置数组初始值 tslarray[j++]:= array(v["n"],typetouser(ctyp),typecv[ctyp]?: 0); //构造数据 end return tslarray; (* csstr := dealcppcomment(csstr1); s := pos("{",csstr)+1; e := pos("}",csstr); //删除注释 str := copy(lowercase(csstr),s,e-s); //获取大括号之间的数据 str :=replacestr(str,"\r"," "); //替换掉换行 str :=replacestr(str,"\n"," "); //替换掉换行 str :=replacestr(str,"\t"," "); //替换掉分隔符 strs := str2array(str,";"); //按照;分割行 //类型对照表 tslarray := array(); j := 0; //正则表达式表 //ctrl := "\\w+[*]?"; for i ,v in strs do begin parserctypestr(v,tp,len,name); if not(name and tp) then continue; ctyp := typecp[tp]?:"intptr"; //设置默认类型为指针 if len then ctyp := ctyp + format("[%d]",len); //设置数组初始值 tslarray[j++] := array(name,ctyp,typecv[ctyp]?:0); //构造数据 end return tslarray; *) end { function writeloglen(len); begin lf := "d:/ts/malocsize.txt"; if FileExists("",lf) then begin pos1 := FileSize('',lf); end else begin pos1 := 0; end a := format("分配空间大小%d",len); writefile(rwraw(),'',lf,pos1,length(a),a); end } function typetouser(tp); begin if tp in array("nmhdr","guid")then begin return "user"; end return tp; end //*************删除c注释***************** function dealcppcomment(strs) begin {** @explan(说明)删除c语言注释 %% @param(strs)(string) c结构体字符串 %% @return(string) **} rets := ""; len := length(strs); i := 1; while ilen then return 0; if str[vpos]<> f[i]then begin bud := false; break; end end if bud then begin pos +=(fl-1); end return bud; end function findstringv(str,f,len,pos,zy,hl); begin {** @explan(说明)查找以f结尾的字符串 **} vs := ""; fl := length(f); while pos=",indx,"sym"); end else setvalue(ret,">",indx,"sym"); end else if vi="<" then begin setvalue(ret,vs,indx,"token"); vip := gdis[indx+1]; if vip="=" then begin indx++; setvalue(ret,"<=",indx,"sym"); end else setvalue(ret,"<",indx,"sym"); end else if vi="=" then begin setvalue(ret,vs,indx,"token"); vip := gdis[indx+1]; if vip="=" then begin indx++; setvalue(ret,"==",indx,"sym"); end else setvalue(ret,"=",indx,"sym"); end else if vi="[" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,"[",indx,"sym"); end else if vi="]" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,"]",indx,"sym"); end else if vi="&" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,"&",indx,"sym"); end else if vi="*" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,"*",indx,"*"); end else if vi="," then begin setvalue(ret,vs,indx,"token"); setvalue(ret,",",indx,","); end else if vi=";" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,";",indx,";"); end else if vi="(" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,"(",indx,"("); end else if vi=")" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,")",indx,")"); end else if vi="{" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,"{",indx,"sym"); end else if vi="}" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,"}",indx,"sym"); end else if vi="|" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,vi,indx,"sym"); end else if vi="?" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,vi,indx,"sym"); end else if vi=":" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,vi,indx,"sym"); end else if vi="'" then begin setvalue(ret,vs,indx,"token"); vs := findstringv(gdis,"'",len,indx,"\\"); setvalue(ret,vs,indx,"'"); end else if vi='"' then begin setvalue(ret,vs,indx,"token"); vs := findstringv(gdis,'"',len,indx,'\\'); setvalue(ret,vs,indx,'"'); end else if vi="/" then begin setvalue(ret,vs,indx,"token"); vip := gdis[indx+1]; if vip="/" then begin indx += 1; vs := findstringv(gdis,"\n",len,indx,nil,"\r"); setvalue(ret,vs,indx,"//"); end else if vip="*" then begin indx += 1; vs := findstringv(gdis,"*/",len,indx,nil); setvalue(ret,vs,indx,"/*"); end else begin setvalue(ret,"/",indx,"/"); end end else if vi in kg then begin setvalue(ret,vs,indx,"token"); end else if vi="\n" then begin setvalue(ret,vs,indx,"token"); setvalue(ret,"\n",indx,"\n"); end else vs += vi; end setvalue(ret,vs,indx,"token"); return ret; //writetofile("",ret); //parsergdifunction(ret); end function nexttoken(tokens,indx,f); begin if not(f)then indx++; return tokens[indx]; end function backtoken(tokens,indx,f); begin if not(f)then indx--; return tokens[indx]; end function parsergdifunction(tokens); begin {** @explan(说明) 提取头文件函数 **} len := length(tokens)-1; indx :=-1; isyms := array("GDIPCONST","_In_","_Out_"); ret := array(); retl := 0; hst := array(); s := "type TGdiplusflat=class\r\n"; s += "{** @explan(说明)gdi+ 的c接口函数 %% **}\r\n"; zs := ""; while indx1)?"var ":"")+gzhparam(pn); ps += ":"+plx; pn := 0; ps += ";"; plx := 0; havlx := true; ddpoint := 0; end else if plx and(tvi="token")then begin pn := vi; end else if vi="*" then begin ddpoint++; if plx in array("integer","single","hbitmap")then begin ddpoint++; end else if plx="string" then begin end else plx := "pointer"; end else if havlx and lex[vi]then begin havlx := false; plx := lex[vi]; end else if havlx then begin havlx := false; plx := "pointer"; end else if vi="[" {or vi = "]"}or vi="&" then begin plx := "pointer"; end end if plx then begin if not(pn)then pn := dpn+inttostr(i); ps +=((ddpoint>1)?"var ":"")+gzhparam(pn)+":"+plx; end ret += ps; //echo tostn(array(tp,ct,fn)); ret += format(')%s;%s;external "gdiplus.dll" name "%s";',tp,ct,fn); //echo tostn(ret),"\r\n"; return ret; end function gzhparam(p); begin {** @explan(说明) 特殊参数名替换 **} if p="order" then return "order_"; else if p="unit" then return "unit_"; else if p="on" then return "on_"; return p; end function parenthesis(tokens,pos,len,iffunc,isyms); begin {** @explan(说明) 函数参数解析 **} ret := array(); retl := 0; tkv := ""; while pos