Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

tc_find.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2004, 2005 by Kevin L. Mitchell <klmitch@mit.edu>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 **
00018 ** @(#)$Id: tc_find.c,v 1.5 2005/06/07 04:53:50 klmitch Exp $
00019 */
00027 #include <errno.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #include "treeconf_int.h"
00032 
00033 RCSTAG("@(#)$Id: tc_find.c,v 1.5 2005/06/07 04:53:50 klmitch Exp $");
00034 
00035 unsigned int
00036 tc_find(treeconf_ctx_t *ctx, const char *name, treeconf_node_t **node_p,
00037       treeconf_node_t *parent)
00038 {
00039   treeconf_node_t *node = 0;
00040   struct result res;
00041   unsigned int err = 0;
00042   int i;
00043 
00044   initialize_trcf_error_table();
00045 
00046   /* Verify the arguments */
00047   if (!tx_verify(ctx) || !name || !node_p ||
00048       (parent && (!tn_verify(parent) || parent->tn_context != ctx)))
00049     return EINVAL;
00050 
00051   /* OK, look up the node... */
00052   if ((err = _tc_lookup_node(ctx, name, parent, &res)))
00053     return err;
00054 
00055   /* Found the specific node? */
00056   if (!res.r_node || res.r_count != res.r_startnew) {
00057     /* No, can we create it? */
00058     if (_tn_call_nodown(res.r_node, &res.r_comps[res.r_startnew],
00059                   res.r_count - res.r_startnew)) {
00060       /* Create the nodes... */
00061       for (i = res.r_startnew, node = res.r_node; i < res.r_count; i++)
00062       /* All created nodes are implicit and default is 0 */
00063       if (!(node = _tc_create_node(ctx, node,
00064                              ((i == res.r_count - 1 ?
00065                                (TC_NODETYPE_VARIABLE) :
00066                                (TC_NODETYPE_NODE)) |
00067                               TC_NODETYPE_IMPLICIT),
00068                              res.r_comps[i].ts_string,
00069                              res.r_comps[i].ts_length, 0,
00070                              res.r_node->tn_nodown,
00071                              res.r_node->tn_change, 0)))
00072         _tc_doerror(ENOMEM);
00073     } else /* no such node and cannot create it... */
00074       _tc_doerror(ENOENT);
00075   } else
00076     node = res.r_node;
00077 
00078   *node_p = node; /* return the node! */
00079 
00080  error:
00081   free(res.r_comps); /* Release temporarily allocated memory */
00082 
00083   return err;
00084 }

Generated on Wed Jun 8 09:18:27 2005 for treeconf by  doxygen 1.3.9.1