这是应群友的要求写的一段代码,用于将 Delphi 源码中 resourcestring 定义的资源字符串解析出来。
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
uses qstring; type TPascalResourceStringParser = class private function GetItems(const AIdx: Integer): QStringW; function GetCount: Integer; function GetNames(const AIdx: Integer): QStringW; function GetValues(const AIdx: Integer): QStringW; protected FItems: TStringList; public constructor Create; overload; destructor Destroy; override; procedure Parse(S: QStringW); property Items[const AIdx: Integer]: QStringW read GetItems; property Names[const AIdx: Integer]: QStringW read GetNames; property Values[const AIdx: Integer]: QStringW read GetValues; property Count: Integer read GetCount; end; { TPascalResourceStringParser } constructor TPascalResourceStringParser.Create; begin FItems := TStringList.Create; end; destructor TPascalResourceStringParser.Destroy; begin FreeAndNil(FItems); inherited; end; function TPascalResourceStringParser.GetCount: Integer; begin Result := FItems.Count; end; function TPascalResourceStringParser.GetItems(const AIdx: Integer): String; begin Result := FItems[AIdx]; end; function TPascalResourceStringParser.GetNames(const AIdx: Integer): QStringW; begin Result := FItems.Names[AIdx]; end; function TPascalResourceStringParser.GetValues(const AIdx: Integer): QStringW; function DecodeString(S: QStringW): QStringW; var ps, pd: PWideChar; V: Int64; AIntToChar: Boolean; begin if Length(S) > 0 then begin ps := PWideChar(S); SkipSpaceW(ps); SetLength(Result, Length(S)); pd := PWideChar(Result); while ps^ <> #0 do begin if ps^ = '''' then begin Inc(ps); while ps^ <> '''' do begin pd^ := ps^; Inc(pd); Inc(ps); end; Inc(ps); end else begin if ps^ = '#' then begin Inc(ps); AIntToChar := true; end else if StartWithW(ps, 'char', true) then begin Inc(ps, 4); SkipSpaceW(ps); if ps^ = '(' then begin Inc(ps); SkipSpaceW(ps); AIntToChar := true; end; end else if StartWithW(ps, 'chr', true) then begin Inc(ps, 3); SkipSpaceW(ps); if ps^ = '(' then begin Inc(ps); SkipSpaceW(ps); AIntToChar := true; end; end else AIntToChar := false; if AIntToChar then begin if ParseInt(ps, V) > 0 then begin pd^ := WideChar(V); Inc(pd); end; end else Inc(ps); end; end; SetLength(Result, pd - PWideChar(Result)); end else Result := ''; end; begin Result := DecodeString(FItems.ValueFromIndex[AIdx]); end; procedure TPascalResourceStringParser.Parse(S: QStringW); var AHelper: TQStringCatHelperW; ps, pl: PQCharW; ALine: QStringW; ALn: Integer; AStarted: Boolean; function DecodeStatement: QStringW; var AQuoter: QCharW; begin SkipSpaceW(ps); AHelper.Position := 0; while ps^ <> #0 do begin // 跳过行注释 if (ps[0] = '/') and (ps[1] = '/') then SkipLineW(ps) // 跳过块注释 else if ps[0] = '{' then begin SkipUntilW(ps, '}'); Inc(ps); end // 另一种块注释 else if (ps[0] = '(') and (ps[0] = '*') then begin repeat SkipUntilW(ps, '*'); if ps^ = '*' then Inc(ps); until (ps^ = ')') or (ps^ = #0); if ps^ = ')' then Inc(ps); end else if ps^ = '''' then begin AHelper.Cat(ps^); AQuoter := ps^; Inc(ps); while ps^ <> #0 do begin AHelper.Cat(ps^); if ps^ = '''' then begin Inc(ps); if ps^ <> '''' then Break else Inc(ps); end else Inc(ps); end; end else if ps^ = ';' then // 语句结束 begin Result := AHelper.Value; Inc(ps); SkipSpaceW(ps); Break; end else begin if Ord(ps^) in [9, 10, 13, 32] then begin if AHelper.Position > 0 then begin if AHelper.Chars[AHelper.Position - 1] <> ' ' then AHelper.Cat(' '); end; end else AHelper.Cat(ps^); Inc(ps); end; end; SkipSpaceW(ps); end; begin AHelper := TQStringCatHelperW.Create; FItems.Clear; try ps := PQCharW(S); ALn := 0; AStarted := false; while ps^ <> #0 do begin ALine := DecodeStatement; pl := PQCharW(ALine); if StartWithW(pl, 'interface ', true) then begin ALine := DeleteLeftW(ALine, 'interface ', true); AStarted := false; end else if StartWithW(pl, 'implementation ', true) then begin ALine := DeleteLeftW(ALine, 'implementation ', true); AStarted := false; end; pl := PQCharW(ALine); if StartWithW(pl, 'resourcestring ', true) then begin AStarted := true; ALine := DeleteLeftW(ALine, 'resourcestring ', true); end else if AStarted then AStarted := not(StartWithW(pl, 'var ', true) or StartWithW(pl, 'function ', true) or StartWithW(pl, 'type ', true) or StartWithW(pl, 'const ', true) or StartWithW(pl, 'uses ', true));; if AStarted then begin FItems.Add(ALine); // DebugOut('Line %d:%s', [ALn, ALine]); Inc(ALn); end; end; finally FreeAndNil(AHelper); FItems.EndUpdate; end; end; |
留此存念,同时分享给大家,有用的就拿去用。