wind_scan.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * \file wind_scan.c
  3. *
  4. * \brief GIS Library - Window scanning functions.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 1999-2008
  14. */
  15. #include <stdio.h>
  16. #include <grass/gis.h>
  17. static int scan_double(const char *, double *);
  18. /**
  19. * \brief ASCII northing to double.
  20. *
  21. * Converts the ASCII "northing" coordinate string in <b>buf</b> to its
  22. * double representation (into <b>northing</b>).
  23. *
  24. * \param[in] buf buffer hold string northing
  25. * \param[in,out] northing
  26. * \param[in] projection
  27. * \return 0 on error
  28. * \return 1 on success
  29. */
  30. int G_scan_northing(const char *buf, double *northing, int projection)
  31. {
  32. if (projection == PROJECTION_LL) {
  33. if (G_lat_scan(buf, northing))
  34. return 1;
  35. if (!scan_double(buf, northing))
  36. return 0;
  37. return (*northing <= 90.0 && *northing >= -90.0);
  38. }
  39. return scan_double(buf, northing);
  40. }
  41. /**
  42. * \brief ASCII easting to double.
  43. *
  44. * Converts the ASCII "easting" coordinate string in <b>buf</b> to its
  45. * double representation (into <b>easting</b>).
  46. *
  47. * \param[in] buf buffer containing string easting
  48. * \param[in,out] easting
  49. * \param[in] projection
  50. * \return 0 on error
  51. * \return 1 on success
  52. */
  53. int G_scan_easting(const char *buf, double *easting, int projection)
  54. {
  55. if (projection == PROJECTION_LL) {
  56. if (G_lon_scan(buf, easting))
  57. return 1;
  58. if (!scan_double(buf, easting))
  59. return 0;
  60. while (*easting > 180.0)
  61. *easting -= 360.0;
  62. while (*easting < -180.0)
  63. *easting += 360.0;
  64. return 1;
  65. }
  66. return scan_double(buf, easting);
  67. }
  68. /**
  69. * \brief ASCII resolution to double.
  70. *
  71. * Converts the ASCII "resolution" string in <b>buf</b> to its double
  72. * representation (into resolution).
  73. *
  74. * \param[in] buf buffer containing string resolution
  75. * \param[in,out] resolution
  76. * \param[in] projection
  77. * \return 0 on error
  78. * \return 1 on success
  79. */
  80. int G_scan_resolution(const char *buf, double *res, int projection)
  81. {
  82. if (projection == PROJECTION_LL) {
  83. if (G_llres_scan(buf, res))
  84. return 1;
  85. }
  86. return (scan_double(buf, res) && *res > 0.0);
  87. }
  88. static int scan_double(const char *buf, double *value)
  89. {
  90. char junk[2];
  91. /* use sscanf to convert buf to double
  92. * make sure value doesn't have other characters after it */
  93. *junk = 0;
  94. *value = 0.0;
  95. if (sscanf(buf, "%lf%1s", value, junk) == 1 && *junk == 0) {
  96. while (*buf)
  97. buf++;
  98. buf--;
  99. if (*buf >= 'A' && *buf <= 'Z')
  100. return 0;
  101. if (*buf >= 'a' && *buf <= 'z')
  102. return 0;
  103. return 1; /* success */
  104. }
  105. return 0; /* failure */
  106. }