00001
00014 #include <stdio.h>
00015 #include <math.h>
00016 #include <ctype.h>
00017 #include "output.h"
00018 #include "globals.h"
00019 #include "convert.h"
00020 #include "object.h"
00021
00026 int convert_from_void(char *buffer,
00027 int size,
00028 void *data,
00029 PROPERTY *prop)
00030 {
00031 if(size < 7)
00032 return 0;
00033 return sprintf(buffer,"%s","(void)");
00034 }
00035
00040 int convert_to_void(char *buffer,
00041 void *data,
00042 PROPERTY *prop)
00043 {
00044 return 1;
00045 }
00046
00052 int convert_from_double(char *buffer,
00053 int size,
00054 void *data,
00055 PROPERTY *prop)
00056 {
00057 char temp[1025];
00058 int count = 0;
00059
00060 double scale = 1.0;
00061 if(prop->unit != NULL){
00062
00063
00064 PROPERTY *ptmp = (prop->oclass==NULL ? prop : class_find_property(prop->oclass, prop->name));
00065
00066 if(prop->unit != ptmp->unit){
00067 if(0 == unit_convert_ex(ptmp->unit, prop->unit, &scale)){
00068 output_error("convert_from_double(): unable to convert unit '%s' to '%s' for property '%s' (tape experiment error)", ptmp->unit->name, prop->unit->name, prop->name);
00069 scale = 1.0;
00070 }
00071 }
00072 }
00073
00074 count = sprintf(temp, global_double_format, *(double *)data * scale);
00075 if(count < size+1){
00076 memcpy(buffer, temp, count);
00077 buffer[count] = 0;
00078 return count;
00079 } else {
00080 return 0;
00081 }
00082 }
00083
00089 int convert_to_double(char *buffer,
00090 void *data,
00091 PROPERTY *prop)
00092 {
00093 return sscanf(buffer,"%lg",data);
00094 }
00095
00101 int convert_from_complex(char *buffer,
00102 int size,
00103 void *data,
00104 PROPERTY *prop)
00105 {
00106 int count = 0;
00107 char temp[1025];
00108 complex *v = (complex*)data;
00109
00110 double scale = 1.0;
00111 if(prop->unit != NULL){
00112
00113
00114 PROPERTY *ptmp = (prop->oclass==NULL ? prop : class_find_property(prop->oclass, prop->name));
00115
00116 if(prop->unit != ptmp->unit){
00117 if(0 == unit_convert_ex(ptmp->unit, prop->unit, &scale)){
00118 output_error("convert_from_complex(): unable to convert unit '%s' to '%s' for property '%s' (tape experiment error)", ptmp->unit->name, prop->unit->name, prop->name);
00119
00120
00121
00122
00123
00124 scale = 1.0;
00125 }
00126 }
00127 }
00128
00129 if (v->Notation()==A)
00130 {
00131 double m = v->Mag()*scale;
00132 double a = v->Arg();
00133 if (a>PI) a-=(2*PI);
00134 count = sprintf(temp,global_complex_format,m,a*180/PI,A);
00135 }
00136 else if (v->Notation()==R)
00137 {
00138 double m = v->Mag()*scale;
00139 double a = v->Arg();
00140 if (a>PI) a-=(2*PI);
00141 count = sprintf(temp,global_complex_format,m,a,R);
00142 }
00143 else {
00144 count = sprintf(temp,global_complex_format,v->Re()*scale,v->Im()*scale,v->Notation()?v->Notation():'i');
00145 }
00146 if(count < size - 1){
00147 memcpy(buffer, temp, count);
00148 buffer[count] = 0;
00149 return count;
00150 } else {
00151 return 0;
00152 }
00153 }
00154
00160 int convert_to_complex(char *buffer,
00161 void *data,
00162 PROPERTY *prop)
00163 {
00164 complex *v = (complex*)data;
00165 char notation[2]={'\0','\0'};
00166 int n;
00167 double a=0, b=0;
00168 if(buffer[0] == 0){
00169
00170 v->SetRect(0.0, 0.0);
00171 return 1;
00172 }
00173 n = sscanf(buffer,"%lg%lg%1[ijdr]",&a,&b,notation);
00174 if (n==1)
00175 v->SetRect(a,0);
00176 else if (n < 3 || strchr("ijdr",notation[0])==NULL)
00177 {
00178 output_error("convert_to_complex('%s',%s): complex number format is not valid", buffer,prop->name);
00179
00180
00181
00182
00183 return 0;
00184 }
00185
00186 else if (notation[0]==A)
00187 v->SetPolar(a,b*PI/180.0);
00188 else if (notation[0]==R)
00189 v->SetPolar(a,b);
00190 else
00191 v->SetRect(a,b);
00192 if (v->Notation()==I)
00193 v->Notation() = (CNOTATION)notation[0];
00194 return 1;
00195 }
00196
00201 int convert_from_enumeration(char *buffer,
00202 int size,
00203 void *data,
00204 PROPERTY *prop)
00205 {
00206 KEYWORD *keys=prop->keywords;
00207 int count = 0;
00208 char temp[1025];
00209
00210 int value = *(unsigned long*)data;
00211
00212
00213 for ( ; keys!=NULL ; keys=keys->next)
00214 {
00215
00216 if (keys->value==value){
00217
00218 count = strncpy(temp,keys->name,1024)?(int)strlen(temp):0;
00219 break;
00220 }
00221 }
00222
00223
00224 if (count == 0){
00225 count = sprintf(temp,"%d",value);
00226 }
00227 if(count < size - 1){
00228 memcpy(buffer, temp, count);
00229 buffer[count] = 0;
00230 return count;
00231 } else {
00232 return 0;
00233 }
00234 }
00235
00240 int convert_to_enumeration(char *buffer,
00241 void *data,
00242 PROPERTY *prop)
00243 {
00244 bool found = false;
00245 KEYWORD *keys=prop->keywords;
00246
00247
00248 for ( ; keys!=NULL ; keys=keys->next)
00249 {
00250 if (strcmp(keys->name,buffer)==0)
00251 {
00252 *(unsigned long*)data=keys->value;
00253 found = true;
00254 break;
00255 }
00256 }
00257 if (found)
00258 return 1;
00259 if (strncmp(buffer,"0x",2)==0)
00260 return sscanf(buffer+2,"%x",(unsigned long*)data);
00261 if (isdigit(*buffer))
00262 return sscanf(buffer,"%d",(unsigned long*)data);
00263 output_error("keyword '%s' is not valid for property %s", buffer,prop->name);
00264 return 0;
00265 }
00266
00271 #define SETDELIM "|"
00272 int convert_from_set(char *buffer,
00273 int size,
00274 void *data,
00275 PROPERTY *prop)
00276 {
00277 KEYWORD *keys;
00278
00279
00280 unsigned int64 value = *(unsigned int64*)data;
00281
00282
00283 int count=0;
00284
00285 int ISZERO = (value == 0);
00286
00287 buffer[0] = '\0';
00288
00289
00290 for ( keys=prop->keywords ; keys!=NULL ; keys=keys->next)
00291 {
00292
00293 if ((!ISZERO && keys->value!=0 && (keys->value&value)==keys->value) || (keys->value==0 && ISZERO))
00294 {
00295
00296 int len = (int)strlen(keys->name);
00297
00298
00299 value -= keys->value;
00300
00301
00302 if (size>count+len+1)
00303 {
00304
00305 if (buffer[0]!='\0')
00306 {
00307
00308 if (!(prop->flags&PF_CHARSET))
00309 {
00310 count++;
00311 strcat(buffer,SETDELIM);
00312 }
00313 }
00314
00315
00316 count += len;
00317 strcat(buffer,keys->name);
00318 }
00319
00320
00321 else
00322
00323
00324 return 0;
00325 }
00326 }
00327
00328
00329 return count;
00330 }
00331
00336 int convert_to_set(char *buffer,
00337 void *data,
00338 PROPERTY *prop)
00339 {
00340 KEYWORD *keys=prop->keywords;
00341 char temp[4096], *ptr;
00342 unsigned long value=0;
00343 int count=0;
00344
00345
00346 if (strnicmp(buffer,"0x",2)==0)
00347 return sscanf(buffer,"0x%x",(unsigned long *)data);
00348 else if (isdigit(buffer[0]))
00349 return sscanf(buffer,"%d",(unsigned long *)data);
00350
00351
00352 if (strlen(buffer)>sizeof(temp)-1)
00353 return 0;
00354
00355
00356 strcpy(temp,buffer);
00357
00358
00359 if ((prop->flags&PF_CHARSET) && strchr(buffer,'|')==NULL)
00360 {
00361 for (ptr=buffer; *ptr!='\0'; ptr++)
00362 {
00363 bool found = false;
00364 KEYWORD *key;
00365 for (key=keys; key!=NULL; key=key->next)
00366 {
00367 if (*ptr==key->name[0])
00368 {
00369 value |= key->value;
00370 count ++;
00371 found = true;
00372 break;
00373 }
00374 }
00375 if (!found)
00376 {
00377 output_error("set member '%c' is not a keyword of property %s", *ptr, prop->name);
00378 return 0;
00379 }
00380 }
00381 }
00382 else
00383 {
00384
00385 for (ptr=strtok(temp,SETDELIM); ptr!=NULL; ptr=strtok(NULL,SETDELIM))
00386 {
00387 bool found = false;
00388 KEYWORD *key;
00389
00390
00391 for (key=keys; key!=NULL; key=key->next)
00392 {
00393 if (strcmp(ptr,key->name)==0)
00394 {
00395 value |= key->value;
00396 count ++;
00397 found = true;
00398 break;
00399 }
00400 }
00401 if (!found)
00402 {
00403 output_error("set member '%s' is not a keyword of property %s", ptr, prop->name);
00404 return 0;
00405 }
00406 }
00407 }
00408 *(unsigned long *)data = value;
00409 return count;
00410 }
00411
00416 int convert_from_int16(char *buffer,
00417 int size,
00418 void *data,
00419 PROPERTY *prop)
00420 {
00421 char temp[1025];
00422 int count = sprintf(temp,"%hd",*(short*)data);
00423 if(count < size - 1){
00424 memcpy(buffer, temp, count);
00425 buffer[count] = 0;
00426 return count;
00427 } else {
00428 return 0;
00429 }
00430 }
00431
00436 int convert_to_int16(char *buffer,
00437 void *data,
00438 PROPERTY *prop)
00439 {
00440 return sscanf(buffer,"%hd",data);
00441 }
00442
00447 int convert_from_int32(char *buffer,
00448 int size,
00449 void *data,
00450 PROPERTY *prop)
00451 {
00452 char temp[1025];
00453 int count = sprintf(temp,"%ld",*(int*)data);
00454 if(count < size - 1){
00455 memcpy(buffer, temp, count);
00456 buffer[count] = 0;
00457 return count;
00458 } else {
00459 return 0;
00460 }
00461 }
00462
00467 int convert_to_int32(char *buffer,
00468 void *data,
00469 PROPERTY *prop)
00470 {
00471 return sscanf(buffer,"%ld",data);
00472 }
00473
00478 int convert_from_int64(char *buffer,
00479 int size,
00480 void *data,
00481 PROPERTY *prop)
00482 {
00483 char temp[1025];
00484 int count = sprintf(temp,"%" FMT_INT64 "d",*(int64*)data);
00485 if(count < size - 1){
00486 memcpy(buffer, temp, count);
00487 buffer[count] = 0;
00488 return count;
00489 } else {
00490 return 0;
00491 }
00492 }
00493
00498 int convert_to_int64(char *buffer,
00499 void *data,
00500 PROPERTY *prop)
00501 {
00502 return sscanf(buffer,"%" FMT_INT64 "d",data);
00503 }
00504
00509 int convert_from_char8(char *buffer,
00510 int size,
00511 void *data,
00512 PROPERTY *prop)
00513 {
00514 char temp[1025];
00515 char *format = "%s";
00516 int count = 0;
00517 if (strchr((char*)data,' ')!=NULL || strchr((char*)data,';')!=NULL || ((char*)data)[0]=='\0')
00518 format = "\"%s\"";
00519 count = sprintf(temp,format,(char*)data);
00520 if(count > size - 1){
00521 return 0;
00522 } else {
00523 memcpy(buffer, temp, count);
00524 buffer[count] = 0;
00525 return count;
00526 }
00527 }
00528
00533 int convert_to_char8(char *buffer,
00534 void *data,
00535 PROPERTY *prop)
00536 {
00537 char c=((char*)buffer)[0];
00538 switch (c) {
00539 case '\0':
00540 return ((char*)data)[0]='\0', 1;
00541 case '"':
00542 return sscanf(buffer+1,"%8[^\"]",data);
00543 default:
00544 return sscanf(buffer,"%8s",data);
00545 }
00546 }
00547
00552 int convert_from_char32(char *buffer,
00553 int size,
00554 void *data,
00555 PROPERTY *prop)
00556 {
00557 char temp[1025];
00558 char *format = "%s";
00559 int count = 0;
00560 if (strchr((char*)data,' ')!=NULL || strchr((char*)data,';')!=NULL || ((char*)data)[0]=='\0')
00561 format = "\"%s\"";
00562 count = sprintf(temp,format,(char*)data);
00563 if(count > size - 1){
00564 return 0;
00565 } else {
00566 memcpy(buffer, temp, count);
00567 buffer[count] = 0;
00568 return count;
00569 }
00570 }
00571
00576 int convert_to_char32(char *buffer,
00577 void *data,
00578 PROPERTY *prop)
00579 {
00580 char c=((char*)buffer)[0];
00581 switch (c) {
00582 case '\0':
00583 return ((char*)data)[0]='\0', 1;
00584 case '"':
00585 return sscanf(buffer+1,"%32[^\"]",data);
00586 default:
00587 return sscanf(buffer,"%32s",data);
00588 }
00589 }
00590
00595 int convert_from_char256(char *buffer,
00596 int size,
00597 void *data,
00598 PROPERTY *prop)
00599 {
00600 char temp[1025];
00601 char *format = "%s";
00602 int count = 0;
00603 if (strchr((char*)data,' ')!=NULL || strchr((char*)data,';')!=NULL || ((char*)data)[0]=='\0')
00604 format = "\"%s\"";
00605 count = sprintf(temp,format,(char*)data);
00606 if(count > size - 1){
00607 return 0;
00608 } else {
00609 memcpy(buffer, temp, count);
00610 buffer[count] = 0;
00611 return count;
00612 }
00613 }
00614
00619 int convert_to_char256(char *buffer,
00620 void *data,
00621 PROPERTY *prop)
00622 {
00623 char c=((char*)buffer)[0];
00624 switch (c) {
00625 case '\0':
00626 return ((char*)data)[0]='\0', 1;
00627 case '"':
00628 return sscanf(buffer+1,"%256[^\"]",data);
00629 default:
00630
00631 return sscanf(buffer,"%256[^\n\r;]",data);
00632 }
00633 }
00634
00639 int convert_from_char1024(char *buffer,
00640 int size,
00641 void *data,
00642 PROPERTY *prop)
00643 {
00644 char temp[4097];
00645 char *format = "%s";
00646 int count = 0;
00647 if (strchr((char*)data,' ')!=NULL || strchr((char*)data,';')!=NULL || ((char*)data)[0]=='\0')
00648 format = "\"%s\"";
00649 count = sprintf(temp,format,(char*)data);
00650 if(count > size - 1){
00651 return 0;
00652 } else {
00653 memcpy(buffer, temp, count);
00654 buffer[count] = 0;
00655 return count;
00656 }
00657 }
00658
00663 int convert_to_char1024(char *buffer,
00664 void *data,
00665 PROPERTY *prop)
00666 {
00667 char c=((char*)buffer)[0];
00668 switch (c) {
00669 case '\0':
00670 return ((char*)data)[0]='\0', 1;
00671 case '"':
00672 return sscanf(buffer+1,"%1024[^\"]",data);
00673 default:
00674 return sscanf(buffer,"%1024[^\n]",data);
00675 }
00676 }
00677
00682 int convert_from_object(char *buffer,
00683 int size,
00684 void *data,
00685 PROPERTY *prop)
00686 {
00687 OBJECT *obj = (data ? *(OBJECT**)data : NULL);
00688 char temp[256];
00689 memset(temp, 0, 256);
00690 if (obj==NULL)
00691 return 0;
00692
00693
00694 if (object_current_namespace()!=obj->space)
00695 {
00696 if (object_get_namespace(obj,buffer,size))
00697 strcat(buffer,"::");
00698 }
00699 else
00700 strcpy(buffer,"");
00701
00702 if (obj->name != NULL){
00703 if ((strlen(obj->name) != 0) && (strlen(obj->name) < (size_t)(size - 1))){
00704 strcat(buffer, obj->name);
00705 return 1;
00706 }
00707 }
00708
00709
00710 if (sprintf(temp,global_object_format,obj->oclass->name,obj->id)<size)
00711 strcat(buffer,temp);
00712 else
00713 return 0;
00714 return 1;
00715 }
00716
00721 int convert_to_object(char *buffer,
00722 void *data,
00723 PROPERTY *prop)
00724 {
00725 CLASSNAME cname;
00726 OBJECTNUM id;
00727 OBJECT **target = (OBJECT**)data;
00728 char oname[256];
00729 if (sscanf(buffer,"\"%[^\"]\"",oname)==1 || (strchr(buffer,':')==NULL && strncpy(oname,buffer,sizeof(oname))))
00730 {
00731 oname[sizeof(oname)-1]='\0';
00732 *target = object_find_name(oname);
00733 return (*target)!=NULL;
00734 }
00735 else if (sscanf(buffer,global_object_scan,cname,&id)==2)
00736 {
00737 OBJECT *obj = object_find_by_id(id);
00738 if(obj == NULL){
00739 *target = NULL;
00740 return 0;
00741 }
00742 if (obj!=NULL && strcmp(obj->oclass->name,cname)==0)
00743 {
00744 *target=obj;
00745 return 1;
00746 }
00747 }
00748 else
00749 *target = NULL;
00750 return 0;
00751 }
00752
00757 int convert_from_delegated(char *buffer,
00758 int size,
00759 void *data,
00760 PROPERTY *prop)
00761 {
00762 DELEGATEDVALUE *value = (DELEGATEDVALUE*)data;
00763 if (value==NULL || value->type==NULL || value->type->to_string==NULL)
00764 return 0;
00765 else
00766 return (*(value->type->to_string))(value->data,buffer,size);
00767 }
00768
00773 int convert_to_delegated(char *buffer,
00774 void *data,
00775 PROPERTY *prop)
00776 {
00777 DELEGATEDVALUE *value = (DELEGATEDVALUE*)data;
00778 if (value==NULL || value->type==NULL || value->type->from_string==NULL)
00779 return 0;
00780 else
00781 return (*(value->type->from_string))(value->data,buffer);
00782 }
00783
00788 int convert_from_boolean(char *buffer, int size, void *data, PROPERTY *prop){
00789 unsigned int b = 0;
00790 if(buffer == NULL || data == NULL || prop == NULL)
00791 return 0;
00792 b = *(bool *)data;
00793 if(b == 1 && (size > 4)){
00794 return sprintf(buffer, "TRUE");
00795 }
00796 if(b == 0 && (size > 5)){
00797 return sprintf(buffer, "FALSE");
00798 }
00799 return 0;
00800 }
00801
00806
00807 int convert_to_boolean(char *buffer, void *data, PROPERTY *prop){
00808 char str[32];
00809 int i = 0;
00810 if(buffer == NULL || data == NULL || prop == NULL)
00811 return 0;
00812 memcpy(str, buffer, 31);
00813 for(i = 0; i < 31; ++i){
00814 if(str[i] == 0)
00815 break;
00816 str[i] = toupper(str[i]);
00817 }
00818 if(0 == strcmp(str, "TRUE")){
00819 *(bool *)data = 1;
00820 return 1;
00821 }
00822 if(0 == strcmp(str, "FALSE")){
00823 *(bool *)data = 0;
00824 return 1;
00825 }
00826 return 0;
00827 }
00828
00829 int convert_from_timestamp_stub(char *buffer, int size, void *data, PROPERTY *prop){
00830 TIMESTAMP ts = *(int64 *)data;
00831 return convert_from_timestamp(ts, buffer, size);
00832
00833 }
00834
00835 int convert_to_timestamp_stub(char *buffer, void *data, PROPERTY *prop){
00836 TIMESTAMP ts = convert_to_timestamp(buffer);
00837 *(int64 *)data = ts;
00838 return 1;
00839 }
00840
00845 int convert_from_double_array(char *buffer, int size, void *data, PROPERTY *prop){
00846 int i = 0;
00847 return 0;
00848 }
00849
00854 int convert_to_double_array(char *buffer, void *data, PROPERTY *prop){
00855 return 0;
00856 }
00857
00862 int convert_from_complex_array(char *buffer, int size, void *data, PROPERTY *prop){
00863 return 0;
00864 }
00865
00870 int convert_to_complex_array(char *buffer, void *data, PROPERTY *prop){
00871 return 0;
00872 }
00873
00877 extern "C" int convert_unit_double(char *buffer,char *unit, double *data)
00878 {
00879 char *from = strchr(buffer,' ');
00880 *data = atof(buffer);
00881
00882 if (from==NULL)
00883 return 1;
00884
00885
00886 while (isspace(*from)) from++;
00887
00888 return unit_convert(from,unit,data);
00889 }
00890