GRASS Programmer's Manual  6.4.2(2012)
segment/put_row.c
Go to the documentation of this file.
00001 
00015 #include <stdio.h>
00016 #include <string.h>
00017 #include <errno.h>
00018 #include <unistd.h>
00019 #include <grass/segment.h>
00020 #include <grass/gis.h>
00021 
00022 
00023 /*      buf is CELL *   WRAT code       */
00024 /* int segment_put_row (SEGMENT *SEG, CELL *buf,int row) */
00025 
00026 
00046 int segment_put_row(const SEGMENT * SEG, const void *buf, int row)
00047 {
00048     int size;
00049     int ncols;
00050     int scols;
00051     int n, index, col;
00052     int result;
00053 
00054     ncols = SEG->ncols - SEG->spill;
00055     scols = SEG->scols;
00056     size = scols * SEG->len;
00057     /*      printf("segment_put_row ncols: %d, scols %d, size: %d, col %d, row: %d,  SEG->fd: %d\n",ncols,scols,size,col,row, SEG->fd); */
00058 
00059     for (col = 0; col < ncols; col += scols) {
00060         segment_address(SEG, row, col, &n, &index);
00061         if (segment_seek(SEG, n, index) < 0) {
00062             G_warning
00063                 ("Failed seek in segment file for index = %d n = %d at col:row %d:%d",
00064                  index, n, col, row);
00065             return -1;
00066         }
00067 
00068         if ((result = write(SEG->fd, buf, size)) != size) {
00069             G_warning("segment_put_row write error %s", strerror(errno));
00070             /*      printf("segment_put_row result = %d. ncols: %d, scols %d, size: %d, col %d, row: %d,  SEG->fd: %d\n",result,ncols,scols,size,col,row, SEG->fd); */
00071             return -1;
00072         }
00073 
00074         /* The buf variable is a void pointer and thus points to anything. */
00075         /* Therefore, it's size is unknown and thus, it cannot be used for */
00076         /* pointer arithmetic (some compilers treat this as an error - SGI */
00077         /* MIPSPro compiler for one). Since the read command is reading in */
00078         /* "size" bytes, cast the buf variable to char * before incrementing */
00079         buf = ((const char *)buf) + size;
00080     }
00081 
00082     if ((size = SEG->spill * SEG->len)) {
00083         segment_address(SEG, row, col, &n, &index);
00084         if (segment_seek(SEG, n, index) < 0) {
00085             G_warning
00086                 ("Failed seek in segment file for index = %d n = %d at col:row %d:%d",
00087                  index, n, col, row);
00088             return -1;
00089         }
00090         if (write(SEG->fd, buf, size) != size) {
00091             G_warning("segment_put_row final write error: %s",
00092                       strerror(errno));
00093             return -1;
00094         }
00095     }
00096 
00097     return 1;
00098 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines