rstconv.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 (i <= Length(s)) and (s[i] <> '''') do
  64. Inc(i);
  65. item.Value := item.Value + Copy(s, j, i - j);
  66. Inc(i);
  67. end else if s[i] = '#' then begin
  68. Inc(i);
  69. j := i;
  70. while (i <= Length(s)) and (s[i] in ['0'..'9']) do
  71. Inc(i);
  72. item.Value := item.Value + Chr(StrToInt(Copy(s, j, i - j)));
  73. end else if s[i] = '+' then begin
  74. ReadLn(f, s);
  75. i := 1;
  76. end else
  77. Inc(i);
  78. end;
  79. end;
  80. Close(f);
  81. end;
  82. procedure ConvertToGettextPO;
  83. var
  84. i, j: Integer;
  85. f: Text;
  86. item: TConstItem;
  87. s: String;
  88. c: Char;
  89. begin
  90. Assign(f, OutFilename);
  91. Rewrite(f);
  92. for i := 0 to ConstItems.Count - 1 do begin
  93. item := TConstItem(ConstItems.items[i]);
  94. // Convert string to C-style syntax
  95. s := '';
  96. for j := 1 to Length(item.Value) do begin
  97. c := item.Value[j];
  98. case c of
  99. #9: s := s + '\t';
  100. #10: s := s + '\n';
  101. #1..#8, #11..#31, #128..#255:
  102. s := s + '\' +
  103. Chr(Ord(c) shr 6 + 48) +
  104. Chr((Ord(c) shr 3) and 7 + 48) +
  105. Chr(Ord(c) and 7 + 48);
  106. '\': s := s + '\\';
  107. '"': s := s + '\"';
  108. else s := s + c;
  109. end;
  110. end;
  111. // Write msg entry
  112. WriteLn(f, '#: ', item.ModuleName, ':', item.ConstName);
  113. WriteLn(f, 'msgid "', s, '"');
  114. WriteLn(f, 'msgstr ""');
  115. WriteLn(f);
  116. end;
  117. Close(f);
  118. end;
  119. type
  120. TConversionProc = procedure;
  121. var
  122. i: Integer;
  123. ConversionProc: TConversionProc;
  124. begin
  125. if (ParamStr(1) = '-h') or (ParamStr(1) = '--help') then begin
  126. WriteLn(help);
  127. exit;
  128. end;
  129. ConversionProc := @ConvertToGettextPO;
  130. i := 1;
  131. while i <= ParamCount do begin
  132. if ParamStr(i) = '-i' then begin
  133. if InFilename <> '' then begin
  134. WriteLn(StdErr, OptionAlreadySpecified, '-i');
  135. Halt(1);
  136. end;
  137. InFilename := ParamStr(i + 1);
  138. Inc(i, 2);
  139. end else if ParamStr(i) = '-o' then begin
  140. if OutFilename <> '' then begin
  141. WriteLn(StdErr, OptionAlreadySpecified, '-o');
  142. Halt(1);
  143. end;
  144. OutFilename := ParamStr(i + 1);
  145. Inc(i, 2);
  146. end else if ParamStr(i) = '-f' then begin
  147. if OutFilename <> '' then begin
  148. WriteLn(StdErr, OptionAlreadySpecified, '-f');
  149. Halt(1);
  150. end;
  151. if ParamStr(i + 1) = 'po' then
  152. else begin
  153. WriteLn(StdErr, InvalidOutputFormat, ParamStr(i + 1));
  154. Halt(1);
  155. end;
  156. Inc(i, 2);
  157. end else begin
  158. WriteLn(StdErr, InvalidOption, ParamStr(i));
  159. Halt(1);
  160. end;
  161. end;
  162. if OutFilename = '' then begin
  163. WriteLn(StdErr, NoOutFilename);
  164. Halt(1);
  165. end;
  166. ConstItems := TCollection.Create(TConstItem);
  167. ReadRSTFile;
  168. ConversionProc;
  169. end.
  170. {
  171. $Log$
  172. Revision 1.2 2000-10-03 20:58:50 sg
  173. * Made some fixes so that rstconv now works with range checks enabled
  174. Revision 1.1.2.1 2000/10/03 20:56:06 sg
  175. * Made some fixes so that rstconv now works with range checks enabled
  176. Revision 1.1 2000/07/13 10:16:22 michael
  177. + Initial import
  178. Revision 1.6 2000/07/04 19:05:55 peter
  179. * be optimistic: version 1.00 for some utils
  180. Revision 1.5 2000/02/07 13:42:39 peter
  181. * fixed notes
  182. Revision 1.4 2000/01/07 16:46:04 daniel
  183. * copyright 2000
  184. Revision 1.3 1999/07/24 18:35:10 michael
  185. + Added check for missing unitname in constant name
  186. Revision 1.2 1999/07/24 16:22:38 michael
  187. + Comments and empty lines are now ignored
  188. Revision 1.1 1999/07/23 18:23:45 michael
  189. + Added rstconv
  190. }