summaryrefslogtreecommitdiff
path: root/fpcsrc/utils/fpcreslipo/fpcreslipo.pp
blob: 0a9acc4cd2de5e37e40bbf9869950053a21b179f (plain)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
{

    FPCResLipo - Free Pascal External Resource Thinner
    Part of the Free Pascal distribution
    Copyright (C) 2008 by Giulio Bernardi

    See the file COPYING, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

program fpcreslipo;

{$MODE OBJFPC} {$H+}

uses
  SysUtils, Classes, paramparser, msghandler, sourcehandler,
  resource, externalreader, externalwriter;
  
const
  halt_no_err = 0;
  halt_param_err = 1;
  halt_read_err = 2;
  halt_write_err = 3;

  progname = 'fpcreslipo';
  progversion = '1.0';

  fpcversion = {$INCLUDE %FPCVERSION%};
  host_arch = {$INCLUDE %FPCTARGETCPU%};
  host_os = {$INCLUDE %FPCTARGETOS%};
  build_date = {$INCLUDE %DATE%};

var
  params : TParameters = nil;
  sourcefiles : TSourceFiles = nil;
  outResources : TResources = nil;

procedure ShowVersion;
begin
  writeln(progname+' - external resource file thinner, version '+progversion+' ['+build_date+'], FPC '+fpcversion);
  writeln('Host platform: '+host_os+' - '+host_arch);
  writeln('Copyright (c) 2008 by Giulio Bernardi.');
end;

procedure ShowHelp;
begin
  ShowVersion;
  writeln('Syntax: '+progname+' [options] <inputfile> [<inputfile>...] -o <outputfile>');
  writeln;
  writeln('Options:');
  writeln('  --help, -h, -?     Show this screen.');
  writeln('  --version, -V      Show program version.');
  writeln('  --verbose, -v      Be verbose.');
  writeln('  --output, -o <x>   Set the output file name.');
  writeln('  --endian, -e <x>   Set shared file endianess (big, little)');
  writeln('                       default is big');
  writeln;
  writeln('Example:');
  writeln('  '+progname+' myprog.i386.fpcres myprog.powerpc.fpcres -o myprog.fpcres');
  writeln;
  writeln('  strips common resources from the two input files and puts them in the');
  writeln('  output file');
end;

const
  SOutputFileAlreadySet = 'Output file name already set.';
  SUnknownParameter = 'Unknown parameter ''%s''';
  SArgumentMissing = 'Argument missing for option ''%s''';
  SUnknownEndianess = 'Unknown endianess ''%s''';
  SNoInputFiles = 'No input files';
  STooFewInputFiles = 'At least two input files must be specified';
  SNoOutputFile = 'No output file name specified';
  SCantOpenFile = 'Can''t open file ''%s''';
  SUnknownInputFormat = 'No known file format detected for file ''%s''';
  SCantCreateFile = 'Can''t create file ''%s''';

function GetCurrentTimeMsec : longint;
var h,m,s,ms : word;
begin
  DecodeTime(Time,h,m,s,ms);
  Result:=h*3600*1000 + m*60*1000 + s*1000 + ms;
end;

procedure CheckInputFiles;
begin
  if params.InputFiles.Count<2 then
  begin
    case params.InputFiles.Count of
      0 : Messages.DoError(SNoInputFiles);
      1 : Messages.DoError(STooFewInputFiles);
    end;
    halt(halt_param_err);
  end;
end;

procedure CheckOutputFile;
begin
  if params.OutputFile<>'' then exit;
  Messages.DoError(SNoOutputFile);
  halt(halt_param_err);
end;

procedure ParseParams;
var msg : string;
begin
  Messages.DoVerbose('parsing command line parameters');
  msg:='';
  if ParamCount = 0 then
  begin
    ShowHelp;
    halt(halt_no_err);
  end;
  params:=TParameters.Create;
  try
    params.Parse;
  except
    on e : EOutputFileAlreadySetException do msg:=SOutputFileAlreadySet;
    on e : EUnknownParameterException do msg:=Format(SUnknownParameter,[e.Message]);
    on e : EArgumentMissingException do msg:=Format(SArgumentMissing,[e.Message]);
    on e : EUnknownEndianessException do msg:=Format(SUnknownEndianess,[e.Message]);
  end;
  Messages.Verbose:=params.Verbose;
  if msg<>'' then
  begin
    Messages.DoError(msg);
    halt(halt_param_err);
  end;
  if params.Version then
  begin
    ShowVersion;
    halt(halt_no_err);
  end;
  if params.Help then
  begin
    ShowHelp;
    halt(halt_no_err);
  end;

  CheckInputFiles;
  CheckOutputFile;

  Messages.DoVerbose('finished parsing command line parameters');
end;

procedure LoadSourceFiles;
var msg : string;
    i : integer;
begin
  msg:='';
  sourcefiles:=TSourceFiles.Create;
  try
    for i:=0 to params.InputFiles.Count-1 do
      sourcefiles.NewSourceFile(params.InputFiles[i]);
  except
    on e : ECantOpenFileException do msg:=Format(SCantOpenFile,[e.Message]);
    on e : EUnknownInputFormatException do msg:=Format(SUnknownInputFormat,[e.Message]);
    on e : Exception do
    begin
      if e.Message='' then msg:=e.ClassName
      else msg:=e.Message;
    end;
  end;
  if msg<>'' then
  begin
    Messages.DoError(msg);
    halt(halt_read_err);
  end;
end;

procedure ProcessFiles;
begin
  Messages.DoVerbose('processing input files...');
  outResources:=TResources.Create;
  sourcefiles.Process(outResources);
  Messages.DoVerbose('input files processed.');
end;

function WriteOutputFile : boolean;
var aStream : TFileStream;
    aWriter : TExternalResourceWriter;
    msg : string;
begin
  if outResources.Count=0 then
  begin
    Result:=false;
    Messages.DoVerbose('Nothing to do');
    exit;
  end;
  Result:=true;
  Messages.DoVerbose(Format('Trying to create file %s...',[params.OutputFile]));
  try
    aStream:=TFileStream.Create(params.OutputFile,fmCreate or fmShareDenyWrite);
  except
    Messages.DoError(Format(SCantCreateFile,[params.OutputFile]));
    halt(halt_write_err);
  end;
  try
    aWriter:=TExternalResourceWriter.Create;
    aWriter.Endianess:=params.Endianess;
    try
      try
        outResources.WriteToStream(aStream,aWriter);
      except
        on e : Exception do
        begin
          if e.Message='' then msg:=e.ClassName
          else msg:=e.Message;
          Messages.DoError(msg);
          halt(halt_write_err);
        end;
      end;
      Messages.DoVerbose(Format('%d resources written.',[outResources.Count]));
      Messages.DoVerbose(Format('File %s written',[params.OutputFile]));
    finally
      aWriter.Free;
    end;
  finally
    aStream.Free;
  end;
  FreeAndNil(outResources);
end;

procedure UpdateFiles;
var msg : string;
begin
  try
    sourcefiles.Update;
  except
    on e : ECantCreateFileException do msg:=Format(SCantCreateFile,[e.Message]);
    on e : Exception do
    begin
      if e.Message='' then msg:=e.ClassName
      else msg:=e.Message;
    end;
  end;
  if msg<>'' then
  begin
    Messages.DoError(msg);
    halt(halt_write_err);
  end;
end;

procedure Cleanup;
begin
  Messages.DoVerbose('Cleaning up');
  if OutResources<>nil then OutResources.Free;
  if SourceFiles<>nil then SourceFiles.Free;
  if Params<>nil then Params.Free;
end;

var before, elapsed : longint;

begin
  try
    before:=GetCurrentTimeMsec;
    ParseParams;
    LoadSourceFiles;
    ProcessFiles;
    if WriteOutputFile then
      UpdateFiles;
    elapsed:=GetCurrentTimeMsec-before;
    if elapsed<0 then elapsed:=24*3600*1000 + elapsed;
    Messages.DoVerbose(Format('Time elapsed: %d.%d seconds',[elapsed div 1000,(elapsed mod 1000) div 10]));
  finally
    Cleanup;
  end;
end.