To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-09-2003, 12:50 AM   #1
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
Source tarball grabber

this is an untested CLI script designed to grab tarballs, rpms, and zips (.tar.(gz|bz2),.tgz,.rpm,.zip, other suggestions welcome) from one dir and copies them into another. this may sound stupid, but it is rather cool because it includes so much error checking. this is my first CLI script, and there may be problems with my version of read(). i havent tested or commented it yet because i just hacked this up in half an hour at a friends house.

it requires the following to be at /usr/local/lib/php/common.shell.lib.php:
PHP Code:
<?php
define
('N',"\n"); //newline so we dont have to go into double-quotes
define('TARBALL_DIR','/winxp'); //this is where your sources are kept
define('DEST_DIR','/usr/local/src/'); //this is where you want things to go
$sdir = TARBALL_DIR; //set defaults
$ddir = DEST_DIR;
function
read($length=255)
{
    
/*****************************\
    * Grabs from stdin up to      *
    * $length, \n, or EOF.        *
    \*****************************/
    
if (!is_int($length) || !$length) //if length is invalid
    
{
        
$length = 255; //fall back to default
    
}
    
$f = STDIN; //stdin link
    
if (!$f)
    {
       die(
'Error opening STDIN. Dying.');
    }
    
$data = fgets($f,1024); //first, get up to 1K of data
    
$data = str_split($data); //PHP5-only function same as preg_split with empty delimeter
    
$outdata = ''; //what we return
    
for ($i = 1; $i < $length; $i++)
    {
        
$outdata .= str_replace(N, '', $data[$i-1]); //no newlines
    
}
    return
str_replace('\n', N, $outdata); // unless you REALLY want them...
} //end read()
function user_abort($message = 'User aborted', $allow_abort = true)
{
    
/*****************************\
    * Echo a dying message, but   *
    * give the user a choice      *
    * (disableable)               *
    \*****************************/
    
if ($allow_abort)
    {
        echo
'Die?';
        if (
strtolower(read(1)) == 'y')
        {
            die(
$message.N);
        }
        else
        {
            echo
'Continuing script...'.N;
            return
0;
        }
    }
    else
    {
        echo
'Dying...'.N.N;
        die(
$message.N);
    }
}
?>
put the main script inside your PATH (eg /usr/local/bin) and you're set to go!

edit: added comments
Attached Files
File Type: txt gsrc.txt (3.7 KB, 83 views)
__________________
there's no place i can be, since i found serenity.

Last edited by Moonglobe; 11-09-2003 at 04:24 PM.
Moonglobe is offline   Reply With Quote
Old 11-09-2003, 01:08 AM   #2
LordShryku
kung foo code monkey
 
LordShryku's Avatar
 
Join Date: Aug 2002
Location: Occupational Hypnotherapy
Posts: 7,473
You forgot .deb's
LordShryku is offline   Reply With Quote
Old 11-09-2003, 01:29 AM   #3
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
done
Attached Files
File Type: txt gsrc.txt (3.7 KB, 79 views)
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 11-09-2003, 07:53 PM   #4
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
ok here's a third version, which has now been tested rather thoroughly

on thing that i thought was cool was the idea of the whole thing being a loop..... but then im sure that's how most good programms work. it was new to me, though, and was fun to use. finally something i once read about the ability to use stuff like 'continue 2;' came in handy


/usr/local/lib/php/common.shell.lib.php:
PHP Code:
<?php
define
('N',"\n"); //newline so we dont have to go into double-quotes
define('TARBALL_DIR','/winxp'); //this is where your sources are kept
define('DEST_DIR','/usr/local/src/'); //this is where you want things to go
$sdir = TARBALL_DIR; //set defaults
$ddir = DEST_DIR;
function
read($length=255)
{
    
/*****************************\
    * Grabs from stdin up to      *
    * $length, \n, or EOF.        *
    \*****************************/

    
$f = fopen('/dev/stdin','r'); //stdin link
    
if (!$f)
    {
       die(
'Error opening STDIN. Dying.');
    }
    
$data = fgets($f,255); //first, get up to .25K of data
    
$outdata = str_replace(N, '', $data);
    return
$outdata;
}
//end read()
function user_abort($message = 'User aborted', $allow_abort = true)
{
    
/*****************************\
    * Echo a dying message, but   *
    * give the user a choice      *
    * (disableable)               *
    \*****************************/
    
if ($allow_abort)
    {
        echo
'Die? ';
        if (
strtolower(substr(read(),0,1) == 'y'))
        {
            die(
$message.N);
        }
        else
        {
            echo
'Continuing script...'.N;
            return
0;
        }
    }
    else
    {
        echo
'Dying...'.N.N;
        die(
$message.N);
    }
}
?>
/usr/local/bin/gsrc:
Attached Files
File Type: txt gsrc.txt (4.9 KB, 72 views)
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 11-10-2003, 01:05 PM   #5
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
ok, there's now modification time checking, and checking of the source directory before the copy (not only on 'c' in the main switch). a bunch of notification bugs fixed.

btw, i'd appreciate some more feedback, perhaps by telling me how stupid this is?
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 11-10-2003, 01:26 PM   #6
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
oops forgot to attach it
Attached Files
File Type: txt gsrc.txt (7.8 KB, 89 views)
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 09:20 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.