Friday, December 05, 2008

Syntax Highlighters (Code to Html)

Since I'm writing a blog that occasionally contains code, I figured I needed to have a nice way to display code. This post will go over the solutions I've used. If you want to search for solutions on your own, the best base term to use is "syntax highlighter." Of course, the term "syntax highlighter" applies to more than just formatting code for html... but the term "code to html" doesn't return very good results.

Code2Html

When I first started blogging, I used code2html at http://www.palfrader.org/code2html. It's a fully functional syntax highlighter... but the result really isn't that pretty. It's actually very bare bones. But it was pretty easy to use. You can do the "code to html"-ing on their website or you can download a stand alone application (it's a perl script) to do the conversion offline. The documentation is good and I had no problem using it. I could easily produce the following code example:
1 alert("Hello World!")
Ruby Rio and Syntax

I really didn't have a problem with using code2html until I started getting interested in ruby, which led me to find blogs with code examples like this (I'm using an image as the example for convenience in this case):
The entire process to format the ruby code this way is very well described on Wolfmans Howling's blog at http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs. But as cool as it is, it only works well with ruby code, and I eventually decided I needed something to highlight the other languages I use too.

Syntax Highlighter

That led me to Syntax Highlighter, which is the solution I currently use. It's an interesting take on syntax highlighting in that it does the conversions on the fly and is entirely written in javascript. It goes through the web page's dom tree at load time and replaces any code blocks contained by a "<pre name='code'>" tag and converts its content to syntax highlighted code. It supports many options, such as line numbering and columns. Take a look at http://code.google.com/p/syntaxhighlighter, the documentation is good and clear. Syntax Highlighter is very good looking and very simple to use.

However, I prefer static html code and don't find it inconvenient to have to convert and paste the code myself. I figured that if I do the highlighting once myself, it's less strain on the user's computer. I also wanted to add a title to the code block as well as change the appearance a little. Since all the javascript is freely available, I made a lot of my own modifications to resolve those issues. It was pretty fun to do and a good javascript exercise for me. It did take some work to finesse everything, but I think I got it all working correctly.

You can download my solution here. Because it's a javascript-only solution, you can run it offline. Just open up the index.html file in your browser, enter your code, and press the convert code link. Then you can copy the code from the result textbox to whatever page you want. Just remember to include a css file, shCoreStaticInclude.js, and the clipboard.swf file in whatever page you paste the code on. I wrote up detailed instructions here. If you want to give it a try online, I have it at http://syntaxhighlighter.relic19.net. That page has a much more detailed description of the process I just described. Here's a sample from my modified syntax highlighter:
  1. alert("Hello World!");  
