Guide to the prdesc.c file

The prdesc.c file contains the prdesc() function. This function sets the pointer p to the address that is provided in the loc_buffer field of the locator structure to access the simple large object. The function then reads the text from the buffer 80 bytes at a time up to the size specified in loc_size. This function is used in several of the simple-large-object demonstration programs so it is in a separate file and included in the appropriate source files.

=======================================================================
1. /* prdesc() prints cat_desc for a row in the catalog table */
2. prdesc()
3. {
4.       int4 size;
5.       char shdesc[81], *p;
6.       size = cat_descr.loc_size;    /* get size of data */
7.       printf("Description for %ld:\n", cat_num);
8.       p = cat_descr.loc_buffer;    /* set p to buffer addr */
9.     /* print buffer 80 characters at a time */
10.       while(size >= 80)
11.          {
12.          ldchar(p, 80, shdesc);      /* mv from buffer to shdesc */
13.          printf("\n%80s", shdesc);   /* display it  */
14.          size -= 80;                 /* decrement length */
15.          p += 80;                    /* bump p by 80 */
16.          }
17.        strncpy(shdesc, p, size);
18.       shdesc[size] = '\0';
19.       printf("%-s\n", shdesc);       /* display last segment */
20. }
=======================================================================

Lines 1 - 20

Lines 2 - 20 make up the main() function, which displays the cat_descr column of the catalog table. Line 4 defines size, a long integer that main() initializes with the value in cat_descr.loc_size. Line 5 defines shdesc[81], an array into which main() temporarily moves 80-byte chunks of the cat_descr text for output. Line 5 also defines *p, a pointer that marks the current position in the buffer as it is being displayed.

In loc_size, the database server returns the size of the buffer that it allocates for a simple large object. Line 6 moves cat_descr.loc_size to size. Line 7 displays the string "Description for:" as a header for the cat_descr text. Line 8 sets the p pointer to the buffer address that the database server returned in cat_descr.loc_size.

Line 10 begins the loop that displays the cat_descr text to the user. The while() repeats the loop until size is less than 80. Line 11 begins the body of the loop. The ldchar() library function copies 80 bytes from the current position in the buffer, which p addresses, to shdesc[] and removes any trailing blanks. Line 13 prints the contents of shdesc[]. Line 14 subtracts 80 from size to account for the portion of the buffer that was printed. Line 15, the last in the loop, adds 80 to p to move it past the portion of the buffer that was displayed.

The process of displaying cat_descr.loc_size 80 bytes at a time continues until fewer than 80 characters are left to be displayed (size < 80). Line 17 copies the remainder of the buffer into shdesc[] for the length of size. Line 18 appends a null to shdesc[size] to mark the end of the array and line 19 displays shdesc[].