/* 14_tokens_and_utils.c -- the small string/utility toolkit.
 *
 * Functions demonstrated:
 *   SANToken()             -- pull whitespace-separated tokens out of
 *                             a line, one at a time (this is what the
 *                             interactive command reader itself uses
 *                             to split up a typed-in command line)
 *   SANCheckShortInteger() / SANEvalShortInteger() -- validate, then
 *                             convert, a small integer given as text
 *   SANCIEqStr()           -- case-insensitive string comparison
 *   SANMemGrab() / SANMemFree() -- the allocator SANToken() itself
 *                             uses for each returned token (so tokens
 *                             must be freed the same way)
 *
 * Note: sanutl.h uses time_t without including <time.h> itself, so
 * make sure <time.h> is included first (see _common.h).
 */

#include "_common.h"

#include "sanint.h"
#include "sanutl.h"
#include "sanpce.h"

int
main (void)
{
  char line[] = "svop  nabd   namd    napr";
  siT i;
  charptrT tok;

  SANInit ();

  printf ("Tokenizing: \"%s\"\n", line);
  i = 0;
  while ((tok = SANToken (line, &i)) != NULL)
    {
      printf ("  token: \"%s\"\n", tok);
      SANMemFree (tok);            /* SANToken() allocates; we free */
    };

  printf ("\nSANCheckShortInteger/SANEvalShortInteger:\n");
  if (SANCheckShortInteger ("31"))
    printf ("  \"31\" is a valid short integer: %d\n",
            SANEvalShortInteger ("31"));
  if (!SANCheckShortInteger ("thirty-one"))
    printf ("  \"thirty-one\" is correctly rejected\n");

  printf ("\nSANCIEqStr (case-insensitive comparison):\n");
  printf ("  \"Eat\" == \"eAT\" ? %s\n",
          SANCIEqStr ("Eat", "eAT") ? "yes" : "no");
  printf ("  \"Eat\" == \"Ran\" ? %s\n",
          SANCIEqStr ("Eat", "Ran") ? "yes" : "no");

  return (0);
}
