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!

Removing <?xml version="1.0"?> from the output of XSLTProcessor->transformToXml()

Just add this as a top level element in your XSL document.

<xsl:output method="html" />


Thanks to this post on php.net for the answer:
http://us3.php.net/manual/en/xsltprocessor.transformtoxml.php#80887

"openterminal" contextual menu item with Automator

Right-click on a file or folder in a Finder window and select Automator -> openterminal to open it in Terminal.app.
(i.e. cd to the folder or open the file in the nano text editor)

open -a Automator

#--------------------------------------------------

Drag or add actions here to build your workflow:
Library: Finder -> Action: Get Selected Finder Items
Library: Automator -> Action: Run Shell Script
                                 - Shell: /bin/sh
                                 - Pass input: as arguments


if [[ $# -gt 1 ]]; then exit 0; fi

if [[ -d "$@" ]]; then

   printf "%s" "$@" | /usr/bin/pbcopy
   #/usr/local/bin/cpath # cf. http://osxutils.sourceforge.net

   /usr/bin/open -a Terminal

   /usr/bin/osascript -e 'tell application "Terminal" to do script with command "printf \"\\e[8;26;115;t\"; printf \"\\e[3;300;240;t\"; cd \"$(/usr/bin/pbpaste)\"; /usr/bin/clear" in (first window whose name contains " ")'

# alternative without /usr/bin/clear (experimental)
# requires:
# defaults write com.apple.Terminal Autowrap NO
# or:
# Terminal menu -> Window Settings ...  -> Buffer -> in the Scrollback 
# section check the box next to "Wrap lines that are too long" 
# -> click "Use Settings as Defaults"

#   /usr/bin/osascript -e 'tell application "Terminal" to do script with command "cd \"$(/usr/bin/pbpaste)\"; /usr/bin/tput cup 0 0; /usr/bin/tput dl1; /usr/bin/tput el; /usr/bin/tput dl1; /usr/bin/tput el" in (first window whose name contains " ")'

   exit 0

elif [[ -r "$@" ]]; then 

   printf "%s" "$@" | /usr/bin/pbcopy

   /usr/bin/open -a Terminal

   /usr/bin/osascript -e 'tell application "Terminal" to do script with command "printf \"\\e[8;26;115;t\"; printf \"\\e[3;300;240;t\"; /usr/bin/clear; /bin/sleep 0.3; /usr/bin/nano \"$(/usr/bin/pbpaste)\";" in (first window whose name contains " ")'

exit 0

fi

exit 0


#--------------------------------------------------


open -a Automator ~/Library/Workflows/Applications/Finder/openterminal.workflow

Bitwise SQL Comparison

// Do a bitwise comparison in mysql

select * from table where (bit_sum & bit) = bit

Super simple XML and PHP

This is a super simple implementation of a concept found here:
http://xtech06.usefulinc.com/schedule/paper/19

This xml class provides a very simple way to open an XML file, get information from the XML file, modify information from the XML file and save the XML file. It also supports automatic creation of new XML files based on an automatically incrementing ID senerio (similar to auto incrementing primary keys in databases).

The creation of new XML files is done by looking for a "template" xml file named 0.xml in the directory passed to the create function. If the template file is found, then it is loaded into the xml class and saved to a new file whose name is one more than the highest filename (of course the xml extension is added on). Eventually, I will try and add some validation code and whatever else, but for now I'm trying to keep things simple.

This requires the DOM module to be built into PHP.
I am using PHP 5.

If anyone has any ideas on how to improve this, feel free to post comments.

<?php

class xml
{
	var $dom;
	var $uri;

	function xml($uri)
	{
		$this->dom = new DOMDocument();

		if(preg_match('/\.xml$/', $uri))
		{
			$this->uri = $uri;
		}
		else
		{
			$this->uri = $this->create($uri);
		}
		$this->dom->load($this->uri);
	}

	function set($query, $value)
	{
		$path = new DOMXPath($this->dom);
		$nodes = $path->query($query);
		$nodes->item(0)->nodeValue = $value;
	}

	function get($query)
	{
		$path = new DOMXPath($this->dom);
		$nodes = $path->query($query);
		return $nodes->item(0)->nodeValue;
	}

	function save()
	{
		$this->dom->save($this->uri);
	}

	function create($uri)
	{
		// Build the URI of the template file.
		$template = sprintf('%s/0.xml', $uri);

		// If the directory doesn't exist, we can't really
		// do anything.
		if(!is_dir($uri))
		{
			exit('No directory');
		}

		// If the template file doesn't exist, we cannot
		// create a new file automatically.
		if(!file_exists($template))
		{
			exit('No template');
		}

		// Load the template XML into our DOMDocument.
		$this->dom->load($template);

		// Scan the directory into an array.
		$dir = scandir($uri);

		// Pull out the highest ID
		$id = str_replace('.xml', '', array_pop($dir));

		// Add one to it
		$id++;

		// Construct the new path with the new ID
		$uri = sprintf('%s/%s.xml', $uri, $id);

		// Save the new file
		$this->dom->save($uri);

		// Return the URI of the new XML file.
		return $uri;
	}
}

// The following code will create a new XML file
// under the directory /var/www/data/users.
$x = new xml('/var/www/data/users');
$x->set('//user/name', 'John Doe');
$x->save();

?>

Joining Tables using a Like statement

-- This SQL code allows you to join a table to another table by finding columns that contain another column's information
-- (instead of where the columns are equal)

SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.col LIKE '%' + Table2.col + '%'

Change package names of java files

changes the package names of java classes.

For example, if you have a bunch of java files in a package and you need to move them to a new package.

#!/bin/sh
                          
for file in `ls *.java` 
do
  sed -e "s/old.package.name/new.package.name/" $file > /tmp/tempfile.tmp
  mv /tmp/tempfile.tmp $file
done

Recursive grep without grep -r

Bumped into this situation recently on a Solaris system. I wanted to do a recursive grep, using the -r flag. That flag is not standard, so I was stuck until I whipped up this little diddy.

After navigating to the directory that you want as the root of the search, enter
find . -type f -exec grep <pattern> {} \;

It will only show the entries that match that pattern. Don't forget that you can also add in addition filters to the find command to limit the search file types, if so needed. So, by way of example, if you wanted to find every time you used the word Vista in your home directory, open up the command line and enter:
[user@host ~]$ find . -type f -exec grep Vista {} \;

My search returned nothing ;-)

HAProxy compile for Ubuntu 7.10 Gutsy Gibbon

cd /usr/src/
sudo wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.15.2.tar.gz
sudo tar zxvf haproxy-1.3.15.2.tar.gz
sudo aptitude install libpcre++-dev
sudo make TARGET=linux26 CPU=i386 USE_STATIC_PCRE=1
sudo make install

sudo haproxy -f /etc/haproxy/haproxy.conf


http:// ..url.. /haproxy?stats

Mongrel cluster starting after restart on Ubuntu GG

sudo mkdir /etc/mongrel_cluster
sudo ln -s /mnt/app/ticketsolve/shared/config/mongrel_cluster_new_production.yml /etc/mongrel_cluster/mongrel_cluster_new_production.yml
sudo cp /usr/bin/mongrel_cluster_ctl /etc/init.d/
sudo chmod +x /etc/init.d/mongrel_cluster_ctl
cd /etc/init.d/
sudo /usr/sbin/update-rc.d -f mongrel_cluster_ctl defaults

How to set the background image using xv.

I was fed up with using fbsetroot... xsetroot... etc to set my background image, so I decided to see if xv would do it for me since it's a program that I actually use for more than just background setting like fbsetroot, xsetroot, etc.

xv -root -quit -maxpect ~/images/blah.jpg