Welcome

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

What next?
1. Bookmark us with del.icio.us or Digg Us!
2. Subscribe to this site's RSS feed
3. Browse the site.
4. Post your own code snippets to the site!

Find solved forum threads with Google

A method of using google which allows for fast and accurate searching of forums and other websites for solved threads matching your criteria.

Just so everyone is aware, I just put the tag "hack" in the tag list so that those looking for google hacks might stumble on this. We'll see how that works out.
http://www.google.com/search?q=intitle:solved+intext:nvidia+5200
// This will find tons of solved issues regarding the NVidia 5200 graphics card

http://www.google.com/search?q=intitle:solved+intext:nvidia+5200+"no+screens+found"
// This should help you fix a problem with "No screens found" on an NVidia 5200 card

Lispify Ruby

// description of your code here

   require 'rubygems'  
   require 'parse_tree'  
   require 'ruby2ruby'  
     
   Sexp.class_eval do  
     def unbox  
       if length == 1  
         self.first  
       else  
         self  
       end  
     end  
   end  
     
   class Lispify < SexpProcessor  
     def initialize  
       super  
       self.auto_shift_type = true  
       self.require_empty   = false  
     end  
     
     def process_vcall(expr)  
       s(expr.first)  
     end  
     
     def process_call(expr)  
       expr = Sexp.from_array(expr)  
     
       first  = process(expr[0]).unbox  
       second = process(expr[2]).unbox  
     
       s(expr[1], first, second)  
     end  
     
     def process_array(expr)  
       process(expr.first)  
     end  
   end  


Which results in:


>> Lispify.new.process(ParseTree.translate(%q{ x + x * x - x }))
=> s(:-, s(:+, :x, s(:*, :x, :x)), :x)

Cross-platform file encryption with TrueCrypt

# first download & install the free, open-source TrueCrypt program from http://www.truecrypt.org/downloads.php

ls -ld /Applications/TrueCrypt.app
ls -l /Applications/TrueCrypt.app/Contents/MacOS/TrueCrypt

open -a TrueCrypt

1. click "Create Volume"
2. TrueCrypt Volume Creation Wizard: select "Create a file container"
3. click Next
4. Volume Type: select "Standard TrueCrypt volume"
5. click Next
6. Volume Location: click "Select File ..."
7. navigate to ~/Desktop with the file browser, then click "New Folder"
8. enter "Name of new folder": TrueCrypt, then click Create
9. enter in "Save As": MyTrueCrypt, then click Save
10. Volume Location: the "Select File ..." field should contain /PATH/TO/Desktop/TrueCrypt/MyTrueCrypt
11. click Next
12. Encryption Options:
    Encryption Algorithm: AES
    Hash Algorithm: RIPEMD-160
13. click Next
14. Volume Size: 3 MB
15. click Next
16. Volume Password: *********************  (more than 20 characters recommended)
17. click Next
18. Format Options: Filesystem Options: Filesystem type: select FAT
19. click Next
20. Volume Format: Move your mouse as randomly as possible within this window
21. click Format
22. "The TrueCrypt volume has been successfully created."
23. click OK
24. Volume Created
25. click Exit
26. click Close (to quit TrueCrypt)

open -a TrueCrypt

1. click "Select File ..."
2. navigate with the file browser to /PATH/TO/Desktop/TrueCrypt/MyTrueCrypt
3. click Open
4. click Mount and enter your password: *********************
5. double-click: Slot 1 : Volume: /PATH/TO/Desktop/TrueCrypt/MyTrueCrypt  Size: ...

touch '/Volumes/NO NAME/file.txt'    # or copy a text file to '/Volumes/NO NAME'
echo 'This is a test!' >> "$_"
cat '/Volumes/NO NAME/file.txt'
ls -l "$_"
# hdiutil unmount '/Volumes/NO NAME'

6. click Dismount
7. click Close


