瀏覽代碼

+ support of IBM MSG files added by Yuri Prokushev

Tomas Hajny 23 年之前
父節點
當前提交
cc750c10cc
共有 1 個文件被更改,包括 120 次插入13 次删除
  1. 120 13
      utils/rstconv.pp

+ 120 - 13
utils/rstconv.pp

@@ -2,6 +2,7 @@
     $Id$
     This file is part of the Free Pascal run time library.
     Copyright (c) 1999-2000 by Sebastian Guenther
+    Added OS/2 support in 2002 by Yuri Prokushev
 
     .rst resource string table file converter.
 
@@ -20,20 +21,29 @@ program rstconv;
 
 uses sysutils, classes;
 
-const
+resourcestring
   help =
-    'rstconv [-h|--help]    Displays this help'#10+
-    'rstconv options        Convert rst file'#10#10+
-    'Options are:'#10+
-    '  -i file    Use specified file instead of stdin as input .rst (OPTIONAL)'#10+
-    '  -o file    Write output to specified file (REQUIRED)'#10+
-    '  -f format  Specifies the output format:'#10+
-    '             po    GNU gettext .po (portable) format (DEFAULT)'#10;
+    'rstconv [-h|--help]    Displays this help'+LineEnding+
+    'rstconv options        Convert rst file'+LineEnding+LineEnding+
+    'Options are:'+LineEnding+
+    '  -i file        Use specified file instead of stdin as input .rst (OPTIONAL)'+LineEnding+
+    '  -o file        Write output to specified file (REQUIRED)'+LineEnding+
+    '  -f format      Specifies the output format:'+LineEnding+
+    '                 po    GNU gettext .po (portable) format (DEFAULT)'+LineEnding+
+    '                 msg   IBM OS/2 MSG file format'+LineEnding+LineEnding+
+    'OS/2 MSG file only options are:'+LineEnding+
+    '  -c identifier  Specifies the component identifier (REQUIRED).'+LineEnding+
+    '                 Identifier is any three chars in upper case.'+LineEnding+
+    '  -n number      Specifies the first message number [1-9999] (OPTIONAL).'+LineEnding;
+
 
   InvalidOption = 'Invalid option - ';
+  RequiredOption = 'Required option is absent - ';
   OptionAlreadySpecified = 'Option has already been specified - ';
   NoOutFilename = 'No output filename specified';
   InvalidOutputFormat = 'Invalid output format -';
+  MessageNumberTooBig = 'Message number too big';
+  InvalidRange = 'Invalid range of the first message number';
 
 
 type
@@ -46,7 +56,8 @@ type
 var
   InFilename, OutFilename: String;
   ConstItems: TCollection;
-
+  Identifier: String[3];
+  FirstMessage: Word;
 
 procedure ReadRSTFile;
 var
@@ -121,7 +132,12 @@ begin
       case c of
         #9:  s := s + '\t';
         #10: s := s + '\n';
+{$IFNDEF UNIX}        
+        #13: ;
+        #1..#8, #11..#12, #14..#31, #128..#255:
+{$ELSE}
         #1..#8, #11..#31, #128..#255:
+{$ENDIF}
           s := s + '\' +
             Chr(Ord(c) shr 6 + 48) +
             Chr((Ord(c) shr 3) and 7 + 48) +
@@ -142,6 +158,54 @@ begin
   Close(f);
 end;
 
