Revision: 41829
http://brlcad.svn.sourceforge.net/brlcad/?rev=41829&view=rev
Author: brlcad
Date: 2010-12-29 05:41:12 +0000 (Wed, 29 Dec 2010)
Log Message:
-----------
make bu_struct_put() and bu_struct_get() return size_t so they match
fwrite/fread return values and errors can be diagnosed. allow the error to be
caught by the caller instead of bombing.
Modified Paths:
--------------
brlcad/trunk/include/bu.h
brlcad/trunk/src/libbu/parse.c
Modified: brlcad/trunk/include/bu.h
===================================================================
--- brlcad/trunk/include/bu.h 2010-12-29 02:04:40 UTC (rev 41828)
+++ brlcad/trunk/include/bu.h 2010-12-29 05:41:12 UTC (rev 41829)
@@ -3618,8 +3618,12 @@
*
* Put a structure in external form to a stdio file. All formatting
* must have been accomplished previously.
+ *
+ * Returns number of bytes written. On error, a short byte count (or
+ * zero) is returned. Use feof(3) or ferror(3) to determine which
+ * errors occur.
*/
-BU_EXPORT BU_EXTERN(int bu_struct_put,
+BU_EXPORT BU_EXTERN(size_t bu_struct_put,
(FILE *fp,
const struct bu_external *ext));
@@ -3627,8 +3631,11 @@
* B U _ S T R U C T _ G E T
*
* Obtain the next structure in external form from a stdio file.
+ *
+ * Returns number of bytes read into the bu_external. On error, zero
+ * is returned.
*/
-BU_EXPORT BU_EXTERN(int bu_struct_get,
+BU_EXPORT BU_EXTERN(size_t bu_struct_get,
(struct bu_external *ext,
FILE *fp));
Modified: brlcad/trunk/src/libbu/parse.c
===================================================================
--- brlcad/trunk/src/libbu/parse.c 2010-12-29 02:04:40 UTC (rev 41828)
+++ brlcad/trunk/src/libbu/parse.c 2010-12-29 05:41:12 UTC (rev 41829)
@@ -408,35 +408,36 @@
}
-int
+size_t
bu_struct_put(FILE *fp, const struct bu_external *ext)
{
BU_CK_GETPUT(ext);
- /* FIXME: possible loss of data here */
- return (int)(fwrite(ext->ext_buf, 1, ext->ext_nbytes, fp));
+ return fwrite(ext->ext_buf, 1, ext->ext_nbytes, fp);
}
-int
+size_t
bu_struct_get(struct bu_external *ext, FILE *fp)
{
- register long i, len;
+ size_t i;
+ uint32_t len;
BU_INIT_EXTERNAL(ext);
ext->ext_buf = (genptr_t) bu_malloc(6, "bu_struct_get buffer head");
bu_semaphore_acquire(BU_SEM_SYSCALL); /* lock */
- i=(long)fread((char *) ext->ext_buf, 1, 6, fp); /* res_syscall */
+ i = fread((char *) ext->ext_buf, 1, 6, fp); /* res_syscall */
bu_semaphore_release(BU_SEM_SYSCALL); /* unlock */
if (i != 6) {
if (i == 0)
return 0;
+ perror("fread");
bu_log("ERROR: bu_struct_get bad fread (%ld), file %s, line %d\n",
i, __FILE__, __LINE__);
- bu_bomb("Bad fread");
+ return 0;
}
i = (((unsigned char *)(ext->ext_buf))[0] << 8)
@@ -457,22 +458,32 @@
ext->ext_buf = (genptr_t) bu_realloc((char *) ext->ext_buf, len,
"bu_struct_get full buffer");
bu_semaphore_acquire(BU_SEM_SYSCALL); /* lock */
- i=(long)fread((char *) ext->ext_buf + 6, 1, len-6, fp); /* res_syscall
*/
+ i = fread((char *) ext->ext_buf + 6, 1, len-6, fp); /* res_syscall
*/
bu_semaphore_release(BU_SEM_SYSCALL); /* unlock */
+
if (UNLIKELY(i != len-6)) {
bu_log("ERROR: bu_struct_get bad fread (%ld), file %s, line %d\n",
i, __FILE__, __LINE__);
- bu_bomb("Bad fread");
+ ext->ext_nbytes = 0;
+ bu_free(ext->ext_buf, "bu_struct_get full buffer");
+ ext->ext_buf = NULL;
+ return 0;
}
- i = (((unsigned char *)(ext->ext_buf))[len-2] <<8) |
- ((unsigned char *)(ext->ext_buf))[len-1];
+
+ i = (((unsigned char *)(ext->ext_buf))[len-2] << 8)
+ | ((unsigned char *)(ext->ext_buf))[len-1];
+
if (UNLIKELY(i != BU_GETPUT_MAGIC_2)) {
bu_log("ERROR: bad getput buffer %p, s/b %x, was %s(0x%lx), file %s,
line %d\n",
(void *)ext->ext_buf, BU_GETPUT_MAGIC_2,
bu_identify_magic(i), i, __FILE__, __LINE__);
- bu_bomb("Bad getput buffer");
+ ext->ext_nbytes = 0;
+ bu_free(ext->ext_buf, "bu_struct_get full buffer");
+ ext->ext_buf = NULL;
+ return 0;
}
- return 1;
+
+ return (size_t)len;
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits