[Gtk-sharp-list] [PATCH] implicitly pass len param for strings

fd fd0h1440@yahoo.co.uk
22 Feb 2003 11:32:16 +0000


--=-3vo4dx8QbZm60b0G1+eS
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

There are about two dozen methods in Gtk# that currently require a
length parameter for string parameters, which is really quite redundant
in managed code.

This patch to the code generator identifies method and callback
signatures where a string is immediately followed by an int that appears
to define the length of the string, and implicitly passes the string's
length to the unmanaged function.

The patch also eliminates several .custom file entries for cases that
are now handled automatically.

Based on a precursory diff of the generated sources, I'd say the
algorithm is fairly resilient and avoids any false-positives.

After applying the patch, you'll need to remove pango/GlyphString.custom
manually.

Let the API beautification begin!


--=-3vo4dx8QbZm60b0G1+eS
Content-Disposition: attachment; filename=strlen.patch
Content-Type: text/x-patch; name=strlen.patch; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Index: ChangeLog
===================================================================
RCS file: /cvs/public/gtk-sharp/ChangeLog,v
retrieving revision 1.327
diff -u -u -r1.327 ChangeLog
--- ChangeLog	22 Feb 2003 04:34:54 -0000	1.327
+++ ChangeLog	22 Feb 2003 10:02:02 -0000
@@ -1,3 +1,13 @@
+2003-02-22  Alp Toker  <alp@atoker.com>
+
+	* generator/CallbackGen.cs:
+	* generator/SymbolTable.cs:
+	* generator/Parameters.cs: implicitly pass len param for strings
+	* gtk/Entry.custom: don't pass a length int
+	* pango/Layout.custom:
+	* gtk/Clipboard.custom: custom overloads no longer needed
+	* pango/GlyphString.custom: removed
+
 2003-02-21  Mike Kestner  <mkestner@speakeasy.net>
 
 	* mapdllnames.pl : a little whitespace action
Index: generator/CallbackGen.cs
===================================================================
RCS file: /cvs/public/gtk-sharp/generator/CallbackGen.cs,v
retrieving revision 1.17
diff -u -u -r1.17 CallbackGen.cs
--- generator/CallbackGen.cs	5 Jan 2003 23:51:37 -0000	1.17
+++ generator/CallbackGen.cs	22 Feb 2003 10:02:05 -0000
@@ -103,6 +103,9 @@
 			{
 				string parm_name = parms[i].Name;
 				string ctype = parms[i].CType;
+
+				if (i != 0 && SymbolTable.IsStringLengthParameter (parms[i-1].CSType, parms[i-1].Name, parms[i].CSType, parms[i].Name)) continue;
+
 				if ((i == count - 1) && ctype == "gpointer" && (parm_name.EndsWith ("data") || parm_name.EndsWith ("data_or_owner"))) 
 					continue;
 				string cstype = parms[i].CSType;
Index: generator/Parameters.cs
===================================================================
RCS file: /cvs/public/gtk-sharp/generator/Parameters.cs,v
retrieving revision 1.25
diff -u -u -r1.25 Parameters.cs
--- generator/Parameters.cs	20 Feb 2003 06:17:23 -0000	1.25
+++ generator/Parameters.cs	22 Feb 2003 10:02:06 -0000
@@ -169,6 +169,7 @@
 		{
 			signature_types = signature = import_sig = call_string = "";
 			bool need_sep = false;
+			bool need_sig_sep = false;
 			bool has_callback = hide_data;
 			bool last_was_user_data = false;
 			bool has_user_data = false;
@@ -188,6 +189,8 @@
 			}
 
 			int i = 0;
+			string prev_name = "";
+			string prev_cs_type = "";
 			foreach (XmlNode parm in elem.ChildNodes) {
 				if (parm.Name != "parameter") {
 					continue;
@@ -240,11 +243,14 @@
 					import_sig += ", ";
 					if (!(type == "GError**" || last_was_user_data) && !(IsVarArgs && i == (len - 1) && VAType == "length_param"))
 					{
-						signature += ", ";
+						if (need_sig_sep) signature += ", ";
+						else need_sig_sep = true;
+
 						signature_types += ":";
 					}
 				} else {
 					need_sep = true;
+					need_sig_sep = true;
 				}
 
 				if (p_elem.HasAttribute("pass_as")) {
@@ -265,11 +271,16 @@
 					call_string += "out ";				
 					import_sig += "out ";				
 				}
-				
+
 				if (IsVarArgs && i == (len - 2) && VAType == "length_param")
 				{
 					call_string += MangleName(last_param.GetAttribute("name")) + ".Length";
 					last_was_user_data = false; 
+				} else if (SymbolTable.IsStringLengthParameter (prev_cs_type, prev_name, cs_type, name))
+				{
+					call_string += MangleName(prev_name) + ".Length";
+					last_was_user_data = false;
+					need_sig_sep = false;
 				}
 				else
 				{
@@ -288,6 +299,9 @@
 					
 					call_string += call_parm;
 				}
+				
+				prev_name = name;
+				prev_cs_type = cs_type;
 				import_sig += (m_type + " " + name);
 				i++;
 			}
Index: generator/SymbolTable.cs
===================================================================
RCS file: /cvs/public/gtk-sharp/generator/SymbolTable.cs,v
retrieving revision 1.42
diff -u -u -r1.42 SymbolTable.cs
--- generator/SymbolTable.cs	25 Dec 2002 00:35:59 -0000	1.42
+++ generator/SymbolTable.cs	22 Feb 2003 10:02:07 -0000
@@ -8,6 +8,7 @@
 
 	using System;
 	using System.Collections;
+	using System.Text.RegularExpressions;
 
 	public class SymbolTable {
 		
@@ -361,6 +362,11 @@
 			c_type = Trim(c_type);
 			c_type = DeAlias(c_type);
 			return manually_wrapped_types.ContainsKey(c_type);
+		}
+
+		public static bool IsStringLengthParameter(string prev_cs_type, string prev_name, string cs_type, string name)
+		{
+			return (prev_name != "" && prev_cs_type == "string" && cs_type == "int" && Regex.Match (name, "(" + Regex.Escape (prev_name) + ")?_?(len|length)").Success);
 		}
 
 	}
