Programming

Programming related entries

The C Paradox

0

Stumbled upon this…

by: Anonymous 2011-08-06 14:58

I don’t think C gets enough credit. Sure, C doesn’t love you. C isn’t about love–C is about thrills. C hangs around in the bad part of town. C knows all the gang signs. C has a motorcycle, and wears the leathers everywhere, and never wears a helmet, because that would mess up C’s punked-out hair. C likes to give cops the finger and grin and speed away. Mention that you’d like something, and C will pretend to ignore you; the next day, C will bring you one, no questions asked, and toss it to you with a you-know-you-want-me smirk that makes your heart race. Where did C get it? “It fell off a truck,” C says, putting away the boltcutters. You start to feel like C doesn’t know the meaning of “private” or “protected”: what C wants, C takes. This excites you. C knows how to get you anything but safety. C will give you anything but commitment…

 

 

In the end, you’ll leave C, not because you want something better, but because you can’t handle the intensity. C says “I’m gonna live fast, die young, and leave a good-looking corpse,” but you know that C can never die, not so long as C is still the fastest thing on the road.

That being said, I still hate C…

Javascript: Get decimal rgb color values from hex

0

Here’s a simple js class to extract rgb data from a hex color string. Only 2 methods so far but I’ll update it later if I need more functionality…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function RGBColor (color) {
	//Param is a hex string of color info
	//Common hex prefixes of '0x' and '#'
	//is accepted.
 
	//Properties
	this.color = color;
 
	//Methods
	this.getColor = function(){
		/*Return original color string*/
 
		return this.color;
	};  
 
	this.getDecimalVals = function(){
		/*Returns an object with red, green, and blue
		properties in decimal value*/
 
		var color = this.color;
 
		var rgb;
		var colorObj;
 
		//Replace hex prefixes if present
		color = color.replace("0x", "");
		color = color.replace("#", "");
 
		//Easier to visualize bitshifts in hex
		rgb = parseInt(color, 16);
 
		//Extract rgb info
		colorObj = new Object();
		colorObj.red = (rgb & (255 << 16)) >> 16;
		colorObj.green = (rgb & (255 << 8)) >> 8;
		colorObj.blue = (rgb & 255); 
 
		return colorObj;   	
	};
};

Here’s how to use it:

1
2
3
4
5
var color = new RGBColor("#4100ff");
var colorDec = color.getDecimalVals();
document.write(colorDec.red); //Writes 65
document.write(colorDec.green); //Writes 0
document.write(colorDec.blue); //Writes 255

Calculate number of days in a month using javascript

0

I haven’t updated this thing in a while so I thought I’d throw this tidbit of code in. I had to make a calendar for a project at work and therefore needed to know how many days are in any given month. Simple enough, just had to catch the leap year condition… referencing http://en.wikipedia.org/wiki/Leap_year

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Returns the number of days for a particular month. We catch leap year condition here
//if the parameter is 1 corresponding to february (0 based)
function monthLength(param){
 
	var ret;
	var d = param;
 
	switch(param.getMonth()){
		case 0:
		case 2:
		case 4:
		case 6:
		case 7:
		case 9:
		case 11:
			ret = 31;
			break;
 
		case 3:
		case 5:
		case 8:
		case 10:
			ret = 30;
			break;
 
		case 1:
			if( (d.getFullYear() % 4 == 0) && (d.getFullYear() % 100 != 0)
 || (d.getFullYear() % 400 == 0) ){
				ret = 29;
			}else{
				ret = 28;
			}
 
			break;
 
	}
 
	d = null;
 
	return ret;	
};

Note that the function parameter is a javascript date object. To add a next/previous month behavior, use something like this:

1
2
3
4
5
//Previous month
curDate = new Date(curDate.getFullYear(), curDate.getMonth() - 1);
 
//Next month
curDate = new Date(curDate.getFullYear(), curDate.getMonth() + 1);

UNIX tips: Learn 10 good UNIX usage habits

0

Good stuff!

