From: <jb...@us...> - 2009-09-24 01:01:16
|
Revision: 10468 http://plplot.svn.sourceforge.net/plplot/?rev=10468&view=rev Author: jbauck Date: 2009-09-24 01:01:06 +0000 (Thu, 24 Sep 2009) Log Message: ----------- Fix a longstanding problem in Ada examples 17 in which all of the stripchart labels are the same and equal to the last one. Modified Paths: -------------- trunk/bindings/ada/plplot.adb.cmake trunk/bindings/ada/plplot.ads.cmake trunk/bindings/ada/plplot_thin.ads.cmake trunk/bindings/ada/plplot_traditional.adb.cmake trunk/bindings/ada/plplot_traditional.ads.cmake trunk/examples/ada/x19a.adb.cmake trunk/examples/ada/xthick19a.adb.cmake Modified: trunk/bindings/ada/plplot.adb.cmake =================================================================== --- trunk/bindings/ada/plplot.adb.cmake 2009-09-23 23:34:11 UTC (rev 10467) +++ trunk/bindings/ada/plplot.adb.cmake 2009-09-24 01:01:06 UTC (rev 10468) @@ -2859,7 +2859,7 @@ Box_Color, Legend_Color : Plot_Color_Type; Pen_Colors : Integer_Array_1D; Line_Styles : Integer_Array_1D; - Pen_Labels : Stripchart_Label_String_Array_Type; + Pen_Labels : in out Stripchart_Label_String_Array_Type; X_Label, Y_Label, Title_Label : String := To_String(Default_Label_String)) is PL_Autoscale_Y, PL_Accumulate : PLBOOL; @@ -2881,17 +2881,30 @@ PL_Accumulate := PLfalse; end if; - -- Adapt strings for Pen_Labels to C. Head truncates or pads as - -- necessary to the length of Stripchart_String - 1; we then append - -- a null terminator which I suppose C needs. This could have also been - -- accomplished using char_array from the C interfaces. - -- fix this NOTE that the current implementation displays all four - -- legend labels the same, and equal to the fourth legend label. The - -- goal is to match const char *legline[4] in plstripc.c and e.g. - -- in x17c.c. - for Index in Pen_Labels'range loop - Temp_C_Stripchart_String := To_String(Head(Pen_Labels(Index), PL_Stripchart_String'length - 1) & Character'val(0)); - PL_Pen_Labels(Index) := Temp_C_Stripchart_String'Access; + -- Adapt strings for Pen_Labels to C. We have to pass an array of + -- pointers (here, System.Address) to C-style strings, except that the + -- C-style strings usually have the null terminator somewhere in the + -- middle of the allocated string memory, after the "significant text." + -- I'm not sure if C allocates the memory or not--probably not, since + -- this is not a callback as in plslabelfunc where string memory was + -- allocated by the intervening C code. Here, the C string length is set + -- by Max_Stripchart_Label_Length, ranging 0 .. Max_Stripchart_Label_Length. + for I in Pen_Labels'range loop + + -- Check length and adjust if necessary. + if Length(Pen_Labels(I)) >= Max_Stripchart_Label_Length then + Put_Line("*** Warning: Stripchart label was truncated to" + & Integer'Image(Max_Stripchart_Label_Length) & " characters. ***"); + Pen_Labels(I) := Head(Pen_Labels(I), Max_Stripchart_Label_Length); + end if; + + -- Make the C-style string with null character immediately after the text. + C_Stripchart_String_Array(I) := To_C(To_String(Pen_Labels(I) + & Character'val(0) + & (Max_Stripchart_Label_Length - Length(Pen_Labels(I))) * " "), False); + + -- Set the I-th pointer in the array of pointers. + PL_Pen_Labels(I) := C_Stripchart_String_Array(I)'Address; end loop; plstripc(ID, To_C(X_Options), To_C(Y_Options), Modified: trunk/bindings/ada/plplot.ads.cmake =================================================================== --- trunk/bindings/ada/plplot.ads.cmake 2009-09-23 23:34:11 UTC (rev 10467) +++ trunk/bindings/ada/plplot.ads.cmake 2009-09-24 01:01:06 UTC (rev 10468) @@ -88,7 +88,7 @@ -- in Custom_Label_Procedure_Pointer_Type, and plslabelfunc (Set_Custom_Label). -- This length, 0 .. 40, is hardwired in the PLplot C code; this type will -- fail if that length is ever changed. - Label_String_Length : Integer renames PLplot_Thin.Label_String_Length; + Max_Label_String_Length : Integer renames PLplot_Thin.Max_Label_String_Length; subtype Label_String_Type is PLplot_Thin.Label_String_Type; -- "Rename" callback for custom label functions. @@ -142,10 +142,12 @@ type Line_Style_Array_Type is array (1..Max_Lines_For_Multiplot) of Line_Style_Type; Default_Line_Style_Array : Line_Style_Array_Type := (1, 1, 1, 1, 1); - -- Arrays of label strings + -- Things for label strings Default_Label_String : constant Unbounded_String := To_Unbounded_String(""); type Label_String_Array_Type is array (1..Max_Lines_For_Multiplot) of Unbounded_String; Default_Label_String_Array : Label_String_Array_Type := (others => Default_Label_String); + + -- Things for stripcharts Maximum_Number_Of_Stripcharts : Integer := 4; -- Limited by PLplot designers. type Stripchart_Label_String_Array_Type is array (1 .. Maximum_Number_Of_Stripcharts) of Unbounded_String; @@ -1737,7 +1739,7 @@ Box_Color, Legend_Color : Plot_Color_Type; Pen_Colors : Integer_Array_1D; Line_Styles : Integer_Array_1D; - Pen_Labels : Stripchart_Label_String_Array_Type; + Pen_Labels : in out Stripchart_Label_String_Array_Type; X_Label, Y_Label, Title_Label : String := To_String(Default_Label_String)); Modified: trunk/bindings/ada/plplot_thin.ads.cmake =================================================================== --- trunk/bindings/ada/plplot_thin.ads.cmake 2009-09-23 23:34:11 UTC (rev 10467) +++ trunk/bindings/ada/plplot_thin.ads.cmake 2009-09-24 01:01:06 UTC (rev 10468) @@ -89,23 +89,30 @@ -- C code of plstripc which creates a strip chart. The C code will accept -- any length of string and each of the four strings can be different lengths. -- These types are a bit of a hack whereby each of the legend strings is the - -- same length, so that they can be accessed as an array. They are 41 + -- same length, so that they can be accessed as an array. They are Max_Stripchart_Label_Length -- characters long here including a null terminator; I suppose any length -- will work since the stripchart program doesn't seem to mind. The user -- will probably never see this type since he is allowed to set up the -- legend strings as an array of unbounded strings. Only in preparing to -- call the underlying C are the fixed-length strings used. If the user -- specifies unbounded legend strings that are longer than allowed here, - -- they are truncated or padded with spaces, as appropriate, to meet the - -- required length. Although that length is now 41, it is OK to make it + -- they are truncated to meet the required length. Although that length + -- is now Max_Stripchart_Label_Length, it should be OK to make it -- longer or shorter simply by changing the following line, since everything - -- else (padding, truncation, conversion to C-style) works automatically. - -- See the fix this note in plplot.adb and plplot_traditional.adb about how - -- all this somehow doesn't work correctly, causing all four stripchart - -- legends to be the same and equal to the fourth one. - subtype PL_Stripchart_String is String(1 .. 41); - type PL_Stripchart_String_Array is array (1 .. 4) of access PL_Stripchart_String; - Temp_C_Stripchart_String : aliased PL_Stripchart_String; + -- else (truncation, conversion to C-style) works automatically. + -- The current implementation uses char_array for the strings and uses + -- System.Address as the pointer which are assigned to an array of type + -- PL_Stripchart_String_Array (see plstripc). I think it is also possible + -- to subtype String and use access variables assigned to an array of + -- suitable access types. + -- One very important note is that in forming the C string, the null + -- terminator will normally come in the middle of the array, right after + -- the "significant" part of the string. This is handled in plstripc. + Max_Stripchart_Label_Length : constant Integer := 100; + subtype PL_Stripchart_String is + Interfaces.C.char_array(0 .. size_t(Max_Stripchart_Label_Length)); + type PL_Stripchart_String_Array is array (1 .. 4) of PLpointer; + C_Stripchart_String_Array : array(1 .. 4) of PL_Stripchart_String; -- Access-to-procedure type for Draw_Vector_Plot and its kin. @@ -162,8 +169,8 @@ -- in Custom_Label_Procedure_Pointer_Type, and plslabelfunc (Set_Custom_Label). -- This length, 0 .. 40, is hardwired in the PLplot C code; this type will -- fail if that length is ever changed. - Label_String_Length : constant Integer := 40; - subtype Label_String_Type is Interfaces.C.char_array (0 .. size_t(Label_String_Length)); + Max_Label_String_Length : constant Integer := 40; + subtype Label_String_Type is Interfaces.C.char_array (0 .. size_t(Max_Label_String_Length)); -- Access-to-function type for making custom labels such as with plslabelfunc. type Custom_Label_Procedure_Pointer_Type is access Modified: trunk/bindings/ada/plplot_traditional.adb.cmake =================================================================== --- trunk/bindings/ada/plplot_traditional.adb.cmake 2009-09-23 23:34:11 UTC (rev 10467) +++ trunk/bindings/ada/plplot_traditional.adb.cmake 2009-09-24 01:01:06 UTC (rev 10468) @@ -2719,7 +2719,7 @@ Box_Color, Legend_Color : Plot_Color_Type; Pen_Colors : Integer_Array_1D; Line_Styles : Integer_Array_1D; - Pen_Labels : Stripchart_Label_String_Array_Type; + Pen_Labels : in out Stripchart_Label_String_Array_Type; X_Label, Y_Label, Title_Label : String := To_String(Default_Label_String)) is PL_Autoscale_Y, PL_Accumulate : PLBOOL; @@ -2727,7 +2727,7 @@ x_LP : Long_Float renames x_Legend_Position; y_LP : Long_Float renames y_Legend_Position; - + begin if Autoscale_Y then PL_Autoscale_Y := PLtrue; @@ -2741,17 +2741,30 @@ PL_Accumulate := PLfalse; end if; - -- Adapt strings for Pen_Labels to C. Head truncates or pads as - -- necessary to the length of Stripchart_String - 1; we then append - -- a null terminator which I suppose C needs. This could have also been - -- accomplished using char_array from the C interfaces. - -- fix this NOTE that the current implementation displays all four - -- legend labels the same, and equal to the fourth legend label. The - -- goal is to match const char *legline[4] in plstripc.c and e.g. - -- in x17c.c. - for Index in Pen_Labels'range loop - Temp_C_Stripchart_String := To_String(Head(Pen_Labels(Index), PL_Stripchart_String'length - 1) & Character'val(0)); - PL_Pen_Labels(Index) := Temp_C_Stripchart_String'Access; + -- Adapt strings for Pen_Labels to C. We have to pass an array of + -- pointers (here, System.Address) to C-style strings, except that the + -- C-style strings usually have the null terminator somewhere in the + -- middle of the allocated string memory, after the "significant text." + -- I'm not sure if C allocates the memory or not--probably not, since + -- this is not a callback as in plslabelfunc where string memory was + -- allocated by the intervening C code. Here, the C string length is set + -- by Max_Stripchart_Label_Length, ranging 0 .. Max_Stripchart_Label_Length. + for I in Pen_Labels'range loop + + -- Check length and adjust if necessary. + if Length(Pen_Labels(I)) >= Max_Stripchart_Label_Length then + Put_Line("*** Warning: Stripchart label was truncated to" + & Integer'Image(Max_Stripchart_Label_Length) & " characters. ***"); + Pen_Labels(I) := Head(Pen_Labels(I), Max_Stripchart_Label_Length); + end if; + + -- Make the C-style string with null character immediately after the text. + C_Stripchart_String_Array(I) := To_C(To_String(Pen_Labels(I) + & Character'val(0) + & (Max_Stripchart_Label_Length - Length(Pen_Labels(I))) * " "), False); + + -- Set the I-th pointer in the array of pointers. + PL_Pen_Labels(I) := C_Stripchart_String_Array(I)'Address; end loop; PLplot_Thin.plstripc(ID, To_C(X_Options), To_C(Y_Options), Modified: trunk/bindings/ada/plplot_traditional.ads.cmake =================================================================== --- trunk/bindings/ada/plplot_traditional.ads.cmake 2009-09-23 23:34:11 UTC (rev 10467) +++ trunk/bindings/ada/plplot_traditional.ads.cmake 2009-09-24 01:01:06 UTC (rev 10468) @@ -88,7 +88,7 @@ -- in Custom_Label_Procedure_Pointer_Type, and plslabelfunc (Set_Custom_Label). -- This length, 0 .. 40, is hardwired in the PLplot C code; this type will -- fail if that length is ever changed. - Label_String_Length : Integer renames PLplot_Thin.Label_String_Length; + Max_Label_String_Length : Integer renames PLplot_Thin.Max_Label_String_Length; subtype Label_String_Type is PLplot_Thin.Label_String_Type; -- "Rename" callback for custom label functions. @@ -141,10 +141,12 @@ type Line_Style_Array_Type is array (1..Max_Lines_For_Multiplot) of Line_Style_Type; Default_Line_Style_Array : Line_Style_Array_Type := (1, 1, 1, 1, 1); - -- Arrays of label strings + -- Things for label strings Default_Label_String : constant Unbounded_String := To_Unbounded_String(""); type Label_String_Array_Type is array (1..Max_Lines_For_Multiplot) of Unbounded_String; Default_Label_String_Array : Label_String_Array_Type := (others => Default_Label_String); + + -- Things for stripcharts Maximum_Number_Of_Stripcharts : Integer := 4; -- Limited by PLplot designers. type Stripchart_Label_String_Array_Type is array (1 .. Maximum_Number_Of_Stripcharts) of Unbounded_String; @@ -1596,7 +1598,7 @@ Box_Color, Legend_Color : Plot_Color_Type; Pen_Colors : Integer_Array_1D; Line_Styles : Integer_Array_1D; - Pen_Labels : Stripchart_Label_String_Array_Type; + Pen_Labels : in out Stripchart_Label_String_Array_Type; X_Label, Y_Label, Title_Label : String := To_String(Default_Label_String)); Modified: trunk/examples/ada/x19a.adb.cmake =================================================================== --- trunk/examples/ada/x19a.adb.cmake 2009-09-23 23:34:11 UTC (rev 10467) +++ trunk/examples/ada/x19a.adb.cmake 2009-09-24 01:01:06 UTC (rev 10468) @@ -125,14 +125,14 @@ -- Function to convert an unbounded string to a fixed-length C string with the -- null terminator somewhere in the middle and spaces after. The result, of type -- Label_String_Type, is fixed to a length by C, currently at 41, and is - -- indexed in Ada as 0 .. PLplot_Traditional.Label_String_Length. + -- indexed in Ada as 0 .. PLplot_Traditional.Max_Label_String_Length. function Unbounded_To_Weird_C (Item : Unbounded_String; C_Length : size_t) return Label_String_Type is Temp : Unbounded_String; begin - -- Check length and adjust if necessary. Put_Line doesn't work here. + -- Check length and adjust if necessary. if Length(Item) >= Integer(C_Length) then Put_Line("*** Warning: Custom label was truncated to" & Integer'Image(Integer(C_Length)) & " characters. ***"); @@ -140,7 +140,7 @@ return To_C(To_String(Temp), True); else return To_C(To_String(Item & ASCII.nul & - (Label_String_Length - Length(Item)) * " "), False); + (Max_Label_String_Length - Length(Item)) * " "), False); end if; end Unbounded_To_Weird_C; Modified: trunk/examples/ada/xthick19a.adb.cmake =================================================================== --- trunk/examples/ada/xthick19a.adb.cmake 2009-09-23 23:34:11 UTC (rev 10467) +++ trunk/examples/ada/xthick19a.adb.cmake 2009-09-24 01:01:06 UTC (rev 10468) @@ -125,14 +125,14 @@ -- Function to convert an unbounded string to a fixed-length C string with the -- null terminator somewhere in the middle and spaces after. The result, of type -- Label_String_Type, is fixed to a length by C, currently at 41, and is - -- indexed in Ada as 0 .. PLplot_Traditional.Label_String_Length. + -- indexed in Ada as 0 .. PLplot_Traditional.Max_Label_String_Length. function Unbounded_To_Weird_C (Item : Unbounded_String; C_Length : size_t) return Label_String_Type is Temp : Unbounded_String; begin - -- Check length and adjust if necessary. Put_Line doesn't work here. + -- Check length and adjust if necessary. if Length(Item) >= Integer(C_Length) then Put_Line("*** Warning: Custom label was truncated to" & Integer'Image(Integer(C_Length)) & " characters. ***"); @@ -140,7 +140,7 @@ return To_C(To_String(Temp), True); else return To_C(To_String(Item & ASCII.nul & - (Label_String_Length - Length(Item)) * " "), False); + (Max_Label_String_Length - Length(Item)) * " "), False); end if; end Unbounded_To_Weird_C; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |