From owner-freebsd-questions@FreeBSD.ORG Mon Oct 19 22:21:32 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 0A2DA1065695 for ; Mon, 19 Oct 2009 22:21:32 +0000 (UTC) (envelope-from kline@thought.org) Received: from aristotle.thought.org (aristotle.thought.org [209.180.213.210]) by mx1.freebsd.org (Postfix) with ESMTP id C6CC58FC1D for ; Mon, 19 Oct 2009 22:21:31 +0000 (UTC) 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 n9JMLKEo030731; Mon, 19 Oct 2009 15:21:21 -0700 (PDT) (envelope-from kline@thought.org) Received: by thought.org (nbSMTP-1.00) for uid 1002 kline@thought.org; Mon, 19 Oct 2009 15:21:26 -0700 (PDT) Date: Mon, 19 Oct 2009 15:21:26 -0700 From: Gary Kline To: Patrick Mahan Message-ID: <20091019222126.GB12488@thought.org> References: <20091019013337.GA9522@thought.org> <4ADBFDBA.6040702@pchotshots.com> <20091019170634.GA12371@thought.org> <4ADCAB4F.5040707@mahan.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4ADCAB4F.5040707@mahan.org> User-Agent: Mutt/1.4.2.3i X-Organization: Thought Unlimited. Public service Unix since 1986. X-Of_Interest: With 23 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 Mailing List Subject: Re: need C help, passing char buffer[] by-value.... 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: Mon, 19 Oct 2009 22:21:32 -0000 On Mon, Oct 19, 2009 at 11:09:19AM -0700, Patrick Mahan wrote: > See comments interspaced below - > > Gary, > > Let me restate your problem: You want to read through a file containing tags > delimited by "<>" and to skip these tags if the user has run your command > with > the "-N" flag. > > In C any thing passed by address is "by reference". For a your static > buffer > of 1024 characters: you can pass it by reference as: > > skiptags(buf); /* passes in the starting address of the buffer */ > skiptags(&buf[0]); /* passes in the starting address of the > buffer */ > skiptags(&buf[10]); /* passes int the starting address of the > buffer > at the 11th character position. */ > > Arrays and pointers are always by reference. Individual data types "int", > "char", etc are by value unless passed in as a pointer. I think this is > where > your confusion is around. You've got it exactly right, Patrick. There were no "C" classes in 1978--I taught myself. Obviously, not that well because I have already dreaded pointers. ---Well, usually. > > A couple of things to keep in mind: > > 1. Remember how fgets() works. It is entirely possible that you might > have > tags that span multiple lines. You will need to take that into > account. > > 2. You can manipulate the fixed buffer two different ways: > > A. You can use pointer arithemtic, eg. > > char buf[1024]; > char *cp; > > while ((cp = fgets(buf, sizeof(buf), fp_in))) { > > if (skiptags) cp = skiptag(buf); > > /* If NULL, end of line reached */ > if (!cp) continue; > > } > > char *skiptags(char *buf) > { > char *tp = buf; > > /* find the start of a tag */ > while (*tp != '\0' && *tp++ != '<'); > > /* if no tag is found return start of buffer */ > if (*tp == '\0') > return buf; > > /* Start of tag, find the end of tag */ > while (*tp != '\0' && *tp != '\n' && *tp++ != '>'); > > /* if end of line reached return NULL */ > if (*tp == '\0' || *tp == '\n') > return NULL; > > /* return the next character start after the end tag */ > return ++tp; > } > > B. Using indexing, eg. > > char buf[1024]; > int i, bsize; > > while (fgets(buf, sizeof(buf), fp_in)) { > i = 0; > bsize = strlen(buf); > > if (skiptags) i = skiptag(buf); > > /* If NULL, end of line reached */ > if (i >= bsize) continue; > > } > > int skiptags(char *buf) > { > int c = 0; > > /* find the start of a tag */ > while (buf[c] != '\0' && buf[c] != '<') > c++; > > /* if no tag is found return start of buffer */ > if (buf[c] == '\0') > return 0; > > /* Start of tag, find the end of tag */ > while (buf[c] != '\0' && buf[c] != '\n' && buf[c] != '>') > c++; > > /* if end of line reached return NULL */ > if (buf[c] == '\0' || buf[c] == '\n') > return strlen(buf); > > /* return the next character start after the end tag */ > return ++c; > } > > Both methods should allow you to skip past any tags found in the file > (provided > you handle the case of a tag spanning more than one line). > > > Hope this clears up your confusion and gets you on your way. Your examples help a lot! Everything works except when there are two or more tags on one line such as: I think I see where is your skiptags--pointer arithematic function--this can be caught. Thanks much! :-) gary > > Patrick > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" -- Gary Kline kline@thought.org http://www.thought.org Public Service Unix http://jottings.thought.org http://transfinite.thought.org The 7.31a release of Jottings: http://jottings.thought.org/index.php