+procedure ConvertToOS2MSG;
+var
+  i, j: Integer;
+  f: Text;
+  item: TConstItem;
+  s: String;
+begin
+  If (ConstItems.Count+FirstMessage-1)>9999 then
+  begin
+    WriteLn(MessageNumberTooBig);
+    Halt(1);
+  end;
+  
+  Assign(f, OutFilename);
+  Rewrite(f);
+  
+  WriteLn(f, Identifier);
+  
+  // Fake entry, because MKMSGF limitation
+  WriteLn(f, Format('%s%.4d?: ',[Identifier, FirstMessage-1]));
+  
+  for i := 0 to ConstItems.Count - 1 do begin
+    item := TConstItem(ConstItems.items[i]);
+    
+    // Prepare comment string
+    // Convert string to C-style syntax
+    s := '';
+    j:=1;
+    while j<=Length(item.Value) do
+    begin
+      if copy(item.Value, j, 2)=#13#10 then
+      begin
+        s:=s+#13#10';';
+        Inc(j, 2);
+      end else begin 
+        s := s + item.Value[j];
+        Inc(j);
+      end;
+    end;
+
+    // Write msg entry
+    WriteLn(f, ';', item.ModuleName, '.', item.ConstName);
+    WriteLn(f, Format(';%s%.4d: %s %%0',[Identifier, i+FirstMessage, s]));
+    WriteLn(f, Format('%s%.4dP: %s %%0',[Identifier, i+FirstMessage, Item.Value]));
+  end;
+
+  Close(f);
+end;
 
 
 type
@@ -149,7 +213,7 @@ type
 var
   i: Integer;
   ConversionProc: TConversionProc;
-
+  OutputFormat: String;
 begin
 
   if (ParamStr(1) = '-h') or (ParamStr(1) = '--help') then begin
@@ -158,6 +222,9 @@ begin
   end;
 
   ConversionProc := @ConvertToGettextPO;
+  OutputFormat:='';
+  Identifier:='';
+  FirstMessage:=0;
 
   i := 1;
   while i <= ParamCount do begin
@@ -176,22 +243,59 @@ begin
       OutFilename := ParamStr(i + 1);
       Inc(i, 2);
     end else if ParamStr(i) = '-f' then begin
-      if OutFilename <> '' then begin
+      if OutputFormat <> '' then begin
         WriteLn(StdErr, OptionAlreadySpecified, '-f');
         Halt(1);
       end;
       if ParamStr(i + 1) = 'po' then
-      else begin
+        OutputFormat:='po'
+      else if ParamStr(i + 1) = 'msg' then begin
+        OutputFormat:='msg';
+        ConversionProc := @ConvertToOS2MSG;
+      end else begin
         WriteLn(StdErr, InvalidOutputFormat, ParamStr(i + 1));
         Halt(1);
       end;
       Inc(i, 2);
+    end else if ParamStr(i) = '-c' then begin
+      if Identifier <> '' then begin
+        WriteLn(StdErr, OptionAlreadySpecified, '-c');
+        Halt(1);
+      end;
+      Identifier:=Copy(UpperCase(ParamStr(i+1)), 1, 3);
+      Inc(i, 2);
+    end else if ParamStr(i) = '-n' then begin
+      if FirstMessage <> 0 then begin
+        WriteLn(StdErr, OptionAlreadySpecified, '-n');
+        Halt(1);
+      end;
+      try
+        FirstMessage := StrToInt(ParamStr(i + 1));
+        If (FirstMessage<1) or (FirstMessage>9999) then raise EConvertError.Create(InvalidRange+' '+ParamStr(i+1));
+      except
+        on EConvertError do
+        begin
+          WriteLn(StdErr, InvalidOption, ParamStr(i));
+          Halt(1);
+        end;
+      end;
+      Inc(i, 2);
     end else begin
       WriteLn(StdErr, InvalidOption, ParamStr(i));
       Halt(1);
     end;
   end;
+  
+  If (OutputFormat<>'msg') and ((Identifier<>'') or (FirstMessage<>0)) then begin
+    WriteLn(StdErr, InvalidOption, '');
+    Halt(1);
+  end;
 
+  If (OutputFormat='msg') and (Identifier='') then begin
+    WriteLn(StdErr, RequiredOption, '-c');
+    Halt(1);
+  end;
+  
   if OutFilename = '' then begin
     WriteLn(StdErr, NoOutFilename);
     Halt(1);
@@ -207,7 +311,10 @@ end.
 
 {
   $Log$
-  Revision 1.3  2002-09-07 15:40:31  peter
+  Revision 1.4  2002-09-22 10:58:25  hajny
+    + support of IBM MSG files added by Yuri Prokushev
+
+  Revision 1.3  2002/09/07 15:40:31  peter
     * old logs removed and tabs fixed
 
 }