From owner-freebsd-questions Mon May 21 15:46: 6 2001 Delivered-To: freebsd-questions@freebsd.org Received: from mail4.mn.rr.com (fe4.rdc-kc.rr.com [24.94.163.51]) by hub.freebsd.org (Postfix) with ESMTP id 50E7737B422 for ; Mon, 21 May 2001 15:45:53 -0700 (PDT) (envelope-from trammell@trammell.dyndns.org) Received: from bayazid.hypersloth.net ([65.25.244.166]) by mail4.mn.rr.com with Microsoft SMTPSVC(5.5.1877.537.53); Mon, 21 May 2001 17:45:47 -0500 Received: from trammell by bayazid.hypersloth.net with local (Exim 3.12 #1 (Debian)) id 151xn2-0000R1-00 for ; Mon, 21 May 2001 17:04:36 -0500 Date: Mon, 21 May 2001 17:04:35 -0500 To: freebsd-questions@freebsd.org Subject: Re: Question for C preprocessor gurus Message-ID: <20010521170435.A1610@mn.rr.com> Mail-Followup-To: freebsd-questions@freebsd.org References: <11895256.990483991242.JavaMail.imail@zero.excite.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <11895256.990483991242.JavaMail.imail@zero.excite.com>; from john_wilson100@excite.com on Mon, May 21, 2001 at 03:26:30PM -0700 From: John Joseph Trammell Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Mon, May 21, 2001 at 03:26:30PM -0700, John Wilson wrote: > In this program I'm writing, I need to define a simple list of errors, e.g. > > #define ERR_OK 0 > #define ERR_BAD 1 > #define ERR_AWFUL 2 > > and a function to return error descriptions: > > char *f(int error_no) > { > switch (error_no) > { > case ERR_OK: > return "OK"; > > case ERR_BAD: > return "Bad"; > > /* .... */ > } > } > > > There is nothing wrong with this code, except one thing - to add a new > error, I need to do it in two different places. I was therefore wondering > if it was possible to avoid adding the same strings twice through (ab)use of > the preprocessor. How about defining the error values/symbols/messages in a text file, then generating the .c and .h files dynamically? [ ~/test/errno-001 ] cat errors.txt 0 ERR_OK okily-dokily! 1 ERR_BAD abandon ship! 2 ERR_AWFUL my hovercraft is full of eels! [ ~/test/errno-001 ] cat gencode.pl use strict; my @lines; while (<>) { chomp; push @lines, $_ } # make .h file open(FOO,">foo.h") or die "Can't open foo.h for writing: $!"; for (@lines) { my ($num,$sym) = (split(' ',$_,3))[0,1]; print FOO "#define $sym $num\n"; } close FOO; # make .c file open(FOO,">foo.c") or die "Can't open foo.c for writing: $!"; print FOO "char *f(int e) { switch (e) {\n"; for (@lines) { my ($sym,$text) = (split(' ',$_,3))[1,2]; print FOO "\tcase $sym: return \"$text\";\n"; } print FOO "}}\n"; close FOO; [ ~/test/errno-001 ] perl -w gencode.pl errors.txt [ ~/test/errno-001 ] cat foo.h foo.c #define ERR_OK 0 #define ERR_BAD 1 #define ERR_AWFUL 2 char *f(int e) { switch (e) { case ERR_OK: return "okily-dokily!"; case ERR_BAD: return "abandon ship!"; case ERR_AWFUL: return "my hovercraft is full of eels! "; }} [ ~/test/errno-001 ] -- If you don't look at the fnord, it can't get you. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message