Example #1
0
/* write a legend
 * MputLegend writes a (version 2) legend to a map replacing
 * the old one if existent.
 * See  csfattr.h for the legend structure.
 *
 * returns 
 * 0 in case of an error,
 * nonzero otherwise
 *
 * Merrno
 * NOACCESS
 * WRITE_ERROR
 */
int MputLegend(
	MAP *m,        /* Map handle */
	CSF_LEGEND *l, /* read-write, array with name and entries, the entries
	                * are sorted before writing to the file.
	                * Strings are padded with zeros.
	                */
	size_t nrEntries) /* number of array elements. That is name plus real legend entries */
{
	int i = NrLegendEntries(m);
	CSF_ATTR_ID id = i < 0 ? ATTR_ID_LEGEND_V1 : ATTR_ID_LEGEND_V2;
	if (i)
		if (! MdelAttribute(m, id))
			return 0;
	SortEntries(l, nrEntries);
	if (CsfSeekAttrSpace(m, ATTR_ID_LEGEND_V2, (size_t)(nrEntries*CSF_LEGEND_ENTRY_SIZE)) == 0)
			return 0;
	for(i = 0; i < (int)nrEntries; i++)
	{
	     if(
		m->write(&(l[i].nr), sizeof(INT4), (size_t)1, m->fp) != 1 ||
		m->write(
		 CsfStringPad(l[i].descr,(size_t)CSF_LEGEND_DESCR_SIZE), 
		 sizeof(char), (size_t)CSF_LEGEND_DESCR_SIZE, m->fp) 
		 != CSF_LEGEND_DESCR_SIZE )
		 {
		 	M_ERROR(WRITE_ERROR);
		 	return 0;
		 }
	}
	return 1;
}
Example #2
0
/* write an attribute to a map (LIBRARY_INTERNAL)
 * MputAttribute writes exactly the number of bytes specified
 * by the size argument starting at the address of argument
 * attr. Which means that you can't simply pass a structure or an
 * array of structures as argument attr, due to the alignment
 * of fields within a structure and internal swapping. You can
 * only pass an array of elementary types (UINT1, REAL4, etc.)
 * or character string.
 * If one wants to refresh an attribute, one should first
 * call MdelAttribute to delete the attribute and then use
 * MputAttribute to write the new value.
 * returns argument id or 0 in case of error.
 *
 * Merrno
 * ATTRDUPL
 * NOACCESS
 * WRITE_ERROR
 */
CSF_ATTR_ID CsfPutAttribute(
	MAP *m,       		/* map handle */
	CSF_ATTR_ID id,               /* attribute identification */
	size_t itemSize,        /* size of each attribute element.
	                         * 1 or sizeof(char) in case of a
	                         * string
	                         */
	size_t nitems,          /* number of attribute elements or
	                         * strlen+1 in case of a variable character
	                         * string field. Don't forget to pad a
	                         * non-variable field with '\0'!
	                         */
	void *attr)       /* buffer containing attribute */
{
	size_t size = nitems * itemSize;

	PRECOND(CsfValidSize(itemSize));
	PRECOND(size > 0);

	if (CsfSeekAttrSpace(m,id,size) == 0)
		goto error;

	if (m->write(attr, itemSize, nitems, m->fp) != nitems)
	{
		M_ERROR(WRITE_ERROR);
		goto error;
	}
	return(id); 		/* succes */
error:	return(0);	/* failure */
}