472,983 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

really weird file-linereading problem

Dear all,

sorry, i know this code is far little too long to debug here, but there is
really annoying logical error. If someone debugs this, I really offer warm
virtual handshake.

What this code SHOULD do:
- read new (=updated) licensetext from file $license_path
then
- read and modify recursively all files from $current_dir, replacing old
licensetexts and licenseplaceholder text EITHER with new licentext OR with
licenseplaceholder

In other words, this is meant to make the updating of license text easy,
because you can put text like
<?php /* MY_APP_NAME AUTO-LICENCE-PLACEHOLDER /*>
to your files and make the replace operation with 1 command for all files.

PROBLEMS/WEIRD BEHAVIOURS:
- one extra linefeed seems to appear after last line in some operations. I
have tried to handle this, but still it comes.
- really weird: the code should (for debugging purpose) print the lines of
ORIGINAL file before the operation, yet it seems to print the lines of the
rewritten file. This is really really weird...'

Thanks very mych if you can help!!

=================================
<?php

$x = new license_compressor("TARU_LICENSE_NOTE","../tarulicense.txt");
$x->dance(1,"files");
//$x->dance(0,"files");
//$x->dance(1,"files");
//$x->dance(0,"files");
error_reporting(E_ALL);
class license_compressor
{
var $version = "License compressor 1.0";
var $linestop ="\r\n";

var $ok = 1;
var $msgs = array();
var $license_filepath;
var $license = "";
var $id = "";

var $php_begin = "";
var $php_end = "";
var $php_comp = "";
var $php_supp = "";

var $html_begin = "";
var $html_end = "";
var $html_comp = "";
var $html_supp = "";

var $gen_begin = "";
var $gen_end = "";
var $gen_comp = "";
var $gen_supp = "";

var $php_str = "";
var $html_str = "";
var $gen_str = "";

//
================================================== ==========================
function license_compressor($id, $license_filepath)
{
$this->msgs[] = $this->version;
$this->msgs[] = "============================================" ;
if(!is_string($id) || !strlen($id))
{$this->ok = 0; $this->msgs[]= "License dentifier is not a valid
string."; }
else
{ $this->id = $id; $this->msgs[]= "License dentifier =
".htmlspecialchars($id); }

if(!($this->license = file_get_contents($license_filepath)))
{ $this->ok = 0; $this->msgs[]= "License filepath is not a valid
path."; }
else
{
$this->license_filepath = $license_filepath;
$this->msgs[]= "License filepath = ".htmlspecialchars($license_filepath);
}

$this->php_begin = "<?php /* ".$this->id." AUTO-LICENSE-BEGIN";
$this->php_end = $this->id." AUTO-LICENSE-END */ ?>";
$this->php_comp = "<?php /* ".$this->id." AUTO-LICENSE-PLACEHOLDER */
?>";
$this->php_supp = $this->php_begin.$this->linestop.$this->license.
$this->linestop.$this->php_end;

$this->html_begin = "<!-- ".$this->id." AUTO-LICENSE-BEGIN";
$this->html_end = $this->id." AUTO-LICENSE-END -->";
$this->html_comp = "<!-- ".$this->id." AUTO-LICENSE-PLACEHOLDER -->";
$this->html_supp = $this->html_begin.$this->linestop.$this->license.
$this->linestop.$this->html_end;

