/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

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