(Edit: Syntax Highlighter has since updated to version 2.0. It can be now found at http://alexgorbatchev.com/wiki/SyntaxHighlighter. I have no intention of updating my Static Syntax Highlighter at this time. If substantial enhancements to appearance have been made, I'll consider it, but right the only graphical change I've noticed is the new icon usage for the toolbar.)

Quick Highlighter

While I was playing with Syntax Highlighter I came across another top notch syntax highlighter called Quick Highlighter at http://quickhighlighter.com. It converts code to html, much like code2html. You can allow the css to be embedded to avoid having to link to a separate css file. It supports way more languages and formatting options compared to Syntax Highlighter. It's almost as pretty as Syntax Highlighter too. The only downside is that it seems to work exclusively through a web interface, which is why I ultimately decided against using it. Syntax Highlighter is a lot more customizable for me since I can download and muck around with the source code, changing the way it operates, a feat I couldn't as easily do with Quick Highlighter. However, if I found quickhighlighter before I found any of the other solutions, then I probably would have never switched from it. Here's a sample from Quick Highlighter:

  1. alert("Hello World!");


Gist

I also came across Gist, at http://gist.github.com/, as a syntax highlighter. The nicest thing about Gist is that it does all the work for you. You type in code on their webpage and it gives you code to embed on your webpage. You don't need to add any extra includes or css files or even register yourself at their website... Gist takes care of everything for you. It's open source, so you can use your own server if you don't trust Gist. I like my Syntax Highlighter more because of the appearance and options... but really, it's just my personal taste. I definitely see the usefulness, in the simple convenience, of this solution.

To use it, simply go through their website and enter your code to get the embedded code snippet, such as...
  1. <script src="http://gist.github.com/24490.js"></script>  
Add that code snippet to your html and it will show up as...


More Highlighters

Here are some other highlighters. Check them out if you'd like. I didn't see anything extraordinarily special about them, so I don't want to do full write ups... but options are always nice, I guess. They have good documentation of their own. Let me know in the comments if you know of any other syntax highlighters!
http://wiki.ahnfelt.dk/Highlighter.html
http://pygments.org/
http://shjs.sourceforge.net/

Saturday, November 29, 2008

C Pointers

So, do you still get confused about C Pointers? Don't fret, unless you've had a fair amount of practice, it's understandable not to have it all straight. But if you want to get them straight fast, I'd suggest you take a half hour and read this excellent three part article on pointers:

1. http://www.freshsources.com/19930276.HTM
2. http://www.freshsources.com/199302BB.HTM
3. http://www.freshsources.com/199302F2.HTM

Need some C pointer reminders? It's especially good to review them before a major interview if you don't use them every day, since you know they'll probably ask you to reverse a string or something. So here's a quick review:

You take the address of something with &:

     // Address of a is:
     ptr = &a;

If you've got an address you can "reach into" it with *:

     // Contents of ptr is *ptr
     a = *ptr;

The only odd case is when declaring a pointer. Declare a pointer to an int as:

     int *p;

which is most easily thought of as "*p is an int." So if you reach into p with *, then you'll get an int.

One more time, in rules form:

1. &: Always takes the address of a variable. Mentally read & as "address of."

2. *: There are two cases:

    a. Declaring types
    b. Returns the contents of the address stored in a variable. Mentally read this as "content of."

Hopefully, you'll never forget your pointers now. By the way, fresh sources has tons of other good in-depth articles that cover C, C++, and Java. You should take a look. And if you're still trying to get comfy with C, but you haven't read and worked through K&R's C Programming Language... umm... what are you waiting for?

Sunday, October 19, 2008

Reactivating Dynamic Drives for Windows XP Professional

Requires: Windows XP Professional and optionally Ruby, AutoIt

Here's a quick thing that I learned and set up. First, the problem. I frequently use a usb external hard drive at home. As a dynamic disk, everytime I plugged the hard drive in I had to right click my computer, select manage, select disk management, select my external hard drive, and reactivate the disk. Or (after I removed the "My Computer" icon from my desktop) control panel->administrative tools->computer management->disk management->external drive->reactivate disk. It's not too much of a hassle, but it is stupidly annoying to have to go through that sequence often. If you have multiple disks that you want to manually individually activate, always have certain dynamic drives plugged in and want them activated at startup, or (like me) you just don't want to touch the mouse if there's another way, then you'd want a way to automate this process. So I found a way to avoid all the mindless clicking.

It's not really anything new. It's just making use of the diskpart command line utility. Here's documentation on diskpart. I wrote a small ruby script to run the diskpart for me. You can use a static batch script and text file to do this as well. In my actual implementation, I have a different ruby script that makes sure I'm activating the "right" disk. But this sample solution will always activate disk 1:
activateDynamicDisk.rb
view plaincopy to clipboardprint?
  1. require 'win32ole'  
  2. f = File.new"online.txt""w" )  
  3. f.print( "select disk 1\n" )  
  4. f.print( "online\n" )  
  5. f.close  
  6. au3 = WIN32OLE.new("AutoItX3.Control")  
  7. au3.Run( "diskpart /s c:\online.txt" )  
  8. File.delete( "online.txt" )  
Really easy, eh? You can easily set it up to run every time windows boots up. I should also mention dynamic drives only work with Windows XP Pro, so my external drive is unusable with a home edition. Now, the next trick is figuring out how to run this script whenever I plug the device in. I wonder how that's done.

Wednesday, October 15, 2008

Requirements for Tweaks

On this blog, I'm going to occasionally talk about tweaks I make to my machines (of various OSes.) When I say tweak, I mean a special configuration or short script to make my computer more suited to my needs. So basically a hack, but without the bad connotation people give the word "hack" nowadays.

As I've been reinstalling machines, I've been making notes about what I do to them and thought it would be useful to have an easy to find reference online, so I'll be posting cleaned up versions of these notes. Each tweak requires a certain environment (OS and programs), so I'm going to go over the common requirements in depth in this post. That way, when I reference a program, I'll just link here. I'll add to the following list as needed. Here's the list so far:

Windows XP Professional (SP2/SP3) - This is one of Microsoft's Windows Operating System (OS) versions. I'll leave it up to you to figure out how to get the OS and how to install it. XP Professional is different than XP Home, so the tweaks I describe may not be compatable with both. I use Professional myself and don't have access to a Home machine to test things on.

Windows Vista Ultimate - This is one of Microsoft's Windows Operating System (OS) versions. It's newer than XP. I'll leave it up to you to figure out how to get the OS and how to install it. Vista is different than XP, but many of the tweaks I describe should be compatable with both. The only version I have access to is Ultimate.

