File: AuxFuncsLibMapper.pas

package info (click to toggle)
mysql-query-browser 1.2.5beta-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 63,792 kB
  • ctags: 46,485
  • sloc: pascal: 249,299; ansic: 80,111; cpp: 72,467; sh: 25,271; objc: 20,015; yacc: 10,755; java: 9,917; xml: 4,580; php: 2,806; python: 1,566; sql: 1,563; makefile: 1,452; perl: 3
file content (162 lines) | stat: -rw-r--r-- 4,399 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
unit AuxFuncsLibMapper;

interface

uses
  SysUtils,
  RegExpr,
  Classes,
  IniFiles,
  StrUtils;

function LoadTextFromFile(filename: string): string;
procedure RemoveTrailingLineWrap(var s: string; keyword: string);
function Get_Ini_Section(file_name, section: string): string;
function GetDatatypeDefinitionFromFile(filename: string;
  NestedStructsPointers: TStringList): string;

implementation

function LoadTextFromFile(filename: string): string;
var Stream: TStream;
  Size: Integer;
  S: string;
begin
  if(FileExists(filename))then
  begin
    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
    try
      Size := Stream.Size - Stream.Position;
      SetString(S, nil, Size);
      Stream.Read(Pointer(S)^, Size);
    finally
      Stream.Free;
    end;
  end
  else
  begin
    S:='';
    WriteLn('ERROR: ['+filename+'] not found.');
    ExitCode:=1;
  end;

  LoadTextFromFile:=S;
end;

procedure RemoveTrailingLineWrap(var s: string; keyword: string);
begin
  while(Pos(keyword+#13#10, s)>0)do
    s:=Copy(s, 1, Pos(keyword+#13#10, s)+Length(keyword)-1)+
      Copy(s, Pos(keyword+#13#10, s)+Length(keyword)+2, Length(s));
end;

function Get_Ini_Section(file_name, section: string): string;
var DatatypeMappingIni: TMemIniFile;
  theValues: TStringList;
begin
  Result:='';

  DatatypeMappingIni:=TMemIniFile.Create(file_name);
  theValues:=TStringList.Create;
  try
    DatatypeMappingIni.ReadSectionValues(section,
      theValues);

    Result:=theValues.Text;
  finally
    DatatypeMappingIni.Free;
    theValues.Free;
  end;
end;

function GetDatatypeDefinitionFromFile(filename: string;
  NestedStructsPointers: TStringList): string;
var Source, SourceNoComments: string;
  RegExpr, Enums, Structs: TRegExpr;
  DatatypeMapping: TStringList;
begin
  DatatypeMapping:=TStringList.Create;
  RegExpr:=TRegExpr.Create;
  Enums:=TRegExpr.Create;
  Structs:=TRegExpr.Create;
  try
    Source:=LoadTextFromFile(filename);

    //-------------------------------------------------------------
    //Remove comments from file
    RegExpr.Expression:='(?img)(/\*([\w\s=\,\(\)\;/\\\.\-\''\?\:\*\<\>\#\!]+?)\*/)|'+
      '(//([\w\t \=\,\(\)\;/\\\.\-\''\?\:\*\<\>\#\!]+))';

    SourceNoComments:=RegExpr.Replace(Source, '', True);

    //-------------------------------------------------------------
    //Scan through file

    Enums.Expression:='(?img)(typedef enum|^enum)\s{0,}(\w+){0,}\s{0,}\{'+
      '([a-zA-Z0-9_\s=\,]+)\}\s*(\w*)\;';

    //--------------------------------
    //Find Enum-Definitions
    if(Enums.Exec(SourceNoComments))then
    begin
      repeat
      begin
        //Matches
        // 0..complete match string
        // 1..typedef enum/enum
        // 2..enum name
        // 3..enum values
        // 4..enum type name

        //Add this enum to datatype mapping
        if(CompareText(Enums.Match[1], 'typedef enum')=0)then
        begin
          DatatypeMapping.Add(Enums.Match[4]+' *=P'+Enums.Match[4]);
          DatatypeMapping.Add(Enums.Match[4]+'='+Enums.Match[4]);
        end;

        if(Enums.Match[2]<>'')then
        begin
          DatatypeMapping.Add(Enums.Match[2]+' *=P'+Enums.Match[2]);
          DatatypeMapping.Add('enum '+Enums.Match[2]+'='+Enums.Match[2]);
        end;
      end
      until not Enums.ExecNext;
    end;

    //--------------------------------
    //Find Struct-Definition

    Structs.Expression:='(?img)typedef\s{0,}struct\s{0,}\w{0,}\s{0,}\{'+
      '\s{0,}([\w\s\*\;\,/\?\(\)\.\[\]]+)\}\s(\w+){1,}\;\s*';

    //Scan for Datatype mapping
    if(Structs.Exec(SourceNoComments))then
    begin
      repeat
      begin
        //Matches
        // 0..complete match string
        // 1..variables
        // 2..struct name

        //Add pointer to this struct to datatype mapping
        DatatypeMapping.Add(Structs.Match[2]+' *=P'+Structs.Match[2]);
        DatatypeMapping.Add('struct '+Structs.Match[2]+' *=P'+Structs.Match[2]);

        //Add Datatype to NestedStructsPointers
        NestedStructsPointers.Add('P'+Structs.Match[2]);
      end
      until not Structs.ExecNext;
    end;

    Result:=DatatypeMapping.Text;
  finally
    DatatypeMapping.Free;
    RegExpr.Free;
    Enums.Free;
    Structs.Free;
  end;
end;

end.