core/convert.c

Go to the documentation of this file.
00001 
00013 #include <stdio.h>
00014 #include <math.h>
00015 #include <ctype.h>
00016 #include "globals.h"
00017 #include "convert.h"
00018 #include "object.h"
00019 
00024 int convert_from_void(char *buffer, 
00025                       int size, 
00026                       void *data, 
00027                       PROPERTY *prop) 
00028 {
00029     if(size < 7)
00030         return 0;
00031     return sprintf(buffer,"%s","(void)");
00032 }
00033 
00038 int convert_to_void(char *buffer, 
00039                       void *data, 
00040                       PROPERTY *prop) 
00041 {
00042     return 1;
00043 }
00044 
00050 int convert_from_double(char *buffer, 
00051                         int size, 
00052                         void *data, 
00053                         PROPERTY *prop) 
00054 {
00055     char temp[1025];
00056     int count = sprintf(temp, global_double_format, *(double *)data);
00057     if(count < size+1){
00058         memcpy(buffer, temp, count);
00059         buffer[count] = 0;
00060         return count;
00061     } else {
00062         return 0;
00063     }
00064 }
00065 
00071 int convert_to_double(char *buffer, 
00072                       void *data, 
00073                       PROPERTY *prop) 
00074 {
00075     return sscanf(buffer,"%lg",data);
00076 }
00077 
00083 int convert_from_complex(char *buffer, 
00084                         int size, 
00085                         void *data, 
00086                         PROPERTY *prop) 
00087 {
00088     int count = 0;
00089     char temp[1025];
00090     complex *v = data;
00091     if (v->f==A)
00092     {
00093         double m = sqrt(v->r*v->r+v->i*v->i);
00094         double a = (v->r==0) ? (v->i>0 ? PI/2 : (v->i==0 ? 0 : -PI/2)) : ((v->i>0) ? (v->r>0 ? atan(v->i/v->r) : PI-atan(v->i/v->r)) : (v->r>0 ? -atan(v->i/v->r) : PI+atan(v->i/v->r)));
00095         count = sprintf(temp,global_complex_format,m,a*180/PI,A);
00096     } else {
00097         count = sprintf(temp,global_complex_format,v->r,v->i,v->f?v->f:'i');
00098     }
00099     if(count < size - 1){
00100         memcpy(buffer, temp, count);
00101         buffer[count] = 0;
00102         return count;
00103     } else {
00104         return 0;
00105     }
00106 }
00107 
00113 int convert_to_complex(char *buffer, 
00114                        void *data, 
00115                        PROPERTY *prop) 
00116 {
00117     complex *v = (complex*)data;
00118     char notation[2]={CNOTATION_DEFAULT,'\0'};
00119     int n;
00120     double a=0, b=0; 
00121     n = sscanf(buffer,"%lg%lg%1[ijd]",&a,&b,notation);
00122     if (n < 2)
00123     {
00124         char signage[2] = {0, 0};
00125         /* printf("alt complex form?"); */
00126         n = sscanf(buffer, "%lg %1[+-] %lg%1[ijd]", &a, signage, &b, notation);
00127         if (n > 0 && signage[0] == '-')
00128             b *= -1.0;
00129     }
00130     if (n>0)
00131     {
00132         if (n>1 && notation[0]==A)
00133         {
00134             double m = a*a+b*b;
00135             v->r = m*cos(b*PI/180);
00136             v->i = m*sin(b*PI/180);
00137         }
00138         else
00139         {
00140             v->r = a;
00141             v->i = (n>1?b:0);
00142         }
00143         v->f = notation[0];
00144         return 1;
00145     }
00146     else
00147         return 0;
00148 }
00149 
00154 int convert_from_enumeration(char *buffer, 
00155                              int size, 
00156                              void *data, 
00157                              PROPERTY *prop) 
00158 {
00159     KEYWORD *keys=prop->keywords;
00160     int count = 0;
00161     char temp[1025];
00162     /* get the true value */
00163     int value = *(unsigned long*)data;
00164 
00165     /* process the keyword list, if any */
00166     for ( ; keys!=NULL ; keys=keys->next)
00167     {
00168         /* if the key value matched */
00169         if (keys->value==value){
00170             /* use the keyword */
00171             count = strncpy(temp,keys->name,1024)?(int)strlen(temp):0;
00172             break;
00173         }
00174     }
00175 
00176     /* no keyword found, return the numeric value instead */
00177     if (count == 0){
00178          count = sprintf(temp,"%d",value);
00179     }
00180     if(count < size - 1){
00181         memcpy(buffer, temp, count);
00182         buffer[count] = 0;
00183         return count;
00184     } else {
00185         return 0;
00186     }
00187 }
00188 
00193 int convert_to_enumeration(char *buffer, 
00194                            void *data, 
00195                            PROPERTY *prop) 
00196 {
00197     KEYWORD *keys=prop->keywords;
00198 
00199     /* process the keyword list */
00200     for ( ; keys!=NULL ; keys=keys->next)
00201     {
00202         if (strcmp(keys->name,buffer)==0)
00203         {
00204             *(unsigned long*)data=keys->value;
00205             return 1;
00206         }
00207     }
00208     return sscanf(buffer,"%d",(unsigned long*)data);
00209 }
00210 
00215 #define SETDELIM "|"
00216 int convert_from_set(char *buffer, 
00217                         int size, 
00218                         void *data, 
00219                         PROPERTY *prop) 
00220 {
00221     KEYWORD *keys=prop->keywords;
00222 
00223     /* get the actual value */
00224     long value = *(unsigned long*)data;
00225 
00226     /* keep track of how characters written */
00227     int count=0;
00228 
00229     /* clear the buffer */
00230     buffer[0] = '\0';
00231 
00232     /* process each keyword */
00233     for ( ; keys!=NULL ; keys=keys->next)
00234     {
00235         /* if the keyword matches */
00236         if ((keys->value&value)==keys->value || (keys->value==0 && value==0))
00237         {
00238             /* get the length of the keyword */
00239             int len = (int)strlen(keys->name);
00240 
00241             /* if there's room for it in the buffer */
00242             if (size>count+len+1)
00243             {
00244                 /* if the buffer already has keywords in it */
00245                 if (buffer[0]!='\0')
00246                 {
00247                     /* add a separator to the buffer */
00248                     if (!(prop->flags&PF_CHARSET))
00249                     {
00250                         count++;
00251                         strcat(buffer,SETDELIM);
00252                     }
00253                 }
00254 
00255                 /* add the keyword to the buffer */
00256                 count += len;
00257                 strcat(buffer,keys->name);
00258             }
00259 
00260             /* no room in the buffer */
00261             else
00262 
00263                 /* fail */
00264                 return 0;
00265         }
00266     }
00267 
00268     /* succeed */
00269     return count;
00270 }
00271 
00276 int convert_to_set(char *buffer, 
00277                         void *data, 
00278                         PROPERTY *prop) 
00279 {
00280     KEYWORD *keys=prop->keywords;
00281     char temp[4096], *ptr;
00282     unsigned long value=0;
00283     int count=0;
00284 
00285     /* directly convert numeric strings */
00286     if (strnicmp(buffer,"0x",2)==0)
00287         return sscanf(buffer,"0x%x",(unsigned long *)data);
00288     else if (isdigit(buffer[0]))
00289         return sscanf(buffer,"%d",(unsigned long *)data);
00290 
00291     /* prevent long buffer from being scanned */
00292     if (strlen(buffer)>sizeof(temp)-1)
00293         return 0;
00294 
00295     /* make a temporary copy of the buffer */
00296     strcpy(temp,buffer);
00297 
00298     /* check for CHARSET keys (single character keys) and usage without | */
00299     if ((prop->flags&PF_CHARSET) && strchr(buffer,'|')==NULL)
00300     {
00301         for (ptr=buffer; *ptr!='\0'; ptr++)
00302         {
00303             KEYWORD *key;
00304             for (key=keys; key!=NULL; key=key->next)
00305             {
00306                 if (*ptr==key->name[0])
00307                 {
00308                     value |= key->value;
00309                     count ++;
00310                     break; /* we found our key */
00311                 }
00312             }
00313         }
00314     }
00315     else
00316     {
00317         /* process each keyword in the temporary buffer*/
00318         for (ptr=strtok(temp,SETDELIM); ptr!=NULL; ptr=strtok(NULL,SETDELIM))
00319         {
00320             KEYWORD *key;
00321 
00322             /* scan each of the keywords in the set */
00323             for (key=keys; key!=NULL; key=key->next)
00324             {
00325                 if (strcmp(ptr,key->name)==0)
00326                 {
00327                     value |= key->value;
00328                     count ++;
00329                     break; /* we found our key */
00330                 }
00331             }
00332         }
00333     }
00334     *(unsigned long *)data = value;
00335     return count;
00336 }
00337 
00342 int convert_from_int16(char *buffer, 
00343                         int size, 
00344                         void *data, 
00345                         PROPERTY *prop) 
00346 {
00347     char temp[1025];
00348     int count = sprintf(temp,"%hd",*(short*)data);
00349     if(count < size - 1){
00350         memcpy(buffer, temp, count);
00351         buffer[count] = 0;
00352         return count;
00353     } else {
00354         return 0;
00355     }
00356 }
00357 
00362 int convert_to_int16(char *buffer, 
00363                         void *data, 
00364                         PROPERTY *prop) 
00365 {
00366     return sscanf(buffer,"%hd",data);
00367 }
00368 
00373 int convert_from_int32(char *buffer, 
00374                         int size, 
00375                         void *data, 
00376                         PROPERTY *prop) 
00377 {
00378     char temp[1025];
00379     int count = sprintf(temp,"%ld",*(int*)data);
00380     if(count < size - 1){
00381         memcpy(buffer, temp, count);
00382         buffer[count] = 0;
00383         return count;
00384     } else {
00385         return 0;
00386     }
00387 }
00388 
00393 int convert_to_int32(char *buffer, 
00394                         void *data, 
00395                         PROPERTY *prop) 
00396 {
00397     return sscanf(buffer,"%ld",data);
00398 }
00399 
00404 int convert_from_int64(char *buffer, 
00405                         int size, 
00406                         void *data, 
00407                         PROPERTY *prop) 
00408 {
00409     char temp[1025];
00410     int count = sprintf(temp,"%" FMT_INT64 "d",*(int64*)data);
00411     if(count < size - 1){
00412         memcpy(buffer, temp, count);
00413         buffer[count] = 0;
00414         return count;
00415     } else {
00416         return 0;
00417     }
00418 }
00419 
00424 int convert_to_int64(char *buffer, 
00425                         void *data, 
00426                         PROPERTY *prop) 
00427 {
00428     return sscanf(buffer,"%" FMT_INT64 "d",data);
00429 }
00430 
00435 int convert_from_char8(char *buffer, 
00436                         int size, 
00437                         void *data, 
00438                         PROPERTY *prop) 
00439 {
00440     char temp[1025];
00441     char *format = "%s";
00442     int count = 0;
00443     if (strchr((char*)data,' ')!=NULL || strchr((char*)data,';')!=NULL || ((char*)data)[0]=='\0')
00444         format = "\"%s\"";
00445     count = sprintf(temp,format,(char*)data);
00446     if(count > size - 1){
00447         return 0;
00448     } else {
00449         memcpy(buffer, temp, count);
00450         buffer[count] = 0;
00451         return count;
00452     }
00453 }
00454 
00459 int convert_to_char8(char *buffer, 
00460                         void *data, 
00461                         PROPERTY *prop) 
00462 {
00463     char c=((char*)data)[0];
00464     switch (c) {
00465     case '"':
00466         return sscanf(buffer+1,"%8[^\"]",data);
00467     default:
00468         return sscanf(buffer,"%8s",data);
00469     }
00470 }
00471 
00476 int convert_from_char32(char *buffer, 
00477                         int size, 
00478                         void *data, 
00479                         PROPERTY *prop) 
00480 {
00481     char temp[1025];
00482     char *format = "%s";
00483     int count = 0;
00484     if (strchr((char*)data,' ')!=NULL || strchr((char*)data,';')!=NULL || ((char*)data)[0]=='\0')
00485         format = "\"%s\"";
00486     count = sprintf(temp,format,(char*)data);
00487     if(count > size - 1){
00488         return 0;
00489     } else {
00490         memcpy(buffer, temp, count);
00491         buffer[count] = 0;
00492         return count;
00493     }
00494 }
00495 
00500 int convert_to_char32(char *buffer, 
00501                         void *data, 
00502                         PROPERTY *prop) 
00503 {
00504     char c=((char*)data)[0];
00505     switch (c) {
00506     case '"':
00507         return sscanf(buffer+1,"%32[^\"]",data);
00508     default:
00509         return sscanf(buffer,"%32s",data);
00510     }
00511 }
00512 
00517 int convert_from_char256(char *buffer, 
00518                         int size, 
00519                         void *data, 
00520                         PROPERTY *prop) 
00521 {
00522     char temp[1025];
00523     char *format = "%s";
00524     int count = 0;
00525     if (strchr((char*)data,' ')!=NULL || strchr((char*)data,';')!=NULL || ((char*)data)[0]=='\0')
00526         format = "\"%s\"";
00527     count = sprintf(temp,format,(char*)data);
00528     if(count > size - 1){
00529         return 0;
00530     } else {
00531         memcpy(buffer, temp, count);
00532         buffer[count] = 0;
00533         return count;
00534     }
00535 }
00536 
00541 int convert_to_char256(char *buffer, 
00542                         void *data, 
00543                         PROPERTY *prop) 
00544 {
00545     char c=((char*)data)[0];
00546     switch (c) {
00547     case '"':
00548         return sscanf(buffer+1,"%256[^\"]",data);
00549     default:
00550         return sscanf(buffer,"%256s",data);
00551     }
00552 }
00553 
00558 int convert_from_char1024(char *buffer, 
00559                         int size, 
00560                         void *data, 
00561                         PROPERTY *prop) 
00562 {
00563     char temp[1025];
00564     char *format = "%s";
00565     int count = 0;
00566     if (strchr((char*)data,' ')!=NULL || strchr((char*)data,';')!=NULL || ((char*)data)[0]=='\0')
00567         format = "\"%s\"";
00568     count = sprintf(temp,format,(char*)data);
00569     if(count > size - 1){
00570         return 0;
00571     } else {
00572         memcpy(buffer, temp, count);
00573         buffer[count] = 0;
00574         return count;
00575     }
00576 }
00577 
00582 int convert_to_char1024(char *buffer, 
00583                         void *data, 
00584                         PROPERTY *prop) 
00585 {
00586     char c=((char*)buffer)[0];
00587     switch (c) {
00588     case '"':
00589         return sscanf(buffer+1,"%1024[^\"]",data);
00590     default:
00591         return sscanf(buffer,"%1024[^\n]",data);
00592     }
00593 }
00594 
00599 int convert_from_object(char *buffer, 
00600                         int size, 
00601                         void *data, 
00602                         PROPERTY *prop) 
00603 {
00604     OBJECT *obj = (data ? *(OBJECT**)data : NULL);
00605     char temp[256];
00606     if (obj==NULL)
00607         return 0;
00608     if (obj->name != NULL){
00609         if ((strlen(obj->name) != 0) && (strlen(obj->name) < (size_t)(size - 1))){
00610             strcpy(buffer, obj->name);
00611             return 1;
00612         }
00613     }
00614     if (sprintf(temp,global_object_format,obj->oclass->name,obj->id)<size)
00615         strcpy(buffer,temp);
00616     else
00617         return 0;
00618     return 1;
00619 }
00620 
00625 int convert_to_object(char *buffer, 
00626                         void *data, 
00627                         PROPERTY *prop) 
00628 {
00629     CLASSNAME cname;
00630     OBJECTNUM id;
00631     OBJECT **target = (OBJECT**)data;
00632     char oname[256];
00633     if (sscanf(buffer,"\"%[^\"]\"",oname)==1 || (strchr(buffer,':')==NULL && strncpy(oname,buffer,sizeof(oname))))
00634     {
00635         oname[sizeof(oname)-1]='\0'; /* terminate unterminated string */
00636         *target = object_find_name(oname);
00637         return (*target)!=NULL;
00638     }
00639     else if (sscanf(buffer,global_object_scan,cname,&id)==2)
00640     {
00641         OBJECT *obj = object_find_by_id(id);
00642         if(obj == NULL){ /* failure case, make noisy if desired. */
00643             *target = NULL;
00644             return 0;
00645         }
00646         if (obj!=NULL && strcmp(obj->oclass->name,cname)==0)
00647         {
00648             *target=obj;
00649             return 1;
00650         }
00651     }
00652     else
00653         *target = NULL;
00654     return 0;
00655 }
00656 
00661 int convert_from_delegated(char *buffer, 
00662                         int size, 
00663                         void *data, 
00664                         PROPERTY *prop) 
00665 {
00666     DELEGATEDVALUE *value = (DELEGATEDVALUE*)data;
00667     if (value==NULL || value->type==NULL || value->type->to_string==NULL) 
00668         return 0;
00669     else
00670         return (*(value->type->to_string))(value->data,buffer,size);
00671 }
00672 
00677 int convert_to_delegated(char *buffer, 
00678                         void *data, 
00679                         PROPERTY *prop) 
00680 {
00681     DELEGATEDVALUE *value = (DELEGATEDVALUE*)data;
00682     if (value==NULL || value->type==NULL || value->type->from_string==NULL) 
00683         return 0;
00684     else
00685         return (*(value->type->from_string))(value->data,buffer);
00686 }
00687 
00692 int convert_from_boolean(char *buffer, int size, void *data, PROPERTY *prop){
00693     unsigned int b = 0;
00694     if(buffer == NULL || data == NULL || prop == NULL)
00695         return 0;
00696     b = *(unsigned int *)data;
00697     if(b == 1 && (size > 4)){
00698         return sprintf(buffer, "TRUE");
00699     }
00700     if(b == 0 && (size > 5)){
00701         return sprintf(buffer, "FALSE");
00702     }
00703     return 0;
00704 }
00705 
00710 int convert_to_boolean(char *buffer, void *data, PROPERTY *prop){
00711     char str[32];
00712     int i = 0;
00713     if(buffer == NULL || data == NULL || prop == NULL)
00714         return 0;
00715     memcpy(str, buffer, 31);
00716     for(i = 0; i < 31; ++i){
00717         if(str[i] == 0)
00718             break;
00719         str[i] = toupper(str[i]);
00720     }
00721     if(0 == strcmp(str, "TRUE")){
00722         *(unsigned int *)data = 1;
00723         return 1;
00724     }
00725     if(0 == strcmp(str, "FALSE")){
00726         *(unsigned int *)data = 0;
00727         return 1;
00728     }
00729     return 0;
00730 }
00731 
00732 int convert_from_timestamp_stub(char *buffer, int size, void *data, PROPERTY *prop){
00733     TIMESTAMP ts = *(int64 *)data;
00734     return convert_from_timestamp(ts, buffer, size);
00735     //return 0;
00736 }
00737 
00738 int convert_to_timestamp_stub(char *buffer, void *data, PROPERTY *prop){
00739     TIMESTAMP ts = convert_to_timestamp(buffer);
00740     *(int64 *)data = ts;
00741     return 1;
00742 }
00743 
00748 int convert_from_double_array(char *buffer, int size, void *data, PROPERTY *prop){
00749     int i = 0;
00750     return 0;
00751 }
00752 
00757 int convert_to_double_array(char *buffer, void *data, PROPERTY *prop){
00758     return 0;
00759 }
00760 
00765 int convert_from_complex_array(char *buffer, int size, void *data, PROPERTY *prop){
00766     return 0;
00767 }
00768 
00773 int convert_to_complex_array(char *buffer, void *data, PROPERTY *prop){
00774     return 0;
00775 }
00776 

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy