ncurses2-genericputs.adb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- ncurses --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 2000-2006,2008 Free Software Foundation, Inc. --
  11. -- --
  12. -- Permission is hereby granted, free of charge, to any person obtaining a --
  13. -- copy of this software and associated documentation files (the --
  14. -- "Software"), to deal in the Software without restriction, including --
  15. -- without limitation the rights to use, copy, modify, merge, publish, --
  16. -- distribute, distribute with modifications, sublicense, and/or sell --
  17. -- copies of the Software, and to permit persons to whom the Software is --
  18. -- furnished to do so, subject to the following conditions: --
  19. -- --
  20. -- The above copyright notice and this permission notice shall be included --
  21. -- in all copies or substantial portions of the Software. --
  22. -- --
  23. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
  24. -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
  25. -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
  26. -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
  27. -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
  28. -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
  29. -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
  30. -- --
  31. -- Except as contained in this notice, the name(s) of the above copyright --
  32. -- holders shall not be used in advertising or otherwise to promote the --
  33. -- sale, use or other dealings in this Software without prior written --
  34. -- authorization. --
  35. ------------------------------------------------------------------------------
  36. -- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
  37. -- Version Control
  38. -- $Revision: 1.3 $
  39. -- $Date: 2008/07/26 18:46:18 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  43. with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
  44. package body ncurses2.genericPuts is
  45. procedure myGet (Win : in Window := Standard_Window;
  46. Str : out BS.Bounded_String;
  47. Len : in Integer := -1)
  48. is
  49. function Wgetnstr (Win : Window;
  50. Str : char_array;
  51. Len : int) return int;
  52. pragma Import (C, Wgetnstr, "wgetnstr");
  53. N : Integer := Len;
  54. Txt : char_array (0 .. size_t (Max_Length));
  55. xStr : String (1 .. Max_Length);
  56. Cnt : Natural;
  57. begin
  58. if N < 0 then
  59. N := Max_Length;
  60. end if;
  61. if N > Max_Length then
  62. raise Constraint_Error;
  63. end if;
  64. Txt (0) := Interfaces.C.char'First;
  65. if Wgetnstr (Win, Txt, C_Int (N)) = Curses_Err then
  66. raise Curses_Exception;
  67. end if;
  68. To_Ada (Txt, xStr, Cnt, True);
  69. Str := To_Bounded_String (xStr (1 .. Cnt));
  70. end myGet;
  71. procedure myPut (Str : out BS.Bounded_String;
  72. i : Integer;
  73. Base : in Number_Base := 10) is
  74. package Int_IO is new Integer_IO (Integer); use Int_IO;
  75. tmp : String (1 .. BS.Max_Length);
  76. begin
  77. Put (tmp, i, Base);
  78. Str := To_Bounded_String (tmp);
  79. Trim (Str, Ada.Strings.Trim_End'(Ada.Strings.Left));
  80. end myPut;
  81. procedure myAdd (Str : BS.Bounded_String) is
  82. begin
  83. Add (Str => To_String (Str));
  84. end myAdd;
  85. -- from ncurses-aux
  86. procedure Fill_String (Cp : in chars_ptr;
  87. Str : out BS.Bounded_String)
  88. is
  89. -- Fill the string with the characters referenced by the
  90. -- chars_ptr.
  91. --
  92. Len : Natural;
  93. begin
  94. if Cp /= Null_Ptr then
  95. Len := Natural (Strlen (Cp));
  96. if Max_Length < Len then
  97. raise Constraint_Error;
  98. end if;
  99. declare
  100. S : String (1 .. Len);
  101. begin
  102. S := Value (Cp);
  103. Str := To_Bounded_String (S);
  104. end;
  105. else
  106. Str := Null_Bounded_String;
  107. end if;
  108. end Fill_String;
  109. end ncurses2.genericPuts;