/[eiffelstudio]/FreeELKS/trunk/library/support/c_string.e
ViewVC logotype

Contents of /FreeELKS/trunk/library/support/c_string.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91635 - (show annotations)
Wed Oct 29 21:03:39 2008 UTC (11 years, 3 months ago) by kwaxer
File size: 9343 byte(s)
Removed duplicated parentheses.
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 library: "Free implementation of ELKS library"
6 copyright: "Copyright (c) 1986-2008, Eiffel Software and others"
7 license: "Eiffel Forum License v2 (see forum.txt)"
8 date: "$Date$"
9 revision: "$Revision$"
10
11 class
12 C_STRING
13
14 inherit
15 STRING_HANDLER
16
17 create
18 make,
19 make_empty,
20 make_by_pointer,
21 make_by_pointer_and_count,
22 share_from_pointer,
23 share_from_pointer_and_count
24
25 feature --{NONE} -- Initialization
26
27 make (a_string: READABLE_STRING_GENERAL) is
28 -- Make a C string from `a_string'.
29 require
30 a_string_not_void: a_string /= Void
31 do
32 make_empty (a_string.count)
33 set_string (a_string)
34 end
35
36 make_empty (a_length: INTEGER) is
37 -- Make an empty C string of `a_length' characters.
38 -- C memory area is not initialized.
39 require
40 a_length_positive: a_length >= 0
41 do
42 create managed_data.make (a_length + 1)
43 count := 0
44 end
45
46 make_by_pointer (a_ptr: POINTER) is
47 -- Make a copy of string pointed by `a_ptr'.
48 require
49 a_ptr_not_null: a_ptr /= default_pointer
50 do
51 make_by_pointer_and_count (a_ptr, c_strlen (a_ptr))
52 end
53
54 make_by_pointer_and_count (a_ptr: POINTER; a_length: INTEGER) is
55 -- Make a copy of first `a_length' byte of string pointed by `a_ptr'.
56 require
57 a_ptr_not_null: a_ptr /= default_pointer
58 a_length_non_negative: a_length >= 0
59 do
60 count := a_length
61 create managed_data.make ((a_length + 1))
62 managed_data.item.memory_copy (a_ptr, a_length)
63 end
64
65 feature -- Initialization
66
67 share_from_pointer (a_ptr: POINTER) is
68 -- New instance sharing `a_ptr'.
69 require
70 a_ptr_not_null: a_ptr /= default_pointer
71 do
72 share_from_pointer_and_count (a_ptr, c_strlen (a_ptr))
73 end
74
75 share_from_pointer_and_count (a_ptr: POINTER; a_length: INTEGER) is
76 -- New instance sharing `a_ptr' of `a_length' byte.
77 require
78 a_ptr_not_null: a_ptr /= default_pointer
79 a_length_non_negative: a_length >= 0
80 do
81 count := a_length
82 if managed_data = Void or else not managed_data.is_shared then
83 create managed_data.share_from_pointer (a_ptr, a_length + 1)
84 else
85 managed_data.set_from_pointer (a_ptr, a_length + 1)
86 end
87 end
88
89 feature -- Access
90
91 substring (start_pos, end_pos: INTEGER): STRING is
92 -- Copy of substring containing all characters at indices
93 -- between `start_pos' and `end_pos'.
94 require
95 start_position_big_enough: start_pos >= 1
96 end_position_big_enough: start_pos <= end_pos + 1
97 end_position_not_too_big: end_pos <= capacity
98 local
99 l_count: INTEGER
100 do
101 l_count := end_pos - start_pos + 1
102 create Result.make (l_count)
103 Result.set_count (l_count)
104 read_substring_into (Result, start_pos, end_pos)
105 ensure
106 susbstring_not_void: Result /= Void
107 end
108
109 string: STRING is
110 -- Eiffel string, ignoring `count'. Reads until a null character is being read.
111 do
112 Result := substring (1, c_strlen (item))
113 ensure
114 string_not_void: Result /= Void
115 end
116
117 copy_to_string (a_string: STRING_GENERAL; source_index, destination_index, n: INTEGER) is
118 -- Copy `n' characters of `Current' from `source_index' position to `a_string' at
119 -- `destination_index'. Other characters of `a_string' remain unchanged.
120 require
121 a_string_not_void: a_string /= Void
122 source_index_positive: source_index >= 1
123 destination_index_positive: destination_index >= 1
124 n_non_negative: n >= 0
125 n_is_small_enough_for_source: source_index + (n - 1) <= capacity
126 n_is_small_enough_for_destination: destination_index + (n - 1) <= a_string.count
127 local
128 l_data: like managed_data
129 i, j, nb: INTEGER
130 do
131 from
132 i := source_index - 1
133 j := destination_index
134 nb := source_index + (n - 1)
135 l_data := managed_data
136 until
137 i = nb
138 loop
139 a_string.put_code (l_data.read_natural_8 (i), j)
140 i := i + 1
141 j := j + 1
142 end
143 end
144
145 read_substring_into (a_string: STRING_GENERAL; start_pos, end_pos: INTEGER) is
146 -- Copy of substring containing all characters at indices
147 -- between `start_pos' and `end_pos' into `a_string'.
148 require
149 a_string_not_void: a_string /= Void
150 start_position_big_enough: start_pos >= 1
151 end_position_big_enough: start_pos <= end_pos + 1
152 end_position_not_too_big: end_pos <= capacity
153 a_string_large_enough: a_string.count >= end_pos - start_pos + 1
154 local
155 l_data: like managed_data
156 i, j, nb: INTEGER
157 do
158 from
159 i := start_pos - 1
160 nb := end_pos - start_pos
161 l_data := managed_data
162 j := 1
163 until
164 i > nb
165 loop
166 a_string.put_code (l_data.read_natural_8 (i), j)
167 i := i + 1
168 j := j + 1
169 end
170 end
171
172 read_string_into (a_string: STRING_GENERAL) is
173 -- Copy of substring containing all characters at indices
174 -- between `start_pos' and `end_pos' into `a_string' replacing any
175 -- existing characters.
176 require
177 a_string_not_void: a_string /= Void
178 a_string_large_enough: a_string.count >= count
179 do
180 read_substring_into (a_string, 1, count)
181 end
182
183 read_substring_into_character_8_area (a_area: SPECIAL [CHARACTER_8]; start_pos, end_pos: INTEGER) is
184 -- Copy of substring containing all characters at indices
185 -- between `start_pos' and `end_pos' into `a_area'.
186 require
187 a_area_not_void: a_area /= Void
188 start_position_big_enough: start_pos >= 1
189 end_position_big_enough: start_pos <= end_pos + 1
190 end_position_not_too_big: end_pos <= capacity
191 a_area_large_enough: a_area.count >= end_pos - start_pos + 1
192 local
193 l_data: like managed_data
194 i, j, nb: INTEGER
195 do
196 from
197 i := start_pos - 1
198 nb := end_pos - start_pos
199 l_data := managed_data
200 until
201 i > nb
202 loop
203 a_area.put (l_data.read_natural_8 (i).to_character_8, j)
204 i := i + 1
205 j := j + 1
206 end
207 ensure
208 copied: -- for i in 0..end_pos - start_pos, a_area [i] = Current [i + start_pos]
209 end
210
211 read_substring_into_character_32_area (a_area: SPECIAL [CHARACTER_32]; start_pos, end_pos: INTEGER) is
212 -- Copy of substring containing all characters at indices
213 -- between `start_pos' and `end_pos' into `a_area'.
214 require
215 a_area_not_void: a_area /= Void
216 start_position_big_enough: start_pos >= 1
217 end_position_big_enough: start_pos <= end_pos + 1
218 end_position_not_too_big: end_pos <= capacity
219 a_area_large_enough: a_area.count >= end_pos - start_pos + 1
220 local
221 l_data: like managed_data
222 i, j, nb: INTEGER
223 do
224 from
225 i := start_pos - 1
226 nb := end_pos - start_pos
227 l_data := managed_data
228 until
229 i > nb
230 loop
231 a_area.put (l_data.read_natural_32 (i).to_character_32, j)
232 i := i + 1
233 j := j + 1
234 end
235 ensure
236 copied: -- for i in 0..end_pos - start_pos, a_area [i] = Current [i + start_pos]
237 end
238
239 item: POINTER is
240 -- Get pointer to allocated area.
241 do
242 Result := managed_data.item
243 ensure
244 item_not_null: Result /= default_pointer
245 end
246
247 managed_data: MANAGED_POINTER
248 -- Hold data of Current.
249
250 feature -- Measurement
251
252 capacity: INTEGER is
253 -- Number of characters in Current.
254 do
255 Result := managed_data.count
256 end
257
258 bytes_count: INTEGER is
259 -- Number of bytes represented by the string.
260 do
261 Result := count
262 end
263
264 count: INTEGER
265 -- Number of characters in Current.
266
267 character_size: INTEGER is 1
268 -- Size of a character
269
270 feature -- Element change
271
272 set_string (a_string: READABLE_STRING_GENERAL) is
273 -- Set `string' with `a_string'.
274 require
275 a_string_not_void: a_string /= Void
276 a_string_is_string_8: a_string.is_valid_as_string_8
277 do
278 set_substring (a_string, 1, a_string.count)
279 end
280
281 set_substring (a_string: READABLE_STRING_GENERAL; start_pos, end_pos: INTEGER) is
282 -- Set `string' with `a_string'.
283 require
284 a_string_not_void: a_string /= Void
285 start_position_big_enough: start_pos >= 1
286 end_position_big_enough: start_pos <= end_pos + 1
287 end_pos_small_enough: end_pos <= a_string.count
288 local
289 i, nb: INTEGER
290 new_size: INTEGER
291 do
292 nb := end_pos - start_pos + 1
293 count := nb
294
295 new_size := nb + 1
296
297 if managed_data.count < new_size then
298 managed_data.resize (new_size)
299 end
300
301 from
302 i := 0
303 until
304 i = nb
305 loop
306 managed_data.put_natural_8 (a_string.code (i + start_pos).to_natural_8, i)
307 i := i + 1
308 end
309 managed_data.put_natural_8 (0, nb)
310 end
311
312 set_count (a_count: INTEGER) is
313 -- Set `count' with `a_count'.
314 -- Note: Current content from index `1' to
315 -- `count.min (a_count)' is unchanged.
316 require
317 a_count_non_negative: a_count >= 0
318 local
319 new_size: INTEGER
320 do
321 new_size := a_count + 1
322 if managed_data.count < new_size then
323 managed_data.resize (new_size)
324 end
325 count := a_count
326 ensure
327 count_set: count = a_count
328 end
329
330 fill_blank is
331 -- Fill Current with zeros.
332 do
333 fill_value (0)
334 ensure
335 -- all_values: For every `i' in 1..`count', `item' (`i') = `0'
336 end
337
338 fill_value (a_value: INTEGER_8) is
339 -- Fill Current with `a_value'.
340 do
341 managed_data.item.memory_set (a_value, managed_data.count)
342 ensure
343 -- all_values: For every `i' in 1..`count', `item' (`i') = `a_value'
344 end
345
346 feature {NONE} -- Implementation
347
348 c_strlen (ptr: POINTER): INTEGER is
349 external
350 "C signature (char *): EIF_INTEGER use <string.h>"
351 alias
352 "strlen"
353 end
354
355 invariant
356 managed_data_not_void: managed_data /= Void
357 count_not_negative: count >= 0
358
359 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23