$this->gen_begin = "/* ".$this->id." AUTO-LICENSE-BEGIN";
$this->gen_end = $this->id." AUTO-LICENSE-END */";
$this->gen_comp = "/* ".$this->id." AUTO-LICENSE-PLACEHOLDER */";
$this->gen_supp = $this->gen_begin.$this->linestop.$this->license.
$this->linestop.$this->gen_end;
}
//
================================================== ==========================
function dance($suppress, $current_dir)
{

if(!is_dir($current_dir))
{$this->ok = 0; $this->msgs[]= "Current directory path is not a valid
directory path."; }
else
{
$this->msgs[]= "Current directory = ".htmlspecialchars($current_dir);
}
$this->show_msgs(); $this->msgs = array();
if(!$this->ok) { $this->msgs[]= "Execution terminated.";
$this->show_msgs(); $this->msgs = array(); return;}

$this->msgs[] = $suppress?"Mode = suppress":"Mode = compress";
if($suppress)
{ $this->php_str = $this->php_supp; $this->html_str = $this->html_supp;
$this->gen_str = $this->gen_supp;}
else
{ $this->php_str = $this->php_comp; $this->html_str = $this->html_comp;
$this->gen_str = $this->gen_comp;}

$this->do_recursion($current_dir);
$this->show_msgs();
$this->msgs = array();
return;
}
//
================================================== ==========================
function do_recursion($current_dir)
{
if (($dir_handle = @opendir($current_dir)) !== false)
{
$this->msgs[] = "Opening directory
'".htmlspecialchars($current_dir)."'.";
}
else
{
$this->msgs[] = "Opening of directory '".
htmlspecialchars($current_dir)."' failed.";
return;
}

$entries = array();
while (false !== ($dir_entry = readdir($dir_handle)))
{
$goodentry =$current_dir."/".$dir_entry;
if(in_array($goodentry,$entries)) continue;
else $entries[]= $goodentry;
if(is_file($goodentry))
{
$this->msgs[] = "Found file '".$goodentry."'.";
$arr = file($goodentry);
$lines = count($arr);
$this->msgs[]= "Rows in $goodentry: ".$lines;
$new_content ="";
$copyflag = 1;
$expected = false;
$error = 0;
$changes = 0;
foreach($arr as $line_nr =$line)
{
if($error) {continue 2;}

$stop = ($line_nr == $lines-1)?'':$this->linestop;
if($stop==='') $stopnote ="-"; else $stopnote ="r&n";
$line = trim($line);
$this->msgs[]= "Line nr:". ($line_nr+1).
" | Copyflag: $copyflag | Line: ".
htmlspecialchars($line)." | Stop: $stopnote";
if($line === $this->php_comp && $copyflag)
{ $new_content .= $this->php_str.$stop; $changes = 1;}
elseif($line === $this->html_comp && $copyflag)
{ $new_content .= $this->html_str.$stop; $changes = 1;}
elseif($line === $this->gen_comp && $copyflag)
{ $new_content .= $this->gen_str.$stop; $changes = 1;}

elseif($line === $this->php_begin && $copyflag)
{ $new_content .= $this->php_str.$stop; $copyflag = 0; $expected =
"PHPEND";
$changes = 1;}
elseif($line === $this->html_begin && $copyflag)
{ $new_content .= $this->html_str.$stop; $copyflag = 0; $expected =
"HTMLEND";
$changes = 1;}
elseif($line === $this->gen_begin && $copyflag)
{ $new_content .= $this->gen_str.$stop; $copyflag = 0; $expected =
"GENEND";
$changes = 1;}

elseif($line === $this->php_end)
{
if(!$copyflag && $expected === "PHPEND")
{$copyflag = 1; $expected = false;}
else
{
$this->msgs[]="File '"
.htmlspecialchars($goodentry)
."' could not be processed.";
$error=1;
}
}
elseif($line === $this->html_end)
{
if(!$copyflag && $expected === "HTMLEND")
{$copyflag = 1; $expected = false;}
else
{
$this->msgs[]="File '"
.htmlspecialchars($goodentry)
."' could not be processed.";
$error=1;
}
}
elseif($line === $this->gen_end)
{
if(!$copyflag && $expected === "GENEND")
{$copyflag = 1; $expected = false;}
else
{
$this->msgs[]="File '"
.htmlspecialchars($goodentry)
."' could not be processed.";
$error=1;
}
}
elseif($copyflag)
{ $new_content .= $arr[$line_nr]; }

}

if(!$changes)
{
$this->msgs[]="No need to rewrite file '".
htmlspecialchars($goodentry)."'.<br>";
}
elseif
(
(!$error)
&&
($filehandle = fopen($goodentry, 'w'))
&&
(fwrite($filehandle , $new_content))
&&
(fclose($filehandle))
)
{
$this->msgs[]="File '".
htmlspecialchars($goodentry)."' has been succesfully
rewritten.<br>";
}
else
{
$this->msgs[]="There was an error while trying to save file '".
htmspecialchars($goodentry)."'.<br>";
}
}
elseif(is_dir($goodentry) && $dir_entry !== "." && $dir_entry !== "..")
{
$this->do_recursion($goodentry);
}
}
closedir($dir_handle);
}
//
================================================== ==========================
function show_msgs()
{
echo "<p style='font-family: courier'>";
foreach($this->msgs as $msg)
echo "$msg<br>";
echo "</p>";

}
//
================================================== ==========================
}


Feb 14 '07 #1
0 1767

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: python newbie | last post by:
hey, okay, I'm trying to figure out why my books: Quick Python, Python in a Nutshell, Python Cookbook and Learning Python don't say anything about the weird behavior of a list when you have one as...
3
by: sean | last post by:
HI There, I am recieving a weird error when I try to run asp scripts on my local machine, I can use pages that access a database, but when I try to write to the file system ie upload a file I...
21
by: Hattuari | last post by:
I'm learning C++ after having spent several years in the computer industry doing both system administration and engineering. I've written code in Perl, Bash, Pascal, Ada, C, Mathematica (hundreds...
31
by: N.Davis | last post by:
I am very new to Python, but have done plenty of development in C++ and Java. One thing I find weird about python is the idea of a module. Why is this needed when there are already the ideas of...
0
by: Alan Silver | last post by:
Hello, I have two weird problems here. I have a master page file that works absolutely fine. When I load it up in VWD, I get a couple of weird (to me) errors. First, I get the error...
2
by: jerrygarciuh | last post by:
Hi all, The following script is giving me weird problems. I have in this directory an index.php and hurricane.php. If the script gets $i = 'on' it is supposed to back up the current index...
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
5
by: robinsiebler | last post by:
I have a data structure that looks like this: # dates = {'2007': {'25': {'06/23/07': {'aerosmith': , # 'Metallica': }, # 'last_song': }}}...
0
by: rugman | last post by:
Hi. Let me immediately declare - I'm a designer/owner of a website written in jsp pages running under Apache Tomcat 5.5.25. I make simple changes to code but don't understand much about...
4
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.