GRASS Programmer's Manual  6.4.2(2012)
segment/get_row.c
Go to the documentation of this file.
00001 
00015 #include <stdio.h>
00016 #include <unistd.h>
00017 #include <string.h>
00018 #include <errno.h>
00019 #include <grass/segment.h>
00020 
00021 
00042 int segment_get_row(const SEGMENT * SEG, void *buf, int row)
00043 {
00044     int size;
00045     int ncols;
00046     int scols;
00047     int n, index, col;
00048 
00049     ncols = SEG->ncols - SEG->spill;
00050     scols = SEG->scols;
00051     size = scols * SEG->len;
00052 
00053     for (col = 0; col < ncols; col += scols) {
00054         segment_address(SEG, row, col, &n, &index);
00055         if (segment_seek(SEG, n, index) < 0)
00056             return -1;
00057 
00058         if (read(SEG->fd, buf, size) != size) {
00059             G_warning("segment_get_row: %s", strerror(errno));
00060             return -1;
00061         }
00062 
00063         /* The buf variable is a void pointer and thus points to anything. */
00064         /* Therefore, it's size is unknown and thus, it cannot be used for */
00065         /* pointer arithmetic (some compilers treat this as an error - SGI */
00066         /* MIPSPro compiler for one). Since the read command is reading in */
00067         /* "size" bytes, cast the buf variable to char * before incrementing */
00068         buf = ((char *)buf) + size;
00069     }
00070     if ((size = SEG->spill * SEG->len)) {
00071         segment_address(SEG, row, col, &n, &index);
00072         if (segment_seek(SEG, n, index) < 0)
00073             return -1;
00074 
00075         if (read(SEG->fd, buf, size) != size) {
00076             G_warning("segment_get_row: %s", strerror(errno));
00077             return -1;
00078         }
00079     }
00080 
00081     return 1;
00082 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines