sexp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. ppedit - A pattern plate editor for Spiro splines.
  3. Copyright (C) 2007 Raph Levien
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include "sexp.h"
  20. /* This is handcoded to avoid locale problems. */
  21. static int
  22. parse_double(sexp_reader *sr)
  23. {
  24. double sign = 1.0, val = 0.0;
  25. int i;
  26. int numstart;
  27. const char * const b = sr->tokbuf;
  28. int is_double = 1;
  29. i = 0;
  30. if (b[i] == '+') {
  31. i++;
  32. } else if (b[i] == '-') {
  33. sign = -1.0;
  34. i++;
  35. }
  36. numstart = i;
  37. while (b[i] >= '0' && b[i] <= '9')
  38. val = val * 10.0 + b[i++] - '0';
  39. if (b[i] == '.') {
  40. double frac = 1.0;
  41. for (i++; b[i] >= '0' && b[i] <= '9'; i++) {
  42. frac *= 0.1;
  43. val += (b[i] - '0') * frac;
  44. }
  45. /* A '.' without any digits on either side isn't valid. */
  46. if (i == numstart + 1)
  47. is_double = 0;
  48. }
  49. if (b[i] == 'e' || b[i] == 'E') {
  50. int expsign = 1, exp = 0;
  51. int expstart;
  52. if (b[i] == '+') {
  53. i++;
  54. } else if (b[i] == '-') {
  55. expsign = -1;
  56. i++;
  57. }
  58. expstart = i;
  59. while (b[i] >= '0' && b[i] <= '9')
  60. exp = exp * 10 + b[i++] - '0';
  61. if (i == expstart)
  62. is_double = 0;
  63. val *= pow(10.0, expsign * exp);
  64. }
  65. val *= sign;
  66. sr->d = val;
  67. if (b[i] != 0) is_double = 0;
  68. sr->is_double = is_double;
  69. return is_double;
  70. }
  71. /* Return values: 0 = EOF, 1 = token but not double, 2 = valid double */
  72. int
  73. sexp_token(sexp_reader *sr)
  74. {
  75. int c;
  76. int i;
  77. sr->singlechar = -1;
  78. if (sr->f == NULL)
  79. return 0;
  80. for (;;) {
  81. c = getc(sr->f);
  82. if (c == EOF) {
  83. sr->f = NULL;
  84. return 0;
  85. } else if (c == '#') {
  86. do {
  87. c = getc(sr->f);
  88. } while (c != EOF && c != '\r' && c != '\n');
  89. } else if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
  90. break;
  91. }
  92. sr->tokbuf[0] = c;
  93. i = 1;
  94. if (c != '(' && c != ')') {
  95. for (;;) {
  96. c = getc(sr->f);
  97. if (c == EOF) {
  98. sr->f = NULL;
  99. break;
  100. } else if (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
  101. break;
  102. } else if (c == '(' || c == ')' || c == '#') {
  103. ungetc(c, sr->f);
  104. break;
  105. } else if (i < sizeof(sr->tokbuf) - 1)
  106. sr->tokbuf[i++] = c;
  107. }
  108. }
  109. sr->tokbuf[i] = 0;
  110. if (i == 1)
  111. sr->singlechar = sr->tokbuf[0];
  112. return 1 + parse_double(sr);
  113. }