Summary: Adopt 10 good habits that improve your UNIX® command line efficiency — and break away from bad usage patterns in the process. This article takes you step-by-step through several good, but too often neglected, techniques for command-line operations. Learn about common errors and how to overcome them, so you can learn exactly why these UNIX habits are worth picking up.

via UNIX tips: Learn 10 good UNIX usage habits.

Firefox 3.6 breaks Flex’s doubleClick event

6

I’m surprised that no one’s mentioned this yet as I couldn’t find anything online. This morning I noticed that I couldn’t double click on a list component that is part of a work project. I had recently done some changes to the app so I initally panicked thinking I had broken something. After noticing that everything still worked perfection in Safari, I remembered that I had just upgraded to the latest firefox release 3 days ago (3.6) and that perhaps one of the plugins were not working correctly. I disabled all my plugins but unfortunately it had no effect, which leaves FF 3.6 itself as the culprit. I asked Adrian to test it using 3.5.7 and sure enough it worked as expected.

Try it

This should affect all flex applications that are being run with Firefox 3.6 Seems to be OS dependent and only affecting the OSX folks. You can test it out yourself here: Double Click button @ FlexExamples (Scroll down a bit to find the app)

My output using Safari:

[13:02:08 GMT-0800] click
[13:02:08 GMT-0800] doubleClick
[13:02:08 GMT-0800] click
[13:02:08 GMT-0800] doubleClick
[13:02:09 GMT-0800] click
[13:02:09 GMT-0800] doubleClick

My output using Firefox 3.6:

[13:02:43 GMT-0800] click
[13:02:43 GMT-0800] click
[13:02:44 GMT-0800] click
[13:02:44 GMT-0800] click
[13:02:44 GMT-0800] click
[13:02:44 GMT-0800] click

I’m not sure if there’s anything we can do. I even tried using the latest beta flash player (10.1) but it didn’t help. I also started a thread over at actionscript.org, maybe someone knowledgeable can chime in there.

Hopefully Mozilla/Adobe can fix this issue soon.

Time manipulation in AS3

0

A piece of my current project at work needs to convert numbers back into time. Basically a 24 hour day is split into 15 minute divisions starting from 0 corresponding to 12:00 AM and 95 corresponding to 11:45 PM. Given let’s say “52″, I need to come up with nicely formatted string of the  time (1:00 PM). There’s probably an easier way to do this but the following code works quite nicely:

//Returns a nicely formatted time string given a time division and the value
private function getTimeStringFromMinutes(division:int, value:String):String{
 
	//Start out with a zero'ed out date object holding 12:00 AM
	var d:Date = new Date(0,0,1,0,0);
 
	//Add the new minutes
	d.setMinutes(d.getMinutes() + (Number(value)*division));
 
	var ret:String = "";
	var isAM:Boolean = Boolean(d.getHours() < 12);
 
	//Format hours
	if(d.getHours() == 0){
		ret += "12";
	}else if(d.getHours() <= 12){
		ret += d.getHours().toString();
	}else{
		ret += String(d.getHours() - 12);
	}
 
	ret += ":";
 
	//Pad minutes with 0's if necessary
	ret += (d.getMinutes() < 10) ? ("0" + d.getMinutes().toString()) : d.getMinutes().toString();
 
	//Add am pm
	ret+= isAM ? " AM" : " PM";
 
	return ret;
}

Flex: Using the RichTextEditor’s ToolBar class as a flowlayout container

0

So I needed to have a container with a flowlayout implementation for its children and came upon Flexlib’s FlowBox class, which was exactly what I needed. Unfortunately the implementation has its own set of issues, the major one being that the height of the FlowBox grows as you add items into it. Furthermore, whenever the row wraps it does not respect the padding information defined in the css and will just stick the item all the way against the border. Since I wasn’t really in the mood to go through the code to fix it, I did some more googling and turns out the ToolBar class also does the same thing.

Perfect! Well… almost. When I started using it, it certainly does what I wanted it to but I was unable to access the methods through code hinting on Flex Builder. According to a post by Tianzhen Lin from the link above, the ToolBar class is marked as [ExcludeClass] and is thus excluded from the code hinting (excerpt from sdks/3.x/frameworks/projects/framework/src/mx/controls/richTextEditorClasses/ToolBar.as):

