TOptionConverterUnit.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. {
  2. Copyright 2005-2006 Log4Delphi Project
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. }
  13. {*----------------------------------------------------------------------------
  14. Contains the TOptionConverter class.
  15. @version 0.5
  16. @author <a href="mailto:tcmiller@users.sourceforge.net">Trevor Miller</a>
  17. ----------------------------------------------------------------------------}
  18. unit TOptionConverterUnit;
  19. {$ifdef fpc}
  20. {$mode objfpc}
  21. {$h+}
  22. {$endif}
  23. interface
  24. uses
  25. TPropertiesUnit;
  26. const
  27. DELIM_START = '${';
  28. DELIM_STOP = '}';
  29. DELIM_START_LEN = 2;
  30. DELIM_STOP_LEN = 1;
  31. type
  32. TOptionConverter = class (TObject)
  33. private
  34. protected
  35. public
  36. class function FindAndSubst(const AKey : String; AProps : TProperties) : String;
  37. class function SubstVars(const AValue : String; AProps : TProperties) : String;
  38. class function IndexOf(const S : String; subStr : String; index : Integer) : Integer;
  39. class function SubString(const S: String; startIndex : Integer; endIndex : Integer) : String;
  40. end;
  41. implementation
  42. uses
  43. SysUtils;
  44. class function TOptionConverter.IndexOf(const S : String; subStr : String; index : Integer) : Integer;
  45. var
  46. tmp : String;
  47. begin
  48. tmp := Copy(subStr, Index, System.Length(subStr) - Index);
  49. result := Pos(subStr, S);
  50. if (result > 0) then
  51. result := result + Index
  52. else
  53. result := -1;
  54. end;
  55. class function TOptionConverter.SubString(const S: String; startIndex : Integer; endIndex : Integer) : String;
  56. begin
  57. substring := Copy(S, StartIndex, (StartIndex + EndIndex));
  58. end;
  59. class function TOptionConverter.FindAndSubst(const AKey : String; AProps : TProperties) : String;
  60. var
  61. value : STring;
  62. begin
  63. value := AProps.GetProperty(AKey);
  64. try
  65. result := SubstVars(value, AProps);
  66. except
  67. on Exception do
  68. result := value;
  69. end;
  70. end;
  71. class function TOptionConverter.SubstVars(const AValue : String; AProps : TProperties) : String;
  72. var
  73. buf, key, replacement : String;
  74. i,j,k : Integer;
  75. begin
  76. buf := '';
  77. i := 0;
  78. while (true) do begin
  79. j := indexOf(AValue, DELIM_START, i);
  80. if (j = -1) then begin // No DELIM found
  81. if (i = 0) then begin
  82. result := AValue;
  83. exit;
  84. end else begin
  85. buf := buf + Substring(AValue, i, System.Length(AValue));
  86. result := buf;
  87. exit;
  88. end;
  89. end else begin // DELIM found
  90. buf := buf + SubString(AVAlue, i, j);
  91. k := IndexOf(AVAlue, DELIM_STOP, j);
  92. if (k = -1) then begin
  93. raise Exception.Create('"' + AVAlue + '" has no closing brace.'
  94. + ' Opening brace at ' + IntToStr(j) + '.');
  95. end else begin
  96. j := j + DELIM_START_LEN;
  97. key := SubString(AVAlue, j, k);
  98. replacement := AProps.GetProperty(key);
  99. if (replacement <> '') then
  100. buf := buf + replacement;
  101. i := k + DELIM_STOP_LEN;
  102. end;
  103. end;
  104. end;
  105. end;
  106. end.