All posts by roman

HtmlDecode inside SQLCLR without System.Web reference

I grabbed code from the WebUtility class which is available in System.Net of the .NET Framework 4.0. Can be compiled inside .NET 2.0.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace System.Net
{
public static class WebUtility
{
private static class HtmlEntities
{
private static string[] _entitiesList = new string[]
{
""-quot",
"&-amp",
"'-apos",
"<-lt>-gt",
"u00a0-nbsp",
"¡-iexcl",
"¢-cent",
"£-pound",
"¤-curren",
"¥-yen",
"¦-brvbar",
"§-sect",
"¨-uml",
"©-copy",
"ª-ordf",
"«-laquo",
"¬-not",
"­-shy",
"®-reg",
"¯-macr",
"°-deg",
"±-plusmn",
"²-sup2",
"³-sup3",
"´-acute",
"µ-micro",
"¶-para",
"·-middot",
"¸-cedil",
"¹-sup1",
"º-ordm",
"»-raquo",
"¼-frac14",
"½-frac12",
"¾-frac34",
"¿-iquest",
"À-Agrave",
"Á-Aacute",
"Â-Acirc",
"Ã-Atilde",
"Ä-Auml",
"Å-Aring",
"Æ-AElig",
"Ç-Ccedil",
"È-Egrave",
"É-Eacute",
"Ê-Ecirc",
"Ë-Euml",
"Ì-Igrave",
"Í-Iacute",
"Î-Icirc",
"Ï-Iuml",
"Ð-ETH",
"Ñ-Ntilde",
"Ò-Ograve",
"Ó-Oacute",
"Ô-Ocirc",
"Õ-Otilde",
"Ö-Ouml",
"×-times",
"Ø-Oslash",
"Ù-Ugrave",
"Ú-Uacute",
"Û-Ucirc",
"Ü-Uuml",
"Ý-Yacute",
"Þ-THORN",
"ß-szlig",
"à-agrave",
"á-aacute",
"â-acirc",
"ã-atilde",
"ä-auml",
"å-aring",
"æ-aelig",
"ç-ccedil",
"è-egrave",
"é-eacute",
"ê-ecirc",
"ë-euml",
"ì-igrave",
"í-iacute",
"î-icirc",
"ï-iuml",
"ð-eth",
"ñ-ntilde",
"ò-ograve",
"ó-oacute",
"ô-ocirc",
"õ-otilde",
"ö-ouml",
"÷-divide",
"ø-oslash",
"ù-ugrave",
"ú-uacute",
"û-ucirc",
"ü-uuml",
"ý-yacute",
"þ-thorn",
"ÿ-yuml",
"Œ-OElig",
"œ-oelig",
"Š-Scaron",
"š-scaron",
"Ÿ-Yuml",
"ƒ-fnof",
"ˆ-circ",
"˜-tilde",
"Α-Alpha",
"Β-Beta",
"Γ-Gamma",
"Δ-Delta",
"Ε-Epsilon",
"Ζ-Zeta",
"Η-Eta",
"Θ-Theta",
"Ι-Iota",
"Κ-Kappa",
"Λ-Lambda",
"Μ-Mu",
"Ν-Nu",
"Ξ-Xi",
"Ο-Omicron",
"Π-Pi",
"Ρ-Rho",
"Σ-Sigma",
"Τ-Tau",
"Υ-Upsilon",
"Φ-Phi",
"Χ-Chi",
"Ψ-Psi",
"Ω-Omega",
"α-alpha",
"β-beta",
"γ-gamma",
"δ-delta",
"ε-epsilon",
"ζ-zeta",
"η-eta",
"θ-theta",
"ι-iota",
"κ-kappa",
"λ-lambda",
"μ-mu",
"ν-nu",
"ξ-xi",
"ο-omicron",
"π-pi",
"ρ-rho",
"ς-sigmaf",
"σ-sigma",
"τ-tau",
"υ-upsilon",
"φ-phi",
"χ-chi",
"ψ-psi",
"ω-omega",
"ϑ-thetasym",
"ϒ-upsih",
"ϖ-piv",
"u2002-ensp",
"u2003-emsp",
"u2009-thinsp",
"‌-zwnj",
"‍-zwj",
"‎-lrm",
"‏-rlm",
"–-ndash",
"—-mdash",
"‘-lsquo",
"’-rsquo",
"‚-sbquo",
"“-ldquo",
"”-rdquo",
"„-bdquo",
"†-dagger",
"‡-Dagger",
"•-bull",
"…-hellip",
"‰-permil",
"′-prime",
"″-Prime",
"‹-lsaquo",
"›-rsaquo",
"‾-oline",
"⁄-frasl",
"€-euro",
"ℑ-image",
"℘-weierp",
"ℜ-real",
"™-trade",
"ℵ-alefsym",
"←-larr",
"↑-uarr",
"→-rarr",
"↓-darr",
"↔-harr",
"↵-crarr",
"⇐-lArr",
"⇑-uArr",
"⇒-rArr",
"⇓-dArr",
"⇔-hArr",
"∀-forall",
"∂-part",
"∃-exist",
"∅-empty",
"∇-nabla",
"∈-isin",
"∉-notin",
"∋-ni",
"∏-prod",
"∑-sum",
"−-minus",
"∗-lowast",
"√-radic",
"∝-prop",
"∞-infin",
"∠-ang",
"∧-and",
"∨-or",
"∩-cap",
"∪-cup",
"∫-int",
"∴-there4",
"∼-sim",
"≅-cong",
"≈-asymp",
"≠-ne",
"≡-equiv",
"≤-le",
"≥-ge",
"⊂-sub",
"⊃-sup",
"⊄-nsub",
"⊆-sube",
"⊇-supe",
"⊕-oplus",
"⊗-otimes",
"⊥-perp",
"⋅-sdot",
"⌈-lceil",
"⌉-rceil",
"⌊-lfloor",
"⌋-rfloor",
"〈-lang",
"〉-rang",
"◊-loz",
"♠-spades",
"♣-clubs",
"♥-hearts",
"♦-diams"
};
private static Dictionary _lookupTable = WebUtility.HtmlEntities.GenerateLookupTable();
private static Dictionary GenerateLookupTable()
{
Dictionary dictionary = new Dictionary(StringComparer.Ordinal);
string[] entitiesList = WebUtility.HtmlEntities._entitiesList;
for (int i = 0; i < entitiesList.Length; i++)
{
string text = entitiesList[i];
dictionary.Add(text.Substring(2), text[0]);
}
return dictionary;
}
public static char Lookup(string entity)
{
char result;
WebUtility.HtmlEntities._lookupTable.TryGetValue(entity, out result);
return result;
}
}
private static char[] _htmlEntityEndingChars = new char[]
{
';',
'&'
};
public static string HtmlDecode(string value)
{
if (string.IsNullOrEmpty(value))
{
return value;
}
if (value.IndexOf('&') < 0)
{
return value;
}
StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
WebUtility.HtmlDecode(value, stringWriter);
return stringWriter.ToString();
}
public static void HtmlDecode(string value, TextWriter output)
{
if (value == null)
{
return;
}
if (output == null)
{
throw new ArgumentNullException("output");
}
if (value.IndexOf('&') < 0)
{
output.Write(value);
return;
}
int length = value.Length;
int i = 0;
while (i < length)
{
char c = value[i];
if (c != '&')
{
goto IL_110;
}
int num = value.IndexOfAny(WebUtility._htmlEntityEndingChars, i + 1);
if (num <= 0 || value[num] != ';')
{
goto IL_110;
}
string text = value.Substring(i + 1, num - i - 1);
if (text.Length > 1 && text[0] == '#')
{
ushort num2;
if (text[1] == 'x' || text[1] == 'X')
{
ushort.TryParse(text.Substring(2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out num2);
}
else
{
ushort.TryParse(text.Substring(1), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out num2);
}
if (num2 != 0)
{
c = (char)num2;
i = num;
goto IL_110;
}
goto IL_110;
}
else
{
i = num;
char c2 = WebUtility.HtmlEntities.Lookup(text);
if (c2 != '')
{
c = c2;
goto IL_110;
}
output.Write('&');
output.Write(text);
output.Write(';');
}
IL_117:
i++;
continue;
IL_110:
output.Write(c);
goto IL_117;
}
}
}
}

OpenOffice service init.d script for CentOS

This script will start soffice headless listening on port 8100. You can use it eg. with JODConverter.

#!/bin/bash
# openoffice.org headless server script
#
# chkconfig: 2345 80 30
# description: headless openoffice server script
# processname: openoffice
#
OOo_HOME=/usr/bin
SOFFICE_PATH=$OOo_HOME/soffice
PIDFILE=/var/run/openoffice-server.pid

set -e

case "$1" in
start)
if [ -f $PIDFILE ]; then
echo "OpenOffice headless server has already started."
sleep 5
exit
fi
echo "Starting OpenOffice headless server"
$SOFFICE_PATH -nologo -nodefault -norestore -nocrashreport -nolockcheck -headless -nofirststartwizard -accept="socket,host=localhost,port=8100;urp" & > /dev/null 2>&1
touch $PIDFILE
;;
stop)
if [ -f $PIDFILE ]; then
echo "Stopping OpenOffice headless server."
killall -9 soffice && killall -9 soffice.bin
rm -f $PIDFILE
exit
fi
echo "Openoffice headless server is not running."
exit
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0

