fields.cpp
来自「一个通讯管理机的源代码。比较好用。推荐」· C++ 代码 · 共 735 行 · 第 1/2 页
CPP
735 行
if( SchemaPtr[FieldNo].Type == 'C' && SchemaPtr[FieldNo].NoOfDecs > 0 ) length = SchemaPtr[FieldNo].LongFieldLen; else length = SchemaPtr[FieldNo].FieldLen; if( RecBufSw ) sf.assign( xbString(SchemaPtr[FieldNo].Address2, length), 0, length );// sf.assign( SchemaPtr[FieldNo].Address2, length ); else sf.assign( xbString(SchemaPtr[FieldNo].Address, length), 0, length );// sf.assign( SchemaPtr[FieldNo].Address, length ); return( length ); }/************************************************************************//* This function fills a field in the record buffer with data from a buffer for a particular field. Use GetFieldNo to get a number based on a field's name Field type N or F is loaded as right justified, left blank filled. Other fields are loaded as left justified, right blank filled. This method does check the data's validity. If successful, this function returns 0, if invalid data, it returns -1 or XB_INVALID_FIELDNO*///! Put a value into the specified field./*!*/xbShort xbDbf::PutField(const char *Name, const char *buf) { return PutField(GetFieldNo(Name), buf);}//! Put a raw value into the specified field./*!*/xbShort xbDbf::PutRawField(const char *Name, const char *buf) { return PutRawField(GetFieldNo(Name), buf);}//! Put a value into the specified field./*!*/xbShort xbDbf::PutField(const xbShort FieldNo, const char *buf) { xbShort len, i; char * startpos; char * tp; /* target pointer */ const char * sp; /* source pointer */ if( FieldNo < 0 || FieldNo >= NoOfFields ) xb_error(XB_INVALID_FIELDNO); if( DbfStatus != XB_UPDATED ) { DbfStatus = XB_UPDATED; memcpy( RecBuf2, RecBuf, RecordLen ); } if( SchemaPtr[FieldNo].Type == 'L' && !ValidLogicalData( buf )) xb_error(XB_INVALID_DATA) else if(( SchemaPtr[FieldNo].Type == 'F' || SchemaPtr[FieldNo].Type == 'N' ) && !ValidNumericData( buf )) xb_error(XB_INVALID_DATA) else if( SchemaPtr[FieldNo].Type == 'D' ){ xbDate d; if( !d.DateIsValid( buf )) xb_error(XB_INVALID_DATA); } if( SchemaPtr[FieldNo].Type == 'C' && SchemaPtr[FieldNo].NoOfDecs > 0 ) memset( SchemaPtr[FieldNo].Address, 0x20, SchemaPtr[FieldNo].LongFieldLen ); else memset( SchemaPtr[FieldNo].Address, 0x20, SchemaPtr[FieldNo].FieldLen ); len = strlen( buf ); if(( SchemaPtr[FieldNo].Type == 'N' || SchemaPtr[FieldNo].Type == 'F') && len > SchemaPtr[FieldNo].FieldLen ) xb_error(XB_INVALID_DATA) else if( len > SchemaPtr[FieldNo].FieldLen ) len = SchemaPtr[FieldNo].FieldLen; if( SchemaPtr[FieldNo].Type == 'F' || SchemaPtr[FieldNo].Type == 'N' || SchemaPtr[FieldNo].Type == 'M' ) { const char *sdp = strchr( buf, '.' ); /* source decimal point */ len = 0; sp =buf; while( *sp && *sp != '.' ) { len++; sp++; } if( SchemaPtr[FieldNo].NoOfDecs > 0 ) { /* do the right of decimal area */ tp = SchemaPtr[FieldNo].Address; tp += SchemaPtr[FieldNo].FieldLen - SchemaPtr[FieldNo].NoOfDecs - 1; *tp++ = '.'; sp = sdp; if( sp ) sp++; for( i = 0; i < SchemaPtr[FieldNo].NoOfDecs; i++ ) if( sp && *sp ) *tp++ = *sp++; else *tp++ = '0'; startpos= SchemaPtr[FieldNo].Address + SchemaPtr[FieldNo].FieldLen - SchemaPtr[FieldNo].NoOfDecs - len - 1; } else { startpos=SchemaPtr[FieldNo].Address+SchemaPtr[FieldNo].FieldLen-len; } } else startpos = SchemaPtr[FieldNo].Address; memcpy( startpos, buf, len ); return 0;}//! Put a raw value into the specified field./*!*/xbShort xbDbf::PutRawField(const xbShort FieldNo, const char *buf) { xbShort len; char * startpos; if( FieldNo < 0 || FieldNo >= NoOfFields ) xb_error(XB_INVALID_FIELDNO); if( DbfStatus != XB_UPDATED ) { DbfStatus = XB_UPDATED; memcpy( RecBuf2, RecBuf, RecordLen ); } startpos = SchemaPtr[FieldNo].Address; len = SchemaPtr[FieldNo].FieldLen; memcpy( startpos, buf, len ); return 0;}/************************************************************************///! Get the value of the specified field./*!*/xbShort xbDbf::GetField(const xbShort FieldNo, char *buf) const { return GetField(FieldNo, buf, 0);}/************************************************************************///! Get the raw value of the specified field./*!*/xbShort xbDbf::GetRawField(const xbShort FieldNo, char *buf) const { return GetField(FieldNo, buf, 0);}/************************************************************************///! Get the long value of the specified field./*!*/xbLong xbDbf::GetLongField( const xbShort FieldNo ) const{ char buf[18]; memset( buf, 0x00, 18 ); GetField( FieldNo, buf ); return atol( buf );}/************************************************************************///! Get the long value of the specified field./*!*/xbLong xbDbf::GetLongField( const char * FieldName ) const{ return( GetLongField( GetFieldNo( FieldName )));}/************************************************************************///! Put a long value into the specified field./*!*/xbShort xbDbf::PutLongField( const xbShort FieldNo, const xbLong Val ){ char buf[18]; memset( buf, 0x00, 18 ); sprintf( buf, "%ld", Val ); return( PutField( FieldNo, buf ));}/************************************************************************///! Put a long value into the specified field./*!*/xbShort xbDbf::PutLongField(const char *FieldName, const xbLong Val) { return (PutLongField(GetFieldNo(FieldName), Val));}/************************************************************************///! Get the float value of the specified field./*!*/xbFloat xbDbf::GetFloatField(const xbShort FieldNo) { char buf[21]; memset( buf, 0x00, 21 ); if( GetField( FieldNo, buf ) != 0 ) return atof( buf ); else return 0;}/************************************************************************///! Get the float value of the specified field./*!*/xbFloat xbDbf::GetFloatField(const char * FieldName) {xbShort fnum; if ((fnum = GetFieldNo(FieldName)) != -1) return GetFloatField(fnum); else return 0;}/************************************************************************///! Put a float value into the specified field./*!*/xbShort xbDbf::PutFloatField( const xbShort FldNo, const xbFloat f ){ char buf[25]; char buf2[12]; memset( buf, 0x00, 25 ); memset( buf2, 0x00, 12 ); sprintf( buf, "%d.%df", GetFieldLen( FldNo ), GetFieldDecimal( FldNo )); strcpy( buf2, "%-" ); strcat( buf2, buf ); sprintf( buf, buf2, f ); /* remove trailing space */ xbShort i = 0; while( i < 25 ) if( buf[i] == 0x20 ){ buf[i] = 0x00; break; } else i++; return PutField( FldNo, buf );}/************************************************************************///! Put a float value into the specified field./*!*/xbShort xbDbf::PutFloatField(const char *FieldName, const xbFloat f) { xbShort fnum; if ((fnum = GetFieldNo(FieldName)) != -1) return PutFloatField(fnum, f); else return 0;}/************************************************************************///! Get the double value of the specified field./*!*/xbDouble xbDbf::GetDoubleField( const xbShort FieldNo, xbShort RecBufSw ){ char buf[21]; memset( buf, 0x00, 21 ); if( GetField( FieldNo, buf, RecBufSw ) != 0 ) return strtod( buf, NULL ); else return 0;}/************************************************************************///! Get the double value of the specified field./*!*/xbDouble xbDbf::GetDoubleField(const char *FieldName) { xbShort fnum; if ((fnum = GetFieldNo(FieldName)) != -1) return GetDoubleField(fnum); else return 0;}/************************************************************************///! Put a double value into the specified field./*!*/xbShort xbDbf::PutDoubleField( const xbShort FieldNo, const xbDouble d) { return PutFloatField(FieldNo, (xbFloat)d);}/************************************************************************///! Put a double value into the specified field./*!*/xbShort xbDbf::PutDoubleField(const char *FieldName, const xbDouble d) { xbShort fnum; if ((fnum = GetFieldNo(FieldName)) != -1) return PutFloatField(fnum, (xbFloat)d); else return 0;}/************************************************************************///! Get the logical value of the specified field./*!*/xbShort xbDbf::GetLogicalField( const xbShort FieldNo ){ char buf[3]; if( GetFieldType( FieldNo ) != 'L' ) return -1; memset( buf, 0x00, 3 ); GetField( FieldNo, buf ); if( buf[0] == 'Y' || buf[0] == 'y' || buf[0] == 'T' || buf[0] == 't' ) return 1; else return 0;}/************************************************************************///! Get the logical value of the specified field./*!*/xbShort xbDbf::GetLogicalField( const char * FieldName ){ xbShort fnum; if(( fnum = GetFieldNo( FieldName )) != -1 ) return GetLogicalField( fnum ); else return -1;}/************************************************************************///! Get the string value of the specified field./*!*/char * xbDbf::GetStringField( const char * FieldName ){ return GetStringField(GetFieldNo(FieldName));}/************************************************************************///! Get the string value of the specified field./*!*/char * xbDbf::GetStringField( const xbShort FieldNo ){ /* allocate memory if needed */ if( !SchemaPtr[FieldNo].fp ) SchemaPtr[FieldNo].fp = new char[GetFieldLen(FieldNo)+1]; if( !SchemaPtr[FieldNo].fp )#ifdef HAVE_EXCEPTIONS throw xbOutOfMemoryException();#else return 0;#endif GetField( FieldNo, SchemaPtr[FieldNo].fp ); char* p = SchemaPtr[FieldNo].fp; while ((*p != ' ') && (*p != 0)) { if (*p++ == ']') { *p++ = ':'; break; } } *p++ = 0; *p = 0; return SchemaPtr[FieldNo].fp;}/************************************************************************/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?