package mx.controls.richTextEditorClasses
{
 
import mx.controls.VRule;
import mx.core.Container;
import mx.core.EdgeMetrics;
import mx.core.IUIComponent;
 
//--------------------------------------
//  Styles
//--------------------------------------
 
/**
 *  Number of pixels between children in the horizontal direction.
 *  The default value is 8.
 */
[Style(name="horizontalGap", type="Number", format="Length", inherit="no")]
 
/**
 *  Number of pixels between children in the vertical direction.
 *  The default value is 8.
 */
[Style(name="verticalGap", type="Number", format="Length", inherit="no")]
 
//--------------------------------------
//  Other metadata
//--------------------------------------
 
[ExcludeClass]

The solution is just to subclass it as something else. Create a new .as file like so:

package com.gp.controls
{
	//Get around the ExcludeClass by reimplementation
	import mx.controls.richTextEditorClasses.ToolBar;
 
	public class CustomFlowBox extends ToolBar
	{
		public function CustomFlowBox()
		{
			super();
		}
 
	}
}

and use that instead.

Configuring outgoing mail + google apps

2

After I had installed WordPress on my fresh off the oven Linode server, I noticed that PHP wasn’t able to send outgoing emails. Makes sense since I haven’t configured sendmail on the operating system (Ubuntu 9.10 Karmic) which is needed by PHP’s mail() function. I had also configured google apps for my domain, which means google is handling my incoming mail:

dig glasspants.com mx
 
; <<>> DiG 9.6.1-P1 <<>> glasspants.com mx
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 35070
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 4, ADDITIONAL: 0
 
;; QUESTION SECTION:
;glasspants.com.			IN	MX
 
;; ANSWER SECTION:
glasspants.com.		82472	IN	MX	5 ALT1.ASPMX.L.GOOGLE.com.
glasspants.com.		82472	IN	MX	5 ALT2.ASPMX.L.GOOGLE.com.
glasspants.com.		82472	IN	MX	10 ASPMX2.GOOGLEMAIL.com.
glasspants.com.		82472	IN	MX	10 ASPMX3.GOOGLEMAIL.com.
glasspants.com.		82472	IN	MX	1 ASPMX.L.GOOGLE.com.

We need an MTA to handle outgoing mail and from what I can gather from the general consensus of the internet, Postfix is the best and easiest solution for what I need to accomplish. I spent some time figuring out how to connect Postfix with my Google Apps mail service but turns out I was overlooking something important (and obvious): Google Apps is only responsible for incoming mail — there’s no need to tie Postfix to Google’s smtp servers. (Though entirely possible and you need to muck with certificates and the postfix configuration).

Here’s how to do it:

#Install postfix:
sudo apt-get install postfix
 
#The postfix package configuration setup should appear (Blue window).
#If you don't see it after the install, run the following command:
sudo dpkg-reconfigure postfix
  1. Select ‘Internet Site’ and tab to ‘OK’, press enter
  2. Type your server’s FQDN, mine’s pythagoras.glasspants.com. Tab to ‘OK’, press enter
  3. Leave root mail empty. Tab to ‘OK’, press enter
  4. Important!! – on this step remove yourdomain.com from the line of domains postfix will consider as a final destination. I removed glasspants.com to tell Postfix not to be responsible for emails destined for @glasspants.com. If you leave this in, it will try to send it locally.
  5. Leave the rest as their default values

Now we need to edit /etc/postfix/main.cf so Postfix will only send email from localhost. I’m only doing this because I don’t want my server to be used as an smtp relay from the outside. You don’t have to do this if your implementation differs from mine.

sudo nano /etc/postfix/main.cf

Find the line with ‘inet_interfaces=all’ and change it to ‘inet_interfaces=loopback-only’. Save, exit the text editor, and reload postfix:

sudo /etc/init.d/postfix reload

Now PHP should be able to send out emails through Postfix and all incoming emails will still be handled by Google Apps.

Go to Top