Re: feedback, comments on this php-delimiter scrubbing program?

2009-06-16 Thread Gary Kline
On Tue, Jun 16, 2009 at 06:32:43PM -0500, Jeffrey Goldberg wrote:
> On Jun 16, 2009, at 12:02 PM, Gary Kline wrote:
> 
> > this works, but still gives a warning.  it's sloppy coding, but
> > as a second version...
> 
> You've got some superfluous tests for EOF in some places, and you may  
> also be missing some.
> 
> Your approach has been to "look ahead" with an extra getc() when you  
> come across an interesting character.  I recommended that instead of  
> doing that you keep a variable "state" to keep track of where you are  
> (and have very recently been) instead of looking ahead.
> 
> I haven't tried your code, but I suspect that it behaves incorrectly  
> with input
> 
>   (1) that has a '<' as a final character
>   (2) that includes things like "<<<   (3) that includes things like "??>"
> 


this is exactly why i asked here.  i've removed at least one of
the EOF checks and will rewrite in my usual style of 

while ((ch = getc(fp)) != EOF)
{
}

as my next cut.  yes, i shamelessly cribbed this code from else.
it originally deleted both C and C++ comments.  i think it was
written in C# that i'm unfamiliar with.

i'm most familiar with lokahead, not that familar with the STATE
method.  when you have time could you say a few more words? or
point me at a url?  ...i'm all but certain this kind of function
has been invented and re-invented dozens of times. 

> There is a systematic (if a bit tedious) way to make sure that you  
> check every condition.  When you've worked enough on this, you can  
> peek at an answer which I've attached.


you're right above with numbers two and three.  pretty sure that
the first one passes.

gary


> 
> (For the rest of you, I know that it would be more efficient to make  
> the big switch on state instead of on input character, but for  
> pedagogical reasons I did it the other way around.  I deliberately  
> avoided other available tunings).
> 
> The extensive comments in the code should make it clear what is going  
> on.  Once you understand the concepts here it should be very easy to  
> write code to do similar things in the future.
> 
> -j
> 
> 
> 
> -- 
> Jeffrey Goldberghttp://www.goldmark.org/jeff/
> 


> 
> 


-- 
 Gary Kline  kl...@thought.org  http://www.thought.org  Public Service Unix
http://jottings.thought.org   http://transfinite.thought.org
   For FBSD list: http://transfinite.thought.org/slicejourney.php
The 4.98a release of Jottings: http://jottings.thought.org/index.php

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: feedback, comments on this php-delimiter scrubbing program?

2009-06-16 Thread Jeffrey Goldberg

On Jun 16, 2009, at 12:02 PM, Gary Kline wrote:


this works, but still gives a warning.  it's sloppy coding, but
as a second version...


You've got some superfluous tests for EOF in some places, and you may  
also be missing some.


Your approach has been to "look ahead" with an extra getc() when you  
come across an interesting character.  I recommended that instead of  
doing that you keep a variable "state" to keep track of where you are  
(and have very recently been) instead of looking ahead.


I haven't tried your code, but I suspect that it behaves incorrectly  
with input


  (1) that has a '<' as a final character
  (2) that includes things like "<<<"

There is a systematic (if a bit tedious) way to make sure that you  
check every condition.  When you've worked enough on this, you can  
peek at an answer which I've attached.


(For the rest of you, I know that it would be more efficient to make  
the big switch on state instead of on input character, but for  
pedagogical reasons I did it the other way around.  I deliberately  
avoided other available tunings).


The extensive comments in the code should make it clear what is going  
on.  Once you understand the concepts here it should be very easy to  
write code to do similar things in the future.


-j



--
Jeffrey Goldberghttp://www.goldmark.org/jeff/



gkline.c
Description: Binary data



___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"

Re: feedback, comments on this php-delimiter scrubbing program?

2009-06-16 Thread Jeffrey Goldberg

On Jun 16, 2009, at 10:30 AM, Gary Kline wrote:


