📄 type-info.c
字号:
{ /* * List types (SET OF/ SEQ OF) * fill in component type */ FillCTypeRefInfo (r, m, head, t->basicType->a.setOf, C_LIST); } else if (r->typeConvTbl[basicTypeId].cTypeId == C_CHOICE) { /* * Choice - set up choice Id elmt names, choiceid enum name * choiceid enum fieldName, choice union name. * this will only be the first type in the typedef * ie will not be embedded (those are turned into type * refs in nomalize.c) */ /* * make union name (tag) from enclosing typedefs name plus "Choice" * put in the cTypeName part. (the typeDef name is already unique * but make sure union tag/name does not conflict with other types) */ len = strlen (head->cTypeDefInfo->cTypeName); unionName = (char*) Malloc (len + strlen (r->choiceUnionDefSuffix) + r->maxDigitsToAppend + 1); strcpy (unionName, head->cTypeDefInfo->cTypeName); strcat (unionName, r->choiceUnionDefSuffix); MakeCStrUnique (definedNamesG, unionName, r->maxDigitsToAppend, 1); DefineObj (&definedNamesG, unionName); ctri->cTypeName = unionName; ctri->choiceIdEnumName = Malloc (len + strlen (r->choiceIdEnumSuffix) + r->maxDigitsToAppend + 1); strcpy (ctri->choiceIdEnumName, head->cTypeDefInfo->cTypeName); strcat (ctri->choiceIdEnumName, r->choiceIdEnumSuffix); MakeCStrUnique (definedNamesG, ctri->choiceIdEnumName, r->maxDigitsToAppend, 1); DefineObj (&definedNamesG, ctri->choiceIdEnumName); ctri->choiceIdEnumFieldName = r->choiceIdFieldName; /* "choiceId" */ ctri->cFieldName = r->choiceUnionFieldName; /* "a" */ /* * must fill field names BEFORE filling choice elmts * (allows better naming for choice ids */ FillCFieldNames (r, t->basicType->a.choice); FillCChoiceElmts (r, m, head, t->basicType->a.choice); } else if (r->typeConvTbl[basicTypeId].cTypeId == C_STRUCT) { /* * SETs and SEQUENCEs */ /* * make struct name (tag) (the typeDef name is already unique) * the same as the enclosing typeDef */ unionName = Malloc (strlen (head->cTypeDefInfo->cTypeName) +1); strcpy (unionName, head->cTypeDefInfo->cTypeName); ctri->cTypeName = unionName; FillCStructElmts (r, m, head, t->basicType->a.set); FillCFieldNames (r, t->basicType->a.set); } /* * figure out whether this is a ptr based on the enclosing * type (if any) and optionality/default */ ctri->isPtr = IsCPtr (r, head, t, parentTypeId); /* let user overide any defaults with the --snacc attributes */ ParseTypeRefAttribs (ctri, t->attrList);} /* FillCTypeRefInfo */static voidFillCStructElmts PARAMS ((r, m, head, elmts), CRules *r _AND_ Module *m _AND_ TypeDef *head _AND_ NamedTypeList *elmts){ NamedType *et; FOR_EACH_LIST_ELMT (et, elmts) { FillCTypeRefInfo (r, m, head, et->type, C_STRUCT); }} /* FillCStructElmts *//* * Figures out non-conflicting enum names for the * choice id's */static voidFillCChoiceElmts PARAMS ((r, m, head, elmts), CRules *r _AND_ Module *m _AND_ TypeDef *head _AND_ NamedTypeList *elmts){ NamedType *et; int idCount = 0; CTRI *ctri; char *firstName; char *secondName; int len; /* * fill in type info for elmt types first */ FOR_EACH_LIST_ELMT (et, elmts) FillCTypeRefInfo (r, m, head, et->type, C_CHOICE); /* * set choiceId Symbol & value * eg * Car ::= CHOICE { typedef struct Car { * chev ChevCar, -> enum CarChoiceId { * ford FordCar CAR_CHEV, <- typename_fieldName * toyota ToyotaCar CAR_FORD, * } CAR_TOYOTA } choiceId; * union CarChoiceUnion { * ChevCar *chev; * FordCar *ford; * ToyotaCar *toyota; } a; * } */ FOR_EACH_LIST_ELMT (et, elmts) { ctri = et->type->cTypeRefInfo; if (ctri == NULL) continue; /* wierd type */ ctri->choiceIdValue = idCount++; firstName = Asn1TypeName2CTypeName (head->cTypeDefInfo->cTypeName); secondName = ctri->cFieldName; ctri->choiceIdSymbol = Malloc (strlen (firstName) + strlen (secondName) + 2 + r->maxDigitsToAppend); strcpy (ctri->choiceIdSymbol, firstName); strcat (ctri->choiceIdSymbol, "_"); strcat (ctri->choiceIdSymbol, secondName); Free (firstName); len = strlen (ctri->choiceIdSymbol); if (r->capitalizeNamedElmts) Str2UCase (ctri->choiceIdSymbol, len); MakeCStrUnique (definedNamesG, ctri->choiceIdSymbol, r->maxDigitsToAppend, 0); DefineObj (&definedNamesG, ctri->choiceIdSymbol); }} /* FillCChoiceElmts *//* * takes a list of "sibling" (eg same level in a structure) * ElmtTypes and fills sets up the c field names in * the CTypeRefInfo struct */static voidFillCFieldNames PARAMS ((r, elmts), CRules *r _AND_ NamedTypeList *elmts){ NamedType *et; CTRI *ctri; DefinedObj *fieldNames; int len, num, digit, i, tmpLen; char *tmpName; char *asn1FieldName; char *cFieldName; fieldNames = NewObjList(); /* * Initialize fieldname data * allocate (if nec) and fill in CTRI fieldname if poss * from asn1 field name. leave blank otherwise */ FOR_EACH_LIST_ELMT (et, elmts) { ctri = et->type->cTypeRefInfo; if (ctri == NULL) { ctri = MT (CTRI); et->type->cTypeRefInfo = ctri; } if (et->fieldName != NULL) { asn1FieldName = et->fieldName; ctri->cFieldName = Asn1FieldName2CFieldName (asn1FieldName); DefineObj (&fieldNames, ctri->cFieldName); } } FOR_EACH_LIST_ELMT (et, elmts) { ctri = et->type->cTypeRefInfo; /* * generate field names for those without them */ if (ctri->cFieldName == NULL) { if ((et->type->basicType->choiceId == BASICTYPE_LOCALTYPEREF) || (et->type->basicType->choiceId == BASICTYPE_IMPORTTYPEREF)) { /* * take ref'd type name as field name * convert first let to lower case */ tmpName = et->type->basicType->a.localTypeRef->link->cTypeDefInfo->cTypeName; tmpName = Asn1TypeName2CTypeName (tmpName); cFieldName = Malloc (strlen (tmpName) + r->maxDigitsToAppend +1); strcpy (cFieldName, tmpName); Free (tmpName); if (isupper (cFieldName[0])) cFieldName[0] = tolower (cFieldName[0]); } else { /* * get default field name for this type */ tmpName = r->typeConvTbl[et->type->basicType->choiceId].defaultFieldName; cFieldName = Malloc (strlen (tmpName) + r->maxDigitsToAppend +1); strcpy (cFieldName, tmpName); if (isupper (cFieldName[0])) cFieldName[0] = tolower (cFieldName[0]); } MakeCStrUnique (fieldNames, cFieldName, r->maxDigitsToAppend, 1); DefineObj (&fieldNames, cFieldName); ctri->cFieldName = cFieldName; } } FreeDefinedObjs (&fieldNames);} /* FillCFieldNames *//* * returns true if this c type for this type should be * be ref'd as a ptr */static intIsCPtr PARAMS ((r, td, t, parentCTypeId), CRules *r _AND_ TypeDef *td _AND_ Type *t _AND_ CTypeId parentCTypeId){ CTDI *ctdi; int retVal = FALSE; /* * inherit ptr attriubutes from ref'd type if any * otherwise grab lib c type def from the CRules */ if ((t->basicType->choiceId == BASICTYPE_LOCALTYPEREF) || (t->basicType->choiceId == BASICTYPE_IMPORTTYPEREF)) { ctdi = t->basicType->a.localTypeRef->link->cTypeDefInfo; } else ctdi = &r->typeConvTbl[GetBuiltinType (t)]; if ((parentCTypeId == C_TYPEDEF) && (ctdi->isPtrForTypeDef)) retVal = TRUE; else if ((parentCTypeId == C_STRUCT) && (ctdi->isPtrForTypeRef)) retVal = TRUE; else if ((parentCTypeId == C_CHOICE) && (ctdi->isPtrInChoice)) retVal = TRUE; else if (((t->optional) || (t->defaultVal != NULL)) && (ctdi->isPtrForOpt)) retVal = TRUE; return retVal;} /* IsCPtr */#define BAD_VALUE(attrValue, attrType)\ fprintf (stderr,"Warning: ignoring attribute with improper value (%s/%s)\n",attrType, attrValue)/* * attrList is a list of strings that hold attribute value * pairs. A list is used in case the attr/value pairs are * given in multiple ASN.1 comments around the type. */void ParseTypeDefAttribs PARAMS ((ctdi, attrList), CTDI *ctdi _AND_ AttributeList *attrList){ char *attrName; char *attrValue; int loc; MyString attr; int result; if (attrList == NULL) return; FOR_EACH_LIST_ELMT (attr, attrList) { loc = 0; /* loc is location to start/continue parse from */ while (1) { ParseAttr (attr, &loc, &attrName, &attrValue); if (attrName == NULL) break; if (strcmp (attrName, "asn1TypeId") == 0) { if (ParseTypeId (attrValue, &result) < 0) BAD_VALUE (attrValue, attrName); else ctdi->asn1TypeId = result; Free (attrValue); } else if (strcmp (attrName, "cTypeId") == 0) { if (ParseCTypeId (attrValue, &result) < 0) BAD_VALUE (attrValue, attrName); else ctdi->cTypeId = result; Free (attrValue); } else if (strcmp (attrName, "cTypeName") == 0) ctdi->cTypeName = attrValue; else if (strcmp (attrName, "isPdu") == 0) { if (ParseBool (attrValue, &result) < 0) BAD_VALUE (attrValue, attrName); else ctdi->isPdu = result; Free (attrValue); } else if (strcmp (attrName, "isPtrForTypeDef") == 0) { if (ParseBool (attrValue, &result) < 0) BAD_VALUE (attrValue, attrName); else ctdi->isPtrForTypeDef = result; Free (attrValue); } else if (strcmp (attrName, "isPtrForTypeRef") == 0) { if (ParseBool (attrValue, &result) < 0) BAD_VALUE (attrValue, attrName); else ctdi->isPtrForTypeRef = result; Free (attrValue); } else if (strcmp (attrName, "isPtrInChoice") == 0) { if (ParseBool (attrValue, &result) < 0) BAD_VALUE (attrValue, attrName); else ctdi->isPtrInChoice = result; Free (attrValue); } else if (strcmp (attrName, "isPtrForOpt") == 0) { if (ParseBool (attrValue, &result) < 0) BAD_VALUE (attrValue, attrName); else ctdi->isPtrForOpt = result; Free (attrValue); } else if (strcmp (attrName, "optTestRoutineName") == 0) ctdi->optTestRoutineName = attrValue; else if (strcmp (attrName, "defaultFieldName") == 0) ctdi->defaultFieldName = attrValue; else if (strcmp (attrName, "printRoutineName") == 0) ctdi->printRoutineName = attrValue; else if (strcmp (attrName, "encodeRoutineName") == 0) ctdi->encodeRoutineName = attrValue; else if (strcmp (attrName, "decodeRoutineName") == 0) ctdi->decodeRoutineName = attrValue; else if (strcmp (attrName, "freeRoutineName") == 0) ctdi->freeRoutineName = attrValue; else if (strcmp (attrName, "isEncDec") == 0) { if (ParseBool (attrValue, &result) < 0) BAD_VALUE (attrValue, attrName); else { ctdi->isEncDec = result; } Free (attrValue); } else if (strcmp (attrName, "genTypeDef") == 0) { if (ParseBool (attrValue, &result) < 0) BAD_VALUE (attrValue, attrName); else { ctdi->genTypeDef = result; } Free (attrValue); } else if (strcmp (attrName, "genPrintRoutine") == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -