internal static void serialize(object settings, Type settingsType, string filename, SettingsSerializer serializer) { var tempname = filename + ".~tmp"; Ut.WaitSharingVio(() => { switch (serializer) { case SettingsSerializer.ClassifyXml: // SerializeToFile automatically creates the folder if necessary ClassifyXml.SerializeToFile(settingsType, settings, tempname, format: ClassifyXmlFormat.Create("Settings")); break; case SettingsSerializer.ClassifyJson: // SerializeToFile automatically creates the folder if necessary ClassifyJson.SerializeToFile(settingsType, settings, tempname); break; case SettingsSerializer.ClassifyBinary: // SerializeToFile automatically creates the folder if necessary ClassifyBinary.SerializeToFile(settingsType, settings, tempname); break; case SettingsSerializer.DotNetBinary: PathUtil.CreatePathToFile(tempname); var bf = new BinaryFormatter(); using (var fs = File.Open(tempname, FileMode.Create, FileAccess.Write, FileShare.Read)) bf.Serialize(fs, settings); break; default: throw new InternalErrorException("4968453"); } File.Delete(filename); File.Move(tempname, filename); }, TimeSpan.FromSeconds(5)); }
private static void wrap(StringBuilder text, ref int lineLength, int maxLineLength) { Ut.Assert(text.Length > 0); // This method MAY be called with a lineLength less than the max line length. // It MUST add a line wrap, ideally before the max line length, but if not possible then after the max length, possibly even at the very end of the text. // If the line already fits within max line length, just insert a newline at the very end and be done if (lineLength <= maxLineLength) { lineLength = 1; text.Append("\r\n "); return; } // First search back from the max line length position for a wrappable location int pos = text.Length - lineLength + maxLineLength; // "pos < text.Length" is guaranteed by the test above while (true) { if (pos < (text.Length - lineLength) || text[pos] == '\n') // exit if we've reached the start of the current line { pos = -1; break; } if (text[pos] == ' ') { break; } pos--; } // If that worked, make sure it isn't all spaces until the start of the line if (pos >= 0) { int pos2 = pos; while (true) { pos2--; if (pos2 < (text.Length - lineLength) || text[pos2] == '\n') { pos = -1; // found nothing but spaces until the start of the line break; } if (text[pos2] != ' ') { break; // found a non-space, so OK to wrap at "pos" } } } // If that failed, seek forward if (pos < 0) { pos = text.Length - lineLength + maxLineLength + 1; while (true) { if (pos >= text.Length) { break; } if (text[pos] == ' ') { break; } pos++; } } // Insert \r\n at "pos", which either points at the space that becomes the (mandatory) indent for the next line, or is at the very end of the line if (pos >= text.Length) { lineLength = 1; text.Append("\r\n "); } else { lineLength = text.Length - pos; text.Insert(pos, "\r\n"); } }