Every coder has a collection of “snippets” – little bits of code that did something useful at some point in time.

Perl Snippets

Useful perl code fragments.

base64.pl

#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use MIME::Base64 qw(encode_base64);
my $filename;
my $buffer;$filename = param('f');
print "Content-type: text/html\r\n\r\n";
print "\<FILE name='$filename' encoding='base64'\>\r\n";
open(FILE, "$filename") or die "$!";
while (read(FILE, $buffer, 60*57)) {
 print encode_base64($buffer);
}
print "\<\/FILE\>\r\n";

url-encode.pl

sub URLEncode {
 my $theURL = $_[0];
 my $MetaChars = quotemeta( ';,/?\|=+)(*&^%$#@!~:');
 $theURL =~ s/([$MetaChars\"\'\x80-\xFF])/"%" .
 uc(sprintf("%2.2x",         ord($1)))/eg;
 $theURL =~ s/ /\+/g;
 return $theURL;
}

Unicode to ASCII Conversion

use Unicode::String;
local $/; # slurp mode
$line=<>;
$u=Unicode::String::utf8($line);
$latin1=$u->latin1;
print $latin1;

Load a Google-News Search as RSS Feed

use LWP::UserAgent;
# RSS feed to load
$url = "http://news.google.com/news?hl=en&ned=&q=cancer&ie=UTF-8&output=rss";
# Load data
$ua = LWP::UserAgent->new;
$ua->timeout(5);
$ua->agent("Mozilla/5.0");
$response = $ua->get($url);
if ($response->is_success()) {
 # Data is content of request
 $rss_xml = $response->content;
 # Info and count
 $result .= "XML loaded from URL '$url' ...\n";
} else {
 # Load failed
 $result .= "Load error for URL '$url' ...\n";
 $result .= $response->status_line . "\n";
}
print $result;
JavaScript Snippets

Useful JavaScript code fragments.

IsEmail()

function IsEmail(emailStr) {
  if (  emailStr.match( new RegExp(
                        '^(\\s*)(("[^"]*")|\\w+([*&+~!-\\.]\\w+)*)
                         @\\w+([-\\.]\\w+)*\\.\\w{1,3}(\\s*)$'
                                   ) ) == null )
	return false;
  return true;
}

IsNumber()

function IsNumber(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
      }
   }
   return IsNumber;
}

GetUrlParameter()

function GetUrlParameter(param) {
 var val = "";
 var qs = window.location.search;
 var start = qs.indexOf(param); if (start != -1) {
  start += param.length + 1;
  var end = qs.indexOf("&", start);
  if (end == -1) {
   end = qs.length
  }
  val = qs.substring(start,end);
 }
 return val;
}
C/C++ Snippets

Useful C/C++ code fragments.

Fast Line Algorithm
fastLineAlgorithm.txt

Fast Triangle Intersection Test
fastTriangleIntersectionTest.txt

TwoFish Encryption
Reference Optimized

DLL demo
dll-demo.zip

Shell Snippets

Various shell scripts and commandline fragments that come in handy from time to time.

rsync Folder Mirroring Syntax

rsync -e ssh -av /home/user/Source/ user@www.hostname.net:/home/user/Target

Start a VNC Server

vncserver -kill :1
vncserver :1 -geometry 1024×768 -depth 16 -name MyVNC -rfbport 5021 -httpport 5022

Snippet Mania
Tagged on:                     

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.