DZone 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

Snippets

  • submit to reddit

Recent Snippets

                    A neat trick in iTunes 4.5 that changes the little arrows next to each artist/album/title to search your library for that string instead of the iTunes Music Store.

defaults write com.apple.iTunes invertStoreLinks -bool YES
                
                    Sometimes you want to log generated SQL selectively, so popping -DEOAdaptorDebugEnabled=true on the command line of your app is verbose overkill. Here's how you can selectively enable and disable SQL logging:

NSLog.allowDebugLoggingForGroups(0x10000L);
// Do something that roundtrips the DB, like editingContext.saveChanges().
NSLog.refuseDebugLoggingForGroups(0x10000L);
                
                    
(new DOMConfigurator()).doConfigure(resourceManager().inputStreamForResourceNamed("log4j.xml", "app", null), LogManager.getLoggerRepository());
                
                    
NSLog.debug.setAllowedDebugLevel(NSLog.DebugLevelDetailed);
NSLog.allowDebugLoggingForGroups(NSLog.DebugGroupRules);
                
                    
# -*- coding: utf-8 -*-

Add this line at the beginning of your code. Python won't complain anymore about non-ascii characters in your code.                
                    Sometimes I find it useful having an overview about the keys an array has; PHP function Get_Array_Keys_UL will create an nested HTML Unordered List string of the keys of a given array. Makes best sense with literal keys, e.g. $_POST:
/**
 * Creates recursively a nested HTML UL of array keys.
 * @param array $array Array
 * @return string Nested UL string
 */
function Get_Array_Keys_UL($array=array()) {
 $recursion=__FUNCTION__;
 if (empty($array)) return '';
 $out='<ul>'."\n";
 foreach ($array as $key => $elem)
   $out .= '<li>'.$key.$recursion($elem).'</li>'."\n";
 $out .= '</ul>'."\n";  
 return $out;
}

Use like this:
$ul=Get_Array_Keys_UL($_POST);
echo $ul;
                
                    XoloX's cute function directoryToArray($directory, $recursive) will become "real recursive" by adding the recursion's directory content to array $array_items:
if($recursive) { 
  #directoryToArray($directory. "/" . $file, true); 
  $array_items=array_merge($array_items,directoryToArray($directory. "/" . $file, $recursive));
} 
                
                    class A
  def A.inherited(clazz)
    puts "Hey, #{clazz} is subclassing me"
  end
end
 
class B < A; end
 
 
Produces:
 
$ ruby x.rb
Hey, B is subclassing me                
                    Here are some file manipulation functions in PHP:

function fileToArray($file) {
	if (!$array = file($file)) {
		die("fileToArray: Could not read file!");
	}
	return $array;
}

function fileToString($file) {
	if (!$string = file_get_contents($file)) {
		die("fileToString: Could not read file!");
	}
	return $string;
}

function stringToFile($file, $string) {
	if (!$handle = fopen($file,"w")) {
		die("stringToFile: Could not open file");
	}
	if (!fwrite($handle, $string))
		die("stringToFile: Could not write file");
	}
	fclose($handle); // Should the close handle be error checked, or would I be over doing it?
}

function arrayToFile($file, $array) {
	if (!$handle = fopen($file,"w")) {
		die("arrayToFile: Could not open file");
	}
	for ($i = 0; $i < count($array); $i++) {
		if ($i == count($array) - 1) {
			if (!fputs($handle, $array[$i])) {
				die("arrayToFile: Could not write file");
			}
		} else {
			if (!fputs($handle, $array[$i]."\n")) {
				die("arrayToFile: Could not write file");
			}
		}
	}
	fclose($handle);
}
                
                    Updated to include drwitt's fix. Shame on me! Well, as long as I learn from my mistakes :)

Get a (recursive) directory listing in an array. Directory's are included in this list. If this behavior is not wanted, remove the two lines:

$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);

To get a non recursive directory listing, use it like this:

$files = directoryToArray("./", false);

And to get a recursive directory listing, use it like this:

$files = directoryToArray("./", true);

Once you have an array of files, you can iterate over the directories/files like this:

echo '<ul>';

foreach ($files as $file) {
	echo '<li>' . $file . '</li>';
}

echo '</ul>';

or like this:

echo '<ul>';

for ($i = 0; $i <= count($files); $i++) {
	echo '<li>' . $files[$i] . '</li>';
}

echo '</ul>';

Code:
function directoryToArray($directory, $recursive) {
	$array_items = array();
	if ($handle = opendir($directory)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..") {
				if (is_dir($directory. "/" . $file)) {
					if($recursive) {
						$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
					}
					$file = $directory . "/" . $file;
					$array_items[] = preg_replace("/\/\//si", "/", $file);
				} else {
					$file = $directory . "/" . $file;
					$array_items[] = preg_replace("/\/\//si", "/", $file);
				}
			}
		}
		closedir($handle);
	}
	return $array_items;
}

Hope someone can use it!