util.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <stdio.h>
  2. #include <string.h>
  3. #ifdef __MINGW32__
  4. #include <process.h>
  5. #else
  6. #include <sys/wait.h>
  7. #endif
  8. #include <grass/gis.h>
  9. #include <grass/raster.h>
  10. #include <grass/dbmi.h>
  11. #include <grass/vector.h>
  12. #include <grass/glocale.h>
  13. #include "global.h"
  14. /* function prototypes */
  15. static int blank_line(void *buf);
  16. /* move - move to next point in line */
  17. struct COOR *move(struct COOR *point)
  18. {
  19. if (direction == FORWARD) {
  20. if (point->fptr == NULL) /* at open end of line */
  21. return (NULL);
  22. if (point->fptr->fptr == point) /* direction change coming up */
  23. direction = BACKWARD;
  24. return (point->fptr);
  25. }
  26. else {
  27. if (point->bptr == NULL)
  28. return (NULL);
  29. if (point->bptr->bptr == point)
  30. direction = FORWARD;
  31. return (point->bptr);
  32. }
  33. }
  34. /* find_end - search for end of line, starting at a given point and */
  35. /* moving in a given direction */
  36. struct COOR *find_end(struct COOR *seed, int dir, int *result, int *n)
  37. {
  38. struct COOR *start;
  39. start = seed;
  40. direction = dir;
  41. *result = *n = 0;
  42. while (!*result) {
  43. seed = move(seed);
  44. (*n)++;
  45. if (seed == start)
  46. *result = LOOP;
  47. else {
  48. if (seed == NULL)
  49. *result = OPEN;
  50. else {
  51. if (at_end(seed))
  52. *result = END;
  53. }
  54. }
  55. }
  56. return (seed);
  57. }
  58. /* at_end - test whether a point is at the end of a line; if so, give */
  59. /* the direction in which to move to go away from that end */
  60. int at_end(struct COOR *ptr)
  61. {
  62. if (ptr->fptr == ptr)
  63. return (BACKWARD);
  64. if (ptr->bptr == ptr)
  65. return (FORWARD);
  66. return (0);
  67. }
  68. int read_row(void *buf)
  69. {
  70. void *p;
  71. if (last_read)
  72. return (0);
  73. if (first_read) {
  74. blank_line(buf);
  75. first_read = 0;
  76. }
  77. else {
  78. if (row_count >= n_rows) {
  79. last_read = 1;
  80. blank_line(buf);
  81. }
  82. else {
  83. /* The buf variable is a void pointer and thus */
  84. /* points to anything. Therefore, it's size is */
  85. /* unknown and thus, it cannot be used for pointer */
  86. /* arithmetic (some compilers treat this as an error */
  87. /* - SGI MIPSPro compiler for one). Make the */
  88. /* assumption that data_size is the proper number of */
  89. /* bytes and cast the buf variable to char * before */
  90. /* incrementing */
  91. p = ((char *)buf) + data_size;
  92. Rast_get_row(input_fd, p, row_count++, data_type);
  93. p = buf;
  94. Rast_set_null_value(p, 1, data_type);
  95. /* Again we need to cast p to char * under the */
  96. /* assumption that the increment is the proper */
  97. /* number of bytes. */
  98. p = ((char *)p) + (row_length + 1) * data_size;
  99. Rast_set_null_value(p, 1, data_type);
  100. }
  101. }
  102. return (row_length + 2);
  103. }
  104. static int blank_line(void *buf)
  105. {
  106. Rast_set_null_value(buf, row_length + 2, data_type);
  107. return 0;
  108. }
  109. /* insert values into the table (cat, val | dval) */
  110. void insert_value(int cat, int val, double dval)
  111. {
  112. char buf[1000];
  113. sprintf(buf, "insert into %s values (%d", Fi->table, cat);
  114. db_set_string(&sql, buf);
  115. if (data_type == CELL_TYPE)
  116. sprintf(buf, ", %d", val);
  117. else
  118. sprintf(buf, ", %f", dval);
  119. db_append_string(&sql, buf);
  120. if (has_cats) {
  121. char *lab;
  122. lab = Rast_get_c_cat(&val, &RastCats); /*cats are loaded only for CELL type */
  123. db_set_string(&label, lab);
  124. db_double_quote_string(&label);
  125. sprintf(buf, ", '%s'", db_get_string(&label));
  126. db_append_string(&sql, buf);
  127. }
  128. db_append_string(&sql, ")");
  129. G_debug(3, "%s", db_get_string(&sql));
  130. if (db_execute_immediate(driver, &sql) != DB_OK)
  131. G_fatal_error(_("Cannot insert new row: %s"), db_get_string(&sql));
  132. }
  133. int free_ptr(struct COOR *ptr)
  134. {
  135. G_free(ptr);
  136. return (--n_alloced_ptrs);
  137. }