# References:
# Beginner's Tutorial, http://www.truecrypt.org/docs/?s=tutorial
# http://www.truecrypt.org/faq.php
# http://blogs.oreilly.com/digitalmedia/2008/03/truecrypt-51-open-source-file.html
# http://www.askstudent.com/security/a-step-by-step-guide-on-encrypting-files-using-truecrypt/
# http://techvj.blogspot.com/2007/03/secure-email-attachments-with-truecrypt.html
# http://www.osxcrypt.org

television

Shows to follow:
Battlestar Galactica
The Office
Weeds
Dollhouse
Dr. Horrible's Sing-Along Blog
How I Met Your Mother
Grey's Anatomy
Diggnation?
Totally Rad Show
House M.D.
The Soup
The Cleaner
Gossip Girl
Heroes
Kitchen Nightmares
Fringe

To Catch up on and decide if I wanna follow:
Lost
Eureka
Doctor Who
Supernatural
Jericho
It's Always Sunny in Philadelphia
Hell's Kitchen
Dexter
The Sarah Connor Chronicles
Torchwood
Rescue Me?
The Big Bang Theory?
My Boys?
Chuck?
Kyle XY?
Bones?

movies

Wanted
Hellboy II
21
Indiana Jones and The Kingdom of the Crystal Skull
The Chronicles of Narnia: Prince Caspian
Iron Man
The Incredible Hulk
WALL-E
The Happening
Hancock
Cloverfield
Forgetting Sarah Marshall
27 Dresses
Harold and Kumar Escape from Guantanamo Bay
88 Minutes
The Spiderwick Chronicles
Presto
An American Crime
Young People Fucking
Charlie Bartlett
The Other Boleyn Girl
--
3:10 to Yuma
Sunshine
Enchanted
Stardust
Wristcutters
Goodfellas
Cheaters
It's a Boy/Girl Thing
Spun
28 Days Later
28 Weeks Later
Castaway
The Golden Compass
Scoop
50 First Dates
The Shining
Indiana Jones and the Raiders of the Lost Ark
Alpha Dog
I'm Reed Fish
The Ballad of Jack and Rose
Cashback
Helvetica
There Will Be Blood
Into the Wild
The Dangerous Lives of Altar Boys
My Blueberry Nights
Penelope
Starter for 10
Lucky Number Slevin
The Nanny Diaries
In Good Company
Dedication

ipfwdump, ipfwto & ipfwfrom

Three basic Bash functions to get some current ipfw ruleset information.

unset -f ipfwfrom
function ipfwfrom() {

   declare sudo=/usr/bin/sudo ipfw=/sbin/ipfw
   declare IF CIF ipnum rule url num

   OPATH=$PATH; OIFS=$IFS
   export PATH="/usr/bin:/bin:/usr/sbin:/sbin"; export IFS=$' \t\n'

   /usr/sbin/ipconfig waitall

   IF="en0"
   CIF="$(/sbin/route -n get default | grep interface | awk '{ print $NF }')"   # current default interface
   #CIF="$(/usr/sbin/netstat -rn | grep default | awk '{ print $NF }')"          # current default interface

   if [[ "$1" = "-n" ]]; then    # print IP numbers

      $sudo $ipfw -de list | awk '/ \(.*\) / { print $1, $7 }'  | sort -n | uniq | while read -d $'\n' line; do
         ipnum="${line##* }"
         rule="$(/usr/bin/sudo /sbin/ipfw list ${line%% *} )"
         printf "%-27s %s\n" "${ipnum}" "${rule}"
      done

   else

      if [[ "${IF}" != "${CIF}" ]]; then echo "No internet connection!"; return 1; fi

      $sudo $ipfw -de list | awk '/ \(.*\) / { print $1, $7 }'  | sort -n | uniq | while read -d $'\n' line; do
         ipnum="${line##* }"
         url="$(/usr/bin/dig +short +time=3 +tries=2 -x ${ipnum} | head -n 1)"
         if [[ -z "${url}" ]]; then url=${ipnum}; fi
         rule="$(/usr/bin/sudo /sbin/ipfw list ${line%% *} )"
         printf "%-27s %-45s %s\n" "${ipnum}" "${url}" "${rule}"
      done

   fi

   export PATH=$OPATH; export IFS=$OIFS

   return 0
}
export -f ipfwfrom



