/* 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
*
*  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 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.
*
*/

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

main(int argc, char *argv[])
{
  dt_Handle mol;
  char      *smi, *cansmi, *name, *p;
  int       count, goodcount = 0, modcount = 0, lencan, len;
  int       iso = 0;

  /*** Make Isomeric SMILES if "-i" option.  ***/

  if (argc > 1 && !strcmp("-i", argv[1])) iso = 1;

  for (count = 0; NULL != (smi = du_fgetline(&len, stdin)); ++count ) {
    mol    = dt_smilin(strlen(smi), smi);
    if (NULL_OB == mol) {
      fprintf(stderr,
        "ERROR: (dt_smilin) Can't make unique SMILES for %s\n", smi);
    } else {
      cansmi = dt_cansmiles(&lencan, mol, iso);
      if (0 >= lencan) {
        fprintf(stderr,
          "ERROR: (dt_cansmiles) Can't make unique SMILES for %s\n", smi);
      } else {
        if (NULL != (p = strchr(smi, ' '))) {
          name = p + 1;
          printf("%.*s %s\n", lencan, cansmi, name);
          if (strncmp(smi, cansmi, p - smi)) ++modcount;
        }
        else {
          printf("%.*s\n", lencan, cansmi);
          if (strcmp(smi, cansmi)) ++modcount;
        }
        ++goodcount;
      }
    }
    du_printerrors(stderr, DX_ERR_ERROR);
    dt_errorclear();

/*** printf("Handles in use = %d\n", dt_vh_count());    DEBUG ***/

    dt_dealloc(mol);
  }
  free(smi);
  fprintf(stderr, "SMILES in: %d; SMILES out: %d; SMILES changed: %d\n",
          count, goodcount, modcount);
  fprintf(stderr, "So long, baby!\n");
}