static VALUE date_new(ValueStruct *val) { VALUE cTime , ret; ENTER_FUNC; cTime = rb_const_get(rb_cObject, rb_intern("Time")); ret = rb_funcall(cTime, rb_intern("local"), 3, INT2NUM((ValueDateTimeYear(val))), INT2NUM((ValueDateTimeMon(val)+1)), INT2NUM((ValueDateTimeMDay(val)))); LEAVE_FUNC; return ret; }
extern Bool SetValueFixed( ValueStruct *val, Fixed *fval) { Bool rc; char *str; if ( val == NULL ) { fprintf(stderr,"no ValueStruct\n"); rc = FALSE; } else { ValueIsNonNil(val); switch (ValueType(val)) { case GL_TYPE_CHAR: case GL_TYPE_VARCHAR: case GL_TYPE_DBCODE: case GL_TYPE_TEXT: case GL_TYPE_SYMBOL: rc = SetValueString(val,fval->sval,NULL); break; case GL_TYPE_NUMBER: FixedRescale(&ValueFixed(val),fval); rc = TRUE; break; case GL_TYPE_INT: ValueInteger(val) = FixedToInt(fval); rc = TRUE; break; case GL_TYPE_FLOAT: ValueFloat(val) = FixedToFloat(fval); rc = TRUE; break; case GL_TYPE_TIMESTAMP: str = fval->sval; ValueDateTimeYear(val) = StrToInt(str,4); str += 4; ValueDateTimeMon(val) = StrToInt(str,2) - 1; str += 2; ValueDateTimeMDay(val) = StrToInt(str,2); str += 2; ValueDateTimeHour(val) = StrToInt(str,2); str += 2; ValueDateTimeMin(val) = StrToInt(str,2); str += 2; ValueDateTimeSec(val) = StrToInt(str,2); str += 2; rc = mktime(&ValueDateTime(val)) >= 0 ? TRUE : FALSE; break; case GL_TYPE_DATE: str = fval->sval; ValueDateTimeYear(val) = StrToInt(str,4); str += 4; ValueDateTimeMon(val) = StrToInt(str,2) - 1; str += 2; ValueDateTimeMDay(val) = StrToInt(str,2); str += 2; ValueDateTimeHour(val) = 0; ValueDateTimeMin(val) = 0; ValueDateTimeSec(val) = 0; rc = mktime(&ValueDateTime(val)) >= 0 ? TRUE : FALSE; break; case GL_TYPE_TIME: str = fval->sval; ValueDateTimeYear(val) = 0; ValueDateTimeMon(val) = 0; ValueDateTimeMDay(val) = 0; ValueDateTimeHour(val) = StrToInt(str,2); str += 2; ValueDateTimeMin(val) = StrToInt(str,2); str += 2; ValueDateTimeSec(val) = StrToInt(str,2); str += 2; rc = TRUE; break; #if 0 case GL_TYPE_BOOL: val->body.BoolData = ( *fval->sval == 0 ) ? FALSE : TRUE; rc = TRUE; break; #endif default: ValueIsNil(val); rc = FALSE; break; } } return (rc); }
extern Bool SetValueStringWithLength( ValueStruct *val, char *str, size_t slen, char *codeset) { Bool rc , fMinus , fPoint; size_t len; byte *p; byte buff[SIZE_NUMBUF+1] , sbuff[SIZE_LONGNAME+1]; Fixed from; size_t size; #ifdef WITH_I18N byte *q; iconv_t cd; size_t sob , sib; char *istr; #endif ENTER_FUNC; if ( val == NULL ) { fprintf(stderr,"no ValueStruct\n"); rc = FALSE; } else if ( ( str == NULL ) || ( *str == CHAR_NIL ) ) { ValueIsNil(val); rc = TRUE; } else { ValueIsNonNil(val); switch (ValueType(val)) { case GL_TYPE_CHAR: case GL_TYPE_VARCHAR: case GL_TYPE_DBCODE: case GL_TYPE_TEXT: case GL_TYPE_SYMBOL: #ifdef WITH_I18N if ( ( codeset != NULL ) && ( ( stricmp(codeset,"utf8") == 0 ) || ( stricmp(codeset,"utf-8") == 0 ) ) ) { codeset = NULL; } if ( codeset != NULL ) { if ( IS_VALUE_EXPANDABLE(val) ) { len = slen; } else { len = ValueStringLength(val) < slen ? ValueStringLength(val) : slen; } cd = iconv_open("utf8",codeset); while (TRUE) { istr = str; sib = len; sob = ValueStringSize(val); if ( ( q = ValueString(val) ) != NULL ) { memclear(ValueString(val),ValueStringSize(val)); if ( iconv(cd,&istr,&sib,(void*)&q,&sob) == 0 ) break; if ( errno == E2BIG ) { xfree(ValueString(val)); ValueStringSize(val) *= 2; } else break; } else { ValueStringSize(val) = 1; } ValueString(val) = (byte *)xmalloc(ValueStringSize(val)); }; iconv_close(cd); *q = 0; len = ValueStringSize(val) - sob; } else { #endif size = slen + 1; len = slen; if ( size > ValueStringSize(val) ) { if ( ValueString(val) != NULL ) { xfree(ValueString(val)); } ValueStringSize(val) = size; ValueString(val) = (byte *)xmalloc(size); } memclear(ValueString(val),ValueStringSize(val)); strcpy(ValueString(val),str); #ifdef WITH_I18N } #endif if ( IS_VALUE_EXPANDABLE(val) ) { ValueStringLength(val) = len; } rc = TRUE; break; case GL_TYPE_BYTE: DecodeStringToBinary(ValueByte(val),ValueByteLength(val),str); rc = TRUE; break; case GL_TYPE_BINARY: #ifdef BINARY_IS_BASE64 p = (byte *)xmalloc(strlen(str)); size = strlen(str); size = DecodeBase64(p,size,str,size); if ( ValueByte(val) != NULL ) { xfree(ValueByte(val)); } ValueByteSize(val) = strlen(str); ValueByte(val) = p; #else size = 0; for ( p = str ; *p != 0 ; ) { if ( *p == '\\' ) { p ++; if ( *p == 'u' ) { p += 3; } else { p ++; } } else { p ++; } size ++; } if ( size > ValueByteSize(val) ) { if ( ValueByte(val) != NULL ) { xfree(ValueByte(val)); } ValueByteSize(val) = size; ValueByte(val) = (byte *)xmalloc(size); } memclear(ValueByte(val),ValueByteSize(val)); DecodeStringToBinary(ValueByte(val),size,str); #endif ValueByteLength(val) = size; rc = TRUE; break; case GL_TYPE_OBJECT: ValueObjectId(val) = 0; for ( ; slen > 0 ; slen -- ) { if ( isdigit(*str) ) { ValueObjectId(val) = ValueObjectId(val) * 10 + ( *str - '0' ); } str ++; } if ( ValueObjectFile(val) != NULL ) { xfree(ValueObjectFile(val)); ValueObjectFile(val) = NULL; } rc = TRUE; break; case GL_TYPE_NUMBER: case GL_TYPE_INT: case GL_TYPE_FLOAT: case GL_TYPE_BOOL: case GL_TYPE_TIMESTAMP: case GL_TYPE_DATE: case GL_TYPE_TIME: #ifdef WITH_I18N if ( codeset != NULL ) { cd = iconv_open("utf8",codeset); istr = str; sib = slen; sob = SIZE_NUMBUF; p = sbuff; iconv(cd,&istr,&sib,(void*)&p,&sob); iconv_close(cd); *p = 0; str = sbuff; } else { #endif strncpy(sbuff,str,SIZE_NUMBUF); str = sbuff; #ifdef WITH_I18N } #endif switch (ValueType(val)) { case GL_TYPE_NUMBER: p = buff; from.flen = 0; from.slen = 0; from.sval = buff; fPoint = FALSE; fMinus = FALSE; while ( *str != 0 ) { if ( fPoint ) { from.slen ++; } if ( *str == '-' ) { fMinus = TRUE; } else if ( isdigit(*str) ) { *p ++ = *str; from.flen ++; } else if ( *str == '.' ) { fPoint = TRUE; } str ++; } *p = 0; if ( fMinus ) { *buff |= 0x40; } FixedRescale(&ValueFixed(val),&from); rc = TRUE; break; case GL_TYPE_INT: ValueInteger(val) = StrToInt(str,slen); rc = TRUE; break; case GL_TYPE_FLOAT: ValueFloat(val) = atof(str); rc = TRUE; break; case GL_TYPE_BOOL: ValueBool(val) = ( toupper(*str) == 'T' ) ? TRUE : FALSE; rc = TRUE; break; case GL_TYPE_TIMESTAMP: ValueDateTimeYear(val) = StrToInt(str,4); str += 4; ValueDateTimeMon(val) = StrToInt(str,2) - 1; str += 2; ValueDateTimeMDay(val) = StrToInt(str,2); str += 2; ValueDateTimeHour(val) = StrToInt(str,2); str += 2; ValueDateTimeMin(val) = StrToInt(str,2); str += 2; ValueDateTimeSec(val) = StrToInt(str,2); str += 2; rc = mktime(&ValueDateTime(val)) >= 0 ? TRUE : FALSE; break; case GL_TYPE_DATE: ValueDateTimeYear(val) = StrToInt(str,4); str += 4; ValueDateTimeMon(val) = StrToInt(str,2) - 1; str += 2; ValueDateTimeMDay(val) = StrToInt(str,2); str += 2; ValueDateTimeHour(val) = 0; ValueDateTimeMin(val) = 0; ValueDateTimeSec(val) = 0; rc = mktime(&ValueDateTime(val)) >= 0 ? TRUE : FALSE; break; case GL_TYPE_TIME: ValueDateTimeYear(val) = 0; ValueDateTimeMon(val) = 0; ValueDateTimeMDay(val) = 0; ValueDateTimeHour(val) = StrToInt(str,2); str += 2; ValueDateTimeMin(val) = StrToInt(str,2); str += 2; ValueDateTimeSec(val) = StrToInt(str,2); str += 2; rc = TRUE; break; default: rc = TRUE; break; } break; default: ValueIsNil(val); rc = FALSE; } } LEAVE_FUNC; return (rc); }
extern LargeByteString * ValueToLBS( ValueStruct *val, char *codeset) { byte work[SIZE_NUMBUF+1]; byte work2[SIZE_NUMBUF+2]; byte *p , *q; int i; int size; LargeByteString *ret; ENTER_FUNC; if ( val == NULL ) { ret = NULL; } else { dbgprintf("type = %X\n",(int)ValueType(val)); if ( ValueStr(val) == NULL ) { ValueStr(val) = NewLBS(); } LBS_EmitStart(ValueStr(val)); if ( IS_VALUE_NIL(val) ) { LBS_EmitChar(ValueStr(val),CHAR_NIL); } else switch (ValueType(val)) { case GL_TYPE_CHAR: case GL_TYPE_VARCHAR: case GL_TYPE_TEXT: case GL_TYPE_SYMBOL: case GL_TYPE_DBCODE: if ( ValueString(val) != NULL ) { RewindLBS(ValueStr(val)); if ( IS_VALUE_EXPANDABLE(val) ) { LBS_EmitStringCodeset(ValueStr(val),ValueString(val), ValueStringSize(val), ValueStringLength(val),codeset); if ( ( size = ValueStringLength(val) - ValueSize(val) ) > 0 ) { for ( ; size > 0 ; size -- ) { LBS_EmitChar(ValueStr(val),0); } } } else { LBS_EmitStringCodeset(ValueStr(val),ValueString(val), ValueStringSize(val), 0,codeset); } } break; case GL_TYPE_BYTE: case GL_TYPE_BINARY: #ifdef BINARY_IS_BASE64 size = ( ( ValueByteLength(val) + 2 ) / 3 ) * 4; p = (char *)xmalloc(size); size = EncodeBase64(p,size,ValueByte(val),ValueByteLength(val)); LBS_ReserveSize(ValueStr(val),size+1); strcpy(ValueStrBody(val),p); xfree(p); #else p = ValueByte(val); for ( i = 0 ; i < ValueByteLength(val) ; i ++ , p ++ ) { switch (*p) { case '\\': LBS_EmitChar(ValueStr(val),'\\'); LBS_EmitChar(ValueStr(val),'\\'); break; case '"': LBS_EmitChar(ValueStr(val),'\\'); LBS_EmitChar(ValueStr(val),'"'); break; case '/': LBS_EmitChar(ValueStr(val),'\\'); LBS_EmitChar(ValueStr(val),'/'); break; case '\b': LBS_EmitChar(ValueStr(val),'\\'); LBS_EmitChar(ValueStr(val),'b'); break; case '\f': LBS_EmitChar(ValueStr(val),'\\'); LBS_EmitChar(ValueStr(val),'f'); break; case '\n': LBS_EmitChar(ValueStr(val),'\\'); LBS_EmitChar(ValueStr(val),'n'); break; case '\r': LBS_EmitChar(ValueStr(val),'\\'); LBS_EmitChar(ValueStr(val),'r'); break; case '\t': LBS_EmitChar(ValueStr(val),'\\'); LBS_EmitChar(ValueStr(val),'t'); break; default: if ( isprint(*p) ) { LBS_EmitChar(ValueStr(val),*p); } else { sprintf(work,"\\u%02X",(int)*p); LBS_EmitString(ValueStr(val),work); } break; } } if ( ( size = ValueStringLength(val) - ValueSize(val) ) > 0 ) { for ( ; size > 0 ; size -- ) { LBS_EmitByte(ValueStr(val),0); } } #endif break; case GL_TYPE_NUMBER: strcpy(work,ValueFixedBody(val)); p = work; q = work2; if ( *p >= 0x70 ) { *q ++ = '-'; *p ^= 0x40; } strcpy(q,p); if ( ValueFixedSlen(val) > 0 ) { p = work2 + strlen(work2); *(p + 1) = 0; q = p - 1; for ( i = 0 ; i < ValueFixedSlen(val) ; i ++ ) { *p -- = *q --; } *p = '.'; } LBS_EmitString(ValueStr(val),work2); break; case GL_TYPE_INT: sprintf(work,"%d",ValueInteger(val)); LBS_EmitString(ValueStr(val),work); break; case GL_TYPE_OBJECT: sprintf(work,"%d",(int)ValueObjectId(val)); LBS_EmitString(ValueStr(val),work); break; case GL_TYPE_FLOAT: sprintf(work,"%g",ValueFloat(val)); LBS_EmitString(ValueStr(val),work); break; case GL_TYPE_BOOL: sprintf(work,"%s",ValueBool(val) ? "TRUE" : "FALSE"); LBS_EmitString(ValueStr(val),work); break; case GL_TYPE_TIMESTAMP: sprintf(work,"%04d%02d%02d%02d%02d%02d", ValueDateTimeYear(val), ValueDateTimeMon(val) + 1, ValueDateTimeMDay(val), ValueDateTimeHour(val), ValueDateTimeMin(val), ValueDateTimeSec(val)); LBS_EmitString(ValueStr(val),work); break; case GL_TYPE_DATE: sprintf(work,"%04d%02d%02d", ValueDateTimeYear(val), ValueDateTimeMon(val) + 1, ValueDateTimeMDay(val)); LBS_EmitString(ValueStr(val),work); break; case GL_TYPE_TIME: sprintf(work,"%02d%02d%02d", ValueDateTimeHour(val), ValueDateTimeMin(val), ValueDateTimeSec(val)); LBS_EmitString(ValueStr(val),work); break; default: break; } LBS_EmitEnd(ValueStr(val)); ret = ValueStr(val); } LEAVE_FUNC; return (ret); }
extern Fixed * ValueToFixed( ValueStruct *val) { Fixed *ret; Fixed *xval; if ( val == NULL ) { ret = NewFixed(0,0); } else switch (ValueType(val)) { case GL_TYPE_CHAR: case GL_TYPE_VARCHAR: case GL_TYPE_TEXT: case GL_TYPE_SYMBOL: ret = NewFixed(0,0); IntToFixed(ret,StrToInt(ValueString(val),strlen(ValueString(val)))); break; case GL_TYPE_NUMBER: xval = &ValueFixed(val); ret = NewFixed(xval->flen,xval->slen); strcpy(ret->sval,xval->sval); break; case GL_TYPE_INT: ret = NewFixed(0,0); IntToFixed(ret,ValueInteger(val)); break; case GL_TYPE_FLOAT: ret = NewFixed(SIZE_NUMBUF,(SIZE_NUMBUF / 2)); FloatToFixed(ret,ValueFloat(val)); break; case GL_TYPE_BOOL: ret = NewFixed(0,0); IntToFixed(ret,(int)ValueBool(val)); break; case GL_TYPE_TIMESTAMP: ret = NewFixed(14,0); sprintf(ret->sval,"%04d%02d%02d%02d%02d%02d", ValueDateTimeYear(val), ValueDateTimeMon(val) + 1, ValueDateTimeMDay(val), ValueDateTimeHour(val), ValueDateTimeMin(val), ValueDateTimeSec(val)); break; case GL_TYPE_DATE: ret = NewFixed(8,0); sprintf(ret->sval,"%04d%02d%02d", ValueDateTimeYear(val), ValueDateTimeMon(val) + 1, ValueDateTimeMDay(val)); break; case GL_TYPE_TIME: ret = NewFixed(6,0); sprintf(ret->sval,"%02d%02d%02d", ValueDateTimeHour(val), ValueDateTimeMin(val), ValueDateTimeSec(val)); break; default: ret = NewFixed(0,0); break; } return (ret); }
extern size_t OpenCOBOL_PackValue(CONVOPT *opt, unsigned char *p, ValueStruct *value) { int i; size_t size; unsigned char *pp; pp = p; if (value != NULL) { switch (ValueType(value)) { case GL_TYPE_INT: *(int32_t *)p = (int32_t)ValueInteger(value); IntegerC2Cobol(opt, (int32_t *)p); p += sizeof(int32_t); break; case GL_TYPE_FLOAT: *(double *)p = ValueFloat(value); p += sizeof(double); break; case GL_TYPE_BOOL: *(char *)p = ValueBool(value) ? 'T' : 'F'; p++; break; case GL_TYPE_BYTE: memcpy(p, ValueByte(value), ValueByteLength(value)); p += ValueByteLength(value); break; case GL_TYPE_BINARY: memclear(p, opt->textsize); size = (opt->textsize < ValueByteLength(value)) ? opt->textsize : ValueByteLength(value); memcpy(p, ValueToBinary(value), size); p += opt->textsize; break; case GL_TYPE_TEXT: case GL_TYPE_SYMBOL: size = (opt->textsize < ValueStringLength(value)) ? opt->textsize : ValueStringLength(value); memcpy(p, ValueToString(value, ConvCodeset(opt)), size); StringC2Cobol(p, opt->textsize); p += opt->textsize; break; case GL_TYPE_CHAR: case GL_TYPE_VARCHAR: case GL_TYPE_DBCODE: if (IS_VALUE_NIL(value)) { memclear(p, ValueStringLength(value)); /* LOW-VALUE */ } else { strncpy(p, ValueToString(value, ConvCodeset(opt)), ValueStringLength(value)); StringC2Cobol(p, ValueStringLength(value)); } p += ValueStringLength(value); break; case GL_TYPE_NUMBER: memcpy(p, ValueFixedBody(value), ValueFixedLength(value)); FixedC2Cobol(p, ValueFixedLength(value)); p += ValueFixedLength(value); break; case GL_TYPE_OBJECT: memcpy(p, ValueBody(value), SIZE_UUID); StringC2Cobol(p, SIZE_UUID); p += SIZE_UUID; break; case GL_TYPE_TIMESTAMP: p += sprintf(p, "%04d%02d%02d%02d%02d%02d", ValueDateTimeYear(value), ValueDateTimeMon(value) + 1, ValueDateTimeMDay(value), ValueDateTimeHour(value), ValueDateTimeMin(value), ValueDateTimeSec(value)); break; case GL_TYPE_DATE: p += sprintf(p, "%04d%02d%02d", ValueDateTimeYear(value), ValueDateTimeMon(value) + 1, ValueDateTimeMDay(value)); break; case GL_TYPE_TIME: p += sprintf(p, "%02d%02d%02d", ValueDateTimeHour(value), ValueDateTimeMin(value), ValueDateTimeSec(value)); break; case GL_TYPE_ARRAY: for (i = 0; i < ValueArraySize(value); i++) { p += OpenCOBOL_PackValue(opt, p, ValueArrayItem(value, i)); } break; case GL_TYPE_ROOT_RECORD: case GL_TYPE_RECORD: for (i = 0; i < ValueRecordSize(value); i++) { p += OpenCOBOL_PackValue(opt, p, ValueRecordItem(value, i)); } break; default: break; } } return (p - pp); }
extern size_t OpenCOBOL_UnPackValue(CONVOPT *opt, unsigned char *p, ValueStruct *value) { int32_t i; char buff[SIZE_NUMBUF + 1]; unsigned char *q; ValueStruct *child; q = p; if (value != NULL) { ValueIsNonNil(value); switch (ValueType(value)) { case GL_TYPE_INT: i = *(int32_t*)p; IntegerCobol2C(opt, &i); SetValueInteger(value, (int64_t)i); p += sizeof(int32_t); break; case GL_TYPE_FLOAT: ValueFloat(value) = *(double *)p; p += sizeof(double); break; case GL_TYPE_BOOL: SetValueBool(value,(*(char *)p == 'T') ? TRUE : FALSE); p++; break; case GL_TYPE_OBJECT: StringCobol2C(p, SIZE_UUID); memcpy(ValueBody(value), p, SIZE_UUID); p += SIZE_UUID; break; case GL_TYPE_BYTE: memcpy(ValueByte(value), p, ValueByteLength(value)); p += ValueByteLength(value); break; case GL_TYPE_BINARY: SetValueBinary(value, p, opt->textsize); p += opt->textsize; break; case GL_TYPE_TEXT: case GL_TYPE_SYMBOL: StringCobol2C(p, opt->textsize); SetValueStringWithLength(value, p, opt->textsize, ConvCodeset(opt)); p += opt->textsize; break; case GL_TYPE_CHAR: case GL_TYPE_VARCHAR: case GL_TYPE_DBCODE: StringCobol2C(p, ValueStringLength(value)); SetValueStringWithLength(value, p, ValueStringLength(value), ConvCodeset(opt)); p += ValueStringLength(value); break; case GL_TYPE_NUMBER: memcpy(buff, p, ValueFixedLength(value)); FixedCobol2C(buff, ValueFixedLength(value)); strcpy(ValueFixedBody(value), buff); p += ValueFixedLength(value); break; case GL_TYPE_TIMESTAMP: ValueDateTimeYear(value) = StrToInt(p, 4); p += 4; ValueDateTimeMon(value) = StrToInt(p, 2) - 1; p += 2; ValueDateTimeMDay(value) = StrToInt(p, 2); p += 2; ValueDateTimeHour(value) = StrToInt(p, 2); p += 2; ValueDateTimeMin(value) = StrToInt(p, 2); p += 2; ValueDateTimeSec(value) = StrToInt(p, 2); p += 2; mktime(ValueDateTime(value)); break; case GL_TYPE_TIME: ValueDateTimeYear(value) = 0; ValueDateTimeMon(value) = 0; ValueDateTimeMDay(value) = 0; ValueDateTimeHour(value) = StrToInt(p, 2); p += 2; ValueDateTimeMin(value) = StrToInt(p, 2); p += 2; ValueDateTimeSec(value) = StrToInt(p, 2); p += 2; break; case GL_TYPE_DATE: ValueDateTimeYear(value) = StrToInt(p, 4); p += 4; ValueDateTimeMon(value) = StrToInt(p, 2) - 1; p += 2; ValueDateTimeMDay(value) = StrToInt(p, 2); p += 2; ValueDateTimeHour(value) = 0; ValueDateTimeMin(value) = 0; ValueDateTimeSec(value) = 0; mktime(ValueDateTime(value)); break; case GL_TYPE_ARRAY: for (i = 0; i < ValueArraySize(value); i++) { child = ValueArrayItem(value, i); if (child != NULL) { p += OpenCOBOL_UnPackValue(opt, p, child); ValueParent(child) = value; } } break; case GL_TYPE_ROOT_RECORD: case GL_TYPE_RECORD: for (i = 0; i < ValueRecordSize(value); i++) { child = ValueRecordItem(value, i); if (child != NULL) { p += OpenCOBOL_UnPackValue(opt, p, child); ValueParent(child) = value; } } break; default: ValueIsNil(value); break; } } return (p - q); }