unset -f ipfwto
function ipfwto() { 

   declare sudo=/usr/bin/sudo ipfw=/sbin/ipfw
   declare IF CIF ipnum rule url num

   OPATH=$PATH; OIFS=$IFS
   export PATH="/usr/bin:/bin:/usr/sbin:/sbin"; export IFS=$' \t\n'

   /usr/sbin/ipconfig waitall

   IF="en0"
   CIF="$(/sbin/route -n get default | grep interface | awk '{ print $NF }')"   # current default interface

   if [[ "$1" = "-n" ]]; then    # print IP numbers

      $sudo $ipfw -de list | awk '/ \(.*\) / { print $1, $10 }'  | sort -n | uniq | while read -d $'\n' line; do
         ipnum="${line##* }"
         rule="$(/usr/bin/sudo /sbin/ipfw list ${line%% *} )"
         printf "%-27s %s\n" "${ipnum}" "${rule}"
      done

   else

      if [[ "${IF}" != "${CIF}" ]]; then echo "No internet connection!"; return 1; fi

      $sudo $ipfw -de list | awk '/ \(.*\) / { print $1, $10 }'  | sort -n | uniq | while read -d $'\n' line; do
         ipnum="${line##* }"
         url="$(/usr/bin/dig +short +time=3 +tries=2 -x ${ipnum} | head -n 1)"
         if [[ -z "${url}" ]]; then url=${ipnum}; fi
         rule="$(/usr/bin/sudo /sbin/ipfw list ${line%% *} )"
         printf "%-27s %-45s %s\n" "${ipnum}" "${url}" "${rule}"
      done

   fi

   export PATH=$OPATH; export IFS=$OIFS

   return 0
}
export -f ipfwto



unset -f ipfwdump
function ipfwdump() { 

   declare sudo=/usr/bin/sudo ipfw=/sbin/ipfw
   declare IF CIF ipnum rule url num ipfrom ipto ip1 ip2

   OPATH=$PATH; OIFS=$IFS
   export PATH="/usr/bin:/bin:/usr/sbin:/sbin"; export IFS=$' \t\n'

   /usr/sbin/ipconfig waitall

   IF="en0"
   CIF="$(/sbin/route -n get default | grep interface | awk '{ print $NF }')"   # current default interface

   if [[ "$1" = "-n" ]]; then    # print IP numbers

      $sudo $ipfw -de list | awk '/ \(.*\) / { print $1,$7,$10 }' | sort -n | uniq | while read -d $'\n' line; do
         read num ipfrom ipto <<< "${line}"
         rule="$(/usr/bin/sudo /sbin/ipfw list ${num} )"
         printf "%-45s %s\n" "${ipfrom}  ->  ${ipto}" "${rule}"
      done

   else

      if [[ "${IF}" != "${CIF}" ]]; then echo "No internet connection!"; return 1; fi

      $sudo $ipfw -de list | awk '/ \(.*\) / { print $1,$7,$10 }' | sort -n | uniq | while read -d $'\n' line; do
         read num ipfrom ipto <<< "${line}"
         rule="$(/usr/bin/sudo /sbin/ipfw list ${num})"
         ip1="$(/usr/bin/dig +short +time=3 +tries=2 -x ${ipfrom} | head -n 1)"
         ip2="$(/usr/bin/dig +short +time=3 +tries=2 -x ${ipto} | head -n 1)"
         if [[ -z "${ip1}" ]]; then ip1=${ipfrom}; fi
         if [[ -z "${ip2}" ]]; then ip2=${ipto}; fi
         printf "%-65s %s\n" "${ip1}  ->  ${ip2}" "${rule}"
      done
   fi


   export PATH=$OPATH; export IFS=$OIFS

   return 0
}

