Re: [CODE] More fun!

From: Mike Breuer (mbreuer@new.rr.com)
Date: 06/05/01


From: "Peter Ajamian" <peter@PAJAMIAN.DHS.ORG>
> You can use strtol() instead since it has defined behavior in an
> out-of-range situation:

You're right, of course.  No more posting messages after midnight for me.

> I'm not sure of this so someone correct me if I'm wrong, but I think that
> it would be undefined to do something like use strtoll() (on a C99
> compiler) and cast the result to an unsigned long if the result is out of
> range for an unsigned long so you should probably avoid that solution.

strtoll() would not work on a signed long if the value is negative, as the
sign bit would not be in the same place.  Here is a slight modification to the
code that I posted last night which will allow for both signed and unsigned
values (based on your suggestion).  This change would allow you to modify all
of your bitvectors to be unsigned while still maintaining backward
compatibility in your mob (and other) files:

-long asciiflag_conv(char *flag)
+unsigned long asciiflag_conv(char *flag)
{
- long flags = 0;
+ unsigned long flags = 0;
  int is_number = 1;
  int is_negative = 0;
  register char *p;

+ p = flag;
+ if (*p == '-') {
+   p++;
+   is_negative = 1;
+ }
-  for (p = flag; *p; p++) {
+  for (;*p; p++) {
    if (islower(*p))
      flags |= 1 << (*p - 'a');
    else if (isupper(*p))
      flags |= 1 << (26 + (*p - 'A'));

    if (!isdigit(*p))
      is_number = 0;
  }

  if (is_number)
-   flags = atol(flag);
+   flags = (is_negative) ? ((unsigned long)atol(flag)) : strtoul(flag, NULL,
10);

  return (flags);
}

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/05/01 PST