PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/src/wrappers/common/library/null_terminated_string_array.e

http://github.com/tybor/Liberty
Specman e | 66 lines | 38 code | 13 blank | 15 comment | 3 complexity | 5d4e8b2c85974cfd5f1382e793e7e79e MD5 | raw file
 1indexing
 2	description: "Wrapper for NULL terminated array of strings."
 3	copyright: "[
 4					Copyright (C) 2007 Paolo Redaelli 
 5					
 6					This library is free software; you can redistribute it and/or
 7					modify it under the terms of the GNU Lesser General Public License
 8					as published by the Free Software Foundation; either version 2.1 of
 9					the License, or (at your option) any later version.
10					
11					This library is distributed in the hopeOA that it will be useful, but
12					WITHOUT ANY WARRANTY; without even the implied warranty of
13					MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14					Lesser General Public License for more details.
15
16					You should have received a copy of the GNU Lesser General Public
17					License along with this library; if not, write to the Free Software
18					Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19					02110-1301 USA
20			]"
21
22class NULL_TERMINATED_STRING_ARRAY
23	-- A NULL-terminated STRING_ARRAY, useful since many low-level C
24	-- functions expect to receive such arrays. 
25
26	-- The NULL (default_pointer in Eiffel) is not counted as an
27	-- element of the array.
28
29inherit  STRING_ARRAY --  redefine with_capacity end
30								 
31creation from_collection, from_external --, with_capacity
32
33feature {} -- Creation
34	-- Note: space allocated in storage must always be capacity+1 large, to
35	-- store the ending NULL pointer
36
37
38	from_external (an_array: POINTER) is
39			-- Initialize the NULL_TERMINATED_C_ARRAY from `an_array'
40			-- pointer. The array is inspected from the beginning to
41			-- discover the first NULL pointer that marks its end.
42		require array_not_null: an_array.is_not_null
43		local a_lenght: INTEGER
44		do
45			storage := storage.from_pointer (an_array)
46			-- Look for NULL
47			from a_lenght:=lower until storage.item(a_lenght).is_null loop a_lenght:=a_lenght+1 end
48			create strings.make(a_lenght)
49			fill_strings
50		end
51	
52	-- with_capacity (a_capacity: INTEGER) is do
53	-- storage:=storage.calloc(a_capacity+1) create
54	-- strings.make(a_capacity+1) end
55
56feature
57	is_null_terminated: BOOLEAN is
58		do
59			-- 0 1 2 3 4
60			-- a b c d NULL
61			Result:=storage.item(upper+1).is_null
62		end
63
64invariant 
65	null_terminated: is_null_terminated
66end -- class NULL_TERMINATED_STRING_ARRAY