Hosting ClickOnce application on Amazon S3

When hosting ClickOnce applications on Amazon S3 you might notice download problems on some versions of Internet Explorer. Although other browsers does not have problems downloading ClickOnce .application file, Internet Explorer complains with “The requested site is either unavailable or cannot be found.”.

The problem is most probably in the Content-Type of the .application file. It is set to application/binary, but should be application/x-ms-application. To fix it, you have to connect to S3 Management Console and fix the Content-Type under file Properties.

IE9 bookmarks are not searched in address bar

Address bar in IE9 displays history and favorites results while you are typing by default. IE9 uses Windows Search to improve the results and also to shorten time necessary to get them. So the reason why favorites might not be displayed is because Favorites folder is not indexed. There are two solutions to fix this situation:

Disable use of Windows Search for these results. Go to Tools > Internet Options > Content tab > AutoComplete Settings button and unclick Use Windows Search for better results.

The second and better solution is to add Favorites to the Windows Search index locations. Go to Start and type Indexing Options, click on the Modify button and select Favorites folder under your Windows profile folder.

Diablo on Widows Vista / 7

If you would like to play this old but legendary game on new versions of Windows you may face some problems.

  1. The first problem is that on the start or during play, colors may get corrupted like on the picture above. But the solution is quite easy, just open the Screen Resolution dialog using context menu on Desktop and leave the dialog opened before you start the game 🙂
  2. IPX is not available since Windows Vista, so multiplayer on LAN does not work. But there is a IPXWrapper library which will wrap communication over UDP protocol. Just download it and copy all dll files from the package to the Diablo installation folder.
  3. Do not forget to use the latest patch available.

Check whether text is UPPER case in T-SQL

I have prepared small snippet which will help you check whether some text is UPPER case in T-SQL. I have not found any good example while Googling. You can easily update to code to check whether the text is lower case by replacing UPPER with LOWER in the snippet.

DECLARE @Name AS NVARCHAR(64)
SET @Name = 'NAME'

IF BINARY_CHECKSUM(@Name) = BINARY_CHECKSUM(UPPER(@Name))
SELECT 1;
ELSE
SELECT 0;