I thought my initial getchar() != EOF would handle that.
But then there's that do-forever loop.


As I said, the most common problem people had was failing to check of  
EOF in all the places it could occur, and so looping forever.


Do not rely on the input being well formed.


 I remember Jeffrey's
post and tried a case 'EOF' or case '-1';  thar gives me
compiler errors.


Look at the man page for getchar() paying close attention to the type  
of what it returns.


You should really take the pointers from Jeffrey Goldberg and  
record states
and decide based on the state, rather then inlined switch  
statements, if only

for readability.


Even for a very simple task, the logic of your code is very very hard  
to read.  Clarify the logic (using the idea of a "state") and you will  
find that this can be programmed very simply.



-j


--
Jeffrey Goldberghttp://www.goldmark.org/jeff/

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: feedback, comments on this php-delimiter scrubbing program?

2009-06-16 Thread Gary Kline
On Tue, Jun 16, 2009 at 08:30:40AM -0700, Gary Kline wrote:
> On Mon, Jun 15, 2009 at 06:57:45PM -0800, Mel Flynn wrote:
> > On Monday 15 June 2009 17:21:16 Gary Kline wrote:
> > 
> > > Encl: dephp.c, test
> >  case '?':
> > ch = getchar();
> > while (1)
> > {
> >if (ch == '?' && (ch = getchar()) == '>')
> >{
> >   break;
> >} 
> >else
> >{
> >   ch = getchar();
> >}
> > }
> > break;
> > 
> > As has been hinted before you're not handling the EOF case. Files like:
> >  > class foo
> > {
> > function __construct() { echo 'foo'; };
> > };
> > 
> > Are perfectly valid php files and actually preferred for included files, 
> > rather then a terminating ?>, because one can start filling the output by 
> > trailing whitespace before EOF and thus not set any header() anymore. The 
> > above code will wait indefinitely for the next char or spin like mad if 
> > you're 
> > using non-blocking IO.
> 
> 
>   YUP.
> 
>   I thought my initial getchar() != EOF would handle that.  
>   But then there's that do-forever loop.  I remember Jeffrey's
>   post and tried a case 'EOF' or case '-1';  thar gives me
>   compiler errors.  
> 
>   Suggestions?
> 
> 
> > 
> > You should really take the pointers from Jeffrey Goldberg and record states 
> > and decide based on the state, rather then inlined switch statements, if 
> > only 
> > for readability.
> > 
> > You're also in trouble with  > and 
> > you might actually be doing the right thing from your usage perspective.
> > -- 
> > Mel


this works, but still gives a warning.  it's sloppy coding, but
as a second version... 

gary


#include 
#include 

int
main(int argc, char *argv[])
{
   FILE   *fp;
   int results=0;

   *argv++;

   if ((fp = fopen(*argv, "r")) == NULL)
  exit(printf("[%s] not found\n", *argv));
   else
  results = foo(fp);

   printf("\nend of main(), %s, results = [%d]\n", __FILE__, results);

}

int 
foo(FILE *fp)
{
   int ch;
   do
   {
  if ((ch = getc(fp)) != EOF)
  switch (ch)
  {
  case 'EOF':
feof(fp);
exit(0);
  case '>':
 putchar (ch);
 break;
  case '<':
 putchar (ch);
 switch ((ch = getc(fp)) )
 {
 case '?':
ch = getc(fp);
while ( (ch = getc(fp)) != EOF)
{
   if (ch == '?' && (ch = getc(fp)) == '>' )
   {
  break;
   } 
   else
   {
  ch = getc(fp);
   }
}
break;
  case '>':
 putchar (ch);
 break;
 default:
putchar(ch);
break;
 }
 break;
  default:
 putchar (ch);
  }
   }
   while (ch != EOF);

return 0;
}

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: feedback, comments on this php-delimiter scrubbing program?

