PageRenderTime 20ms CodeModel.GetById 11ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tools/liberty_environment.e

http://github.com/tybor/Liberty
Specman e | 147 lines | 120 code | 7 blank | 20 comment | 9 complexity | ee951036ccc1cca575a34864f52d5ed3 MD5 | raw file
  1-- This file is part of Liberty Eiffel.
  2--
  3-- Liberty Eiffel is free software: you can redistribute it and/or modify
  4-- it under the terms of the GNU General Public License as published by
  5-- the Free Software Foundation, version 3 of the License.
  6--
  7-- Liberty Eiffel is distributed in the hope that it will be useful,
  8-- but WITHOUT ANY WARRANTY; without even the implied warranty of
  9-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 10-- GNU General Public License for more details.
 11--
 12-- You should have received a copy of the GNU General Public License
 13-- along with Liberty Eiffel.  If not, see <http://www.gnu.org/licenses/>.
 14--
 15expanded class LIBERTY_ENVIRONMENT
 16
 17insert
 18   LOGGING
 19   SYSTEM
 20      export
 21         {ANY} copy, is_equal, standard_is_equal, generating_type;
 22         {} all
 23      end
 24
 25feature {ANY}
 26   item (a_name: ABSTRACT_STRING): STRING is
 27      require
 28         a_name /= Void
 29      local
 30         name: FIXED_STRING
 31         value: STRING
 32         n, v: POINTER
 33      do
 34         name := a_name.intern
 35         value := environment.fast_reference_at(name)
 36         if value /= Void then
 37            Result := once ""
 38            Result.copy(value)
 39         else
 40            n := name.to_external
 41            v := basic_getenv(n)
 42            if v.is_not_null then
 43               Result := once ""
 44               Result.from_external_copy(v)
 45            end
 46         end
 47      end
 48
 49   set (a_name, a_value: ABSTRACT_STRING) is
 50      require
 51         a_name /= Void
 52         a_value /= Void
 53      local
 54         name: FIXED_STRING
 55         value: STRING
 56      do
 57         name := a_name.intern
 58         value := environment.fast_reference_at(name)
 59         if value /= Void then
 60            value.clear_count
 61            value.append(a_value)
 62         else
 63            environment.add(a_value.out, name)
 64         end
 65
 66         log.trace.put_string(a_name)
 67         log.trace.put_string(once " = ")
 68         log.trace.put_line(a_value)
 69      ensure
 70         item(a_name).is_equal(a_value.out)
 71      end
 72
 73feature {ANY}
 74   substitute (line: STRING) is
 75         -- The only one accepted notation is:  ${...}. The substitution is performed in `line'.
 76         --| Taken from SmartEiffel: SYSTEM_TOOLS.environment_variable_substitution - but de-recursivated
 77      require
 78         line /= Void
 79      local
 80         i, state, offset_start, offset_end: INTEGER; c: CHARACTER; value, variable: STRING; done: BOOLEAN
 81      do
 82         from
 83            variable := once ""
 84         until
 85            done
 86         loop
 87            from
 88               state := 0
 89               i := line.lower
 90            until
 91               i > line.upper
 92            loop
 93               c := line.item(i)
 94               inspect
 95                  state
 96               when 0 then
 97                  -- Initial state.
 98                  if c = '$' then
 99                     state := 1
100                     offset_start := i
101                  end
102               when 1 then
103                  -- "$" read.
104                  if c = '{' then
105                     state := 2
106                     variable.clear_count
107                  else
108                     state := 0
109                  end
110               when 2 then
111                  -- "${" read.
112                  if c = '}' then
113                     state := 3
114                     offset_end := i
115                     i := line.upper
116                  else
117                     variable.extend(c)
118                  end
119               else -- First correct variable found.
120               end
121               i := i + 1
122            end
123            if state = 3 then
124               -- One variable found:
125               value := item(variable)
126               if value = Void then
127                  done := True
128               else
129                  variable.copy(line)
130                  line.keep_head(offset_start - 1)
131                  line.append(value)
132                  variable.remove_head(offset_end)
133                  line.append(variable)
134               end
135            else
136               done := True
137            end
138         end
139      end
140
141feature {}
142   environment: DICTIONARY[STRING, FIXED_STRING] is
143      once
144         create {HASHED_DICTIONARY[STRING, FIXED_STRING]} Result.make
145      end
146
147end