wind_scan.c 2.9 KB

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