diff --git a/Document.cs b/Document.cs index 85c5317..1d93015 100755 --- a/Document.cs +++ b/Document.cs @@ -37,6 +37,8 @@ namespace TidyManaged /// public class Document : IDisposable { + internal static IPInvoke PInvoke = IntPtr.Size == 8 ? new PInvoke64() as IPInvoke : new PInvoke32() as IPInvoke; + #region Constructors Document() @@ -939,26 +941,50 @@ public bool WriteBack #region Methods - /// - /// Parses input markup, and executes configured cleanup and repair operations. - /// - public void CleanAndRepair() - { - if (fromString) - { - EncodingType tempEnc = this.InputCharacterEncoding; - this.InputCharacterEncoding = EncodingType.Utf8; - PInvoke.tidyParseString(this.handle, this.htmlString); - this.InputCharacterEncoding = tempEnc; - } - else - { - InputSource input = new InputSource(this.stream); - PInvoke.tidyParseSource(this.handle, ref input.TidyInputSource); - } - PInvoke.tidyCleanAndRepair(this.handle); - cleaned = true; - } + /// + /// Parses input markup, and executes configured cleanup and repair operations. + /// + /// A log of the errors encountered during the CleanAndRepair operation. + public string CleanAndRepair() + { + using (Stream stream = new MemoryStream()) + { + CleanAndRepair(stream); + stream.Seek(0, SeekOrigin.Begin); + using (StreamReader reader = new StreamReader(stream)) + { + return reader.ReadToEnd(); + } + } + } + + /// + /// Parses input markup, and executes configured cleanup and repair operations. + /// + /// A stream to which errors encountered during the CleanAndRepair operation will be written to. + public void CleanAndRepair(Stream logStream) + { + //Config Error + EncodingType tempOutEnc = this.OutputCharacterEncoding; + this.OutputCharacterEncoding = EncodingType.Utf8; + OutputSink sink = new OutputSink(logStream); + PInvoke.tidySetErrorSink(this.handle, ref sink.TidyOutputSink); + if (fromString) + { + EncodingType tempEnc = this.InputCharacterEncoding; + this.InputCharacterEncoding = EncodingType.Utf8; + PInvoke.tidyParseString(this.handle, this.htmlString); + this.InputCharacterEncoding = tempEnc; + } + else + { + InputSource input = new InputSource(this.stream); + PInvoke.tidyParseSource(this.handle, ref input.TidyInputSource); + } + PInvoke.tidyCleanAndRepair(this.handle); + this.OutputCharacterEncoding = tempOutEnc; + cleaned = true; + } /// /// Saves the processed markup to a string. diff --git a/Interop/IPInvoke.cs b/Interop/IPInvoke.cs new file mode 100644 index 0000000..35061a3 --- /dev/null +++ b/Interop/IPInvoke.cs @@ -0,0 +1,25 @@ +using System; +namespace TidyManaged.Interop +{ + internal interface IPInvoke + { + int tidyCleanAndRepair(IntPtr tdoc); + IntPtr tidyCreate(); + bool tidyOptGetBool(IntPtr tdoc, TidyOptionId optId); + uint tidyOptGetInt(IntPtr tdoc, TidyOptionId optId); + IntPtr tidyOptGetValue(IntPtr tdoc, TidyOptionId optId); + string tidyOptGetValueString(IntPtr tdoc, TidyOptionId optId); + bool tidyOptSetBool(IntPtr tdoc, TidyOptionId optId, bool val); + bool tidyOptSetInt(IntPtr tdoc, TidyOptionId optId, uint val); + bool tidyOptSetValue(IntPtr tdoc, TidyOptionId optId, string val); + int tidyParseFile(IntPtr tdoc, string filename); + int tidyParseSource(IntPtr tdoc, ref TidyInputSource source); + int tidyParseString(IntPtr tdoc, string content); + void tidyRelease(IntPtr tdoc); + IntPtr tidyReleaseDate(); + int tidySaveFile(IntPtr tdoc, string filname); + int tidySaveSink(IntPtr tdoc, ref TidyOutputSink sink); + int tidySaveString(IntPtr tdoc, IntPtr buffer, ref uint buflen); + int tidySetErrorSink(IntPtr tdoc, ref TidyOutputSink sink); + } +} diff --git a/Interop/PInvoke.cs b/Interop/PInvoke.cs deleted file mode 100644 index ffd98d0..0000000 --- a/Interop/PInvoke.cs +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (c) 2009 Mark Beaton -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. - -using System; -using System.Runtime.InteropServices; - -namespace TidyManaged.Interop -{ - internal class PInvoke - { - [DllImport("libtidy.dll")] - internal static extern IntPtr tidyCreate(); - - [DllImport("libtidy.dll")] - internal static extern void tidyRelease(IntPtr tdoc); - - [DllImport("libtidy.dll")] - internal static extern IntPtr tidyReleaseDate(); - - [DllImport("libtidy.dll")] - internal static extern IntPtr tidyOptGetValue(IntPtr tdoc, TidyOptionId optId); - - [DllImport("libtidy.dll")] - internal static extern bool tidyOptSetValue(IntPtr tdoc, TidyOptionId optId, string val); - - [DllImport("libtidy.dll")] - internal static extern uint tidyOptGetInt(IntPtr tdoc, TidyOptionId optId); - - [DllImport("libtidy.dll")] - internal static extern bool tidyOptSetInt(IntPtr tdoc, TidyOptionId optId, uint val); - - [DllImport("libtidy.dll")] - internal static extern bool tidyOptGetBool(IntPtr tdoc, TidyOptionId optId); - - [DllImport("libtidy.dll")] - internal static extern bool tidyOptSetBool(IntPtr tdoc, TidyOptionId optId, bool val); - - [DllImport("libtidy.dll")] - internal static extern int tidyParseFile(IntPtr tdoc, string filename); - - [DllImport("libtidy.dll")] - internal static extern int tidyParseString(IntPtr tdoc, string content); - - [DllImport("libtidy.dll")] - internal static extern int tidyParseSource(IntPtr tdoc, ref TidyInputSource source); - - [DllImport("libtidy.dll")] - internal static extern int tidyCleanAndRepair(IntPtr tdoc); - - [DllImport("libtidy.dll")] - internal static extern int tidySaveFile(IntPtr tdoc, string filname); - - [DllImport("libtidy.dll")] - internal static extern int tidySaveString(IntPtr tdoc, IntPtr buffer, ref uint buflen); - - [DllImport("libtidy.dll")] - internal static extern int tidySaveSink(IntPtr tdoc, ref TidyOutputSink sink); - - internal static string tidyOptGetValueString(IntPtr tdoc, TidyOptionId optId) - { - return Marshal.PtrToStringAnsi(tidyOptGetValue(tdoc, optId)); - } - } -} diff --git a/Interop/PInvoke32.cs b/Interop/PInvoke32.cs new file mode 100644 index 0000000..f5620ce --- /dev/null +++ b/Interop/PInvoke32.cs @@ -0,0 +1,181 @@ +// Copyright (c) 2009 Mark Beaton +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +using System; +using System.Runtime.InteropServices; + +namespace TidyManaged.Interop +{ + internal class PInvoke32 : IPInvoke + { + [DllImport("libtidy32.dll")] + internal static extern IntPtr tidyCreate(); + + [DllImport("libtidy32.dll")] + internal static extern void tidyRelease(IntPtr tdoc); + + [DllImport("libtidy32.dll")] + internal static extern IntPtr tidyReleaseDate(); + + [DllImport("libtidy32.dll")] + internal static extern IntPtr tidyOptGetValue(IntPtr tdoc, TidyOptionId optId); + + [DllImport("libtidy32.dll")] + internal static extern bool tidyOptSetValue(IntPtr tdoc, TidyOptionId optId, string val); + + [DllImport("libtidy32.dll")] + internal static extern uint tidyOptGetInt(IntPtr tdoc, TidyOptionId optId); + + [DllImport("libtidy32.dll")] + internal static extern bool tidyOptSetInt(IntPtr tdoc, TidyOptionId optId, uint val); + + [DllImport("libtidy32.dll")] + internal static extern bool tidyOptGetBool(IntPtr tdoc, TidyOptionId optId); + + [DllImport("libtidy32.dll")] + internal static extern bool tidyOptSetBool(IntPtr tdoc, TidyOptionId optId, bool val); + + [DllImport("libtidy32.dll")] + internal static extern int tidyParseFile(IntPtr tdoc, string filename); + + [DllImport("libtidy32.dll")] + internal static extern int tidyParseString(IntPtr tdoc, string content); + + [DllImport("libtidy32.dll")] + internal static extern int tidyParseSource(IntPtr tdoc, ref TidyInputSource source); + + [DllImport("libtidy32.dll")] + internal static extern int tidyCleanAndRepair(IntPtr tdoc); + + [DllImport("libtidy32.dll")] + internal static extern int tidySaveFile(IntPtr tdoc, string filname); + + [DllImport("libtidy32.dll")] + internal static extern int tidySaveString(IntPtr tdoc, IntPtr buffer, ref uint buflen); + + [DllImport("libtidy32.dll")] + internal static extern int tidySaveSink(IntPtr tdoc, ref TidyOutputSink sink); + + [DllImport("libtidy32.dll")] + internal static extern int tidySetErrorSink(IntPtr tdoc, ref TidyOutputSink sink); + + internal static string tidyOptGetValueString(IntPtr tdoc, TidyOptionId optId) + { + return Marshal.PtrToStringAnsi(tidyOptGetValue(tdoc, optId)); + } + + #region IPInvoke Members + + int IPInvoke.tidyCleanAndRepair(IntPtr tdoc) + { + return tidyCleanAndRepair(tdoc); + } + + IntPtr IPInvoke.tidyCreate() + { + return tidyCreate(); + } + + bool IPInvoke.tidyOptGetBool(IntPtr tdoc, TidyOptionId optId) + { + return tidyOptGetBool(tdoc, optId); + } + + uint IPInvoke.tidyOptGetInt(IntPtr tdoc, TidyOptionId optId) + { + return tidyOptGetInt(tdoc, optId); + } + + IntPtr IPInvoke.tidyOptGetValue(IntPtr tdoc, TidyOptionId optId) + { + return tidyOptGetValue(tdoc, optId); + } + + string IPInvoke.tidyOptGetValueString(IntPtr tdoc, TidyOptionId optId) + { + return tidyOptGetValueString(tdoc, optId); + } + + bool IPInvoke.tidyOptSetBool(IntPtr tdoc, TidyOptionId optId, bool val) + { + return tidyOptSetBool(tdoc, optId, val); + } + + bool IPInvoke.tidyOptSetInt(IntPtr tdoc, TidyOptionId optId, uint val) + { + return tidyOptSetInt(tdoc, optId, val); + } + + bool IPInvoke.tidyOptSetValue(IntPtr tdoc, TidyOptionId optId, string val) + { + return tidyOptSetValue(tdoc, optId, val); + } + + int IPInvoke.tidyParseFile(IntPtr tdoc, string filename) + { + return tidyParseFile(tdoc, filename); + } + + int IPInvoke.tidyParseSource(IntPtr tdoc, ref TidyInputSource source) + { + return tidyParseSource(tdoc, ref source); + } + + int IPInvoke.tidyParseString(IntPtr tdoc, string content) + { + return tidyParseString(tdoc, content); + } + + void IPInvoke.tidyRelease(IntPtr tdoc) + { + tidyRelease(tdoc); + } + + IntPtr IPInvoke.tidyReleaseDate() + { + return tidyReleaseDate(); + } + + int IPInvoke.tidySaveFile(IntPtr tdoc, string filname) + { + return tidySaveFile(tdoc, filname); + } + + int IPInvoke.tidySaveSink(IntPtr tdoc, ref TidyOutputSink sink) + { + return tidySaveSink(tdoc, ref sink); + } + + int IPInvoke.tidySaveString(IntPtr tdoc, IntPtr buffer, ref uint buflen) + { + return tidySaveString(tdoc, buffer, ref buflen); + } + + int IPInvoke.tidySetErrorSink(IntPtr tdoc, ref TidyOutputSink sink) + { + return tidySetErrorSink(tdoc, ref sink); + } + + #endregion + } +} diff --git a/Interop/PInvoke64.cs b/Interop/PInvoke64.cs new file mode 100644 index 0000000..900a461 --- /dev/null +++ b/Interop/PInvoke64.cs @@ -0,0 +1,181 @@ +// Copyright (c) 2009 Mark Beaton +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +using System; +using System.Runtime.InteropServices; + +namespace TidyManaged.Interop +{ + internal class PInvoke64 : IPInvoke + { + [DllImport("libtidy64.dll")] + internal static extern IntPtr tidyCreate(); + + [DllImport("libtidy64.dll")] + internal static extern void tidyRelease(IntPtr tdoc); + + [DllImport("libtidy64.dll")] + internal static extern IntPtr tidyReleaseDate(); + + [DllImport("libtidy64.dll")] + internal static extern IntPtr tidyOptGetValue(IntPtr tdoc, TidyOptionId optId); + + [DllImport("libtidy64.dll")] + internal static extern bool tidyOptSetValue(IntPtr tdoc, TidyOptionId optId, string val); + + [DllImport("libtidy64.dll")] + internal static extern uint tidyOptGetInt(IntPtr tdoc, TidyOptionId optId); + + [DllImport("libtidy64.dll")] + internal static extern bool tidyOptSetInt(IntPtr tdoc, TidyOptionId optId, uint val); + + [DllImport("libtidy64.dll")] + internal static extern bool tidyOptGetBool(IntPtr tdoc, TidyOptionId optId); + + [DllImport("libtidy64.dll")] + internal static extern bool tidyOptSetBool(IntPtr tdoc, TidyOptionId optId, bool val); + + [DllImport("libtidy64.dll")] + internal static extern int tidyParseFile(IntPtr tdoc, string filename); + + [DllImport("libtidy64.dll")] + internal static extern int tidyParseString(IntPtr tdoc, string content); + + [DllImport("libtidy64.dll")] + internal static extern int tidyParseSource(IntPtr tdoc, ref TidyInputSource source); + + [DllImport("libtidy64.dll")] + internal static extern int tidyCleanAndRepair(IntPtr tdoc); + + [DllImport("libtidy64.dll")] + internal static extern int tidySaveFile(IntPtr tdoc, string filname); + + [DllImport("libtidy64.dll")] + internal static extern int tidySaveString(IntPtr tdoc, IntPtr buffer, ref uint buflen); + + [DllImport("libtidy64.dll")] + internal static extern int tidySaveSink(IntPtr tdoc, ref TidyOutputSink sink); + + [DllImport("libtidy64.dll")] + internal static extern int tidySetErrorSink(IntPtr tdoc, ref TidyOutputSink sink); + + internal static string tidyOptGetValueString(IntPtr tdoc, TidyOptionId optId) + { + return Marshal.PtrToStringAnsi(tidyOptGetValue(tdoc, optId)); + } + + #region IPInvoke Members + + int IPInvoke.tidyCleanAndRepair(IntPtr tdoc) + { + return tidyCleanAndRepair(tdoc); + } + + IntPtr IPInvoke.tidyCreate() + { + return tidyCreate(); + } + + bool IPInvoke.tidyOptGetBool(IntPtr tdoc, TidyOptionId optId) + { + return tidyOptGetBool(tdoc, optId); + } + + uint IPInvoke.tidyOptGetInt(IntPtr tdoc, TidyOptionId optId) + { + return tidyOptGetInt(tdoc, optId); + } + + IntPtr IPInvoke.tidyOptGetValue(IntPtr tdoc, TidyOptionId optId) + { + return tidyOptGetValue(tdoc, optId); + } + + string IPInvoke.tidyOptGetValueString(IntPtr tdoc, TidyOptionId optId) + { + return tidyOptGetValueString(tdoc, optId); + } + + bool IPInvoke.tidyOptSetBool(IntPtr tdoc, TidyOptionId optId, bool val) + { + return tidyOptSetBool(tdoc, optId, val); + } + + bool IPInvoke.tidyOptSetInt(IntPtr tdoc, TidyOptionId optId, uint val) + { + return tidyOptSetInt(tdoc, optId, val); + } + + bool IPInvoke.tidyOptSetValue(IntPtr tdoc, TidyOptionId optId, string val) + { + return tidyOptSetValue(tdoc, optId, val); + } + + int IPInvoke.tidyParseFile(IntPtr tdoc, string filename) + { + return tidyParseFile(tdoc, filename); + } + + int IPInvoke.tidyParseSource(IntPtr tdoc, ref TidyInputSource source) + { + return tidyParseSource(tdoc, ref source); + } + + int IPInvoke.tidyParseString(IntPtr tdoc, string content) + { + return tidyParseString(tdoc, content); + } + + void IPInvoke.tidyRelease(IntPtr tdoc) + { + tidyRelease(tdoc); + } + + IntPtr IPInvoke.tidyReleaseDate() + { + return tidyReleaseDate(); + } + + int IPInvoke.tidySaveFile(IntPtr tdoc, string filname) + { + return tidySaveFile(tdoc, filname); + } + + int IPInvoke.tidySaveSink(IntPtr tdoc, ref TidyOutputSink sink) + { + return tidySaveSink(tdoc, ref sink); + } + + int IPInvoke.tidySaveString(IntPtr tdoc, IntPtr buffer, ref uint buflen) + { + return tidySaveString(tdoc, buffer, ref buflen); + } + + int IPInvoke.tidySetErrorSink(IntPtr tdoc, ref TidyOutputSink sink) + { + return tidySetErrorSink(tdoc, ref sink); + } + + #endregion + } +} diff --git a/TidyManaged.csproj b/TidyManaged.csproj index 8cf85a2..5005283 100644 --- a/TidyManaged.csproj +++ b/TidyManaged.csproj @@ -1,4 +1,4 @@ - + Debug @@ -45,7 +45,9 @@ - + + +