Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made it so TidyManaged will automatically switch between the 32bit and 64bit version of libtidy. #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 46 additions & 20 deletions Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace TidyManaged
/// </summary>
public class Document : IDisposable
{
internal static IPInvoke PInvoke = IntPtr.Size == 8 ? new PInvoke64() as IPInvoke : new PInvoke32() as IPInvoke;

#region Constructors

Document()
Expand Down Expand Up @@ -939,26 +941,50 @@ public bool WriteBack

#region Methods

/// <summary>
/// Parses input markup, and executes configured cleanup and repair operations.
/// </summary>
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;
}
/// <summary>
/// Parses input markup, and executes configured cleanup and repair operations.
/// </summary>
/// <returns>A log of the errors encountered during the CleanAndRepair operation.</returns>
public string CleanAndRepair()
{
using (Stream stream = new MemoryStream())
{
CleanAndRepair(stream);
stream.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}

/// <summary>
/// Parses input markup, and executes configured cleanup and repair operations.
/// </summary>
/// <param name="logStream">A stream to which errors encountered during the CleanAndRepair operation will be written to.</param>
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;
}

/// <summary>
/// Saves the processed markup to a string.
Expand Down
25 changes: 25 additions & 0 deletions Interop/IPInvoke.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
84 changes: 0 additions & 84 deletions Interop/PInvoke.cs

This file was deleted.

181 changes: 181 additions & 0 deletions Interop/PInvoke32.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading