association.c

Go to the documentation of this file.
00001 /*                   A S S O C I A T I O N . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 1995-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup bu_log */
00023 /*@{*/
00024 /** @file association.c
00025  * Look up the association for a specified value.
00026  *
00027  *  @author -
00028  *      Paul Tanenbaum
00029  *
00030  *  @par Source -
00031  *      The U. S. Army Research Laboratory
00032  *@n    Aberdeen Proving Ground, Maryland  21005-5068  USA
00033  */
00034 /*@}*/
00035 
00036 #ifndef lint
00037 static const char libbu_association_RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/association.c,v 14.14 2006/08/31 23:16:38 lbutler Exp $ (ARL)";
00038 #endif
00039 
00040 #include "common.h"
00041 
00042 #include <stdlib.h>
00043 #include <stdio.h>
00044 #include <math.h>
00045 #if HAVE_STRING_H
00046 #  include <string.h>
00047 #endif
00048 
00049 #include "machine.h"
00050 #include "bu.h"
00051 
00052 
00053 /**
00054  *                       B U _ A S S O C I A T I O N
00055  *
00056  *          Look up the association for a specified value
00057  *
00058  *      This function reads the specified file, searches for the
00059  *      first line of the form
00060  *
00061 @code
00062                     <value><field_sep>...
00063 @endcode
00064  *
00065  *      and returns the rest of the line beyond the field separator.
00066  */
00067 struct bu_vls *
00068 bu_association (const char *fname,
00069                 const char *value,
00070                 int        field_sep)
00071 {
00072     char                *cp;
00073     FILE                *fp;
00074     size_t              len;
00075     struct bu_vls       *vp = 0;
00076     struct bu_vls       buffer;
00077 
00078         /* XXX NONPARALLEL */
00079         /* I'd prefer using "bu_open_mapped_file()" here instead, I think  -Mike */
00080     if ((fp = fopen(fname, "r")) == NULL) {
00081         /*      XXX
00082          *      Should we be exiting here?
00083          *      I don't want to just return 0,
00084          *      because the application probably needs to distinguish
00085          *      between ERROR_PERFORMING_THE_LOOKUP
00086          *      and VALUE_NOT_FOUND.
00087          */
00088         bu_log("bu_association:  Cannot open association file '%s'\n", fname);
00089         exit (1);
00090     }
00091 
00092     bu_vls_init(&buffer);
00093     len = strlen(value);
00094 
00095     do {
00096         bu_vls_trunc(&buffer, 0);
00097         if (bu_vls_gets(&buffer, fp) == -1)
00098             goto wrap_up;
00099         cp = bu_vls_addr(&buffer);
00100 
00101     } while ((*cp != *value) || (*(cp + len) != field_sep)
00102              || (strncmp(cp, value, len) != 0));
00103 
00104     vp = (struct bu_vls *) bu_malloc(sizeof(struct bu_vls), "value of bu_association");
00105     bu_vls_init(vp);
00106     bu_vls_strcpy(vp, cp + len + 1);
00107 
00108 wrap_up:
00109     bu_vls_trunc(&buffer, 0);
00110     fclose(fp);
00111     return (vp);
00112 }
00113 /*@}*/
00114 /*
00115  * Local Variables:
00116  * mode: C
00117  * tab-width: 8
00118  * c-basic-offset: 4
00119  * indent-tabs-mode: t
00120  * End:
00121  * ex: shiftwidth=4 tabstop=8
00122  */

Generated on Mon Sep 18 01:24:47 2006 for BRL-CAD by  doxygen 1.4.6