getformat.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include "format.h"
  4. int getformat(FILE * fd, int raster_type, int *null)
  5. {
  6. char buf[1024];
  7. long x;
  8. CELL max, min, cat;
  9. unsigned char Umin, Umax;
  10. char Cmin, Cmax;
  11. short Smin, Smax;
  12. int first;
  13. if (raster_type == FCELL_TYPE)
  14. return USE_FCELL;
  15. if (raster_type == DCELL_TYPE)
  16. return USE_DCELL;
  17. if (null)
  18. return USE_CELL;
  19. max = min = 0;
  20. first = 1;
  21. G_fseek(fd, 0L, 0);
  22. while (G_getl2(buf, (sizeof buf) - 1, fd)) {
  23. G_strip(buf);
  24. if (*buf != '=')
  25. continue;
  26. if (sscanf(buf + 1, "%ld", &x) != 1)
  27. continue;
  28. cat = (CELL) x;
  29. /* if we want to write zeros, we must use CELL */
  30. if (cat == 0)
  31. return USE_CELL;
  32. if (first) {
  33. first = 0;
  34. max = cat;
  35. min = cat;
  36. }
  37. else if (cat > max)
  38. max = cat;
  39. else if (cat < min)
  40. min = cat;
  41. }
  42. /* test char */
  43. Cmax = (char)max;
  44. Cmin = (char)min;
  45. if (Cmin == min && Cmax == max)
  46. return (USE_CHAR);
  47. /* test unsigned char */
  48. Umax = (unsigned char)max;
  49. Umin = (unsigned char)min;
  50. if (Umin == min && Umax == max)
  51. return (USE_UCHAR);
  52. /* test short */
  53. Smax = (short)max;
  54. Smin = (short)min;
  55. if (Smin == min && Smax == max)
  56. return (USE_SHORT);
  57. return (USE_CELL);
  58. }