Monday, January 04, 2010

detecting a floating point number on input in C

This could be useful code in the future. Found in comments on stackoverflow:
// convert input string to double int isfloat (const char *s) { char *ep = NULL; double f = strtod (s, &ep); if (!ep || *ep) return false; // has non-floating digits after number, if any return true; }
and
// distinguish between floats and ints int isfloat (const char *s) { char *ep = NULL; long i = strtol (s, &ep); if (!*ep) return false; // it's an int if (*ep == 'e' || *ep == 'E' || *ep == '.') return true; return false; // it not a float, but there's more stuff after it }

Sunday, November 08, 2009

User Interface gripes

A great discussion on Stack Overflow about what doesn't work in user interfaces.

Tuesday, October 06, 2009

Web page performance and CSS selectors

Interesting article on web page performance and impact of CSS selectors. The article includes some test metrics to backup the author's position and history on the question of CSS selectors impact on page performance.

Bottom line: tweaking for performance only matters with extremely large page sizes. For general to moderately complex pages there is no noticeable benefit gained with CSS selector optimization.

Also has some links in comments section which may be worth following.

Monday, October 05, 2009

Creating objects in JavaScript

Finally a decent article on the various ways that an object can be created in JavaScript with encapsulated methods. Cleanly written, concise, and easy to follow.

Tuesday, September 29, 2009

Bitwise operations

A very cool article on bit operations. Samples provided using C language, but overall the article is language agnostic. Easy to read and very informative.

Thursday, September 24, 2009

Online tools for Regular Expressions

This site is very useful and performs well. Lets you test the expresson, view matches, and replace string elements. Not pretty, but performed flawlessly.

This one looks interesting but I've found the performance a bit twitchy. Still, it lets you visually examine your expression and can probably help you find some syntax errors quickly. It's probably going to be worth re-visiting this site to see how it grows.

Sunday, September 13, 2009

Semicolons in javascript

So, after examining some Google code I found they use semicolons more for statement delimiting than termination. Last statements in a code block are not semicolon terminated, only preceding code lines are. This got me searching for the meaning of semicolon in javascript. I found many sites stating it is optional, and many sites which warn of the danger of following this practice.

Although the script interpreter will insert semicolons at the end of lines, this may break down if the script is stripped of all needless white space. Therefore a good rule of thumb seems to be to put semicolons at the end of lines to clearly indicate the end of statement. Google's practice of using semicolon as statement delimiters seems to be adequate as well.

On a related topic, i have also found some advice for properly formatting javascript code because the interpreter will insert semicolons where it thinks appropriate. This is something I was not aware of before and knowing this now will probably help debug some code down the road.

Thursday, December 25, 2008

Simple (and accurate) countdown timer in JS

Sample countdown timer:

The above example is a simple and straightforward implementation a javascript countdown timer to a future event. The script will use the computer's internal clock to calculate the time so it will be accurate no matter how long it runs (at least down to the second). The time is calculated using epoch time and the following calculation:

timeleft = targetepoch - currentepoch;

As you can see, the only requirements are that we know the target time and the current time. Current time is easy, and can be obtained using the following code:

var currentepoch = Math.floor(new Date().getTime()/1000);

The target time you either already have as input, or as in the example below, you calculate it based on the current time plus the remaining time. Here's the full body of the javascript method.

// the input time below is 1 day and 15 seconds var timeleft = 86415; // in seconds, this is the input time. var s; var m; var h; var d; var currentepoch = Math.floor(new Date().getTime()/1000); var targetepoch = currentepoch + timeleft; // this is when the timer runs out x=window.setTimeout("timerUpdate();", 1000); function timerUpdate() { currentepoch = Math.floor(new Date().getTime()/1000); timeleft = targetepoch - currentepoch; s = timeleft%60; if (s < 10) s = '0'+s; m = (timeleft-s)/60%60; if (m < 10) m = '0'+m; h = ((timeleft-s)/60 - m)/60%24; if (h < 10) h = '0'+h; d = (((timeleft-s)/60 - m)/60 - h)/24; if (d < 10) d = '0'+d; var txt = "" + d + ":" + h +":" + m +":" +s; document.getElementById("mytimer").innerHTML = txt; x=window.setTimeout("timerUpdate();", 1000); }

In the html document body I have defined a span of id "mytimer", and the javascript method displays the countdown timer as above.

I should note that this script has only been tested in Firefox, I don't know if it will work in other browsers. Also, please note the example has seconds resolution, not milliseconds (to get milliseconds, don't divide the epoch times by 1000). That's all, this script is simple, but that should now allow you to take it and improve it as you may see fit.

Monday, November 03, 2008

Adding new hard drive to Linux

Dead simple instructions for adding a new hard drive to an existing linux system are here.

I used GParted instead of fdisk and mkfs to partition and format the new drive.

Saturday, September 20, 2008

X on RADEONHD with two monitors

Technical details:

  • Linux version 2.6.24-1-686 (Debian 2.6.24-7).
  • X.org X Server version 1.4.2.
  • Video chipset is ATI Radeon HD 3650
  • I am running with the testing (Lenny) packages

The following xorg.conf file is what I am currently running with, it is sufficient for providing me with an X desktop spanning both my displays.

