There are a number of routines in 'C' that convert ascii to int/long/etc. for you. Very useful. Problem is, they return '0' when there's an error! Here's where exception handling would really solve the problem. Well, here's how I attempted to get around this problem 'cheap and dirty'. I'm sure there's a better way in straight 'C', and if so please post it!

BOOL isOkULONG( char* inputBuf, ULONG* result)
{
   //char copyBuf[MAX_PATH];
   LONG readInLong;

   readInLong = strtoul(inputBuf, NULL, 0);
   
   if (errno == ERANGE)
   {    return FALSE;
   }

   // if we get a zero, make sure there's actually a zero in the
   //  string.  Otherwise, we might've just gotten whitespace or some
   //  garbage.  This isn't totally foolproof but should catch most
   //  of the cases.

   if (readInLong == 0 && strrchr( inputBuf, '0' ) == NULL)
   {    return FALSE;
   }

   *result = readInLong;
   return TRUE;
}

-- MattWalsh - 27 Feb 2002

Topic revision: r1 - 28 Feb 2002 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback