chk_scale.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "local_proto.h"
  4. /****************************************************************
  5. *
  6. * check_scale (text)
  7. *
  8. * text of scale request to be checked
  9. ***************************************************************/
  10. int check_scale(char *text)
  11. {
  12. char unit1[30];
  13. char unit2[30];
  14. char equals[30];
  15. char dummy[2];
  16. long n1, n2;
  17. double u1;
  18. /*
  19. * absolute horizontal width specification
  20. * x inches
  21. * x panels
  22. * convert text to 1 : n
  23. */
  24. u1 = 0;
  25. *unit1 = 0;
  26. *dummy = 0;
  27. if (sscanf(text, "%lf %s %1s", &u1, unit1, dummy) == 2 && *dummy == 0) {
  28. if (strncmp(unit1, "panel", 5) == 0 && u1 > 0)
  29. return 1;
  30. if (strncmp(unit1, "inch", 4) == 0 && u1 > 0)
  31. return 1;
  32. }
  33. /*
  34. * unitless ratio specification
  35. * n : m
  36. */
  37. *dummy = 0;
  38. n1 = n2 = 0;
  39. if (sscanf(text, "%ld : %ld%1s", &n1, &n2, dummy) == 2) {
  40. if (n1 <= 0 || n2 <= 0 || *dummy)
  41. return 0;
  42. return 1;
  43. }
  44. /*
  45. *
  46. * ratio specification with unit conversions
  47. * x inches equals y miles
  48. * x inches equals y meters
  49. * x inches equals y kilometers
  50. */
  51. *unit1 = 0;
  52. *unit2 = 0;
  53. *equals = 0;
  54. n1 = n2 = 0;
  55. if (sscanf(text, "%ld %s %s %ld %s", &n1, unit1, equals, &n2, unit2) == 5) {
  56. if (n1 <= 0 || n2 <= 0)
  57. return 0;
  58. if (strcmp(equals, "=") != 0 && strncmp(equals, "equal", 5) != 0)
  59. return 0;
  60. /* unit1: inches */
  61. if (strncmp(unit1, "inch", 4) == 0)
  62. u1 = 1;
  63. else
  64. return 0;
  65. /* unit2: meters, miles, kilometers */
  66. if (strncmp(unit2, "mile", 4) == 0)
  67. return 1;
  68. if (strncmp(unit2, "meter", 5) == 0)
  69. return 1;
  70. if (strncmp(unit2, "kilometer", 9) == 0)
  71. return 1;
  72. }
  73. return 0;
  74. }