Iterating using a for loop
Here is a neat tidbit I picked up, Iterating through a linked list with a for loop. I saw it as voodoo, but really neat. Let’s take a bit of a look.
Given the situation where linkeditem is a basic object with a forward pointer to the next item in the list, and an integer value, we can use a for loop to process the list with less code then the respective while loop.
Given the following setup:
linkeditem a(0);
linkeditem b(1);
linkeditem c(2);
linkeditem d(3);
linkeditem e(4);
and the given linked list pointers setup as follows:
a.forward = &b;
b.forward = &c;
c.forward = &d;
d.forward = &e;
and our function-level variables:
linkeditem * item = &a;
int current;
Here is how one would first think to iterate the list:
while ( item != NULL ) {
current = item->value;
item = item->getNext();
}
We can also loop through the items as follows, just by using a for loop. Why you ask? I ask why not.
for ( ; item != NULL; item = item->getNext() ) {
current = item->value;
}
It’s magic!
Filed under Tips and Tricks | Tags: c | Comment (0)PHP MVC Framework
PHP is just great. I know this, the world knows this, and I don’t just say it to attract all those JavaEE folks, or even to rouse the Ruby fanboys. But the PHP code of late has me irked a bit. The issue is, as I see it, under utilizing Object Oriented design, and the MVC layout. This is my opinion, sure, but having either one massive, procedural PHP file, or ten thousand ill-designed pages, haphazardly including files in the directory is just hell to maintain. What we ( as a loving PHP using community ) need is Structure, design and forethought. Web applications are becoming larger, and more CPU heavy. PHP / Web Development is no longer a task of the graphic designer, but something that requires a solid understanding of how to code right.
My solution? I thought you would never ask.
Here is what I have came up with. It’s a full MVC scheme for PHP, and is not even close to done. I will be sure to F/OSS the end result, but this is by no means the be-all end-all solution for PHP. There is still room for everyone, a MVC framework is not the answer when it comes to small, light and embedded applications, or the quick, two minute project. This is aimed for the heavy hitting web application, with a code complexity above n ( where n >= the airspeed of an unladen swallow ).
Here is the basic folder layout:
model
Model contains all objects that are used within the scope of this project, only files that declare classes should be in here. This includes abstracted interfaces to SQL, so that one can replace the SQL class with their their own wrapper ( allowing one to switch databases by replacing one class )
view
View contains all code that will output data to the user’s screen. ( well, not entirely true, but I will get to caveats later )
conf
Conf contains all the files that are to be included by a model, this lets you move the web app, and reconfigure without running through lines of code, replacing every reference to $MYSQL_HOSTNAME.
css
CSS should contain, well, CSS of course. That was easy.
imgs
IMGS will contain any images used by the web app, again, not very hard.
libs
Libs will contain all static libraries used in the web app, anything that you include or link, that does not declare a class. Most Javascript goes here, some PHP as well.
This is all well and good, but how do we leverage this framework?
Here is an example of a controller file.
session_start();
$app_root = dirname( __FILE__ ) . "/";
$controller = basename( __FILE__ );
$controller_name = htmlentities( preg_replace('/\.[^.]*$/', '', $controller ) );
$p = htmlentities( $_GET['p'] );
switch ( $p ) {
case "about":
$action = "about";
break;
default:
$action = "default";
break;
}
include( $app_root . "view/" . $controller_name . "-" . $action . ".php" );
include( $app_root . "view/template.php" );
This is a fancy way of linking the GET value P to a file in the view. If P is declared, and matches a case statement, it includes the controller name ( the name of the file, in this case we will conceded that this file is index.php for the sake of clarity, so in this case the controller name is ‘index’ ), a dash, then the action name ( the $action variable declared by the case switch. ) Given the case of index.php?p=about the included file will be view/index-about.php. After that is included, three variables may or may not be set. $title, $content, and $script.
$title contains information about what the title of the page should be. This can be used in more then one place, however it is usually the variable set to the <title> tag.
$content contains the main meat of the page. This may or may not contain XHTML ( I don’t like HTML muchly ) and will be inserted into the template.
$script contains any javascript that is unique to the page, and should be included in the head.
After these three variables are set, we include view/template.php, that in turn includes head.php, calls echo $content; and includes the foot.php
At this point, we have rendered the page, and can exit the controller.
Whew. That seamed like a lot of work, and it was. So, Why do all this work? I can hear all of you hardened PHP scripters from years past yelling at me from here.
Well, because now when you add a page, you create one file, add two or three variables, and add three lines to a controller. Want to change the design? No problem, just replace template, or head.php / foot.php new model? Sure! Another controller? Why, that’s just fine.
Try and, and you will love it.
Filed under Life, the Universe and Everything | Tags: mvc, php | Comments (8)Making a bootable USB drive
In this post, I will go over how to set up a USB disk in order to make it bootable. This is all pretty scary at first glance, but if you think before you do, and really examine what is going on here, it will be a bit more simple.
The only catch here is that the USB disk will come out as the same machine arch as the host box ( uname -m )
sdc should be replaced with the drive target of choice, this will be the correct drive if the USB key is the last drive plugged in, with two other internal drives, or one other flash drive. Please ( PLEASE ) check this before you begin, I don’t want to see anyone out there hose their Hard Drive!
First, we need to setup the device. Again, this is providing that sdc is your device.
This will wipe any data that is on the dive right now. First unmount the drive:
sudo umount `df | grep /dev/sdc | awk '{print $1}'`
If /dev/sdc is not mounted ( or any partition therein ) this will bomb with anger, but don’t worry. It won’t hurt your box. Best case is it runs without any output, and succeeds.
Then you will need to partition out the drive, use cfdisk to create a single partition on sdc, for use with the new live OS.
sudo cfdisk /dev/sdc
or, you can do it with fdisk if you are one of my more elite readers.
OK, so now that we have set up the partition table, it’s time to set up the device as ext3.
mkfs.ext3 /dev/sdc1
Great! So we have a usable flash drive again! ( Was it really that hard? )
OK, so now we need to mount the drive, and set the file system up.
Let’s first set up a folder to mount the flash drive. If you are like me and have one folder you always use ( and it’s not flash ) feel free to adjust this as necessary.
sudo mkdir /mnt/flash
OK, so now let’s actually mount the drive.
sudo mount /dev/sdc1 /mnt/flash
And a Kernel Interface ( proc ), along with the current Devices ( dev ).
Let’s create the correct folders, and mount filesystems.
sudo mkdir /mnt/flash/proc
sudo mkdir /mnt/flash/dev
sudo mount -t proc none /mnt/flash/proc
sudo mount -o bind /dev /mnt/flash/dev
OK, Awesome. Let’s check out some basic information about debootstrap. Debootstrap creates a basic debian install by downloading bleeding edge packages from the repo given. The basic syntax is debootstrap distro /place/to/point/
Debootstrap is not installed by default, so if you have not already done so, please install debootstrap. As we are using debootstrap, I figure you are on a debian system, so the natural command is
sudo apt-get install debootstrap
Debootstrap will create a fully chroot’able environment to use. Let’s get to it.
sudo debootstrap jaunty /mnt/flash/
Wahoo! Awesome. So we have a nice environment to use, lets move into it and set this guy up.
sudo chroot /mnt/flash
Let’s install GRUB. For some reason ( Please, someone comment about this, I would love a better way to do this ) files are not included. We will pull ‘em from the host box.
Well, let’s get to it.
sudo apt-get install grub
Then let’s exit the chroot, to copy in critical files.
exit
Let’s copy out the GRUB boot information / stage files.
sudo cp /boot/grub /mnt/flash/boot -r
Now that we have the boot files in the chroot, let’s go back and add finish the setup in the drive.
sudo chroot /mnt/flash
OK, first thing, lets set up the fabled device.map. This file scares small children, torments kids and frightens adults to tears when not setup right. This single file has accounted for more headache on my part then any other system config file. Let’s just set it up.
Vim is not installed by default, so if you would like to use vim, then install it with a simple apt-get. For the sake of simplicity, I will reference vi, but again, feel free to break from this guide and use vim.
vi /boot/grub/device.map
OK. Clear out this file, and insert this.
(hd0) /dev/sda
Or, if you are lazy, you can do this:
echo "(hd0) /dev/sda" > /boot/grub/device.map
So, now that we have the device map set up, let’s install the Linux image. This is, again, for Ubuntu Linux with a vanilla kernel. Feel free to change to the -rt kernel, PAE kernel, or roll your own, just deviate as necessary to make it work. If you get something cool working, feel free to email me, or throw a comment letting us all know!
apt-get install linux-image-generic
OK Let’s setup the menu.lst. First we need to remove whatever is in there. Remember, only do this in a chroot! Don’t hose your host box!
echo "" > /boot/grub/menu.lst
Let’s open up your menu configuration.
vi /boot/grub/menu.lst
This is an example entry that I used to boot, again, if your setup has been different feel free to change, and apply salt to flavor.
title Flash Drive
kernel /vmlinuz root=/dev/sda1 ro quiet
initrd /initrd.img
quiet
Awesome. We are almost there. Let’s get GRUB all setup on this device block.
grub
OK, Now that we are in the GRUB shell, let’s use it. I have pasted both the command, and the response. I will be sure to separate them.
grub> find /boot/grub/stage1
And, the resultant spew is:
find /boot/grub/stage1
(hd0,4)
(hd2,0)
OK, now we need to set root to our drive. hd2,0 is my drive, and usually it is safe to go with the highest number.
grub> root (hd2,0)
And our spew:
root (hd2,0)
Next, we need to setup the drive to ensure GRUB will work.
grub> setup (hd2)
And the spew:
setup (hd2)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/e2fs_stage1_5" exists... yes
Running "embed /boot/grub/e2fs_stage1_5 (hd2)"... 16 sectors are embedded.
succeeded
Running "install /boot/grub/stage1 (hd2) (hd2)1+16 p (hd2,0)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded
Done.
Whoh, that was a lot. OK, well all is well, looks great. Let’s get outa here.
grub> quit
Let’s also get out of the chroot.
exit
Lastly, let’s unmount all of our devices.
sudo umount /mnt/flash/proc
sudo umount /mnt/flash/dev
sudo umount /mnt/flash/
There ya’ have it. A bootable flash drive, debian based, and all around nifty start. Remember, this is just a base, feel free to add on GNOME etc, make it your own
I’d like to thanks drs305 for reviewing this, catching a bunch of stuff I overlooked. Same goes to Bodsda, Thanks, guys!
Filed under Tips and Tricks, Wisdom on Ubuntu | Tags: boot, debian, flash, grub, usb key, Wisdom on Ubuntu | Comments (7)