Adobe.com
Contents Files

SampleLib.cpp

Go to the documentation of this file.
00001 /**************************************************************************
00002 * ADOBE SYSTEMS INCORPORATED
00003 * Copyright 2007 Adobe Systems Incorporated
00004 * All Rights Reserved
00005 *
00006 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the
00007 * terms of the Adobe license agreement accompanying it.  If you have received this file from a
00008 * source other than Adobe, then your use, modification, or distribution of it requires the prior
00009 * written permission of Adobe.
00010 **************************************************************************/
00011 
00053 #include "SampleLib.h"
00054 #include "SoSharedLibDefs.h"
00055 #include "SoCClient.h"
00056 #include <stdlib.h>
00057 #include <stdio.h>
00058 #include <string.h>
00059 #include <math.h>
00060 
00061 
00062 #if defined (_WINDOWS)
00063 #pragma warning( push )
00064 #pragma warning(disable : 4996) // Security warning about strcpy on win
00065 #define strdup _strdup
00066 #endif
00067 
00069 static long libraryVersionNumber = 1;
00070 
00077 static char* signatures =
00078         "setVersion_d,"                 // setVersion (int) 
00079         "createArray,"                  // createArray() 
00080         "paramAny_a,"                   // paramAny (any)
00081         "paramString_s,"                // paramString (string)
00082         "paramBool_b,"                  // paramBool (bool) 
00083         "paramUInt32_u,"                // paramUInt (unsigned int)
00084         "paramInt32_d,"                 // paramInt (signed int) 
00085         "paramFloat64_f"                // paramFloat64 (double)
00086         "built"                                 // built() -> string
00087 ;
00088 
00089 #if MAC
00090 #define unused(a) (void*) a ;
00091 #else
00092 void* unused(void* x){ return x;} ;
00093 #endif
00094 
00101 extern "C" SAMPLIB long ESGetVersion()
00102 {
00103         return libraryVersionNumber;
00104 }
00105 
00113 extern "C" SAMPLIB char* ESInitialize (TaggedData* argv, long argc)
00114 {
00115         unused(&argv);
00116         unused(&argc);
00117 
00118         return signatures;
00119 }
00120 
00126 extern "C" SAMPLIB void ESTerminate()
00127 {
00128 }
00129 
00137 extern "C" SAMPLIB void ESFreeMem (void* p)
00138 {
00139         if (p)
00140                 free (p);
00141 }
00142 
00143 
00151 extern "C" SAMPLIB void* ESMallocMem (size_t nBytes)
00152 {
00153         void* p = malloc(nBytes);
00154 
00155         return p ;
00156 }
00157 
00168 extern "C" SAMPLIB long setVersion (TaggedData* argv, long argc, TaggedData* result)
00169 {
00170         /* to keep compiler warnings from popping up */
00171         result = NULL;
00172         if (argc >= 1)
00173                 libraryVersionNumber = argv [0].data.intval;
00174 
00175         return kESErrOK;
00176 }
00177 
00187 static char* stringize (TaggedData* p)
00188 {
00189         char* result, *q, *r;
00190         size_t size;
00191 
00192         char temp [128];
00193 
00194         switch (p->type)
00195         {
00196         case kTypeUndefined:
00197                 strcpy (temp, "undefined");
00198                 break;
00199         case kTypeBool:
00200                 strcpy (temp, (p->data.intval == 0) ? "false" : "true");
00201                 break;
00202         case kTypeInteger:
00203                 break;
00204         case kTypeUInteger:
00205                 break;
00206         case kTypeDouble:
00207                 break;
00208         }
00209         if (p->type != kTypeString)
00210         {
00211                 result = (char*) malloc (strlen (temp) + 1);
00212                 strcpy (result, temp);
00213         }
00214         else
00215         {
00216                 // Strings: count the quote characters inside
00217                 size = 3; // "" plus trailing zero
00218                 for (q = p->data.string; *q; q++)
00219                         size += (*q == '"') ? 2 : 1;
00220                 result = (char*) malloc (size);
00221                 // transfer the string 
00222                 r = result;
00223                 *r++ = '"';
00224                 for (q = p->data.string; *q; )
00225                 {
00226                         if (*q == '"')
00227                                 *r++ = '\\';
00228                         *r++ = *q++;
00229                 }
00230                 *r++ = '"';
00231                 *r = 0;
00232         }
00233         return result;
00234 }
00235 
00236 /*
00237 * \brief Example of a function that takes an arbitrary number of arguments of any type.
00238 * 
00239 * This function creates a script that contains a JavaScript array. It stringizes
00240 * every argument and creates a comma separated list surrounded by square brackets.
00241 *
00242 * \param argv - The JavaScript argument
00243 * \param argc the argument count
00244 * \param retval The return value to be passed back to JavaScript
00245 */
00246 extern "C" SAMPLIB long createArray (TaggedData* argv, long argc, TaggedData* result)
00247 {
00248         int i;
00249         size_t size;
00250         char* script, **stringArgs;
00251 
00252         // Create the string argument array - we need to stringize each argument
00253         stringArgs = (char**) malloc (argc * sizeof (char*));
00254         for (i = 0; i < argc; i++)
00255                 stringArgs [i] = stringize (&argv [i]);
00256 
00257         // Determine the size of the needed return buffer 
00258         size = 3;       /* [] plus the trailing zero */
00259         for (i = 0; i < argc; i++)
00260                 size += strlen (stringArgs [i]) + 1; // plus a comma
00261 
00262         // Create the script
00263         script = (char*) malloc (size);
00264         strcpy (script, "[");
00265         for (i = 0; i < argc; i++)
00266         {
00267                 if (i > 0)
00268                         strcat (script, ",");
00269                 strcat (script, stringArgs [i]);
00270         }
00271         strcat (script, "]");
00272         result->data.string = script;
00273         result->type = kTypeScript;
00274 
00275         // clean up
00276         for (i = 0; i < argc; i++)
00277                 free (stringArgs [i]);
00278         free (stringArgs);
00279 
00280         return kESErrOK;
00281 }
00282 
00292 extern "C" SAMPLIB long paramAny (TaggedData* argv, long argc, TaggedData* result)
00293 {
00294         int errval = kESErrOK;
00295 
00296         if (1 != argc)
00297                 return kESErrBadArgumentList;
00298 
00299         result->type = argv[0].type;
00300 
00301         switch (argv[0].type)
00302         {
00303         case kTypeUndefined:
00304                 break;
00305         case kTypeBool:
00306         case kTypeInteger:
00307         case kTypeUInteger:
00308                 result->data.intval = argv[0].data.intval;
00309                 break;
00310         case kTypeDouble:
00311                 result->data.fltval = argv[0].data.fltval;
00312                 break;
00313         case kTypeString:
00314                 result->data.string = (char*) malloc (strlen (argv[0].data.string) + 1);
00315                 if ( NULL != result->data.string )
00316                 {
00317                         strcpy (result->data.string, argv[0].data.string);
00318                 }
00319                 else
00320                 {
00321                         errval = kESErrNoMemory;
00322                 }
00323                 break;
00324         default:
00325                 errval = kESErrTypeMismatch;
00326                 break;
00327         }
00328 
00329         return errval;
00330 }
00331 
00341 extern "C" SAMPLIB long paramBool (TaggedData* argv, long argc, TaggedData* result)
00342 {
00343         int errval = kESErrOK;
00344 
00345         // Method requires 1 and only 1 argument
00346         if (1 != argc)
00347                 return kESErrBadArgumentList;
00348 
00349         // If the value undefined is passed in, its value is
00350         // not converted to the specified type.
00351         if ( kTypeUndefined != argv[0].type )
00352         {
00353                 if ( kTypeBool == argv[0].type )
00354                 {
00355                         result->type = kTypeBool;
00356                         result->data.intval = argv[0].data.intval;
00357                 }
00358                 else
00359                 {
00360                         errval = kESErrTypeMismatch;
00361                 }
00362         }
00363 
00364         return errval;
00365 }
00366 
00377 extern "C" SAMPLIB long paramUInt32 (TaggedData* argv, long argc, TaggedData* result)
00378 {
00379         int errval = kESErrOK;
00380 
00381         if (1 != argc)
00382                 return kESErrBadArgumentList;
00383 
00384         if ( kTypeUndefined != argv[0].type )
00385         {
00386                 if ( kTypeUInteger == argv[0].type )
00387                 {
00388                         result->type = kTypeUInteger;
00389                         result->data.intval = argv[0].data.intval;
00390                 }
00391                 else
00392                 {
00393                         errval = kESErrTypeMismatch;
00394                 }
00395         }
00396 
00397         return errval;
00398 }
00399 
00410 extern "C" SAMPLIB long paramInt32 (TaggedData* argv, long argc, TaggedData* result)
00411 {
00412         int errval = kESErrOK;
00413 
00414         if (1 != argc)
00415                 return kESErrBadArgumentList;
00416 
00417         if ( kTypeUndefined != argv[0].type )
00418         {
00419                 if ( kTypeInteger == argv[0].type )
00420                 {
00421                         result->type = kTypeInteger;
00422                         result->data.intval = argv[0].data.intval;
00423                 }
00424                 else
00425                 {
00426                         errval = kESErrTypeMismatch;
00427                 }
00428         }
00429 
00430         return errval;
00431 }
00432 
00443 extern "C" SAMPLIB long paramFloat64 (TaggedData* argv, long argc, TaggedData* result)
00444 {
00445         int errval = kESErrOK;
00446 
00447         if (1 != argc)
00448                 return kESErrBadArgumentList;
00449 
00450         if ( kTypeUndefined != argv[0].type )
00451         {
00452                 if ( kTypeDouble == argv[0].type )
00453                 {
00454                         result->type = kTypeDouble;
00455                         result->data.fltval = argv[0].data.fltval;
00456                 }
00457                 else
00458                 {
00459                         errval = kESErrTypeMismatch;
00460                 }
00461         }
00462 
00463         return errval;
00464 }
00465 
00477 extern "C" SAMPLIB long paramString (TaggedData* argv, long argc, TaggedData* result)
00478 {
00479         int errval = kESErrOK;
00480 
00481         if (1 != argc)
00482                 return kESErrBadArgumentList;
00483 
00484         if ( kTypeUndefined != argv[0].type )
00485         {
00486                 if ( kTypeString == argv[0].type )
00487                 {
00488                         result->type = kTypeString;
00489                         result->data.string = (char*) malloc (strlen (argv[0].data.string) + 1);
00490                         if ( NULL == result->data.string )
00491                         {
00492                                 errval = kESErrNoMemory;
00493                         }
00494                         else
00495                         {
00496                                 strcpy( result->data.string, argv[0].data.string );
00497                         }
00498                 }
00499                 else
00500                 {
00501                         errval = kESErrTypeMismatch;
00502                 }
00503         }
00504 
00505         return errval;
00506 }
00507 
00517 extern "C" SAMPLIB long built(TaggedData* argv, long argc, TaggedData* result)
00518 {
00519         int errval = kESErrOK;
00520         char sResult[1000] ;
00521 
00522         if  ( strlen(__DATE__) + strlen(__TIME__) < (sizeof(sResult)-10) )  {
00523 
00524                 sResult[0] = 0 ;
00525                 strcpy(sResult,__DATE__) ;
00526                 strcat(sResult," ") ;
00527                 strcat(sResult,__TIME__) ;
00528 
00529                 result->type = kTypeString;
00530                 result->data.string = strdup(sResult) ;
00531                 if ( !result->data.string ) errval = kESErrNoMemory;
00532         } else {
00533                 errval = kESErrNoMemory ;
00534         }
00535 
00536         unused(&argv) ;
00537         unused(&argc) ;
00538 
00539         return errval;
00540 }
00541 
00542 // ------------------------------------------------
00543 // START SampleLibObject                          
00544 
00545 // Statics
00546 
00550 SoObjectInterface*      gpObjectInterface       = NULL  ;
00551 
00555 SoServerInterface*      gpServer                        = NULL  ;
00556 
00560 SoHServer                       ghServer                        = NULL  ;
00561 
00565 int                                     ME                                      = 0             ;
00566 
00567 struct myData_s
00568 {
00569         TaggedData      a   ; // value of property a
00570         TaggedData      b   ; // value of property b
00571         int                     me      ;                       // unique identifier for this instance
00572 } ;
00573 
00574 typedef struct myData_s  myData_t ;
00575 typedef        myData_t* myData_p ;
00576 
00577 // prototypes
00578 ESerror_t objectInitialize(SoHObject hObject,int argc,TaggedData* argv) ;
00579 ESerror_t objectGet       (SoHObject hObject,SoCClientName* name,TaggedData* pResult) ;
00580 ESerror_t objectPut       (SoHObject hObject,SoCClientName* name,TaggedData* pValue) ;
00581 ESerror_t objectCall      (SoHObject hObject,SoCClientName* name,int argc,TaggedData* argv,TaggedData* pResult) ;
00582 ESerror_t objectToString  (SoHObject hObject,TaggedData* pResult) ;
00583 ESerror_t objectValueOf   (SoHObject hObject,TaggedData* pResult) ;
00584 ESerror_t objectFinalize  (SoHObject hObject) ;
00585 
00597 ESerror_t objectInitialize(SoHObject hObject,int argc, TaggedData* argv)
00598 {
00599 
00600         // An array of SoCClientName structures representing 
00601         // the methods of this object
00602         SoCClientName Methods[] =
00603         {   {   "reverse"       , 0 , NULL      }
00604         ,       {       "sine"          , 0 , NULL      }
00605         ,   { NULL }
00606         } ;
00607 
00608         // An array of SoCClientName structures representing 
00609         // the properties of this object
00610         SoCClientName Properties[] =
00611         {       {       "a"                     , 0 , NULL      }
00612         ,       {       "b"                     , 0 , NULL      }
00613         ,       {       "pi"            , 0 , NULL      }
00614         ,       {       "me"            , 0 , NULL      }
00615         ,       {       "built"         , 0 , NULL      }
00616         ,   {   NULL }
00617         } ;
00618 
00619         size_t   size    = sizeof(myData_t) ;
00620         myData_p pMyData = NULL  ;
00621 
00622         unused(&argc) ;
00623         unused( argv) ;
00624 
00625         pMyData = (myData_p)(malloc(size))   ;
00626 
00627         memset  (pMyData,0,size) ;
00628         pMyData->me = ++ME        ;
00629         gpServer->taggedDataInit(hObject,&pMyData->a) ;
00630         gpServer->taggedDataInit(hObject,&pMyData->b) ;
00631         gpServer->setClientData(hObject,pMyData) ;
00632 
00633         gpServer->addProperties(hObject,Properties) ;
00634         gpServer->addMethods   (hObject,Methods   ) ; 
00635 
00636         return kESErrOK ;
00637 }
00638 
00646 ESerror_t objectGet(SoHObject hObject,SoCClientName* name,TaggedData* pResult)
00647 {
00648         ESerror_t error = kESErrOK ;
00649 
00650         myData_p pMyData = NULL ;
00651         gpServer->getClientData(hObject,(void**)&pMyData);
00652 
00653 
00654         if ( strcmp(name->name_sig,"a") == 0 )
00655         {
00656                 *pResult = pMyData->a ;
00657                 if ( pResult->type == kTypeString ) pResult->data.string = strdup(pResult->data.string) ;
00658         }
00659 
00660         else if ( strcmp(name->name_sig,"b") == 0 )
00661         {
00662                 *pResult = pMyData->b ;
00663                 if ( pResult->type == kTypeString ) pResult->data.string = strdup(pResult->data.string) ;
00664         }
00665 
00666         else if ( strcmp(name->name_sig,"pi") == 0 )
00667         {
00668                 pResult->type = kTypeDouble ;
00669                 pResult->data.fltval = atan2(1.0,1.0) * 4.0 ;
00670         }
00671 
00672         else if ( strcmp(name->name_sig,"built") == 0 )
00673         {
00674                 error = built(NULL,0,pResult) ;
00675         } 
00676 
00677         else if ( strcmp(name->name_sig,"me") == 0 )
00678         {
00679                 pResult->type = kTypeInteger ;
00680                 pResult->data.intval = pMyData->me ;
00681         }
00682 
00683         return error ;
00684 }
00685 
00693 ESerror_t objectPut(SoHObject hObject,SoCClientName* name,TaggedData* pValue)
00694 {
00695         myData_p    pMyData = NULL ;
00696 
00697         gpServer->getClientData(hObject,(void**)&pMyData);
00698 
00699         if ( strcmp(name->name_sig,"a") == 0 )
00700         {
00701                 pMyData->a = *pValue ;
00702                 if ( pMyData->a.type == kTypeString ) pMyData->a.data.string = strdup(pMyData->a.data.string) ;
00703         }
00704         else if ( strcmp(name->name_sig,"b") == 0 )
00705         {
00706                 pMyData->b = *pValue ;
00707                 if ( pMyData->b.type == kTypeString ) pMyData->b.data.string = strdup(pMyData->b.data.string) ;
00708         }
00709 
00710 
00711         return 0 ;
00712 }
00713 
00723 ESerror_t objectCall(SoHObject hObject,SoCClientName* name,int argc,TaggedData* argv,TaggedData* pResult)
00724 {
00725         ESerror_t result          = kESErrOK ;
00726         int       bReverse        = strcmp(name->name_sig,"reverse"     ) == 0 ;
00727         int       bSine           = strcmp(name->name_sig,"sine"        ) == 0 ;
00728 
00729         int       type = argc == 1 ? argv[0].type : kTypeUndefined ;
00730 
00731         unused(hObject) ;
00732 
00733         if ( bSine ) { 
00734 
00735                 if              ( argc != 1 ) 
00736                         result =  kESErrBadArgumentList ;
00737                 else if ( type != kTypeDouble && type != kTypeInteger  )
00738                         result =  kESErrTypeMismatch ;
00739                 else
00740                 {
00741                         double      angle  = type == kTypeDouble ? argv[0].data.fltval : (double) argv[0].data.intval ;
00742                         double      pi     = 4.0 * atan2(1.0,1.0) ;
00743                         double      radian = 180.0 / pi ;
00744                         angle              = angle / radian ;
00745                         pResult->data.fltval = sin(angle) ;
00746                         pResult->type        = kTypeDouble ;
00747                 }
00748         }
00749 
00750         else if ( bReverse ) {
00751 
00752                 if              ( argc != 1 ) 
00753                         result =  kESErrBadArgumentList ;
00754                 else if ( type != kTypeString )
00755                         result =  kESErrTypeMismatch ;
00756                 else
00757                 {
00758                         size_t i ;
00759                         size_t l ;
00760                         pResult->type   = kTypeString ;
00761                         pResult->data.string = strdup(argv[0].data.string) ;
00762                         l  = strlen(pResult->data.string) ;
00763                         for ( i = 0 ; i < l/2 ; i ++ ) {
00764                                 char t = pResult->data.string[i] ;
00765                                 pResult->data.string[i] = pResult->data.string[l-i-1] ;
00766                                 pResult->data.string[l-i-1] = t                       ;
00767                         }
00768                 }
00769         }
00770 
00771         return result ;
00772 }
00773 
00780 ESerror_t objectValueOf(SoHObject hObject,TaggedData* pResult)
00781 {
00782         myData_p    pMyData = NULL ;
00783         gpServer->getClientData(hObject,(void**)&pMyData);
00784 
00785         pResult->type = kTypeString ;
00786         pResult->data.string = strdup("objectValueOf:: this is the value") ;
00787         return   kESErrOK ;
00788 
00789 }
00790 
00797 ESerror_t objectToString(SoHObject hObject,TaggedData* pResult)
00798 {
00799         myData_p    pMyData = NULL ;
00800         gpServer->getClientData(hObject,(void**)&pMyData);
00801 
00802         pResult->type = kTypeString ;
00803         pResult->data.string = strdup("objectToString::Object") ;
00804         return   kESErrOK ;
00805 }
00806 
00812 ESerror_t objectFinalize(SoHObject hObject)
00813 {
00814         myData_p pMyData = NULL ;
00815         gpServer->getClientData(hObject,(void**)&pMyData);
00816         if (  pMyData ) {
00817                 if ( pMyData->a.type == kTypeString ) free((void*)pMyData->a.data.string) ;
00818                 if ( pMyData->b.type == kTypeString ) free((void*)pMyData->b.data.string) ;
00819                 free(pMyData) ;
00820                 gpServer->setClientData(hObject,NULL) ;
00821         }
00822         return kESErrOK ;
00823 }
00824 
00830 SoObjectInterface objectInterface =
00831 {  objectInitialize
00832 ,  objectPut
00833 ,  objectGet
00834 ,  objectCall
00835 ,  NULL  /* objectValueOf  */
00836 ,  NULL  /* objectToString */
00837 ,  objectFinalize
00838 } ;
00839 
00840 /* ESClientInterface - optional call to support ExternalObject Object Interface */
00841 extern "C" SAMPLIB int  ESClientInterface (SoCClient_e kReason,SoServerInterface* pServer,SoHServer hServer)
00842 {
00843         if ( !gpObjectInterface ) {
00844                 gpObjectInterface = &objectInterface ;
00845         }
00846         ghServer = hServer ;
00847 
00848         switch ( kReason )
00849         {
00850         case kSoCClient_init : {
00851                 gpServer  = pServer ;
00852                 ghServer  = hServer ;
00853 
00854                 gpServer->addClass  (hServer,"SampleLibObject",&objectInterface) ;
00855                 return 0 ;
00856                                                                                                  } break ;
00857 
00858         case kSoCClient_term : {
00859                 ME = 0   ;
00860                 return 0 ;
00861                                                                                                  } break ;
00862         }
00863 
00864         return 0 ;
00865 }
00866 
00867 #if defined (_WINDOWS)
00868 #pragma warning( pop )
00869 #endif
00870 
00871 // END of SampleLibObject                           
00872 // ------------------------------------------------ 
00873 
Contents Files
Adobe Solutions Network
 
Copyright © 2010 Adobe Systems Incorporated. All rights reserved.
Terms of Use Online Privacy Policy Adobe and accessibility Avoid software piracy Permissions and Trademarks