/[eiffelstudio]/trunk/eweasel/tests/incr071/c_string.e
ViewVC logotype

Contents of /trunk/eweasel/tests/incr071/c_string.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 65297 - (show annotations)
Thu Nov 30 20:22:33 2006 UTC (13 years ago) by manus
File size: 6945 byte(s)
Moved from trunk/Src/eweasel to trunk/eweasel so that a simple checkout of the source code is not penalized by the lenghty process of checking out all the tests of eweasel.
1 indexing
2 description: "A low-level string class to solve some garbage %
3 %collector problems (mainly objects moving around) when %
4 %interfacing with C APIs."
5 status: "See notice at end of class."
6 date: "$Date$"
7 revision: "$Revision$"
8
9 class
10 C_STRING
11
12 inherit
13 STRING_HANDLER
14
15 create
16 make,
17 make_empty,
18 make_by_pointer,
19 make_by_pointer_and_count,
20 share_from_pointer,
21 share_from_pointer_and_count
22
23 feature --{NONE} -- Initialization
24
25 make (a_string: STRING) is
26 -- Make a C string from `a_string'.
27 require
28 a_string_not_void: a_string /= Void
29 do
30 make_empty (a_string.count)
31 set_string (a_string)
32 end
33
34 make_empty (a_length: INTEGER) is
35 -- Make an empty C string of `a_length' characters.
36 -- C memory area is not initialized.
37 require
38 a_length_positive: a_length >= 0
39 do
40 create managed_data.make ((a_length + 1))
41 count := 0
42 end
43
44 make_by_pointer (a_ptr: POINTER) is
45 -- Make a copy of string pointed by `a_ptr'.
46 require
47 a_ptr_not_null: a_ptr /= default_pointer
48 do
49 make_by_pointer_and_count (a_ptr, c_strlen (a_ptr))
50 end
51
52 make_by_pointer_and_count (a_ptr: POINTER; a_length: INTEGER) is
53 -- Make a copy of first `a_length' byte of string pointed by `a_ptr'.
54 require
55 a_ptr_not_null: a_ptr /= default_pointer
56 a_length_non_negative: a_length >= 0
57 do
58 count := a_length
59 create managed_data.make ((a_length + 1))
60 managed_data.item.memory_copy (a_ptr, a_length)
61 end
62
63 feature -- Initialization
64
65 share_from_pointer (a_ptr: POINTER) is
66 -- New instance sharing `a_ptr'.
67 require
68 a_ptr_not_null: a_ptr /= default_pointer
69 do
70 share_from_pointer_and_count (a_ptr, c_strlen (a_ptr))
71 end
72
73 share_from_pointer_and_count (a_ptr: POINTER; a_length: INTEGER) is
74 -- New instance sharing `a_ptr' of `a_length' byte.
75 require
76 a_ptr_not_null: a_ptr /= default_pointer
77 a_length_non_negative: a_length >= 0
78 do
79 count := a_length
80 if managed_data = Void or else not managed_data.is_shared then
81 create managed_data.share_from_pointer (a_ptr, a_length + 1)
82 else
83 managed_data.set_from_pointer (a_ptr, a_length + 1)
84 end
85 end
86
87 feature -- Access
88
89 substring (start_pos, end_pos: INTEGER): STRING is
90 -- Copy of substring containing all characters at indices
91 -- between `start_pos' and `end_pos'.
92 require
93 start_position_big_enough: start_pos >= 1
94 end_position_big_enough: start_pos <= end_pos + 1
95 end_position_not_too_big: end_pos <= count
96 local
97 l_count: INTEGER
98 do
99 l_count := end_pos - start_pos + 1
100 create Result.make (l_count)
101 Result.set_count (l_count)
102 read_substring_into (Result, start_pos, end_pos)
103 ensure
104 susbstring_not_void: Result /= Void
105 end
106
107 string: STRING is
108 -- Eiffel string, ignoring `count'. Reads until a null character is being read.
109 do
110 Result := substring (1, c_strlen (item))
111 ensure
112 string_not_void: Result /= Void
113 end
114
115 read_substring_into (a_string: STRING; start_pos, end_pos: INTEGER) is
116 -- Copy of substring containing all characters at indices
117 -- between `start_pos' and `end_pos' into `a_string'.
118 require
119 a_string_not_void: a_string /= Void
120 start_position_big_enough: start_pos >= 1
121 end_position_big_enough: start_pos <= end_pos + 1
122 end_position_not_too_big: end_pos <= count
123 a_string_large_enough: a_string.count >= end_pos - start_pos + 1
124 local
125 l_area: SPECIAL [CHARACTER]
126 l_data: like managed_data
127 i, nb: INTEGER
128 do
129 from
130 i := start_pos - 1
131 nb := end_pos
132 l_area := a_string.area
133 l_data := managed_data
134 until
135 i = nb
136 loop
137 l_area.put (l_data.read_natural_8 (i).to_character_8, i)
138 i := i + 1
139 end
140 end
141
142 read_string_into (a_string: STRING) is
143 -- Copy of substring containing all characters at indices
144 -- between `start_pos' and `end_pos' into `a_string' replacing any
145 -- existing characters.
146 require
147 a_string_not_void: a_string /= Void
148 a_string_large_enough: a_string.count >= count
149 do
150 read_substring_into (a_string, 1, count)
151 end
152
153 item: POINTER is
154 -- Get pointer to allocated area.
155 do
156 Result := managed_data.item
157 ensure
158 item_not_null: Result /= default_pointer
159 end
160
161 feature -- Measurement
162
163 capacity: INTEGER is
164 -- Number of characters in Current.
165 do
166 Result := managed_data.count
167 end
168
169 count: INTEGER
170 -- Number of characters in Current.
171
172 feature -- Element change
173
174 set_string (a_string: STRING) is
175 -- Set `string' with `a_string'.
176 require
177 a_string_not_void: a_string /= Void
178 local
179 i, nb: INTEGER
180 new_size: INTEGER
181 l_area: SPECIAL [CHARACTER]
182 l_c: CHARACTER
183 do
184 nb := a_string.count
185 count := nb
186
187 new_size := nb + 1
188
189 if managed_data.count < new_size then
190 managed_data.resize (new_size)
191 end
192
193 from
194 i := 0
195 l_area := a_string.area
196 until
197 i = nb
198 loop
199 l_c := l_area.item (i)
200 managed_data.put_natural_8 (l_c.code.to_natural_8, i)
201 i := i + 1
202 end
203 managed_data.put_natural_8 (0, nb)
204 end
205
206 set_count (a_count: INTEGER) is
207 -- Set `count' with `a_count'.
208 -- Note: Current content from index `1' to
209 -- `count.min (a_count)' is unchanged.
210 require
211 a_count_non_negative: a_count >= 0
212 local
213 new_size: INTEGER
214 do
215 new_size := a_count + 1
216 if managed_data.count < new_size then
217 managed_data.resize (new_size)
218 end
219 count := a_count
220 ensure
221 count_set: count = a_count
222 end
223
224 fill_blank is
225 -- Fill Current with zeros.
226 do
227 fill_value (0)
228 ensure
229 -- all_values: For every `i' in 1..`count', `item' (`i') = `0'
230 end
231
232 fill_value (a_value: INTEGER_8) is
233 -- Fill Current with `a_value'.
234 do
235 managed_data.item.memory_set (a_value, managed_data.count)
236 ensure
237 -- all_values: For every `i' in 1..`count', `item' (`i') = `a_value'
238 end
239
240 feature {NONE} -- Implementation
241
242 managed_data: MANAGED_POINTER
243 -- Hold data of Current.
244
245 c_strlen (ptr: POINTER): INTEGER is
246 external
247 "C macro signature (char *): EIF_INTEGER use <string.h>"
248 alias
249 "strlen"
250 end
251
252 invariant
253 managed_data_not_void: managed_data /= Void
254 count_not_negative: count >= 0
255
256 end -- class C_STRING
257
258 --|----------------------------------------------------------------
259 --| Windows Eiffel Library: library of reusable components for ISE Eiffel.
260 --| Copyright (c) 1993-2006 University of Southern California and contributors.
261 --| All rights reserved.
262 --| Your use of this work is governed under the terms of the GNU General
263 --| Public License version 2.
264 --| May be used only with ISE Eiffel, under terms of user license.
265 --| Contact ISE for any other use.
266 --|
267 --| Interactive Software Engineering Inc.
268 --| ISE Building
269 --| 360 Storke Road, Goleta, CA 93117 USA
270 --| Telephone 805-685-1006, Fax 805-685-6869
271 --| Electronic mail <info@eiffel.com>
272 --| Customer support: http://support.eiffel.com>
273 --| For latest info see award-winning pages: http://www.eiffel.com
274 --|----------------------------------------------------------------

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23