/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

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