rstconv.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Sebastian Guenther
  5. .rst resource string table file converter.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$MODE objfpc}
  13. {$H+}
  14. program rstconv;
  15. uses sysutils, classes;
  16. const
  17. help =
  18. 'rstconv [-h|--help] Displays this help'#10+
  19. 'rstconv options Convert rst file'#10#10+
  20. 'Options are:'#10+
  21. ' -i file Use specified file instead of stdin as input .rst (OPTIONAL)'#10+
  22. ' -o file Write output to specified file (REQUIRED)'#10+
  23. ' -f format Specifies the output format:'#10+
  24. ' po GNU gettext .po (portable) format (DEFAULT)'#10;
  25. InvalidOption = 'Invalid option - ';
  26. OptionAlreadySpecified = 'Option has already been specified - ';
  27. NoOutFilename = 'No output filename specified';
  28. InvalidOutputFormat = 'Invalid output format -';
  29. type
  30. TConstItem = class(TCollectionItem)
  31. public
  32. ModuleName, ConstName, Value: String;
  33. end;
  34. var
  35. InFilename, OutFilename: String;
  36. ConstItems: TCollection;
  37. procedure ReadRSTFile;
  38. var
  39. f: Text;
  40. s: String;
  41. item: TConstItem;
  42. DotPos, EqPos, i, j: Integer;
  43. begin
  44. Assign(f, InFilename);
  45. Reset(f);
  46. while not eof(f) do begin
  47. ReadLn(f, s);
  48. If (Length(S)=0) or (S[1]='#') then
  49. continue;
  50. item := TConstItem(ConstItems.Add);
  51. DotPos := Pos('.', s);
  52. EqPos := Pos('=', s);
  53. if DotPos > EqPos then // paranoia checking.
  54. DotPos := 0;
  55. item.ModuleName := Copy(s, 1, DotPos - 1);
  56. item.ConstName := Copy(s, DotPos + 1, EqPos - DotPos - 1);
  57. item.Value := '';
  58. i := EqPos + 1;
  59. while i <= Length(s) do begin
  60. if s[i] = '''' then begin
  61. Inc(i);
  62. j := i;
  63. while s[i] <> '''' do Inc(i);
  64. item.Value := item.Value + Copy(s, j, i - j);
  65. Inc(i);
  66. end else if s[i] = '#' then begin
  67. Inc(i);
  68. j := i;
  69. while s[i] in ['0'..'9'] do Inc(i);
  70. item.Value := item.Value + Chr(StrToInt(Copy(s, j, i - j)));
  71. end else if s[i] = '+' then begin
  72. ReadLn(f, s);
  73. i := 1;
  74. end else
  75. Inc(i);
  76. end;
  77. end;
  78. Close(f);
  79. end;
  80. procedure ConvertToGettextPO;
  81. var
  82. i, j: Integer;
  83. f: Text;
  84. item: TConstItem;
  85. s: String;
  86. c: Char;
  87. begin
  88. Assign(f, OutFilename);
  89. Rewrite(f);
  90. for i := 0 to ConstItems.Count - 1 do begin
  91. item := TConstItem(ConstItems.items[i]);
  92. // Convert string to C-style syntax
  93. s := '';
  94. for j := 1 to Length(item.Value) do begin
  95. c := item.Value[j];
  96. case c of
  97. #9: s := s + '\t';
  98. #10: s := s + '\n';
  99. #1..#8, #11..#31, #128..#255:
  100. s := s + '\' +
  101. Chr(Ord(c) shr 6 + 48) +
  102. Chr((Ord(c) shr 3) and 7 + 48) +
  103. Chr(Ord(c) and 7 + 48);
  104. '\': s := s + '\\';
  105. '"': s := s + '\"';
  106. else s := s + c;
  107. end;
  108. end;
  109. // Write msg entry
  110. WriteLn(f, '#: ', item.ModuleName, ':', item.ConstName);
  111. WriteLn(f, 'msgid "', s, '"');
  112. WriteLn(f, 'msgstr ""');
  113. WriteLn(f);
  114. end;
  115. Close(f);
  116. end;
  117. type
  118. TConversionProc = procedure;
  119. var
  120. i: Integer;
  121. ConversionProc: TConversionProc;
  122. begin
  123. if (ParamStr(1) = '-h') or (ParamStr(1) = '--help') then begin
  124. WriteLn(help);
  125. exit;
  126. end;
  127. ConversionProc := @ConvertToGettextPO;
  128. i := 1;
  129. while i <= ParamCount do begin
  130. if ParamStr(i) = '-i' then begin
  131. if InFilename <> '' then begin
  132. WriteLn(StdErr, OptionAlreadySpecified, '-i');
  133. Halt(1);
  134. end;
  135. InFilename := ParamStr(i + 1);
  136. Inc(i, 2);
  137. end else if ParamStr(i) = '-o' then begin
  138. if OutFilename <> '' then begin
  139. WriteLn(StdErr, OptionAlreadySpecified, '-o');
  140. Halt(1);
  141. end;
  142. OutFilename := ParamStr(i + 1);
  143. Inc(i, 2);
  144. end else if ParamStr(i) = '-f' then begin
  145. if OutFilename <> '' then begin
  146. WriteLn(StdErr, OptionAlreadySpecified, '-f');
  147. Halt(1);
  148. end;
  149. if ParamStr(i + 1) = 'po' then
  150. else begin
  151. WriteLn(StdErr, InvalidOutputFormat, ParamStr(i + 1));
  152. Halt(1);
  153. end;
  154. Inc(i, 2);
  155. end else begin
  156. WriteLn(StdErr, InvalidOption, ParamStr(i));
  157. Halt(1);
  158. end;
  159. end;
  160. if OutFilename = '' then begin
  161. WriteLn(StdErr, NoOutFilename);
  162. Halt(1);
  163. end;
  164. ConstItems := TCollection.Create(TConstItem);
  165. ReadRSTFile;
  166. ConversionProc;
  167. end.
  168. {
  169. $Log$
  170. Revision 1.5 2000-02-07 13:42:39 peter
  171. * fixed notes
  172. Revision 1.4 2000/01/07 16:46:04 daniel
  173. * copyright 2000
  174. Revision 1.3 1999/07/24 18:35:10 michael
  175. + Added check for missing unitname in constant name
  176. Revision 1.2 1999/07/24 16:22:38 michael
  177. + Comments and empty lines are now ignored
  178. Revision 1.1 1999/07/23 18:23:45 michael
  179. + Added rstconv
  180. }