<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>UserPixel</title>
	<atom:link href="http://www.userpixel.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.userpixel.com</link>
	<description></description>
	<lastBuildDate>Sat, 12 May 2012 19:17:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Javascript Lorem Ipsum function</title>
		<link>http://www.userpixel.com/1123</link>
		<comments>http://www.userpixel.com/1123#comments</comments>
		<pubDate>Thu, 26 Apr 2012 13:02:17 +0000</pubDate>
		<dc:creator>Alex Hanif</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lorem ipsum]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.userpixel.com/?p=1123</guid>
		<description><![CDATA[I needed a quick and small function that generates random place holder text with a specified minimum and maximum length. Here is the result (shared for free): /** * This function returns a text string that is between "from" to "to" characters long and can be used as a text place holder. */ function loremIpsum(from, [...]]]></description>
			<content:encoded><![CDATA[<p>I needed a quick and small function that generates random place holder text with a specified minimum and maximum length. Here is the result (shared for free):</p>
<pre>/**
 * This function returns a text string that is between "from" to "to" characters long and can be used as a text place holder.
 */
function loremIpsum(from, to) {
	if ( typeof this.loremString == "undefined" ) {
		this.loremString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse turpis nulla, tempus \
		vitae cursus id, facilisis sit amet turpis. Pellentesque vestibulum condimentum orci at congue. Cras mole\
		stie urna ac erat vulputate nec ornare ipsum egestas. Nulla mauris urna, mattis at mattis in, vestibulum \
		sit amet massa. Quisque posuere dapibus arcu id malesuada. Phasellus in porta est. Fusce sit amet dui orc\
		i, sed viverra eros. Sed vulputate elementum odio vel gravida. Sed ut velit a dolor pellentesque cursus a\
		 vitae mauris. Nunc ipsum eros, ultrices nec lacinia ut, iaculis eget metus. Sed ac sem non turpis gravid\
		 a suscipit. Praesent interdum ante a libero pharetra eu volutpat lectus ornare. Sed vestibulum dapibus l\
		 igula sed rhoncus. Nam placerat faucibus scelerisque. Pellentesque habitant morbi tristique senectus et \
		 netus et malesuada fames ac turpis egestas.";
	}

	if ( typeof from == "undefined" ) {
		from = 0;
	} else {
		from = Math.min( from, loremString.length );
	}
	if ( typeof to == "undefined" ) {
		to = this.loremString.length;
	} else {
		to = Math.min( to, loremString.length );
	}

	if ( from &gt; to ) {
		var tmp = from;
		from = to;
		to = tmp;
	}

	length = from + Math.floor( Math.random() * ( to - from ) );

	return this.loremString.substr( 0, length );
}</pre>
<p>Here is a slightly improved version that is attached to JQuery as a function:</p>
<pre>// This plugin fills the text with Lorem Ipsum
(function( $ ){
	$.loremIpsum = function ( length ) {
		if ( typeof this.loremString == "undefined" ) {
			this.loremString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec id enim sed eros imperdiet rutrum molestie " +
			"quis ante. Proin lobortis bibendum mi vitae commodo. Aenean malesuada nisi at lorem malesuada ac posuere nulla molestie. Cura" +
			"bitur mi nibh, gravida id sollicitudin nec, faucibus vitae mi. In quis mauris in nunc interdum tincidunt. Nullam placerat vol" +
			"utpat lacus, sed blandit dui vulputate eu. Sed lectus lacus, luctus sed pellentesque eget, mollis vel nisi. Etiam ut convalli" +
			"s augue. Nunc viverra vestibulum auctor. Mauris id risus vitae mi egestas consequat. Cras in ante ac nisl volutpat faucibus. " +
			"Donec blandit suscipit enim. Fusce consectetur odio id orci laoreet sed pretium purus sodales. Vivamus a nisl lorem. Nulla la" +
			"oreet ornare risus, eget sodales sem vehicula varius. Etiam aliquet luctus eros eget rhoncus. Donec semper arcu non dui facil" +
			"isis sed pellentesque dolor posuere. Etiam sit amet congue erat. Vivamus porttitor feugiat diam quis commodo. Nullam congue, " +
			"tortor vitae nullam.";
		}

		if ( typeof length == "undefined" ) {
			length = this.loremString.length;
		}

		if ( length &lt;= this.loremString.length ) {
			return this.loremString.substr( 0, length );
		} else {
			//it's longer than the string we have, repeat it to generate the result string
			var ret = "";
			for ( var i = 0; i &lt; length / this.loremString.length; i++ ) {
				ret += this.loremString;
			}
			ret += this.loremString.substr( 0, ( length % this.loremString.length ) );
			return ret;
		}
	};
})( jQuery );</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.userpixel.com/1123/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a pure CSS side folding layout</title>
		<link>http://www.userpixel.com/1115</link>
		<comments>http://www.userpixel.com/1115#comments</comments>
		<pubDate>Mon, 16 Apr 2012 20:39:45 +0000</pubDate>
		<dc:creator>Alex Hanif</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[colors]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://www.userpixel.com/?p=1115</guid>
		<description><![CDATA[I was playing around with CSS animations to create a layout for one of my pet projects where the following code turned out to be interestingly simple and useful: &#60;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&#62; &#60;html xmlns="http://www.w3.org/1999/xhtml"&#62; &#60;head&#62; &#60;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&#62; &#60;title&#62;Untitled Document&#60;/title&#62; &#60;style type="text/css"&#62; body { position:absolute; padding:0; margin:0; width:100%; [...]]]></description>
			<content:encoded><![CDATA[<p>I was playing around with CSS animations to create a layout for one of my pet projects where the following code turned out to be interestingly simple and useful:</p>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;style type="text/css"&gt;
body {
 position:absolute;
 padding:0;
 margin:0;
 width:100%;
 height:100%;
}
table {
 width:100%;
 height:100%;
}
td:nth-child(1) {
 background-color:#FFC;
}
td:nth-child(2) {
 background-color:#3C9;
}
td:nth-child(3) {
 background-color:#F99;
}
td:nth-child(4) {
 background-color:#6FF;
}
@-webkit-keyframes mouseover {
 from { width:10%; }
 to { width:25%; }
}
td {
 height:100%;
 width:10%;
}
td:not(.selected):hover {
 -webkit-animation:mouseover 200ms;
 width:25%;
}
td.selected {
 width:55%;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table width="100%" border="0" cellspacing="0" cellpadding="0"&gt;
 &lt;tr&gt;
 &lt;td&gt;&amp;nbsp;&lt;/td&gt;
 &lt;td class="selected"&gt;&amp;nbsp;&lt;/td&gt;
 &lt;td&gt;&amp;nbsp;&lt;/td&gt;
 &lt;td&gt;&amp;nbsp;&lt;/td&gt;
 &lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Here you can try it live: <a href="http://jsfiddle.net/gU3xB/">http://jsfiddle.net/gU3xB/</a></p>
<p>Just  hover over the rectangles to see the smooth movement. See the HTML code for understanding how simple it is.</p>
<p>Update: a slightly more advanced version of it can be found here: <a href="http://jsfiddle.net/mhzSz/">http://jsfiddle.net/mhzSz/</a> this one uses a little javascript code (6 lines of jQuery) to let the user select a panel by mouse click.</p>
<p>Update2:  this version has vertical and horizontal cells: <a href="http://jsfiddle.net/xUd39/">http://jsfiddle.net/xUd39/</a></p>
<p>PS. I know &lt;table&gt; should not be used for layout management. This example shows a simple and fun code, not a production quality manifesto of coding standards.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.userpixel.com/1115/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A powerful and simple function to create DOM from JSon</title>
		<link>http://www.userpixel.com/1089</link>
		<comments>http://www.userpixel.com/1089#comments</comments>
		<pubDate>Mon, 09 Apr 2012 15:10:00 +0000</pubDate>
		<dc:creator>Alex Hanif</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.userpixel.com/?p=1089</guid>
		<description><![CDATA[I&#8217;ve been working on a pet project during the Easter vacation. One of the cool ideas that I implemented was to create a valid HTML DOM tree just by a Javascript object (AKA Json). Here is the code: /** * A utility function that creates a HTML DOM based on the descriptor. * The descriptor is [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on <a title="Spotify Search" href="http://www.userpixel.com/spotify-search">a pet project</a> during the Easter vacation. One of the cool ideas that I implemented was to create a valid HTML DOM tree just by a Javascript object (AKA Json). Here is the code:</p>
<pre>/**
 * A utility function that creates a HTML DOM based on the descriptor.
 * The descriptor is an object that has the following members:
 * tag: (mandatory) string, the name of the HTML tag to be created. Example: "li" for creating a &lt;li&gt; element
 * id: (optional) string, the id that will be assigned to the element's id attribute. Example: "searchButton"
 * className: (optional) string, the name(s) that will be assigned to the element's className. If there are multiple classes, they should be separated by space. Example: "artist popularity"
 * attr: (optional) object, all the key-value pairs of this object will be added as attributes to the created element. Example: {href:"#",name:"badLink"} in an &lt;a&gt; element will be &lt;a href="#" name:"badLink"&gt;&lt;/a&gt;
 * contents: (optional) string, object or an array of objects. If it's string, it will go directly to the element text. If it is an object, it will be treated as a descriptor itself and passed to this function recursively. If it is an array, it will be supposed to be an array of descriptors
 * @return a DOM structure that can be used with appendChild() function to insert
 */
function createTree(descriptor) {
	var ret=document.createElement(descriptor.tag);
	if ( descriptor.id ) {
		ret.id=descriptor.id;
	}
	if ( descriptor.className ) {
		ret.className=descriptor.className;
	}
	if ( descriptor.attr ) {
		for ( var a in descriptor.attr ) {
			ret.setAttribute( a, descriptor.attr[a] );
		}
	}
	if ( descriptor.contents ) {
		switch (typeof descriptor.contents ) {
			case "string":
				//it is simply a string
				ret.innerHTML = descriptor.contents;
				break;
			case "object":
				if ( Array.isArray( descriptor.contents ) ) {
					//it is an array of objects
					for( var i = 0; i &lt; descriptor.contents.length; i++ ) {
						if ( typeof descriptor.contents[i] == "string" ) {
							//it is a string
							ret.appendChild( document.createTextNode( descriptor.contents[i] ) );
						} else {
							//it is a node descriptor object (can't be an array)
							ret.appendChild( createTree( descriptor.contents[i] ) );
						}
					}
				} else {
					//it is an object
					ret.appendChild( createTree( descriptor.contents ) );
				}
				break;
		}
	}
	return ret;
}</pre>
<p>The data structure can be quite sophisticated, yet simple. See the documentation above the function for more information.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.userpixel.com/1089/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scheduling an Ajax call with parameters after it is finished</title>
		<link>http://www.userpixel.com/1050</link>
		<comments>http://www.userpixel.com/1050#comments</comments>
		<pubDate>Thu, 01 Mar 2012 09:25:00 +0000</pubDate>
		<dc:creator>Alex Hanif</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.userpixel.com/?p=1050</guid>
		<description><![CDATA[Keeping a page updated using repeated Ajax calls seems to be pretty common in the projects that I&#8217;m working with. The problem is: I don&#8217;t want the client to slavishly sent Ajax calls on an interval! I want each new call to be initiated after the previous Ajax call returns. Let say there is an [...]]]></description>
			<content:encoded><![CDATA[<p>Keeping a page updated using repeated Ajax calls seems to be pretty common in the projects that I&#8217;m working with. The problem is: I don&#8217;t want the client to slavishly sent Ajax calls on an interval! I want each new call to be initiated after the previous Ajax call returns.</p>
<p>Let say there is an asynchronous Ajax function which runs a command string at the server side and returns the result to the client. It calls a callback function to process the results:</p>
<pre>function ajaxCall(commandStr,callback){
   var url=......//make a url with the command string
   jquery.get(url,function(result){
       //process the result using callback
       callback(result);
   });
}</pre>
<p>The asynchronous call (ajaxCall) may take a while to be finished (between 200ms to 30000ms!) but I want it to do the same command with an interval (1000ms) <em>after</em> the previous Ajax call finishes. I want to write a wrapper function around ajaxCall() with the following signature:</p>
<pre>function ajaxCallRepeated(interval,commandStr,callback)</pre>
<p><a href="http://jibbering.com/faq/notes/closures/#clSto">Closures</a> can be a great solution for this problem. The solution is:</p>
<pre>function ajaxCallRepeated(interval,commandStr,callback){
	//This feature uses closures in Javascript. Please read this to know why and how: http://jibbering.com/faq/notes/closures/#clSto
	function callLater(_interval,_commandString,_callback){
		var closure=(function(){
			ajaxCall(_commandString,function(out,err){
				if(_callback)_callback(out,err);
				setTimeout(closure,_interval);
			});
		});
		return closure;
	}
	//now make a closure for every call to this function
	var functRef = callLater(interval,commandString,callback);
	//the first call
	functRef();
}</pre>
<p>Since it&#8217;s not possible to send parameters to the function call of setTimer(), closures are used to pass these parameters. Inside ajaxCallRepeat(), the callLater() function uses a closures to remember the parameters. The closure sets the timer on itself after the ajaxCall is returned. Then calls the callback function that might be passed to ajaxCallRepeated() and hence callLater().</p>
<p>It&#8217;s a little bit complicated but the idea is simple: the closure is used not only to pass the parameters, but also a reference to the closure itself! It works like a charm now! <img src='http://www.userpixel.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.userpixel.com/1050/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lightweight Metro-like UI in pure CSS</title>
		<link>http://www.userpixel.com/769</link>
		<comments>http://www.userpixel.com/769#comments</comments>
		<pubDate>Sat, 25 Feb 2012 00:17:40 +0000</pubDate>
		<dc:creator>Alex Hanif</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[metro]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.userpixel.com/?p=769</guid>
		<description><![CDATA[I was playing around to create a simple and light-weight UI similar to Microsoft Metro design language. Here is the code and some snapshots: &#60;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&#62; &#60;html xmlns="http://www.w3.org/1999/xhtml"&#62; &#60;head&#62; &#60;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&#62; &#60;title&#62;Index2&#60;/title&#62; &#60;style type="text/css"&#62; .clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; } [...]]]></description>
			<content:encoded><![CDATA[<p>I was playing around to create a simple and light-weight UI similar to <a href="http://en.wikipedia.org/wiki/Metro_(design_language)">Microsoft Metro</a> design language. Here is the code and some snapshots:</p>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Index2&lt;/title&gt;
&lt;style type="text/css"&gt;
.clearfix:after {
 content: ".";
 display: block;
 height: 0;
 clear: both;
 visibility: hidden;
}
html{
 font-family:Verdana, Geneva, sans-serif;
 font-size:14px;
}
div.tiles{
 background-color:black;
 padding:50px;
}
div.tiles &gt; div{
 border-radius:2px;
 padding:10px;
 margin:5px;
 color:white;
 background-color:#666;
 display:marker;
 cursor:pointer;
 position:relative;
 float:left;
 width:25px;
 height:25px;
}
div.tiles div:hover{
 transition:background-color 1s;
 -moz-transition:background-color 1s;
 -webkit-transition:background-color 1s;
 background-color:#060;
 -moz-transform:rotate(6deg);
}
div.tiles div.icon{
 position:absolute;
 width:30px;
 height:30px;
 bottom:0px;
 left:0px;
 z-index:10;
 background-color:red;
}
&lt;!--These classes define how wide a tile should be--&gt;
div.tiles div.w1{width:25px;}
div.tiles div.w2{width:80px;}
div.tiles div.w3{width:160px;}
div.tiles div.w4{width:190px;}
&lt;!--These classes define how tall a tile should be--&gt;
div.tiles div.h1{height:25px;}
div.tiles div.h2{height:80px;}
div.tiles div.h3{height:160px;}
div.tiles div.h4{height:190px;}
&lt;/style&gt;
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="tiles clearfix"&gt;
 &lt;div class="w4 h2"&gt;mattberg@live.com &lt;div class="icon"&gt;icon&lt;/div&gt;&lt;/div&gt;
 &lt;div class="w4 h2"&gt;RSS&lt;/div&gt;
 &lt;div class="w4 h2"&gt;13&lt;/div&gt;
 &lt;div class="w2 h2"&gt;IE&lt;/div&gt;
 &lt;div class="w2 h2"&gt;Photo&lt;/div&gt;
 &lt;div class="w4 h2"&gt;Now Playing&lt;/div&gt;
 &lt;div class="w4 h2"&gt;Photo &lt;div class="icon"&gt;icon&lt;/div&gt;&lt;/div&gt;
 &lt;div class="w2 h2"&gt;Shop&lt;/div&gt;
 &lt;div class="w2 h2"&gt;SMS&lt;/div&gt;
 &lt;div class="w4 h2"&gt;Weather &lt;div class="icon"&gt;icon&lt;/div&gt;&lt;/div&gt;
 &lt;div class="w4 h2"&gt;Investment&lt;/div&gt;
 &lt;div class="w1 h1"&gt;Lilly&lt;/div&gt;
 &lt;div class="w1 h1"&gt;Lilly&lt;/div&gt;
 &lt;div class="w1 h1"&gt;Lilly&lt;/div&gt;
 &lt;div class="w1 h1"&gt;Lilly&lt;/div&gt;
 &lt;div class="w1 h1"&gt;Lilly&lt;/div&gt;
 &lt;div class="w1 h1"&gt;Lilly&lt;/div&gt;
&lt;div class="w1 h1"&gt;Lilly&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p><a href="http://www.userpixel.com/wp-content/uploads/2012/02/metro-ui-icon.jpg"><img class="aligncenter size-large wp-image-770" title="metro ui icon" src="http://www.userpixel.com/wp-content/uploads/2012/02/metro-ui-icon-535x1024.jpg" alt="" width="535" height="1024" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.userpixel.com/769/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lua function to traverse all files in a folder recursively</title>
		<link>http://www.userpixel.com/740</link>
		<comments>http://www.userpixel.com/740#comments</comments>
		<pubDate>Thu, 12 Jan 2012 12:37:17 +0000</pubDate>
		<dc:creator>Alex Hanif</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[lua]]></category>

		<guid isPermaLink="false">http://www.userpixel.com/?p=740</guid>
		<description><![CDATA[Here is a little function that uses Lua File System (lfs module) in order to traverse all the files in a folder and its sub folders (and their sub folders) recursively. DIR_SEP="\\" --should be "/" for Unix platforms (Linux and Mac) function browseFolder(root) for entity in lfs.dir(root) do if entity~="." and entity~=".." then local fullPath=root..DIR_SEP..entity [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a little function that uses Lua File System (lfs module) in order to traverse all the files in a folder and its sub folders (and their sub folders) recursively.</p>
<pre>DIR_SEP="\\" --should be "/" for Unix platforms (Linux and Mac)
function browseFolder(root)
	for entity in lfs.dir(root) do
		if entity~="." and entity~=".." then
			local fullPath=root..DIR_SEP..entity
			--print("root: "..root..", entity: "..entity..", mode: "..(lfs.attributes(fullPath,"mode")or "-")..", full path: "..fullPath)
			local mode=lfs.attributes(fullPath,"mode")
			if mode=="file" then
				--this is where the processing happens. I print the name of the file and its path but it can be any code
				print(root.." &gt; "..entity)
			elseif mode=="directory" then
				browseFolder(fullPath);
			end
		end
	end
end

--this is a sample call
browseFolder(".")
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.userpixel.com/740/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking IP address format in Lua</title>
		<link>http://www.userpixel.com/736</link>
		<comments>http://www.userpixel.com/736#comments</comments>
		<pubDate>Mon, 09 Jan 2012 16:01:41 +0000</pubDate>
		<dc:creator>Alex Hanif</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[tcpip]]></category>

		<guid isPermaLink="false">http://www.userpixel.com/?p=736</guid>
		<description><![CDATA[Here is a little function that I developed to test if a string is a valid IPv4 address (that is 4 numbers between 0 to 255 separated with &#8220;.&#8221;) ---checks if a string represents an ip address -- @return true or false function isIpAddress(ip) if not ip then return false end local a,b,c,d=ip:match("^(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)$") a=tonumber(a) b=tonumber(b) [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a little function that I developed to test if a string is a valid IPv4 address (that is 4 numbers between 0 to 255 separated with &#8220;.&#8221;)</p>
<pre>---checks if a string represents an ip address
-- @return true or false
function isIpAddress(ip)
 if not ip then return false end
 local a,b,c,d=ip:match("^(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)$")
 a=tonumber(a)
 b=tonumber(b)
 c=tonumber(c)
 d=tonumber(d)
 if not a or not b or not c or not d then return false end
 if a&lt;0 or 255&lt;a then return false end
 if b&lt;0 or 255&lt;b then return false end
 if c&lt;0 or 255&lt;c then return false end
 if d&lt;0 or 255&lt;d then return false end
 return true
end</pre>
<p>And here are some tests:</p>
<pre>assert(isIpAddress("1.1.1.1")==true)
assert(isIpAddress("255.1.1.1")==true)
assert(isIpAddress("255.255.0.0")==true)
assert(isIpAddress("0.0.0.0")==true)
assert(isIpAddress("255.255.255.255")==true)
assert(isIpAddress("0.0.0.1")==true)
assert(isIpAddress("0.0.1.1")==true)
assert(isIpAddress("0.1.1.1")==true)
assert(isIpAddress("1.2.3.4")==true)
assert(isIpAddress(nil)==false)
assert(isIpAddress("")==false)
assert(isIpAddress("1")==false)
assert(isIpAddress("1.2")==false)
assert(isIpAddress("1.2.3")==false)
assert(isIpAddress("1.2.3.4.")==false)
assert(isIpAddress(".1.2.3.4")==false)
assert(isIpAddress("0")==false)
assert(isIpAddress("-1")==false)
assert(isIpAddress("256")==false)
assert(isIpAddress("-1.2.3.4")==false)
assert(isIpAddress("1.-2.3.4")==false)
assert(isIpAddress("1.2.-3.4")==false)
assert(isIpAddress("1.2.3.-4")==false)
assert(isIpAddress("256.2.3.4")==false)
assert(isIpAddress("1.256.3.4")==false)
assert(isIpAddress("1.2.256.4")==false)
assert(isIpAddress("1.2.3.256")==false)
assert(isIpAddress("256.256.256.256")==false)
assert(isIpAddress("1000.0.0.0")==false)
assert(isIpAddress("a.b.c.d")==false)
assert(isIpAddress("a.2.c.d")==false)
assert(isIpAddress("a.b.3.d")==false)
assert(isIpAddress("a.b.c.4")==false)</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.userpixel.com/736/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What are all the valid subnet masks</title>
		<link>http://www.userpixel.com/734</link>
		<comments>http://www.userpixel.com/734#comments</comments>
		<pubDate>Mon, 09 Jan 2012 14:32:50 +0000</pubDate>
		<dc:creator>Alex Hanif</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tcpip]]></category>

		<guid isPermaLink="false">http://www.userpixel.com/?p=734</guid>
		<description><![CDATA[Here are all the valid subnet masks for IPv4: 128.0.0.0 192.0.0.0 224.0.0.0 240.0.0.0 248.0.0.0 252.0.0.0 254.0.0.0 255.0.0.0 255.128.0.0 255.192.0.0 255.224.0.0 255.240.0.0 255.248.0.0 255.252.0.0 255.254.0.0 255.255.0.0 255.255.128.0 255.255.192.0 255.255.224.0 255.255.240.0 255.255.248.0 255.255.252.0 255.255.254.0 255.255.255.0 255.255.255.128 255.255.255.192 255.255.255.224 255.255.255.240 255.255.255.248 255.255.255.252 255.255.255.254 The following Lua script was used to calculate those values: map={ ["00000000"]="0", ["10000000"]="128", ["11000000"]="192", ["11100000"]="224", [...]]]></description>
			<content:encoded><![CDATA[<p>Here are all the valid subnet masks for IPv4:</p>
<ol>
<li>128.0.0.0</li>
<li>192.0.0.0</li>
<li>224.0.0.0</li>
<li>240.0.0.0</li>
<li>248.0.0.0</li>
<li>252.0.0.0</li>
<li>254.0.0.0</li>
<li>255.0.0.0</li>
<li>255.128.0.0</li>
<li>255.192.0.0</li>
<li>255.224.0.0</li>
<li>255.240.0.0</li>
<li>255.248.0.0</li>
<li>255.252.0.0</li>
<li>255.254.0.0</li>
<li>255.255.0.0</li>
<li>255.255.128.0</li>
<li>255.255.192.0</li>
<li>255.255.224.0</li>
<li>255.255.240.0</li>
<li>255.255.248.0</li>
<li>255.255.252.0</li>
<li>255.255.254.0</li>
<li>255.255.255.0</li>
<li>255.255.255.128</li>
<li>255.255.255.192</li>
<li>255.255.255.224</li>
<li>255.255.255.240</li>
<li>255.255.255.248</li>
<li>255.255.255.252</li>
<li>255.255.255.254</li>
</ol>
<p>The following Lua script was used to calculate those values:</p>
<pre>map={
  ["00000000"]="0",
  ["10000000"]="128",
  ["11000000"]="192",
  ["11100000"]="224",
  ["11110000"]="240",
  ["11111000"]="248",
  ["11111100"]="252",
  ["11111110"]="254",
  ["11111111"]="255"
}
for i=1,31 do
  s=string.rep("1",i)..string.rep("0",32-i)
  print(map[s:sub( 1, 8)].."."..map[s:sub( 9,16)].."."..map[s:sub(17,24)].."."..map[s:sub(25,32)])
end</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.userpixel.com/734/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up Tele2 Huawei e367 3G modem on Linux (Ubuntu 11.04)</title>
		<link>http://www.userpixel.com/723</link>
		<comments>http://www.userpixel.com/723#comments</comments>
		<pubDate>Sat, 31 Dec 2011 10:21:45 +0000</pubDate>
		<dc:creator>Alex Hanif</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[3g]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[modem]]></category>
		<category><![CDATA[settings]]></category>
		<category><![CDATA[tele2]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.userpixel.com/?p=723</guid>
		<description><![CDATA[I needed to connect my Tele2 usb modem to Ubuntu and here is a good solution. It&#8217;s basically these lines that made it work: $ cd /etc/usb_modeswitch.d $ sudo tar xzf /usr/share/usb_modeswitch/configPack.tar.gz 12d1:1446 $ sudo sed -i -e 's/14ac"/14ac,1506"/' 12d1\:1446 Also it seems that the following command is required for it to work: $ sudo [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to connect my Tele2 usb modem to Ubuntu and <a href="https://bugs.launchpad.net/ubuntu/+source/usb-modeswitch-data/+bug/776959">here is a good solution</a>.</p>
<p>It&#8217;s basically these lines that made it work:</p>
<pre>$ cd /etc/usb_modeswitch.d
$ sudo tar xzf /usr/share/usb_modeswitch/configPack.tar.gz 12d1:1446
$ sudo sed -i -e 's/14ac"/14ac,1506"/' 12d1\:1446</pre>
<p>Also <a href="https://bugs.launchpad.net/ubuntu/+source/usb-modeswitch-data/+bug/776959">it seems</a> that the following command is required for it to work:</p>
<pre>$ sudo modprobe usbserial vendor=0x12d1 product=0x1506</pre>
<p>After that, you need to configure Ubuntu Network Connections (the little network icon on the top right of the screen near the speaker icon). Go to &#8220;Mobile Broadband&#8221; tab, click &#8220;Add&#8221;. Select &#8220;Sweden&#8221; as your country, &#8220;Tele2&#8243; as your operator and &#8220;Mobile Bredband&#8221; as your data plan.<br />
Then use the following configurations:</p>
<ul>
<li>PIN1: ***** (your pin code)</li>
<li>connection type: NDIS</li>
<li>access point: mobileinternet.tele2.se</li>
<li>Phone number: *99#</li>
<li>Username and password: Not used (leave empty)</li>
<li>Password Authentication Protocol: CHAP (to be safe you can select all of them)</li>
<li>IP address: Dynamic</li>
<li>DNS address : Dynamic</li>
<li>WINS address: Dynamic</li>
</ul>
<p>PS. The Linux version of the driver is really stable! I share the Internet using an Asus Eee Box and an access point to 4 other wireless devices at home. In Windows, the device used to get disconnected and sometimes it had to be removed from the USB port and inserted again (even when using the latest driver from Huawei). In Linux this hasn&#8217;t happened during the last week.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.userpixel.com/723/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comment remover script for JavaScript, HTML, XML, CSS and Lua</title>
		<link>http://www.userpixel.com/688</link>
		<comments>http://www.userpixel.com/688#comments</comments>
		<pubDate>Mon, 28 Nov 2011 15:52:14 +0000</pubDate>
		<dc:creator>Alex Hanif</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.userpixel.com/?p=688</guid>
		<description><![CDATA[The following script removes all comments (single line or multi line) from 5 types of files that I use mostly: JavaScript, HTML, XML, CSS and Lua. formats={ ["js"]={ name="JavaScript", single="//.-\n", multi="/%*.-%*/" }, ["xml"]={ name="XML", single=nil, multi="&#60;!--.---&#62;" }, ["html"]={ name="HTML", single=nil, multi="&#60;!--.---&#62;" }, ["css"]={ name="CSS", single=nil, multi="/%*.-%*/" }, ["lua"]={ name="Lua", single="--.-\n", multi="/%[%[.-%]%]/" }, } ---Gets a [...]]]></description>
			<content:encoded><![CDATA[<p>The following script removes all comments (single line or multi line) from 5 types of files that I use mostly: JavaScript, HTML, XML, CSS and Lua.</p>
<pre>formats={
	["js"]={
		name="JavaScript",
		single="//.-\n",
		multi="/%*.-%*/"
	},
	["xml"]={
		name="XML",
		single=nil,
		multi="&lt;!--.---&gt;"
	},
	["html"]={
		name="HTML",
		single=nil,
		multi="&lt;!--.---&gt;"
	},
	["css"]={
		name="CSS",
		single=nil,
		multi="/%*.-%*/"
	},
	["lua"]={
		name="Lua",
		single="--.-\n",
		multi="/%[%[.-%]%]/"
	},

}
---Gets a valid compilable javascript code and removes its comments
-- @note the javascript code should be ready to compile that is: there are no syntax
-- errors whatsoever. Otherwise the behavior of this function is unpredictable.
-- @param txt the compilable javascript code
-- @param patt the pattern table from format table. See how it's called from removeCommentsFile()
-- @return a text string the semantically behaves the same as the txt that was passed
-- to the function but doesn't have comments
function removeComments(txt,patt)
	--remove all multi-line comments
	if patt.multi then
		txt=txt:gsub(patt.multi,"\n")
	end
	--remove all single-line comments
	if patt.single then
		txt=txt:gsub(patt.single,"\n")
	end

	txt=txt:gsub("\n[%s]*","\n")  --remove all soaces at the start of lines
	txt=txt:gsub("[%s\n]*\n","\n")--remove all empty lines and all spaces at the end of lines
	txt=txt:gsub("^\n","")        --remove first empty new line
	txt=txt:gsub("\n$","")        --remove last empty new line
	return txt
end

---Opens a file and detects its extension before passing the contents to removeComments()
function removeCommentsFile(fileName)
	local f=io.open(fileName,"r")
	local txt=f:read("*a")
	local extension=fileName:match("%.(.-)$")
	if not extension then
		return nil,"Could not find extension for file '"..fileName.."'"
	end
	extension=extension:lower()
	if formats[extension] then
		print("Removing comments from '"..fileName.."' (a "..formats[extension].name.." file)")
		return removeComments(txt,formats[extension])
	else
		return nil,"This file is not recognized: '"..fileName.."'"
	end
end

for k,v in ipairs(arg) do
	print(removeCommentsFile(v))
end</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.userpixel.com/688/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