Solaris 10 - This is one of Sun's Solaris Operating System (OS) versions. It is UNIX based and is notably different from previous versions due to its zones. This is one of the Operating Systems I use most at work.

AutoIt - Auto It is a freeware automation language for Windows. It allows you to easily simulate a windows user with a script... you can send key presses or text to any windoe application. AutoIt scripts end with au3. AutoDownload an installer from http://www.autoitscript.com/autoit3/

PS Hot Launch - PS Hot Launch is a Windows program that allows you to map keyboard shortcuts to files. Invoking the keyboard shortcut will execute the file specified. For example, you can make ctrl-win-w open microsoft word and win-v open your winamp. Using too many of these is not a good idea because you'll probably start running into conflicts. But for a few applications, this program can work wonders. Play around with it. Download this from: http://www.pssoftlab.com/pshl_info.phtml

Ruby - Ruby is a dynamic, reflective, general purpose object-oriented programming language. It's a scripting language very much like Perl. Ruby scripts end with either rb or rbw (windowless). In order to run ruby scripts, you need the Ruby VM. You can download it from http://www.ruby-lang.org. There are installers for all of the popular Operating Systems under downloads. Once you've installed ruby, add the fxruby gem by running "gem install fxruby" in a command/terminal prompt. Fxruby is what I use for GUIs.

Batch Files - A batch file is a Windows text file, with the extension .bat or .cmd, containing a series of commands intended to be executed by the command interpreter. When a batch file is run, the shell program (usually COMMAND.COM or cmd.exe) reads the file and executes its commands, normally line-by-line. A batch file is analogous to a shell script in Unix-like operating systems. When working in windows, it's very useful knowing how to batch script as it allows you to lots of automation.

Sunday, October 12, 2008

Creating Windows Icons

So I'm writing a Windows C# application (I'll talk about it later) and I wanted to use my own icon for it. I needed a .ico image, Visual Studio wouldn't let me use any other image file types. I usually use Adobe Photoshop for my editing, but that program doesn't support natively icons. I found a Photoshop plugin for icon files at this website and it worked perfectly. There was no hassle at all.

I created a 16x16 icon and added it to all my forms easily. I set the icon to be used as the default icon through the project properties (under the application submenu) and that worked perfectly too. I'm really happy with how it turned out.

This post was really just to give out the link to the website for the photoshop icon plugin. Oh yeah, I'm probably going to be posting a good deal of C# stuff for a while since I've been playing with it a lot recently. Just some fair warning...

Sunday, October 05, 2008

Virii and XP Royale

Yeesh, it's been a long time since I posted here. Like just about everyone, I'm pretty busy... but the "busy-ness" gives me some good things to write about. So I'll be posting a lot more regularly for a while.

The most recent "busy-ness" was a full reinstallation of one of my desktop computers. It wasn't something that I really planned to do... I ended up catching a virus while testing google adsense (some ringtone ad) with an Internet Explorer 5 browser. I wasn't being that careful on that machine, since I didn't really didn't expect to catch anything through adsense links. It was a serious virus too. Like most of them, it put a bunch of adware on the machine (which would pop up sporadically), but it also tried to add some fetching programs and some dll files. Nasty, nasty... anyways, I didn't entirely trust that the anitvirus on the machine was able to catch everything, so I decided to wipe the whole harddrive (I used Active@KillDisk) and reinstall. I put on Windows XP Professional SP3 instead of Vista and went through the process of configuring the machine to my needs. It kinda needed to be done anyway, the machine was getting cluttered and I didn't lose any data since it's not a machine I really use for anything important.

One nice thing I found while reinstalling was the Windows XP Royale Noir theme. I like its look better than Windows Vista. It's a really sleek, black XP theme that I read the developers never fully released. Someone got a hold of it and finished it up, then made it available. I liked it so much I switched my main desktop computer to the theme... even though I'm pretty sure it makes the machine run slower.

Download and check out the theme here: http://www.softpedia.com/get/Desktop-Enhancements/Themes/Royale-Noir-Theme.shtml

To use the theme, you will need patch your uxtheme dll by running this program: http://www.softpedia.com/get/System/OS-Enhancements/UXTheme-MultiPatcher.shtml

Tuesday, March 25, 2008

Binary Interfaces

Here's something I dug up from an old folder that may have some merit being posted. Heh... It's from when I first started working... I've learned a lot about programming since then, so don't judge me for my choices from back then, ok?

This is the result of research into creating a binary interface for one of our protocols that exclusively used XML strings. The goal was to pass binary data through XML. We wanted to use a standard if possible, if not, then something close to a standard. Today, I'd probably spend all my time arguing to not use XML for binary transfers. Since we were using Java, Java RMI would have been sufficient for what we were doing back then with very little hassle. If not, I bet it would be easier to find solutions.

