/**********************************************************************
 * ti library's routine for getting a node out of a string
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/_ti_str2nd.c,v $
 * $Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/_ti_str2nd.c,v 0.1 92/03/12 15:27:08 brlewis Exp $
 *
 * Copyright 1992 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/

#include <mit-copyright.h>

#ifndef lint
static char rcsid_ti_main_c[] = "$Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/_ti_str2nd.c,v 0.1 92/03/12 15:27:08 brlewis Exp $";
#endif /* lint */

#include <errno.h>
#include <string.h>
#include <ti/memory.h>
#include <ti/ti.h>

/**********************************************************************
 * ti_main(TI_H hti, LPSTR str, ti_node_H hnode)
 *	puts correct node info in nd
 *
 * - returns error code
 **********************************************************************/

/* jms 7/13/92 -- windows-izing completed, pending approval */

/* At some point, different connections might have different string
   formats, so the TI * argument is needed */
/*ARGSUSED*/
long
_ti_str2nd(hti, str, hnode)
     TI_H hti;
     LPSTR str;
     ti_node_H hnode;
{
  long code;
  register LPSTR idx, LPSTR idx2;
  int i;

  ti_node_LP lpnode;

  lpnode = (ti_node_LP)TI_DEREFERENCE(hnode);

  /* level */
  ti_level(lpnode) = atoi(str);
  if (!(idx = index(str, TI_DLM))) goto INVAL;

  /* id */
  ti_id(lpnode) = atol(++idx);
  if (!(idx = index(idx, TI_DLM))) goto INVAL;

  /* flags */
  ti_flags(lpnode) = atoi(++idx);
  if (!(idx = index(idx, TI_DLM))) goto INVAL;

  /* date */
  ti_date(lpnode) = atoi(++idx);
  if (!(idx = index(idx, TI_DLM))) goto INVAL;

  /* strings */
  for (i= -1; i<TI_NDSTRLIM; i++)
    {
      if (!(idx2 = index(++idx, TI_DLM))) goto INVAL;
      *idx2 = '\0';
      if (code=ti_setdata(lpnode, idx, i)) CLEANUP_AND_RETURN(code)
      *idx2 = TI_DLM;
      idx = idx2;
    }

  CLEANUP_AND_RETURN(0L)

 INVAL:
  strcpy(ti_context, str);
  CLEANUP_AND_RETURN(TI_ERR_NDINVAL)

CLEANUP:

  TI_REMOVE_DEREFERENCE(hnode);
  return(code);
}