export -f ipfwdump



ipfwdump
ipfwdump -n
ipfwdump -n | grep 7400
ipfwdump -n | grep allow
ipfwdump -n | grep deny

ipfwto
ipfwto -n | grep deny
ipfwfrom -n
ipfwfrom | grep allow

Vertical Middle DIV

// description of your code here
Vertical Middle DIV.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>Example 7: Vertical alignment of content with JavaScript &amp; CSS</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<style type="text/css">
        <!--
			body {
				margin: 0;
				padding: 0;
				font: 12px/1.5 verdana, arial, helvetica, sans-serif;
				text-align: center; /* Takes care of horizontal alignment in Internet Explorer */
				background-image: url(grid.gif);
			}
			#content {
				position: relative; /* Needed for Safari */
				margin: auto; /* Takes care of horizontal alignment in standards compliant browsers */
				text-align: left;
				width: 200px;
				height: 200px;
				background-color: #fc0;
			}
			h1, p {
				margin: 0;
				padding: 1em;
			}
			h1 {
				font-size: 12px;
				line-height: 1.5em;
			}
        -->
        </style>
		<script type="text/javascript">
		<!--
		function getWindowHeight() {
			var windowHeight = 0;
			if (typeof(window.innerHeight) == 'number') {
				windowHeight = window.innerHeight;
			}
			else {
				if (document.documentElement && document.documentElement.clientHeight) {
					windowHeight = document.documentElement.clientHeight;
				}
				else {
					if (document.body && document.body.clientHeight) {
						windowHeight = document.body.clientHeight;
					}
				}
			}
			return windowHeight;
		}
		function setContent() {
			if (document.getElementById) {
				var windowHeight = getWindowHeight();
				if (windowHeight > 0) {
					var contentElement = document.getElementById('content');
					var contentHeight = contentElement.offsetHeight;
					if (windowHeight - contentHeight > 0) {
						contentElement.style.position = 'relative';
						contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px';
					}
					else {
						contentElement.style.position = 'static';
					}
				}
			}
		}
		window.onload = function() {
			setContent();
		}
		window.onresize = function() {
			setContent();
		}
		//-->
		</script>
	</head>
	<body>
		<div id="content">
			<h1>Content</h1>
			<p>This content should be centered in your browser window. CSS is used for horizontal alignment, while scripting is used for vertical alignment.</p>
		</div>
	</body>
</html>

javascript value of selected option

// what is the value of the selected option?

this.options[this.selectedIndex].value

Fix for EE Multi-relationship field in PHP4

// http://expressionengine.com/forums/viewreply/192755/

Open your /system/modules/weblog/mod.weblog.php file and find from this line:
    
    // -------------------------------------------
    // 'weblog_entries_tagdata' hook.

to this line:
    
    //
    // -------------------------------------------

Replace between those comments with this:

    if (isset($EXT->extensions['weblog_entries_tagdata']))
    {
        // -- PHP4 Fix For call_user_func_array not passing by reference
        global $Weblog; $Weblog = $this;
        // -- End Fix
        
        $tagdata = $EXT->call_extension('weblog_entries_tagdata', $tagdata, $row, $this);
        if ($EXT->end_script === TRUE) return $tagdata;
        
        // -- PHP4 Fix For call_user_func_array not passing by reference
        $this = $Weblog; unset($Weblog);
        // -- End Fix
    

recursevly remove .svn files from an existing repo

If you need to take a project out of scm, you can run this bash script from the top folder - and it will remove all .svn folders recursevly (sp)

Works well for
- changing an svn checkout into and svn export
- switching from svn to git

find . -type d -name '.svn' -exec rm -rf {} \;