473,545 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 licenseplacehol der text EITHER with new licentext OR with
licenseplacehol der

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_compres sor("TARU_LICEN SE_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_compres sor
{
var $version = "License compressor 1.0";
var $linestop ="\r\n";

var $ok = 1;
var $msgs = array();
var $license_filepa th;
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_compres sor($id, $license_filepa th)
{
$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 =
".htmlspecialch ars($id); }

if(!($this->license = file_get_conten ts($license_fil epath)))
{ $this->ok = 0; $this->msgs[]= "License filepath is not a valid
path."; }
else
{
$this->license_filepa th = $license_filepa th;
$this->msgs[]= "License filepath = ".htmlspecialch ars($license_fi lepath);
}

$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.$thi s->linestop.$th is->license.
$this->linestop.$th is->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.$th is->linestop.$th is->license.
$this->linestop.$th is->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.$thi s->linestop.$th is->license.
$this->linestop.$th is->gen_end;
}
//
=============== =============== =============== =============== =============== =
function dance($suppress , $current_dir)
{

if(!is_dir($cur rent_dir))
{$this->ok = 0; $this->msgs[]= "Current directory path is not a valid
directory path."; }
else
{
$this->msgs[]= "Current directory = ".htmlspecialch ars($current_di r);
}
$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($c urrent_dir)
{
if (($dir_handle = @opendir($curre nt_dir)) !== false)
{
$this->msgs[] = "Opening directory
'".htmlspecialc hars($current_d ir)."'.";
}
else
{
$this->msgs[] = "Opening of directory '".
htmlspecialchar s($current_dir) ."' failed.";
return;
}

$entries = array();
while (false !== ($dir_entry = readdir($dir_ha ndle)))
{
$goodentry =$current_dir."/".$dir_entr y;
if(in_array($go odentry,$entrie s)) continue;
else $entries[]= $goodentry;
if(is_file($goo dentry))
{
$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: ".
htmlspecialchar s($line)." | Stop: $stopnote";
if($line === $this->php_comp && $copyflag)
{ $new_content .= $this->php_str.$sto p; $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.$sto p; $changes = 1;}

elseif($line === $this->php_begin && $copyflag)
{ $new_content .= $this->php_str.$sto p; $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.$sto p; $copyflag = 0; $expected =
"GENEND";
$changes = 1;}

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

}

if(!$changes)
{
$this->msgs[]="No need to rewrite file '".
htmlspecialchar s($goodentry)." '.<br>";
}
elseif
(
(!$error)
&&
($filehandle = fopen($goodentr y, 'w'))
&&
(fwrite($fileha ndle , $new_content))
&&
(fclose($fileha ndle))
)
{
$this->msgs[]="File '".
htmlspecialchar s($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_h andle);
}
//
=============== =============== =============== =============== =============== =
function show_msgs()
{
echo "<p style='font-family: courier'>";
foreach($this->msgs as $msg)
echo "$msg<br>";
echo "</p>";

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


Feb 14 '07 #1
0 1817

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

Similar topics

5
1445
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 an object instance member. for instance (my first pun of 2004), if I have, test.py ----------------
3
4863
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 get the error message below. any ideas? Sean
21
2578
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 of lines of OO code, not 1+1), JavaScript, Lisp, and Java, as well as C++. Each of these languages has it's strengths and weaknesses. My...
31
2484
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 class, file, and package? To my mind, although one CAN put many classes in a file, it is better to put one class per file, for readability and...
0
2019
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 "Unrecognized tag prefix or device filter 'asp'" on the third line shown below... <head runat="server">
2
1608
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 into a file called normal.php and then copy hurricane.php into index.php. This should create a backup of the index and then put the hurrican alert in...
14
2704
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 mallocs and ect, but i removed them to help me debug. thanks. #include <stdio.h> #include <stdlib.h> #include <string.h> void freadl ( FILE...
5
1977
by: robinsiebler | last post by:
I have a data structure that looks like this: # dates = {'2007': {'25': {'06/23/07': {'aerosmith': , # 'Metallica': }, # 'last_song': }}} I have a section where I try to loop through a set of dates and print out all of the songs for a given artist: if type == 'artist':
0
2205
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 configuration. My site is a commercial site called http://www.orientalrugsonline.co.uk and hosted by a third party who use cPanel and automatically download...
4
8065
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, nodes is the number of nodes and edges number of edges e v1 v2 //this means an edge connecting v1 and v2, both integers
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7918
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7436
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5981
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5341
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1897
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
715
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.