Index: gtk/Clipboard.custom
===================================================================
RCS file: /cvs/public/gtk-sharp/gtk/Clipboard.custom,v
retrieving revision 1.3
diff -u -u -r1.3 Clipboard.custom
--- gtk/Clipboard.custom	22 Feb 2003 04:34:56 -0000	1.3
+++ gtk/Clipboard.custom	22 Feb 2003 10:02:08 -0000
@@ -50,10 +50,3 @@
 
 			return ret;
 		}
-
-	public void SetText (string new_text)
-        {
-		SetText (new_text, new_text.Length);
-	}
-
-    
Index: gtk/Entry.custom
===================================================================
RCS file: /cvs/public/gtk-sharp/gtk/Entry.custom,v
retrieving revision 1.1
diff -u -u -r1.1 Entry.custom
--- gtk/Entry.custom	10 Feb 2003 00:32:13 -0000	1.1
+++ gtk/Entry.custom	22 Feb 2003 10:02:08 -0000
@@ -8,7 +8,7 @@
 {
 	int position = 0;
 
-	InsertText (new_text, new_text.Length, ref position);
+	InsertText (new_text, ref position);
 
 	return position;
 }
Index: pango/Layout.custom
===================================================================
RCS file: /cvs/public/gtk-sharp/pango/Layout.custom,v
retrieving revision 1.1
diff -u -u -r1.1 Layout.custom
--- pango/Layout.custom	7 Jan 2003 04:03:48 -0000	1.1
+++ pango/Layout.custom	22 Feb 2003 10:02:08 -0000
@@ -15,13 +15,3 @@
     }
 }
 
-/// <summary> SetText Method, overloaded to not having to calculate string length </summary>
-public void SetText(string text) {
-    SetText(text, text.Length);
-}
-
-/// <summary> SetMarkup Method, overloaded to not having to calculate string length </summary>
-public void SetMarkup(string markup) {
-    SetText(markup, markup.Length);
-}
-

--=-3vo4dx8QbZm60b0G1+eS--