PageRenderTime 26ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wrappers/glib/examples/c_array_example.e

http://github.com/tybor/Liberty
Specman e | 84 lines | 69 code | 12 blank | 3 comment | 6 complexity | 21c007a575796f5865317b395a121e9f MD5 | raw file
 1indexing
 2	copyright: "(C) 2007 Paolo Redaelli "
 3	license: "LGPL v2 or later"
 4
 5class C_ARRAY_EXAMPLE
 6
 7inherit WRAPPER_HANDLER -- to play with pointers
 8
 9creation make
10
11feature
12	array: C_ARRAY[G_STRING]
13
14	make is
15		local s: G_STRING; i: INTEGER
16		do
17			create array.with_capacity(4)
18			create s.from_string(first); array.force(s,0)
19			create s.from_string(second); array.force(s,1)
20			create s.from_string(third); array.force(s,2)
21			array.force(Void,3)
22			check
23				array_not_empty: not array.is_empty
24			end
25			
26			print ("First is: ") print (array.first.to_string) print ("%N")
27			from i:=array.lower  until i>array.upper
28			loop
29				s:=array.item(i)
30				if s/=Void then print (s.to_string) print (", ")
31				else print ("Void, ")
32				end
33				i:=i+1
34			end
35			print ("%N")
36			test_null_terminated_array
37		end
38
39	test_null_terminated_array is
40		local i: INTEGER; it: ITERATOR[G_STRING]; s: G_STRING
41		do
42			create {NULL_TERMINATED_C_ARRAY[G_STRING]} array.from_external(null_terminated_array_pointer)
43			check array.count=3 end
44			from print("Using indexes: "); i:=array.lower until i>array.upper
45			loop
46				s:=array.item(i)
47				print(i.out+": ") 
48				if s/=Void then print (s.to_string) print (", ")
49				else print ("Void, ")
50				end
51				i:=i+1
52			end
53
54			print("%NUsing iterator: ") 
55			from it:=array.get_new_iterator; it.start until it.is_off
56			loop
57				s:=it.item
58				if s/=Void then print (s.to_string) print (", ")
59				else print ("Void, ")
60				end
61				it.next
62			end
63			print ("%N")
64		end
65
66	null_terminated_array_pointer: POINTER is
67		do
68			native:=native.calloc(4)
69			-- creating temporary G_STRING objects and throwing them away
70			-- after having added their wrapped string. This tests
71			-- storing/unstorig/disposing of C_STRUCTs.
72			native.put( (create {G_STRING}.from_string(first)).handle,0)
73			native.put( (create {G_STRING}.from_string(second)).handle,1)
74			native.put( (create {G_STRING}.from_string(third)).handle,2)
75			Result:=native.to_external
76		end
77
78	native: NATIVE_ARRAY[POINTER]
79
80	first: STRING is "First Foo"
81	second: STRING is "Second Foo"
82	third: STRING is "Third and last Foo"
83end
84