write.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <grass/raster.h>
  6. #include <grass/glocale.h>
  7. #include "global.h"
  8. int write_matrix(int row, int col)
  9. {
  10. int n;
  11. off_t offset;
  12. select_target_env();
  13. if (!temp_fd) {
  14. temp_name = G_tempfile();
  15. temp_fd = creat(temp_name, 0660);
  16. }
  17. for (n = 0; n < matrix_rows; n++) {
  18. offset =
  19. ((off_t) row++ * target_window.cols +
  20. col) * Rast_cell_size(map_type);
  21. lseek(temp_fd, offset, SEEK_SET);
  22. if (write(temp_fd, cell_buf[n], Rast_cell_size(map_type) * matrix_cols)
  23. != Rast_cell_size(map_type) * matrix_cols) {
  24. unlink(temp_name);
  25. G_fatal_error(_("Error while writing to temp file"));
  26. }
  27. /*Rast_put_c_row_random (outfd, cell_buf[n], row++, col, matrix_cols); */
  28. }
  29. select_current_env();
  30. return 0;
  31. }
  32. int write_map(char *name)
  33. {
  34. int fd, row;
  35. void *rast;
  36. Rast_set_output_window(&target_window);
  37. /* working with split windows, can not use Rast_allocate_buf(map_type); */
  38. rast = G_malloc(target_window.cols * Rast_cell_size(map_type));
  39. close(temp_fd);
  40. temp_fd = open(temp_name, 0);
  41. fd = Rast_open_new(name, map_type);
  42. for (row = 0; row < target_window.rows; row++) {
  43. if (read(temp_fd, rast, target_window.cols * Rast_cell_size(map_type))
  44. != target_window.cols * Rast_cell_size(map_type))
  45. G_fatal_error(_("Error writing row %d"), row);
  46. Rast_put_row(fd, rast, map_type);
  47. }
  48. close(temp_fd);
  49. unlink(temp_name);
  50. Rast_close(fd);
  51. G_free(rast);
  52. return 0;
  53. }