/**********************************************************************
 * ti library's routine for getting a node out of a string
 *
 * $Author: brlewis $
 * $Source: /mit/techinfodev/src/libti/RCS/str2nd.c,v $
 * $Header: /mit/techinfodev/src/libti/RCS/str2nd.c,v 4.2 1995/04/10 20:21:57 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: /mit/techinfodev/src/libti/RCS/str2nd.c,v 4.2 1995/04/10 20:21:57 brlewis Exp $";
#endif /* lint */

#include <errno.h>
#include <string.h>
#include <ti/memory.h>
#include <ti/ti.h>

/**********************************************************************
 * _ti_str2nd(TI *tp, char *str, ti_node *nd)
 *	puts correct node info in nd
 *
 * should only be called from within libti; not at user-interface level
 * - returns error code
 **********************************************************************/

/* At some point, different connections might have different string
   formats, so the TI * argument is needed */

/*ARGSUSED*/
long
_ti_str2nd(tp, str, nd)
     TI *tp;
     char *str;
     ti_node *nd;
{
  long code;
  register char *idx, *idx2;
  int i;

  /* level */
  ti_level(nd) = atoi(str);
  if (!(idx = strchr(str, TI_DLM))) goto INVAL;

  /* id */
  ti_id(nd) = atol(++idx);
  if (!(idx = strchr(idx, TI_DLM))) goto INVAL;

  /* flags */
  idx++;
  ti_flags(nd) = strtoul(idx, (char **)0, 10);

  if (!(idx = strchr(idx, TI_DLM))) goto INVAL;

  /* date */
  ti_date(nd) = atoi(++idx);
  if (!(idx = strchr(idx, TI_DLM))) goto INVAL;

  /* strings */
  for (i= -1; i<TI_NDSTRLIM; i++)
    {
      if (!(idx2 = strchr(++idx, TI_DLM))) goto INVAL;
      *idx2 = '\0';
      if (code=ti_setdata(nd, idx, i)) return(code);
      *idx2 = TI_DLM;
      idx = idx2;
    }
  return(0L);

 INVAL:
  strcpy(ti_context, str);
  return(TI_ERR_NDINVAL);
}
