This file is indexed.

/usr/src/castle-game-engine-6.4/castlescript/castlescriptxml.pas is in castle-game-engine-src 6.4+dfsg1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
{
  Copyright 2016-2017 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" 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.

  ----------------------------------------------------------------------------
}

{ Helpers to read CastleScript expressions from XML config files
  (TCastleConfigScriptHelper, TDOMElementScriptHelper). }
unit CastleScriptXML;

{$I castleconf.inc}

interface

uses Math, DOM,
  CastleKeysMouse, CastleXMLConfig, CastleXMLUtils;

type
  { Class helper to read CastleScript expressions from XML config files.
    Use this unit, and then you can call powerful @link(GetFloatExpression)
    instead of @link(TCastleConfig.GetFloat). }
  TCastleConfigScriptHelper = class helper(TCastleConfigKeysMouseHelper) for TCastleConfig

    { Read a float expression composed in CastleScript,
      like @code("123.0") or @code("3.0 * 2.0") or @code("sin(2.0)").
      @groupBegin }
    function GetFloatExpression(const APath: string;
      const ADefaultValue: Float): Float;
    function GetFloatExpression(const APath: string;
      const ADefaultValue: string): Float;
    function GetFloatExpression(const APath: string): Float;
    { @groupEnd }

    { Read an integer expression composed in CastleScript,
      like @code("123") or @code("3 * 2 + 5").
      @groupBegin }
    function GetIntExpression(const APath: string;
      const ADefaultValue: Int64): Int64;
    function GetIntExpression(const APath: string;
      const ADefaultValue: string): Int64;
    function GetIntExpression(const APath: string): Int64;
    { @groupEnd }
  end;

  { Class helper to read CastleScript expressions from DOM (XML files).
    Use this unit, and then you can call powerful methods
    @link(AttributeFloatExpression), @link(AttributeFloatExpressionDef)
    instead of the
    @link(TDOMElementHelper.AttributeFloat), @link(TDOMElementHelper.AttributeFloatDef). }
  TDOMElementScriptHelper = class helper(TDOMElementHelper) for TDOMElement

    { Read from Element attribute value as a Float expression, and returns @true.
      The expression can be anything in CastleScript
      http://castle-engine.sourceforge.net/castle_script.php ,
      for example something crazy like @code(2.0 * sin(5.0) + Pi).
      You can use this method instead of @link(AttributeFloat) to easily allow
      mathematical exressions in XML attributes.

      If there is no such attribute returns @false and does not modify Value. }
    function AttributeFloatExpression(const AttrName: string; var Value: Float): boolean;

    { Retrieves from Element given attribute as a Float expression,
      raises EDOMAttributeMissing if missing.

      The expression can be anything in CastleScript
      http://castle-engine.sourceforge.net/castle_script.php ,
      for example something crazy like @code(2.0 * sin(5.0) + Pi).
      You can use this method instead of @link(AttributeFloat) to easily allow
      mathematical exressions in XML attributes.

      @raises EDOMAttributeMissing }
    function AttributeFloatExpression(const AttrName: string): Float;

    { Retrieves from Element given attribute as a Float expression, or a default value.

      The expression can be anything in CastleScript
      http://castle-engine.sourceforge.net/castle_script.php ,
      for example something crazy like @code(2.0 * sin(5.0) + Pi).
      You can use this method instead of @link(AttributeFloatDef) to easily allow
      mathematical exressions in XML attributes. }
    function AttributeFloatExpressionDef(const AttrName: string; const DefaultValue: Float): Float;
  end;

implementation

uses SysUtils,
  CastleScript, CastleScriptParser;

{ TCastleConfigScriptHelper -------------------------------------------------- }

function TCastleConfigScriptHelper.GetFloatExpression(const APath: string;
  const ADefaultValue: Float): Float;
begin
  Result := GetFloatExpression(APath, FloatToStr(ADefaultValue));
end;

function TCastleConfigScriptHelper.GetFloatExpression(const APath: string;
  const ADefaultValue: string): Float;
var
  ResultString: string;
  E: TCasScriptExpression;
begin
  ResultString := GetValue(APath, ADefaultValue);
  E := ParseFloatExpression(ResultString, []);
  try
    Result := E.AsFloat;
  finally FreeAndNil(E) end;
end;

function TCastleConfigScriptHelper.GetFloatExpression(const APath: string): Float;
var
  E: TCasScriptExpression;
begin
  E := ParseFloatExpression(GetStringNonEmpty(APath), []);
  try
    Result := E.AsFloat;
  finally FreeAndNil(E) end;
end;

function TCastleConfigScriptHelper.GetIntExpression(const APath: string;
  const ADefaultValue: Int64): Int64;
begin
  Result := GetIntExpression(APath, IntToStr(ADefaultValue));
end;

function TCastleConfigScriptHelper.GetIntExpression(const APath: string;
  const ADefaultValue: string): Int64;
var
  ResultString: string;
  E: TCasScriptExpression;
begin
  ResultString := GetValue(APath, ADefaultValue);
  E := ParseIntExpression(ResultString, []);
  try
    Result := E.AsInt;
  finally FreeAndNil(E) end;
end;

function TCastleConfigScriptHelper.GetIntExpression(const APath: string): Int64;
var
  E: TCasScriptExpression;
begin
  E := ParseIntExpression(GetStringNonEmpty(APath), []);
  try
    Result := E.AsInt;
  finally FreeAndNil(E) end;
end;

{ TDOMElementScriptHelper ---------------------------------------------------- }

function TDOMElementScriptHelper.AttributeFloatExpression(
  const AttrName: string; var Value: Float): boolean;
var
  ValueStr: string;
  E: TCasScriptExpression;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
  begin
    E := ParseFloatExpression(ValueStr, []);
    try
      Value := E.AsFloat;
    finally FreeAndNil(E) end;
  end;
end;

function TDOMElementScriptHelper.AttributeFloatExpression(const AttrName: string): Float;
begin
  if not AttributeFloatExpression(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing required (float) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementScriptHelper.AttributeFloatExpressionDef(const AttrName: string; const DefaultValue: Float): Float;
begin
  if not AttributeFloatExpression(AttrName, Result) then
    Result := DefaultValue;
end;

end.