seg.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <grass/glocale.h>
  4. #include "seg.h"
  5. int
  6. seg_open(SSEG *sseg, int nrows, int ncols, int row_in_seg, int col_in_seg,
  7. int nsegs_in_memory, int size_struct, int fill)
  8. {
  9. char *filename;
  10. int errflag;
  11. int fd;
  12. sseg->filename = NULL;
  13. sseg->fd = -1;
  14. filename = G_tempfile();
  15. if (-1 == (fd = creat(filename, 0666))) {
  16. G_warning(_("Unable to create segment file"));
  17. return -2;
  18. }
  19. if (fill)
  20. errflag = Segment_format(fd, nrows, ncols, row_in_seg,
  21. col_in_seg, size_struct);
  22. else
  23. errflag = Segment_format_nofill(fd, nrows, ncols, row_in_seg,
  24. col_in_seg, size_struct);
  25. if (0 > errflag) {
  26. close(fd);
  27. unlink(filename);
  28. if (errflag == -1) {
  29. G_warning(_("Unable to write segment file"));
  30. return -1;
  31. }
  32. else {
  33. G_warning(_("Illegal configuration parameter(s)"));
  34. return -3;
  35. }
  36. }
  37. close(fd);
  38. if (-1 == (fd = open(filename, 2))) {
  39. unlink(filename);
  40. G_warning(_("Unable to re-open file '%s'"), filename);
  41. return -4;
  42. }
  43. if (0 > (errflag = Segment_init(&(sseg->seg), fd, nsegs_in_memory))) {
  44. close(fd);
  45. unlink(filename);
  46. if (errflag == -1) {
  47. G_warning(_("Unable to read segment file"));
  48. return -5;
  49. }
  50. else {
  51. G_warning(_("Out of memory"));
  52. return -6;
  53. }
  54. }
  55. sseg->filename = filename;
  56. sseg->fd = fd;
  57. return 0;
  58. }
  59. int seg_close(SSEG *sseg)
  60. {
  61. Segment_release(&(sseg->seg));
  62. close(sseg->fd);
  63. unlink(sseg->filename);
  64. return 0;
  65. }
  66. int seg_put(SSEG *sseg, char *value, int row, int col)
  67. {
  68. if (Segment_put(&(sseg->seg), value, row, col) < 0) {
  69. G_warning(_("Unable to write segment file"));
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. int seg_put_row(SSEG *sseg, char *value, int row)
  75. {
  76. if (Segment_put_row(&(sseg->seg), value, row) < 0) {
  77. G_warning(_("seg_put_row(): could not write segment file"));
  78. return -1;
  79. }
  80. return 0;
  81. }
  82. int seg_get(SSEG *sseg, char *value, int row, int col)
  83. {
  84. if (Segment_get(&(sseg->seg), value, row, col) < 0) {
  85. G_warning(_("Unable to read segment file"));
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. int seg_get_row(SSEG *sseg, char *value, int row)
  91. {
  92. if (Segment_get_row(&(sseg->seg), value, row) < 0) {
  93. G_warning(_("Unable to read segment file"));
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. int seg_flush(SSEG *sseg)
  99. {
  100. Segment_flush(&(sseg->seg));
  101. return 0;
  102. }