Skip to main content.
home | support | download

Back to List Archive

Document properties - code sample

Date: Fri Sep 03 1999 - 13:01:16 GMT
Hello  all,

Here is the piece of code from docprop.c (ver 1.3.2) that was modified to 
store and restore the non-portable shorts in network order. It's way more 
elegant than the peice before. Thanks to moo. It was compiled but not 
tested. A file must be included for it to work:

#include <netinet/in.h>

docprop.c lines 64 - 161

void storeDocProperties(docProperties, fp)
     struct docPropertyEntry *docProperties;
     FILE *fp;
  * Dump the document properties into the index file 
  * The format is:
  * <PropID:int><PropValueLen:int><PropValue:null-terminated>
  *   ...
  * <PropID:int><PropValueLen:int><PropValue:null-terminated>
  * <EndofList:int>
  * The list is terminated with a PropID with a value of zero
 short int propID;
 short int len;
 short int net_len; /* length ordered in network order  */

 while (docProperties != NULL)
  /* the length of the property value */
  len = (short int) strlen(docProperties->propValue);
  if (len > 0)
   /* the ID of the property */
   propID = htons((short int) docProperties->metaName);
    /* Convert from host to network format */

   fwrite(&propID, sizeof(propID), 1, fp);
   /* including the length will make retrieval faster */
   net_len = htons(len); /* Convert from host to network format */
   fwrite(&net_len, sizeof(net_len), 1, fp);

   fwrite(docProperties->propValue, len+1, 1, fp);

  docProperties = docProperties->next;

 /* set is terminated by a "zero" ID  */
 propID = 0;
 fwrite(&propID, sizeof(propID), 1, fp);

static char* readNextDocPropEntry(fp, metaName, targetMetaName)
      FILE* fp;
      int* metaName;
      int targetMetaName;
 /* read one entry and return it; also set the metaName.
   * if targetMetaName is zero then return values for all entries.
  * if targetMetaName is non-zero than only return the value
  * for the property matching that value.
  * In all cases, metaName will be zero when the end of the
  * set is reached.
 static char* propValueBuf = NULL;
 static int propValueBufLen = 0;

 short int tempPropID;
 short int len;
 long propPos; /* file pos */

 fread(&tempPropID, sizeof(tempPropID), 1, fp);
 tempPropID = ntohs(tempPropID); /* Convert from network format to host */

 *metaName = (int) tempPropID;

 if (tempPropID == 0)
  return NULL;  /* end of list */

 /* grab the string length */
 fread(&len, sizeof(len), 1, fp);
  len = ntohs(len); /* Convert from network format to host */

 if ((targetMetaName != 0) && (tempPropID != (short int) targetMetaName))
  /* we were looking for something specific, and this is not it */
  /* move to the next property */
  propPos = ftell(fp);
  fseek(fp, propPos+len+1, 0);
  return "";
  /* return the value */
  if (propValueBufLen < len+1)
   /* allocate buffer for prop value */
   /* the buffer will be reused on the next call */
   propValueBufLen = len+100;
   propValueBuf = (char *) emalloc(propValueBufLen);
  fread(propValueBuf, len+1, 1, fp);
  return propValueBuf;

-----Original Message-----
From: []
Sent: 3 septembre 1999 04:35
Subject: [SWISH-E] Re: Document properties

>> big endian without a formal proof - anyone to confim this?
> I don't know, it's not in the man page, and no one I ask knows.  If you
> want to write portable code, use network order.  It will be easier to

Here is documentation on the network byte-order conversion functions in the
GNU C Library.

_BIG_ENDIAN and _LITTLE_ENDIAN are macros provided by many compilers.  They
shouldn't be necessary if the htonl, htons, ntohl, and ntohs functions are
usable across platforms.

As a note: The network byte order is big endian.  Intel 16/32-bit 80x86 
are little endian; SUN SPARCs and Compaq Alpha are big endian; Motorola/IBM
PowerPC, Intel ArmStrong (8096x), Intel IA-64 (64-bit Pentium or whatever),
and other newer CPUs are swappable between big or little endian in hardware
depending on the operating system's requirements.  (Furthermore, 64-bit
Linux runs as little endian on IA-64 while it runs as big endian on Alpha
and SPARC.)

,David Norris

World Wide Web -
Page via mail -
ICQ Universal Internet Number - 412039
E-Mail -
Received on Fri Sep 3 05:56:05 1999