473,837 Members | 1,469 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 1836

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

Similar topics

5
1460
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
4873
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
2625
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 experience with C++ has shown me that it is by far the most difficult for me to learn. I have the sense...
31
2545
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 maintainability. One can then create packages and libraries, using groups of files, one
0
2068
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
1624
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 place.
14
2729
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 *stream, char **string ) {
5
1989
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
2228
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 latest versions of Tomcat and Apache. For about a week, several important bits of my site have...
4
8095
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
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9396
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7806
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5846
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
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
2
4039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3123
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.