# xorg.conf (X.Org X Window System server configuration file) # # This file was generated by dexconf, the Debian X Configuration tool, using # values from the debconf database. # # Edit this file with caution, and see the xorg.conf manual page. # (Type "man xorg.conf" at the shell prompt.) # # This file is automatically updated on xserver-xorg package upgrades *only* # if it has not been modified since the last upgrade of the xserver-xorg # package. # # If you have edited this file but would like it to be automatically updated # again, run the following command: # sudo dpkg-reconfigure -phigh xserver-xorg #--------------------------------------------------------------------- # Section "InputDevice" Identifier "Generic Keyboard" Driver "kbd" Option "XkbRules" "xorg" Option "XkbModel" "pc104" Option "XkbLayout" "us" EndSection #--------------------------------------------------------------------- # Section "InputDevice" Identifier "Mouse0" Driver "mouse" Option "Device" "/dev/input/mice" EndSection #--------------------------------------------------------------------- # Options for the video card. # Current card is Radeon HD 3650 (RV635), running 2 displays. # # NOTE: the option "monitor-" uses the display name reported # by xrandr. In my case that's "DVI-I_1/digital" and "DVI-I_2/digital" Section "Device" Identifier "Card0" driver "radeonhd" Option "monitor-DVI-I_1/digital" "Monitor0" Option "monitor-DVI-I_2/digital" "Monitor1" EndSection #--------------------------------------------------------------------- # Monitor identifier. I'm sure I don't need the VendorName and # ModelName anymore, this is legacy from prior installs. Section "Monitor" Identifier "Monitor0" VendorName "GSM" ModelName "L206W" EndSection #--------------------------------------------------------------------- # Monitor identifier. Same as above. The option "RightOf" lets # X position the screens properly. Section "Monitor" Identifier "Monitor1" VendorName "GSM" ModelName "L206W" Option "RightOf" "Monitor0" EndSection #--------------------------------------------------------------------- # This section is considerably lighter from prior versions of X. # with the RADEONHD driver this is all that's needed. Section "Screen" Identifier "Screen0" Device "Card0" DefaultDepth 24 SubSection "Display" Depth 24 EndSubSection EndSection #--------------------------------------------------------------------- # Section "ServerLayout" Identifier "Server" Screen 0 "Screen0" InputDevice "Mouse0" "CorePointer" EndSection #--------------------------------------------------------------------- # not sure if this is still needed. In any case the current state of # the RADEONHD driver (testing) does not support DRI. (17/09/2008) # this does not seem to hurt anything. Section "dri" Mode 0666 EndSection

Configuring X on a new box was relatively easy. After the new hardware was all in place, and the latest ati driver downloaded, I backed up the original xorg.conf file and run:

dpkg-reconfigure xserver-org

This created a vanilla configuration which got the X server up and running, although the desktop was mirrored across both displays. The next step is to configure dual display to emulate one large screen. For this you will need the xrandr tool, which is part of x11-xserver-utils.

apt-get install x11-xserver-utils

After you have the tools installed, run xrandr at the command prompt, this should give you an output similar to the following:

Orthanc:/home/mike# xrandr Screen 0: minimum 320 x 200, current 3360 x 1050, maximum 3360 x 1680 DVI-I_1/digital connected 1680x1050+0+0 434mm x 270mm 1680x1050 59.9*+ 60.0 60.0 1600x1024 60.0 1400x1050 60.0 1280x1024 75.0 59.9 60.0 1440x900 60.2 60.0 1280x960 60.0 59.9 1280x800 60.0 1152x864 75.0 74.9 1280x768 60.0 1024x768 75.1 70.1 60.0 832x624 74.6 800x600 72.2 75.0 60.3 56.2 640x480 75.0 72.8 75.0 60.0 59.9 720x400 70.1 DVI-I_1/analog disconnected TV_7PIN_DIN disconnected DVI-I_2/digital connected 1680x1050+1680+0 434mm x 270mm 1680x1050 59.9*+ 60.0 60.0 1600x1024 60.0 1400x1050 60.0 1280x1024 75.0 59.9 60.0 1440x900 60.2 60.0 1280x960 60.0 59.9 1280x800 60.0 1152x864 75.0 74.9 1280x768 60.0 1024x768 75.1 70.1 60.0 832x624 74.6 800x600 72.2 75.0 60.3 56.2 640x480 75.0 72.8 75.0 60.0 59.9 720x400 70.1 DVI-I_2/analog disconnected

In my case, I have two monitors connected, DVI-I_1/digital and DVI-I_2/digital. Keep those names handy, you will need them later on. Next, we need to define both monitors in xorg.conf. This is done through the "Monitor" sections in the file. I defined "Monitor1" to be "RightOf" "Monitor0". If your configuration is different, you can reverse that. . . or swap monitor cables at the video card.

Section "Monitor" Identifier "Monitor0" VendorName "GSM" ModelName "L206W" EndSection Section "Monitor" Identifier "Monitor1" VendorName "GSM" ModelName "L206W" Option "RightOf" "Monitor0" EndSection

With the monitors defined, we attach them to the video card through the "Device" section. Notice the monitor labels are the name as is used by xrandr AND prefixed with "monitor-". This is important and took me a while to figure out.

Section "Device" Identifier "Card0" driver "radeonhd" Option "monitor-DVI-I_1/digital" "Monitor0" Option "monitor-DVI-I_2/digital" "Monitor1" EndSection

Now we define the screen through the "Screen" section, this defines the geometry. With older versions of X I had to be pretty explicit here how the screen was to be emulated. With the latest version I just need to identify the graphic device which is driving the display.

Section "Screen" Identifier "Screen0" Device "Card0" DefaultDepth 24 SubSection "Display" Depth 24 EndSubSection EndSection
Since the graphic device has two screens attached to it, and one of the screens defines where it is in relation to the other, the X server knows now to launch with the two monitors defined as one large virtual screen.

This is it, when X restarts, it will now come up with one large virtual screen. If this worked, only one of the screens should now have the login prompt.

Final notes: the RADEONHD driver is still in it's infancy; DRI is not yet supported, so it has no 3d capabilities. You can also use the graphical interface to xrandr to configure multi display after you log into X. It's quick and easy although not permanent - you will need to repeat the task when you re-start X. The tool is called "grandr" and is contained in a debian package by the same name.