Пример #1
0
int DBF::putNumber ( int nth, double val )
{
	char	*p = fieldPtr(nth) ;
	int	l = fieldLength(nth) ;

	if ( fieldType(nth) != 'N' ) {
		_errNo = DB_FIELD_TYPE_MISSING ;
		return -1 ;
	}

	modify() ;

	char	s[32]	;
	sprintf(s,"%*.*f",fieldLength(nth),fieldDecimal(nth),val) ;
	if ( strlen(s) != fieldLength(nth) ) {	/* overflow */
		memset(p,'*',l) ;
		_errNo = DB_FIELD_OVERFLOW ;
		return -1 ;
	}

	memcpy(p,s,fieldLength(nth)) ;
	return 0 ;
}
Пример #2
0
bool Field::Load(std::ifstream& f, int shift){
	S_LOG("Load");
	auto current = std::ios_base::cur;
	if (f.good()){
		f.seekg(shift, current);
                log(logxx::debug) << "Current position: 0x" << std::hex << f.tellg() << std::dec << logxx::endl;
                uint32_t fieldLength(0);
                char optionalField[2];
                f.read(optionalField, 2);
                check_error(optionalField);
                if (
                        std::strncmp(optionalField, "PN", 2) == 0 ||
                        std::strncmp(optionalField, "LO", 2) == 0 ||
                        std::strncmp(optionalField, "DA", 2) == 0
                        ){
                        uint16_t shortFieldLength(0);
                        fread(shortFieldLength);
                        fieldLength = shortFieldLength;
                } else{
                        f.seekg(-2, std::ios_base::cur);
                        fread(fieldLength);
                }
                
                static const decltype(fieldLength) maxLength = 100;
                
                if (fieldLength == 0){
                        log(logxx::warning) << "Field length is zero, nothing to read!" << logxx::endl;
                        return false;
                } else if (fieldLength > maxLength){
                        log(logxx::warning) << "Too large field: " << fieldLength <<
                                "; maximum allowed is: " << maxLength << logxx::endl;
                        return false;
                }
		std::unique_ptr<char[]> cValue(new char[fieldLength]);
		f.read(cValue.get(), fieldLength);
		check_error(cValue);
		try {
			value.assign(cValue.get(), fieldLength);
			ProcessValue();
			return true;
		} catch (const std::exception& e){
			log(logxx::warning) << "Cought an exception: " << e.what() << logxx::endl;
			value.assign('%', fieldLength);
			return true;
		}
	} else {
		log(logxx::error) << "Can't read file" << logxx::endl;
		return false;
	}
}