/*****
                              datevals()

      This function breaks a date string into integer variables. The
   input string may use any nondigit separator (such as a slash, as in
   MM/DD/YY) and may be in any of the formats given for w below.

   Argument list:    int *d         the day
                     int *m          "  month
                     int *y          "  year
                     int w          the format, where:
                                    0 = MM/DD/YY
                                    1 = DD/MM/YY
                                    2 = YY/MM/DD

   Return value:     void

*****/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void datevals(char *s, int *d, int *m, int *y, int w)
{
   char *ptr, temp[5];
   int i, vals[3];

   ptr = s;
   i = 0;
   for (;;) {
      if (isdigit(*s)) {               /* Loop for separator      */
         s++;
         if (*s != '\0') {
            continue;
         }
      }
      strncpy(temp, ptr, s - ptr);     /* Form a digit string     */
      temp[s - ptr] = '\0';
      vals[i++] = atoi(temp);          /* Make it a number        */
      if (*s == '\0') {                /* If done, end it...      */
         break;
      }
      s++;                             /* otherwise keep parsing  */
      ptr = s;
   }
   switch (w) {
      case 0:                 /* Do MM/DD/YY */
         *m = vals[0];
         *d = vals[1];
         *y = vals[2];
         break;
      case 1:                 /* Do DD/MM/YY */
         *m = vals[1];
         *d = vals[0];
         *y = vals[2];
         break;
      case 2:                 /* Do YY/MM/DD */
         *m = vals[1];
         *d = vals[2];
         *y = vals[0];
         break;
      default:                /* Error       */
         *d = *m = *y = 0;
         break;
   }
}
