/* $Id: num_unit_probe.c,v 1.1 1997/04/22 20:32:17 dps Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif /* HAVE_CTYPE_H */
#define __EXCLUDE_READER_CLASSES
#include "lib.h"

struct unit_number n_unit_probe(const char *txt, struct unit_info *dp)
{
   int i,j,n,c,l,unit;
   struct unit_number u_num;

   u_num.unit_num=-1;
   unit=dp->unit_type-1;
   l=strlen(txt);
   i=0;
   while(1)
   {
       n=0;
       for (c=0; i<l && isdigit(txt[i]); i++, c++)
           n=n*10+txt[i]-'0';
       if (c==0)
           break;
       unit++;
       if (unit>=NUNITS)
       {
           unit=NUNITS-1;
           break;
       }
       if (dp->unit_number[unit]==-1)
       {
           if (n>MAX_START_NUM)
               return u_num;

           dp->unit_number[unit]=n;
           for (j=unit+1; j<NUNITS; j++)
               dp->unit_number[j]=0;
       }
       else if (dp->unit_number[unit]+1==n)
       {
           dp->unit_number[unit]++;
           for (j=unit+1; j<NUNITS; j++)
               dp->unit_number[j]=0;
       }
       else if (dp->unit_number[unit]!=n)
          return u_num;

       if (txt[i]!='.')
           break;
       i++;
   }

   if (unit==dp->unit_type-1)
       return u_num;
   u_num.unit_num=unit;
   u_num.offset=i;
   return u_num;
}