2009-06-16 Thread Gary Kline
On Mon, Jun 15, 2009 at 06:57:45PM -0800, Mel Flynn wrote:
> On Monday 15 June 2009 17:21:16 Gary Kline wrote:
> 
> > Encl: dephp.c, test
>  case '?':
> ch = getchar();
> while (1)
> {
>if (ch == '?' && (ch = getchar()) == '>')
>{
>   break;
>} 
>else
>{
>   ch = getchar();
>}
> }
> break;
> 
> As has been hinted before you're not handling the EOF case. Files like:
>  class foo
> {
>   function __construct() { echo 'foo'; };
> };
> 
> Are perfectly valid php files and actually preferred for included files, 
> rather then a terminating ?>, because one can start filling the output by 
> trailing whitespace before EOF and thus not set any header() anymore. The 
> above code will wait indefinitely for the next char or spin like mad if 
> you're 
> using non-blocking IO.


YUP.

I thought my initial getchar() != EOF would handle that.  
But then there's that do-forever loop.  I remember Jeffrey's
post and tried a case 'EOF' or case '-1';  thar gives me
compiler errors.  

Suggestions?


> 
> You should really take the pointers from Jeffrey Goldberg and record states 
> and decide based on the state, rather then inlined switch statements, if only 
> for readability.
> 
> You're also in trouble with  you might actually be doing the right thing from your usage perspective.
> -- 
> Mel

-- 
 Gary Kline  kl...@thought.org  http://www.thought.org  Public Service Unix
http://jottings.thought.org   http://transfinite.thought.org
   For FBSD list: http://transfinite.thought.org/slicejourney.php
The 4.98a release of Jottings: http://jottings.thought.org/index.php

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: feedback, comments on this php-delimiter scrubbing program?

2009-06-15 Thread Mel Flynn
On Monday 15 June 2009 17:21:16 Gary Kline wrote:

> Encl: dephp.c, test
 case '?':
ch = getchar();
while (1)
{
   if (ch == '?' && (ch = getchar()) == '>')
   {
  break;
   } 
   else
   {
  ch = getchar();
   }
}
break;

As has been hinted before you're not handling the EOF case. Files like:
, because one can start filling the output by 
trailing whitespace before EOF and thus not set any header() anymore. The 
above code will wait indefinitely for the next char or spin like mad if you're 
using non-blocking IO.

You should really take the pointers from Jeffrey Goldberg and record states 
and decide based on the state, rather then inlined switch statements, if only 
for readability.

You're also in trouble with http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


feedback, comments on this php-delimiter scrubbing program?

2009-06-15 Thread Gary Kline
guys,

last night i was lost with a slightly shorter version of this.
even looking at a printout did nothing.  this morning i had another go
and went in a corner and started at the code.  finally i figured it out.
this *seems* to work...  The only thing this does is remove the "" delimiters.  i'll go more into my rationale later on.

i'm pretty sure i can tighten up on this; getting it to work was my main
goal.  can any of you break this?

gary

Encl: dephp.c, test


-- 
 Gary Kline  kl...@thought.org  http://www.thought.org  Public Service Unix
$Id: dephp.c,v 1.6 2009/06/16 01:05:47 kline Exp kline $

#include 

int 
main()
{
   int ch;
   do
   {
  if ((ch = getchar()) != EOF)
  switch (ch)
  {
  case '>':
 putchar (ch);
 break;
  case '<':
 putchar (ch);
 switch ((ch = getchar()) )
 {
 case '?':
ch = getchar();
while (1)
{
   if (ch == '?' && (ch = getchar()) == '>')
   {
  break;
   } 
   else
   {
  ch = getchar();
   }
}
break;
  case '>':
 putchar (ch);
 break;
 default:
putchar(ch);
break;
 }
 break;
  default:
 putchar (ch);
  }
   }
   while (ch != EOF);

return 0;
}
// test

foo
bar


blah, blah 
<
>

blah, blah:
more HERE.

<>

<  >

<
>

<

>





___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"