2007-11-10 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / tools / mconfig / Mono.MonoConfig / Helpers.cs
index d68d3d78c61dbaaaece55c906d8efe02cd58b335..7755511a7871288cbdc6b1e2f87530be09d11e41 100644 (file)
@@ -38,16 +38,44 @@ namespace Mono.MonoConfig
        {
                public const string FakeRootName = "F_a_K_e_R_o_O_t_M_o_N_o_C_o_N_f_I_g_N_o_D_e";
                
-               public static FeatureTarget ConvertTarget (string value)
+               public static EnumType ConvertEnum <EnumType> (string value, string attrName)
                {
                        try {
-                               EnumConverter cvt = new EnumConverter (typeof (FeatureTarget));
-                               return (FeatureTarget) cvt.ConvertFromInvariantString (value);
+                               EnumConverter cvt = new EnumConverter (typeof (EnumType));
+                               return (EnumType) cvt.ConvertFromInvariantString (value);
                        } catch (Exception) {
                                throw new ApplicationException (
-                                       String.Format ("Failed to parse the 'target' attribute '{0}'", value));
+                                       String.Format ("Failed to parse the '{0}' attribute '{1}'", attrName, value));
                        }
                }
+
+               public static string BreakLongLine (string line, string indent, int maxLineWidth)
+               {
+                       StringBuilder sb = new StringBuilder ();
+
+                       int lineLen = line.Length;
+                       int segments = lineLen / maxLineWidth;
+                       int segmentStart = 0;
+                       int segmentLen = maxLineWidth - 1;
+                       int idx;
+
+                       while (segments-- >= 0) {
+                               idx = line.LastIndexOf (' ', segmentStart + segmentLen);
+                               if (idx > 0)
+                                       segmentLen = idx - segmentStart;
+                               else
+                                       idx = segmentLen - 1;
+
+                               sb.AppendFormat ("{0}{1}\n", indent, line.Substring (segmentStart, segmentLen));
+                               segmentStart = idx + 1;
+                               if (lineLen - segmentStart > maxLineWidth)
+                                       segmentLen = maxLineWidth;
+                               else
+                                       segmentLen = lineLen - segmentStart - 1;
+                       }
+
+                       return sb.ToString ();
+               }
                
                public static string GetRequiredNonEmptyAttribute (XPathNavigator node, string name)
                {