setup.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <grass/rowio.h>
  4. /*!
  5. * \brief configure rowio structure
  6. *
  7. * Rowio_setup()
  8. * initializes the ROWIO structure <b>r</b> and allocates the required memory
  9. * buffers. The file descriptor <b>fd</b> must be open for reading. The number
  10. * of rows to be held in memory is <b>nrows.</b> The length in bytes of each
  11. * row is <b>len.</b> The routine which will be called to read data from the
  12. * file is <b>getrow()</b> and must be provided by the programmer. If the
  13. * application requires that the rows be written back into the file if changed,
  14. * the file descriptor <b>fd</b> must be open for write as well, and the
  15. * programmer must provide a <b>putrow()</b> routine to write the data into
  16. * the file. If no writing of the file is to occur, specify NULL for
  17. * <b>putrow().</b>
  18. * Return codes:
  19. * 1 ok
  20. * -1 there is not enough memory for buffer allocation
  21. *
  22. * \param
  23. * \return int
  24. */
  25. int rowio_setup(ROWIO * R,
  26. int fd, int nrows, int len,
  27. int (*getrow) (int, void *, int, int),
  28. int (*putrow) (int, const void *, int, int))
  29. {
  30. int i;
  31. R->getrow = getrow;
  32. R->putrow = putrow;
  33. R->nrows = nrows;
  34. R->len = len;
  35. R->cur = -1;
  36. R->buf = NULL;
  37. R->fd = fd;
  38. R->rcb = (struct ROWIO_RCB *)malloc(nrows * sizeof(struct ROWIO_RCB));
  39. if (R->rcb == NULL) {
  40. fprintf(stderr, "rowio_setup: out of memory\n");
  41. return -1;
  42. }
  43. for (i = 0; i < nrows; i++) {
  44. R->rcb[i].buf = malloc(len);
  45. if (R->rcb[i].buf == NULL) {
  46. fprintf(stderr, "rowio_setup: out of memory\n");
  47. return -1;
  48. }
  49. R->rcb[i].row = -1; /* mark not used */
  50. }
  51. return 1;
  52. }