#ident "$URL: svn://elmer/devel/SVN/SDDAS/trunk/libdB/FieldPut.c $ %D% SwRI" #include /* for sprintf */ #include #include #include "dbf.h" /********************* ** >FILE: FieldPut.c *********************/ /******************************************************* ** Function: int FieldPut(int dbf, int fn, void *data) ** ** Description: ** This routine replaces the info in the field with the info pointed to ** by data. ** ** Return Values: ** ***************** *****************/ dbRet_t FieldPut(SDDAS_INT dbf, SDDAS_INT fn, void *data) { dbfRecord_t *D; FieldRecord_t *fld; char *cptr; int j; if ((D = GetOldDbfHandle(dbf)) == NULL) return FAILURE; if (fn >= D->NumFields || fn < 0) { dbf_msg_clr; dbf_code = UNKNOWN_FIELD; return FAILURE; } fld = D->Fields + fn; /* | make sure we replace based on the variable type */ switch(fld->Typ) { case CHARACTER: cptr = (char *) data; for (j=0; jLen && *cptr != '\0'; j++) fld->Parm[j] = *cptr++; /* | pad with spaces */ for (; jLen; j++) { fld->Parm[j] = ' '; } break; case NUMERIC: { char dummy[20]; char fmt[64]; if (fld->Dec == 0) { /* * build format string for an integer and then put it into * a string */ sprintf(fmt, "%%%dld", fld->Len); sprintf(dummy, fmt, *((SDDAS_LONG *) data)); } else { /* * build format string for an integer and then put it into * a string */ sprintf(fmt, "%%%d.%dlf", fld->Len, fld->Dec); sprintf(dummy, fmt, *((double *) data)); } if (strlen(dummy) > fld->Len) { dbf_msg_clr; dbf_code = FIELD_VALUE_ERR; return FAILURE; } memcpy(fld->Parm, dummy, fld->Len); } break; case DATE: memcpy(fld->Parm, (char*)data, fld->Len); break; default: sprintf(msg, "'%c'", fld->Typ); dbf_code = UNKNOWN_FIELD_TYPE; break; } /* | set the status of the current record to Updated so it will be written out | on the next chance */ D->rStatus = Updated; return SUCCESS; } /************************************************************** ** Function: int FieldPutN(int dbf, char *field, void *data) ** ** Description: ** This routine replaces the info in the field with the info pointed to ** by data. ** ** Return Values: ** ***************** *****************/ dbRet_t FieldPutN(SDDAS_INT dbf, char *field, void *data) { return FieldPut(dbf, FieldNo(dbf, field), data); }