chrtoi.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. /*
  11. * Glenn Fowler
  12. * AT&T Bell Laboratories
  13. *
  14. * convert a 0 terminated character constant string to an int
  15. */
  16. #include <ast/ast.h>
  17. #include <limits.h>
  18. #include <stddef.h>
  19. int chrtoi(const char *s)
  20. {
  21. int c;
  22. int x;
  23. char *p;
  24. c = 0;
  25. for (size_t n = 0; n < sizeof(int) * CHAR_BIT; n += CHAR_BIT) {
  26. switch (x = *((const unsigned char *) s++)) {
  27. case '\\':
  28. x = chresc(s - 1, &p);
  29. s = p;
  30. break;
  31. case 0:
  32. return (c);
  33. default: // nothing required
  34. break;
  35. }
  36. c = (c << CHAR_BIT) | x;
  37. }
  38. return (c);
  39. }