setup.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*!
  2. \file rowio/setup.c
  3. \brief RowIO library - Setup
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <grass/gis.h>
  12. #include <grass/glocale.h>
  13. #include <grass/rowio.h>
  14. /*!
  15. * \brief Configure rowio structure
  16. *
  17. * Rowio_setup() initializes the ROWIO structure <i>r</i> and
  18. * allocates the required memory buffers. The file descriptor
  19. * <i>fd</i> must be open for reading. The number of rows to be held
  20. * in memory is <i>nrows</i>. The length in bytes of each row is
  21. * <i>len</i>. The routine which will be called to read data from the
  22. * file is getrow() and must be provided by the programmer. If the
  23. * application requires that the rows be written back into the file if
  24. * changed, the file descriptor <i>fd</i> must be open for write as
  25. * well, and the programmer must provide a putrow() routine to write
  26. * the data into the file. If no writing of the file is to occur,
  27. * specify NULL for putrow().
  28. *
  29. * \param R pointer to ROWIO structure
  30. * \param fd file descriptor
  31. * \param nrows number of rows
  32. * \param getrow get row function
  33. *
  34. * \return 1 on success
  35. * \return -1 no memory
  36. */
  37. int Rowio_setup(ROWIO * R,
  38. int fd, int nrows, int len,
  39. int (*getrow) (int, void *, int, int),
  40. int (*putrow) (int, const void *, int, int))
  41. {
  42. int i;
  43. R->getrow = getrow;
  44. R->putrow = putrow;
  45. R->nrows = nrows;
  46. R->len = len;
  47. R->cur = -1;
  48. R->buf = NULL;
  49. R->fd = fd;
  50. R->rcb = (struct ROWIO_RCB *) G_malloc(nrows * sizeof(struct ROWIO_RCB));
  51. if (R->rcb == NULL) {
  52. G_warning(_("Out of memory"));
  53. return -1;
  54. }
  55. for (i = 0; i < nrows; i++) {
  56. R->rcb[i].buf = G_malloc(len);
  57. if (R->rcb[i].buf == NULL) {
  58. G_warning(_("Out of memory"));
  59. return -1;
  60. }
  61. R->rcb[i].row = -1; /* mark not used */
  62. }
  63. return 1;
  64. }