But here's what I came up with back then:

Option one is to encode the binary data into XML. Simply create a tag and place the bytes in. The only problem is that XML has a limited character set, so the byte stream will need to be encoded into characters and then decoded after it’s received. There are different algorithms for encoding. If large data sets are anticipated to be transferred and the byte value distribution within the set to be skewed, it would be best to go with the Huffman encoding approach (which encodes based on byte frequency). If we anticipate the data set to be small or we want to make a quick application, it’s best to go with a Base-64 encoding scheme (which encodes based on a standard 3 byte to 4 character scheme). The better algorithm (Huffman encoding) gives about 1 character per 1 byte of data and the second best, but more popular, algorithm (Base 64) gives about 1.5 characters per one byte of data. This method is simple, though definitely not ideal due to the overhead of encoding and decoding as well as the not-quite 1 to 1 byte to character conversion. Also, the encoding is not a standard and so the receiver may not know how to decode the data.

Option two is a Multipart/Related MIME. The essential idea for this is to send a MIME multipart/related message over http that can contain multiple types of data, such as xml and an image. There is a SOAP standard that allows for this (which even allows for the xml to reference the data, which is used if the binary data is to be one of the parameters), though it does not seem to be an XML standard. However, at this point, there IS no XML standard (according to my research) and this seems to be as close to a standard as anything else. Multipart/Related MIME messages are currently used for e-mail attachments.

Option Three is DIME messaging, which is a new specification for handling binary data with SOAP messages (or other messages.) It doesn't seem to be widespread, but it seems to be getting there. It is quite similar to Multipart/Related MIME messages, but offers a few key benefits. Essentially, it sacrifices flexibility for simplicity; so it is faster and simple to create. Also, it allows to break up large data sets so that they can be sent in chunks. Furthermore, DIME will have standards that will apply to more than just http. Microsoft will be motivated to focus on DIME for binary attachments in its future with SOAP tools and platforms.

Those are the most feasible options found trying to keep the current XML messaging intact.

Sources:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnservice/html/service01152002.asp - DIME
http://www.w3.org/TR/SOAP-attachments - Multipart MIME
http://www.javaworld.com/javaworld/javatips/jw-javatip117.html?tip - Encoding

We ended up going with Option 1 using the Base 64 approach. For transferring mostly 70KB files around, it's performed decently.

Friday, February 29, 2008

TSOL Tomcat 5.5 Installation

1. TOMCAT 5.5.x Installation

I had this sitting around, I figure someone out there might have a use for it. And so I present, instructions for installing Tomcat 5.5.x on Trusted Solaris 8:

1. Download the Tomcat binary 5.5.x from http://jakarta.apache.org/tomcat/ and extract it into a local directory referred to from here on as $CATALINA_HOME.

2. In $CATALINA_HOME, copy the contents of $CATALINA_HOME/conf into $CATALINA_HOME/confOriginal. Then delete both directories $CATALINA_HOME/logs and $CATALINA_HOME/conf.

3. Make multi-level directories for $CATALINA_HOME/logs and $CATALINA_HOME/conf. Copy all files in $CATALINA_HOME/confOriginal into $CATALINA_HOME/conf under every label you intend to run a server under.

4. If using Tomcat 5.5.x and J2SDK 1.4 or 1.3: Download compat.jar and unzip it in ../$CATALINA_HOME. Verify that it has placed xercesImpl.jar and xml-apis.jar into $CATALINA_HOME/common/endorsed

5. Go into $CATALINA_HOME/bin and run startup.sh. Verify that the installation was successful by visiting http://localhost:8080 in your web browser. You should see the Tomcat startup page. Shutdown by running shutdown.sh in $CATALINA_HOME/bin.

6. The 5.5.x version of Tomcat does not come with the admin service. It can be useful, so install that as well following these steps:
1. Unzip jakarta-tomcat-5.5.x-admin.zip & copy conf\ and server\ folders and contents over your own Tomcat conf & server folders (into $CATALINA_HOME folder)
2. Copy $CATALINA_HOME\server\webapps\admin\admin.xml to $CATALINA_HOME\webapps\admin.xml
3. Add a line to $CATALINA_HOME\conf\tomcat-users.xml to allow a user to login to the manager & admin modules, e.g.:
4. Restart Tomcat
5. Visit http://localhost:8080/admin and login using admin/admin1

7. Verify the installation by visiting http://localhost:8080/admin.

Wednesday, January 09, 2008

Scanlation Notes

This post no longer exists.