Archive for the ‘PHP’ Category

PHP – Nested Tuple

Friday, September 4th, 2009

Here’s a quick nested tuple [list of head and tail, where tail is the list minus the head]. I wrote this for a current project I’m working on. It works with any parameters except one array. If you only pass one array, it will convert your one array into a list of Tuples.

 * Tuple is a list of variables. Really it is a head and a tail, where the tail
 * is also a Tuple. (Tuple(n)=head + Tuple(n-1))

class Tuple
    public $head;
    public $tail;
    public function __construct($args = null)
        if(! (is_array($args) && func_num_args() == 1))
            // get all the arguments and package them as an array
            $args = func_get_args();
        // take the first element and use that as the head of the list
        $this->head = array_shift($args);
        // if there are still args left, make a new list and use that as tail
            // pass the remainder
            $this->tail = new Tuple($args);


Here’s some usage and output:

$list = new Tuple(1, 2, 3, 4);

That prints out:

Tuple Object
    [head] => 1
    [tail] => Tuple Object
            [head] => 2
            [tail] => Tuple Object
                    [head] => 3
                    [tail] => Tuple Object
                            [head] => 4
                            [tail] =>




I updated my gcc formatting php script.

Tuesday, March 24th, 2009

A while ago I wrote a script that formats gcc output to link errors back into TM. I updated that script with support for warnings and added some cool looking construction theme colors for the errors and warnings. Have a look see.

Formatting gcc and g++ output for TextMate.

Formatting gcc, g++ Output Using PHP In TextMate

Saturday, September 20th, 2008

While at ZendCon08 I saw a lot of people sharing the same love for TextMate as myself. I fricken love TextMate. Some of my friends laughed at me, saying “Bah, TM’s not an IDE, why do you want to write shell scripts to try to make it one?” And I’d be like, “I’m an ActionScript developer, there’s no point in using a sledge hammer to pound in nails.” This makes sense because my friends use Eclipse. I tried Eclipse but it’s so huge and the plugins are huge and the interface is cluttered and theming syntax-coloring is a pain. So when I got home from ZendCon, with my renewed love for PHP, and my continual quest to eventually be a good C programmer, I decided to write a gcc parser script that linked back into TextMate to go directly to my errors. I use a dark syntax theme, a slightly modded Amy, and the parser reflects that. Check out a picture of these gcc errors:

Picture of colored gcc output.

Picture of colored gcc output.

To do this you’ll need PHP installed on your machine. After that, the first step is to create a new command in TextMate’s C Bundle. Call it “Build, Format With PHP,” or anything else. Set it to save all files in the project when run [from the pull down at the top] now enter this into the text area:

export FILE=`basename ${TM_FILEPATH}`
~/Library/Application\ Support/TextMate/Support/bin/

Next select Output as “Show as HTML” so we can see our output. Now create a file called “” in the folder ~/Library/Application Support/TextMate/Support/bin/
Once you’ve created the file, use TM or vim to fill it with this:

make &> ${FILE}.mkout
php -f ~/Library/Application\ Support/TextMate/Support/bin/gcc_format.php ${FILE} ${DIR}

This bash script executes the make on the makefile and directs the standard and error output to the same file. Then the script calls php to parse and display the file. So now create a file named “gcc_format.php” in the same folder as and fill it with the meat of our parser:


    // get stdout and stderr then delete temp file
    $dir = $argv[2];
    $commandFile = $argv[1] . '.mkout';
    $commands = file_get_contents($commandFile);
    // break lines up by line ending
    $lines = explode("\n", $commands);
    // find errors and link them to pages and line number
    $errors = 0;
    for ($i=0; $i < count($lines); $i++)
        $fnpos = strpos($lines[$i], ':');
        $filename = substr($lines[$i], 0, $fnpos);
        $lnpos = strpos($lines[$i], ': error:');
        $linestart = $lnpos - $fnpos - 1;
        if($linestart < 0) continue;
        $line = substr($lines[$i], $fnpos+1, $lnpos - $fnpos - 1);
        $link = "txmt://open/?url=file://$dir/$filename&line=$line&column=1";
        $lines[$i] = substr_replace( $lines[$i], '</a>', $lnpos, 0);
        $lines[$i] = "<a href=\"$link\">" . $lines[$i];
    $output = implode("<br />\n", $lines);
    echo '
        <style type="text/css">
        body    {background:#000000; color:#999999;}
        #comment{width:100%; background: #230021; color:#6060BF;}
        #fail   {width:100%; background:#3E0018; color:#9918B8;}
        #succ   {width:100%; background:#230021; color:#B0FFF0;}
        dslsh   {color:#A96AA9;}
        a:link  {color:#008080;}
        a:hover {color:#80A0FF;}
            <div id="comment">
                <dslsh>//</dslsh> '
. $argv[1] .'\'s build results:
    echo $output . '<br />';
    if($errors > 0)
        echo '<div id="fail">Build failed with '. $errors .' errors.</div>';
        echo '<div id="succ">Build Succeeded!</div>';

This example assumes that you are working in a directory with a makefile, but I’m sure it can be easily modded to work with any build script. The colors reflect my favorite theme, but they should also be easily modded with lines 32-38 of the php script. Happy coding, peoples!

Follow me on GitHub
Follow me on Google+
Follow me on Twitter