From owner-freebsd-questions@FreeBSD.ORG Tue Jun 9 23:40:44 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id CEC72106564A for ; Tue, 9 Jun 2009 23:40:44 +0000 (UTC) (envelope-from kline@thought.org) Received: from aristotle.thought.org (ns1.thought.org [209.180.213.210]) by mx1.freebsd.org (Postfix) with ESMTP id 6DFEA8FC15 for ; Tue, 9 Jun 2009 23:40:44 +0000 (UTC) (envelope-from kline@thought.org) Received: from thought.org (tao.thought.org [10.47.0.250]) (authenticated bits=0) by aristotle.thought.org (8.14.2/8.14.2) with ESMTP id n59Nfs9n089176; Tue, 9 Jun 2009 16:41:54 -0700 (PDT) (envelope-from kline@thought.org) Received: by thought.org (nbSMTP-1.00) for uid 1002 kline@thought.org; Tue, 9 Jun 2009 16:40:39 -0700 (PDT) Date: Tue, 9 Jun 2009 16:40:39 -0700 From: Gary Kline To: Jeffrey Goldberg Message-ID: <20090609234039.GB11209@thought.org> References: <20090609001529.GA7166@thought.org> <0F5E9DB6-2342-46E0-B5AB-7BB70837FEF6@goldmark.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0F5E9DB6-2342-46E0-B5AB-7BB70837FEF6@goldmark.org> User-Agent: Mutt/1.4.2.3i X-Organization: Thought Unlimited. Public service Unix since 1986. X-Of_Interest: With 22++ years of service to the Unix community. X-Spam-Status: No, score=-4.4 required=3.6 tests=ALL_TRUSTED,BAYES_00 autolearn=ham version=3.2.3 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on aristotle.thought.org Cc: FreeBSD Questions Subject: Re: flaw found [in my own program] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Jun 2009 23:40:45 -0000 On Tue, Jun 09, 2009 at 06:19:21PM -0500, Jeffrey Goldberg wrote: > On Jun 8, 2009, at 7:15 PM, Gary Kline wrote: > > > not surprisingly, i found a fla w in my getc(fp) program that > > tried to read past "" ... the example i added to my > > test file was simply the 2 bytes "<" and "?". so if you have a > > stray > > > > " > > > with a matching close case, the binary hangs on a read. > > so, again, can anybody suggest a better example, in C, to get > > past two delimiters? > > Back in the days when I taught introductory C programming, one the the > early homework assignments was to write a filter that would strip C- > style comments. As a follow-up they had to do this allowing for > nested comments. > > I don't think I can recover things from the back-up tapes that I have > for that corse material, but the approach I directed people toward was > to have a variable, let's call it status that records one of four states > > OUTSIDE /* just reading normally, not in the material to be striped > */ > AFTER_LT /* You've read in a '<' and are looking for a '?' */ > INSIDE /* You are in the material to be stripped */ > AFTER_Q /* You are in the material to be stripped and have just > read a '?' */ > > then use a switch statement on the character you are reading in. > > switch(c) { > case '<': ... > case '?': ... > case '>': ... > case EOF: ... > default: ... > } > > In each case, you look at the current state, decide whether the write > 'c' to output and what state to change to. The most common mistake > students would make would be to forget the EOF case. I suspect that > you may have done the same. > > -j :-) yup, this is definitely better that initial approach. i have improved my function to return a 1 after "?", 2 after a "?>" and a 3 if both are found. i still think my function hangs in an loop if only the opening php token is found. thanks for the idea! gary > -- Gary Kline kline@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.91a release of Jottings: http://jottings.thought.org/index.php