home *** CD-ROM | disk | FTP | other *** search
/ ftptest.leeds.ac.uk / 2015.02.ftptest.leeds.ac.uk.tar / ftptest.leeds.ac.uk / bionet / CAE-GROUP / SCL-WIN3x / FED_PLUS.EXE / CLASSESM.C < prev    next >
C/C++ Source or Header  |  1994-07-24  |  18KB  |  629 lines

  1. #define    CLASSESM_C
  2. #include "classes.h"
  3.  
  4. /*******************************************************************
  5. ** FedEx parser output module for generating C++  class definitions
  6. ** December  5, 1989
  7. ** release 2 17-Feb-1992
  8. ** release 3 March 1993
  9. ** release 4 December 1993
  10. ** K. C. Morris
  11. **
  12. ** Development of FedEx was funded by the United States Government,
  13. ** and is not subject to copyright.
  14.  
  15. *******************************************************************
  16. The conventions used in this binding follow the proposed specification
  17. for the STEP Standard Data Access Interface as defined in document
  18. N350 ( August 31, 1993 ) of ISO 10303 TC184/SC4/WG7.
  19. *******************************************************************/
  20.  
  21.  
  22.  
  23. /******************************************************************
  24. **        The following functions will be used        ***
  25. ***        through out the the program fedex_plus        ***/
  26.  
  27.  
  28. /******************************************************************
  29.  ** Procedure:  CheckWord 
  30.  ** Description: if word is a reserved word, it is capitalized
  31.  ** Parameters:  word -- string to be checked
  32.  ** Returns:  altered word if word is reserved;  otherwise the original word
  33.  ** Side Effects:  the word is altered in memory
  34.  ** Status:  started 12/1
  35.  ******************************************************************/
  36. const char *    
  37. CheckWord (const char * word)
  38. {
  39. #ifdef NOT_USING_SDAI_BINDING
  40. /*  obsolete with proposed c++ binding  */
  41.  
  42.     static char *reserved_words [] = {
  43. "application_marker",  "asm",    "attributes", "auto",    
  44. "break",    "case",    "char",    "class",    "const",    "continue",
  45. "default",    "delete",    "do",    "double",    
  46. "else",    "enum",    "extern",
  47. "float",    "for",    "friend",    "goto",    "handle",    
  48. "if",    "inline",    "instance_id",    "int",    "long",    
  49. "new",    "nullable",    "opcode",  "operator",    "overload",    
  50. "private",    "protected",    "public",    "register",    "return",
  51. "shared",     "short",    "sizeof",    "static",    "struct",    "switch",
  52. "this",        "template",    "type",    "typedef",    "type_name",
  53. "union",    "unsigned",    
  54. "val",     "virtual",    "void",    "volatile"
  55.       };
  56.     int nwords = (sizeof reserved_words / sizeof reserved_words[0]);
  57.     int cond,
  58.     i, 
  59.         low = 0,
  60.         high = nwords - 1;    
  61.  
  62.     /*  word is obviously not in list, if it is longer than any of the words in the list  */
  63.     if (strlen (word) > 12)   return (word);
  64.  
  65.     while (low <= high) {
  66.     i = (low + high) / 2;
  67.     if ((cond = strcmp (word, reserved_words [i])) < 0) 
  68.         high = i -1;
  69.     else if (cond > 0)
  70.         low = i +1;
  71.     else {    /*    word is a reserved word, capitalize it    */
  72.         printf ("** warning: reserved word  %s  ", word);
  73.         *(word + 0) = toupper (*(word +0));
  74.         printf ("is changed to  %s **\n", word);
  75.         
  76.         }
  77.     }
  78. #endif
  79.     return (word);
  80.  
  81. }
  82.  
  83. /******************************************************************
  84.  ** Procedure:  string functions
  85.  ** Description:  These functions take a character or a string and return
  86.  **    a temporary copy of the string with the function applied to it.
  87.  ** Parameters:  
  88.  ** Returns:  temporary copy of characters
  89.  ** Side Effects:  character or string returned persists until the 
  90.  **    next invocation of the function
  91.  ** Status:  complete
  92.  ******************************************************************/
  93.  
  94. char
  95. ToLower (char c)
  96. {
  97.     if (isupper (c)) return (tolower (c));
  98.     else return (c);
  99.  
  100. }
  101.  
  102. char
  103. ToUpper  (char c)
  104. {
  105.     if (islower (c)) return (toupper (c));
  106.     else return (c);
  107. }
  108.  
  109. const char *
  110. StrToLower (const char * word)
  111. {
  112.     static char newword [MAX_LEN];
  113.     int i =0;
  114.     if (!word)  return 0;    
  115.     while (word [i] != '\0') {
  116.     newword [i] = ToLower (word [i]);
  117.     ++i;    
  118.     }
  119.     newword [i] = '\0';
  120.     return (newword)    ;
  121.     
  122. }
  123.  
  124. const char * 
  125. StrToUpper (const char * word)
  126. {
  127.     static char newword [MAX_LEN];
  128.     int i =0;
  129.     char ToUpper (char c);
  130.     
  131.     while (word [i] != '\0') {
  132.     newword [i] = ToUpper (word [i]);
  133.     ++i;
  134.     
  135.     }
  136.     newword [i] = '\0';
  137.     return (newword);
  138. }
  139.  
  140. const char * 
  141. StrToConstant (const char * word)
  142. {
  143.     static char newword [MAX_LEN];
  144.     int i =0;
  145.     
  146.     while (word [i] != '\0') {
  147.     if (word [i] == '/' || word [i] == '.') newword [i] = '_';
  148.     else newword [i] = ToUpper (word [i]);
  149.     ++i;
  150.     
  151.     }
  152.     newword [i] = '\0';
  153.     return (newword);
  154. }
  155.  
  156. /******************************************************************
  157.  ** Procedure:  FILEcreate  
  158.  ** Description:  creates a file for c++ with header definitions
  159.  ** Parameters:  filename
  160.  ** Returns:  FILE*    pointer to file created or NULL
  161.  ** Side Effects:  creates a file with name filename
  162.  ** Status:  complete
  163.  ******************************************************************/
  164.  
  165. FILE*
  166. FILEcreate (const char * filename)
  167. {
  168.     FILE* file;
  169.     const char * fn;
  170.     
  171.     if ((file = fopen (filename, "w")) == NULL) {
  172.     printf ("**Error in SCHEMAprint:  unable to create file %s ** \n", filename);
  173.     return (NULL);
  174.     }
  175.  
  176.     fprintf (file, "#ifndef  %s\n", fn = StrToConstant (filename));
  177.     fprintf (file, "#define  %s\n", fn );
  178.  
  179.     fprintf(file,"// This file was generated by fedex_plus.  You probably don't want to edit\n");
  180.     fprintf(file,"// it since your modifications will be lost if fedex_plus is used to\n");
  181.     fprintf(file,"// regenerate it.\n");
  182.     return (file);
  183.     
  184. }
  185.  
  186. /******************************************************************
  187.  ** Procedure:  FILEclose 
  188.  ** Description:  closes a file opened with FILEcreate
  189.  ** Parameters:  FILE*  file  --  pointer to file to close
  190.  ** Returns:  
  191.  ** Side Effects:  
  192.  ** Status:  complete
  193.  ******************************************************************/
  194.  
  195. void
  196. FILEclose (FILE* file)
  197. {
  198.     fprintf (file, "#endif\n");
  199.     fclose (file);
  200. }
  201.  
  202.  
  203. /******************************************************************
  204.  ** Procedure:  isAggregate
  205.  ** Parameters:  Attribute a
  206.  ** Returns:  int     indicates whether the attribute is an aggregate
  207.  ** Description:      indicates whether the attribute is an aggregate
  208.  ** Side Effects:  none
  209.  ** Status:  complete 1/15/91
  210.  ******************************************************************/
  211.  
  212. int
  213. isAggregate  (Variable a)
  214. {
  215.     return(TYPEinherits_from(VARget_type(a),aggregate_));
  216. }
  217.  
  218. int 
  219. isAggregateType (const Type t)
  220. {
  221.     return(TYPEinherits_from(t,aggregate_));
  222. }
  223.  
  224.  
  225. /******************************************************************
  226.  ** Procedure:  TYPEget_ctype
  227.  ** Parameters:  const Type t --  type for attribute
  228.  ** Returns:  a string which is the type of the data member in the c++ class
  229.  ** Description:  supplies the type of a data member for the c++ class
  230.  ** Side Effects:  
  231.  ** Status:  complete 1/15/90
  232.  ** Changes: Modified by CD to return the appropiate types as outlined in "SDAI
  233.  **          C++ Binding for PDES, Inc. Prototyping" by Stephen Clark
  234.  ** Change Date: 5/22/91 CD
  235.  ** Change Date: 28-Sep-1993 made entities use their real type instead of base class STEPentityH 
  236. ******************************************************************/
  237.  
  238. const char *
  239. TYPEget_ctype (const Type t)
  240. {      
  241.     Class_Of_Type class, class2;
  242.     Type bt;
  243.     static char retval [BUFSIZ];
  244.     char *n;
  245.     
  246.  
  247.     /*  aggregates are based on their base type  
  248.     case TYPE_ARRAY:
  249.     case TYPE_BAG:
  250.     case TYPE_LIST:
  251.     case TYPE_SET:
  252.     */
  253.     if (isAggregateType (t)) {
  254.     bt = TYPEget_body(t)->base;
  255.  
  256.         if (isAggregateType(bt)) {
  257.         return("GenericAggregate");
  258.     }
  259.  
  260.     class = TYPEget_type(bt);
  261.         
  262.     /*      case TYPE_INTEGER:    */
  263.     if (class == Class_Integer_Type) return ( "IntAggregate" );
  264.     
  265.     /*      case TYPE_REAL:
  266.         case TYPE_NUMBER:    */
  267.     if ((class == Class_Number_Type) || ( class == Class_Real_Type))
  268.         return ( "RealAggregate" );
  269.  
  270.     /*      case TYPE_ENTITY:    */
  271.     if (class == Class_Entity_Type) return( "EntityAggregate" );
  272.     
  273.     /*      case TYPE_ENUM:        */
  274.         /*    case TYPE_SELECT:    */
  275.     if ((class == Class_Enumeration_Type) 
  276.         || (class == Class_Select_Type) )  {
  277.         strcpy (retval, ClassName (TYPEget_name (bt)));
  278.         strcat (retval, "s");
  279.         return (retval);
  280.     }
  281.  
  282.         /*    case TYPE_LOGICAL:    */
  283.     if ((class == Class_Boolean_Type) || ( class == Class_Logical_Type))
  284.         return ("  Logicals"); 
  285.  
  286.         /*    case TYPE_STRING:    */
  287.     if (class == Class_String_Type) return("StringAggregate");
  288.  
  289.         /*    case TYPE_BINARY:    */
  290.     if (class == Class_Binary_Type) return("BinaryAggregate");
  291.  
  292.  
  293.     }
  294.  
  295.     /*  the rest is for things that are not aggregates    */
  296.     
  297.     class = TYPEget_type(t);
  298.  
  299.     /*    case TYPE_LOGICAL:    */
  300.     if ((class == Class_Boolean_Type) || ( class == Class_Logical_Type))
  301.         return ("SdaiLogical"); 
  302.  
  303.     /*      case TYPE_INTEGER:    */
  304.     if ( class == Class_Integer_Type)
  305.     return ("SdaiInteger");
  306.  
  307.     /*      case TYPE_REAL:
  308.         case TYPE_NUMBER:    */
  309.     if ((class == Class_Number_Type) || ( class == Class_Real_Type))
  310.     return ("SdaiReal")      ; 
  311.  
  312.     /*        case TYPE_STRING:    */
  313.     if (class == Class_String_Type)
  314.     return ("SdaiString");
  315.  
  316.     /*        case TYPE_BINARY:    */
  317.     if ( class == Class_Binary_Type)
  318.     return ("SdaiBinary");
  319.  
  320.     /*      case TYPE_ENTITY:    */
  321.     if ( class == Class_Entity_Type)
  322.       {
  323.       strncpy (retval, TypeName (t), BUFSIZ-2);
  324.       strcat (retval, "H");
  325.       return retval;
  326. /*    return ("STEPentityH");    */
  327.     }
  328.     /*    case TYPE_ENUM:    */
  329.     /*    case TYPE_SELECT:    */
  330.     if ((class == Class_Enumeration_Type)  ||  (class == Class_Select_Type))  {
  331.       return (TypeName (t));
  332.     }
  333.  
  334.     /*  default returns undefined   */
  335.     return ("SCLundefined");
  336. }    
  337.  
  338. /******************************************************************
  339.  ** Procedure:  TypeName
  340.  ** Parameters:  Type t
  341.  ** Returns:  name of type as defined in SDAI C++ binding  4-Nov-1993
  342.  ** Status:   4-Nov-1993
  343.  ******************************************************************/
  344. const char *
  345. TypeName (Type t)  
  346. {
  347.   static char name [BUFSIZ];
  348.   strcpy (name, TYPE_PREFIX);
  349.   if (TYPEget_name (t)) 
  350.     strncat (name, FirstToUpper (TYPEget_name (t)), BUFSIZ -strlen(TYPE_PREFIX) -1);
  351.   else return TYPEget_ctype (t);
  352.   return name; 
  353. }
  354.  
  355. /******************************************************************
  356.  ** Procedure:  AccessType
  357.  ** Parameters:  const Type t --  type for attribute
  358.  ** Returns:  a string which is the type used by the access functions to
  359.               the data member in the c++ class
  360.  ** Description:  supplies the type for access functions in a c++ class
  361.  ** Side Effects:  
  362.  ** Status:    3-Nov-1993 -kcm
  363. ******************************************************************/
  364.  
  365. const char *
  366. AccessType (Type t)
  367. {
  368.   static char nm [BUFSIZ];
  369.   strncpy (nm, TypeName (t), BUFSIZ -2);
  370.   if ((TYPEis_entity(t)) || (TYPEis_select (t)) || (TYPEis_aggregate(t)))
  371.     strcat (nm, "H");
  372.   return nm;
  373. }
  374.  
  375. /******************************************************************
  376.  ** Procedure:  ClassName
  377.  ** Parameters:  const char * oldname
  378.  ** Returns:  temporary copy of name suitable for use as a class name
  379.  ** Side Effects:  erases the name created by a previous call to this function
  380.  ** Status:  complete
  381.  ******************************************************************/
  382.  
  383. const char *
  384. ClassName (const char * oldname)
  385. {
  386.     int i= 0, j = 0;
  387.     static char newname [BUFSIZ];
  388.     if (!oldname)  return ("");
  389.     
  390.     
  391.     strcpy (newname, ENTITYCLASS_PREFIX)    ;
  392.     j = strlen (ENTITYCLASS_PREFIX)    ;
  393.     newname [j] = ToUpper (oldname [i]);
  394.     ++i; ++j;
  395.     while ( oldname [i] != '\0') {
  396.     newname [j] = ToLower (oldname [i]);
  397. /*    if (oldname [i] == '_')  */
  398. /*  character is '_'    */
  399. /*        newname [++j] = ToUpper (oldname [++i]);*/
  400.     ++i;
  401.     ++j;
  402.     }
  403.     newname [j] = '\0';
  404.     return (newname);
  405.     
  406. /******  This procedure gets rid of '_' and is no longer being used
  407.     if (oldname [i] != '_') newname [j] = ToLower (oldname [i]);
  408.     else {    *//*  character is '_'    *//*
  409.         newname [j] = ToUpper (oldname [++i]);
  410.         if (oldname [i] == '\0') --i;
  411.     }
  412.     ++i;
  413.     ++j;
  414. *******/
  415. }
  416.  
  417. /******************************************************************
  418.  ** Procedure:  ENTITYget_classname    
  419.  ** Parameters:  Entity ent
  420.  ** Returns:  the name of the c++ class representing the entity 
  421.  ** Status:  complete
  422.  ******************************************************************/
  423.  
  424. const char *
  425. ENTITYget_classname (Entity ent)
  426. {
  427.     const char * oldname = ENTITYget_name (ent);
  428.     return (ClassName (oldname));
  429. }
  430.  
  431. /******************************************************************
  432.  ** Procedure:  PrettyTmpName (char * oldname)
  433.  ** Procedure:  PrettyNewName (char * oldname)
  434.  ** Parameters:  oldname
  435.  ** Returns:  a new capitalized name 
  436.  ** Description:  creates a new name with first character's in caps
  437.  ** Side Effects:  PrettyNewName allocates memory for the new name
  438.  ** Status:   OK  7-Oct-1992 kcm 
  439.  ******************************************************************/
  440. const char *
  441. PrettyTmpName (const char * oldname)
  442. {
  443.     int i= 0;
  444.     static char newname [BUFSIZ];
  445.     newname [0] = '\0';
  446.     while (( oldname [i] != '\0') && (i < BUFSIZ)) {
  447.         newname [i] = ToLower (oldname [i]);
  448.         if (oldname [i] == '_')  /*  character is '_'   */  
  449.       {
  450.           ++i;
  451.           newname [i] = ToUpper (oldname [i]);  
  452.       }
  453.         if (oldname [i] != '\0') ++i;
  454.     }
  455.  
  456.     newname [0] = ToUpper (oldname [0]);
  457.     newname [i] = '\0';
  458.     return newname;
  459. }
  460.  
  461. const char *
  462. EnumName (const char * oldname)  
  463. {
  464.     int i= 0, j = 0;
  465.     static char newname [MAX_LEN];
  466.     if (!oldname)  return ("");
  467.     
  468.     strcpy (newname, ENUM_PREFIX)    ;
  469.     j = strlen (ENUM_PREFIX)    ;
  470.     newname [j] = ToUpper (oldname [0]);
  471.     strncpy (newname+j+1, StrToLower (oldname +1), MAX_LEN -j);
  472.     j = strlen (newname);
  473.     newname [j] = '\0';
  474.     return (newname);
  475. }
  476.  
  477. const char *
  478. SelectName (const char * oldname)  
  479. {
  480.     int i= 0, j = 0;
  481.     static char newname [MAX_LEN];
  482.     if (!oldname)  return ("");
  483.     
  484.     strcpy (newname, ENUM_PREFIX);
  485.     newname [0] = ToUpper( newname [0] );
  486.     j = strlen (ENUM_PREFIX);
  487.     newname [j] = ToUpper (oldname [0]);
  488.     strncpy (newname+j+1, StrToLower (oldname +1), MAX_LEN -j);
  489.     j = strlen (newname);
  490.     newname [j] = '\0';
  491.     return (newname);
  492. }
  493.  
  494. const char *
  495. FirstToUpper (const char * word)
  496. {
  497.     static char newword [MAX_LEN];
  498.     int i =0;
  499.     
  500.     strncpy( newword, word, MAX_LEN );
  501.     newword[0] = ToUpper( newword[0] );
  502.     return (newword);
  503. }
  504.  
  505. const char *
  506. TypeDescriptorName (Type t, Schema s)  
  507. {
  508.   static char b [BUFSIZ];
  509.   sprintf (b, "%s%s%s", SCHEMAget_name(s), TYPEprefix (t), TYPEget_name(t));
  510.   return b;
  511. }
  512.  
  513. int
  514. ENTITYhas_explicit_attributes (Entity e)
  515. {
  516.   Linked_List l =ENTITYget_attributes (e);
  517.   int cnt =0;
  518.   LISTdo (l, a, Variable)
  519.     if (VARget_initializer (a) == EXPRESSION_NULL) 
  520.       ++cnt;
  521.   LISTod;
  522.   return cnt;
  523.  
  524. }
  525.  
  526. Entity
  527. ENTITYput_superclass (Entity entity)
  528. {
  529. #define ENTITYget_type(e)  ((e)->u.entity->type)
  530.  
  531.   Linked_List l =ENTITYget_supertypes (entity);
  532.   Entity super =0;
  533.   Entity ignore =0;
  534.   int super_cnt =0;
  535.   EntityTag tag;
  536.  
  537.   if (! LISTempty (l)) {
  538.     LISTdo (l, e, Entity)
  539.       /*  if there\'s no super class yet, 
  540.       or the super class doesn\'t have any attributes
  541.       */
  542.       if ( (! super) || (! ENTITYhas_explicit_attributes (super) )) {
  543.     ignore = super;
  544.     super = e;
  545.     ++ super_cnt;
  546.       }  else {
  547.     ignore = e;
  548.       }
  549.       if (ignore) {
  550.     printf ("WARNING:  multiple inheritance not implemented.\n");
  551.     printf ("\tin ENTITY %s\n\tSUPERTYPE %s IGNORED.\n\n", 
  552.         ENTITYget_name (entity), ENTITYget_name (e));
  553.       }
  554.     LISTod;
  555.  
  556.     tag = (EntityTag ) malloc (sizeof (struct EntityTag));
  557.     tag -> superclass = super;
  558.     TYPEput_clientData (ENTITYget_type (entity), tag);
  559.     return super;
  560.   } return 0;
  561. }
  562. Entity
  563. ENTITYget_superclass (Entity entity)
  564. {
  565.   EntityTag tag;
  566.   tag = TYPEget_clientData (ENTITYget_type (entity));
  567.   return (tag ? tag -> superclass : 0);
  568. }
  569.  
  570. /* Attributes are divided into four categories: 
  571. **
  572. **      . simple explicit
  573. **    . type shifters
  574. **      . simple derived
  575. **    . overriding
  576. **
  577. ** All of them are added to the dictionary.
  578. ** Only overriding generate a new STEPattribute.
  579. ** Overriding generate access functions and data members, for now. 
  580. ** Type shifters generate access functions and data members, for now. 
  581.  
  582. ** //  type shifting attributes
  583. ** //  ------------------------
  584. ** // before printing new STEPattribute
  585. ** // check to see if it\'s already printed in supertype
  586. ** // still add new access function
  587. **
  588. ** //  overriding attributes
  589. ** //  ---------------------
  590. ** // go through derived attributes
  591. ** // if STEPattribute found with same name
  592. ** // tell it to be * for reading and writing
  593. **/
  594.  
  595. Variable
  596. VARis_type_shifter (Variable a)
  597. {
  598.   char *temp;
  599.  
  600.   if (VARis_derived(a) || VARget_inverse (a))  return 0;
  601.  
  602.   temp = EXPRto_string( VARget_name(a) );
  603.   if (! strncmp (StrToLower (temp), "self\\", 5)) {
  604.     /*    a is a type shifter */
  605.     free (temp);
  606.     return a;
  607.   }  
  608.   free (temp);
  609.   return 0;
  610. }
  611.  
  612. Variable
  613. VARis_overrider (Entity e, Variable a)
  614. {  
  615.  
  616.   Variable other;
  617.   char * tmp;
  618.  
  619.   if (! VARis_derived (a) )  return 0;
  620.     tmp = VARget_simple_name(a);
  621.  
  622.   LISTdo (ENTITYget_supertypes (e), s, Entity) 
  623.   if ((other = ENTITYget_named_attribute (s, tmp))
  624.       && !VARis_derived (other))
  625.     return other;
  626.   LISTod;
  627.   return 0;
  628. }
  629.