/* cansmi.c */

    /**********************************************************************
    * This source code is public domain, and may be freely distributed,	  *
    * modified, and used for any purpose.  IT COMES WITH ABSOLUTELY NO    *
    * WARRANTY OF ANY KIND.						  *
    **********************************************************************/

/*****************************************************************************
*  cansmi.c -- a simple filter to uniquify SMILES
*
*  This is the "Hello world!" of Daylight Toolkit programs.
*
*  Note on Toolkit strings: Toolkit functions returning strings to C calling
*  programs return a pointer to static storage.  There is no need to allocate
*  or free such strings; their memory is automatically allocated when needed
*  and freed when the molecule is deallocated (this is true for components
*  of any object).  Strings which are part of an object may change or be
*  discarded (e.g. when the object is modified or deallocated).  In general,
*  you should copy strings which you will need later.
*
*  The declaration for line (below) sets the limit of the SMILES length
*  (the SMILES Toolkit does not impose a limit).
*
*  The C-interface to the Toolkit references (almost all) strings by address
*  and length (not NULL-terminated strings).  See du_smilin() for an example
*  of how one might use NULL-terminated string conventions.
*
*  Note also that error-handling is done for the sake of the program, not
*  for the sake of the Toolkit.  For instance, this program does not check 
*  the success of dt_smilin() before calling dt_cansmiles().  On error,
*  dt_smilin() returns NULL_OB; asking for the dt_cansmiles() of NULL_OB is
*  a well-defined operation (returns string of length 0), not a "mistake".
*
*  BUGS:  This program only generates non-isomeric unique SMILES.
*         Input overflow (lines >= 2000 chars) is not checked.
*         The SMILES "name" is not copied to output.
*/

#include <stdlib.h>
#include <stdio.h>
#include "dt_smiles.h"

#define MAXSMI 2000

main()
{
  char      line[MAXSMI], *msg, *cansmi;
  dt_Handle mol;
  int       lenmsg, lencan;

   /*** Loop over input ***/

   while( gets(line) ) {

      /*** Parse SMILES, get errors, get & print unique SMILES if able. ***/

      mol    = dt_smilin(strlen(line), line);
      msg    = dt_smilinerrtext(&lenmsg);
      cansmi = dt_cansmiles(&lencan, mol, 1);
      if (0 < lencan) printf("%.*s\n", lencan, cansmi);

      /*** On error (or warning), print input and message to stderr. ***/

      if (0 < lenmsg || 0 >= lencan) {
         if (0 >= lencan)
	    fprintf(stderr, "Can't make unique SMILES for %s\n", line);
         if (0 < lenmsg)
	    fprintf(stderr, "%.*s\n", lenmsg, msg);
      }

      /*** Done with molecule. ***/

      dt_dealloc(mol);
   }
}