Annotation of Amaya/amaya/fetchXMLname.c, revision 1.60

1.1       cvs         1: /*
                      2:  *
1.55      vatton      3:  *  (c) COPYRIGHT MIT and INRIA, 1996-2002
1.1       cvs         4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
                      7:  
                      8: /*
                      9:  *
                     10:  * fetchXMLname
                     11:  *
1.46      cvs        12:  * Authors: I. Vatton
                     13:  *          L. Carcone
1.1       cvs        14:  *
                     15:  */
                     16: 
                     17: #define THOT_EXPORT extern
                     18: #include "amaya.h"
                     19: #include "parser.h"
1.24      cvs        20: #include "HTMLnames.h"
                     21: #include "MathMLnames.h"
1.49      vatton     22: #include "SVGnames.h"
1.24      cvs        23: #include "XLinknames.h"
                     24: 
1.35      cvs        25: /* define some pointers to let other parser functions access the local table */
1.24      cvs        26: int               HTML_ENTRIES = (sizeof(XHTMLElemMappingTable) / sizeof(ElemMapping));
                     27: ElemMapping      *pHTMLGIMapping = XHTMLElemMappingTable;
                     28: AttributeMapping *pHTMLAttributeMapping = XHTMLAttributeMappingTable;
1.14      cvs        29: 
1.35      cvs        30: XmlEntity        *pXhtmlEntityTable = XhtmlEntityTable;
                     31: XmlEntity        *pMathEntityTable = MathEntityTable;
1.1       cvs        32: 
                     33: #include "fetchXMLname_f.h"
                     34: 
1.39      cvs        35: /* Global variables used by the entity mapping */
                     36: static int        XHTMLSup = 0;
                     37: static int        MathSup = 0;
                     38: 
1.1       cvs        39: /*----------------------------------------------------------------------
1.14      cvs        40:    GetXHTMLSSchema returns the XHTML Thot schema for document doc.
                     41:   ----------------------------------------------------------------------*/
1.55      vatton     42: SSchema GetXHTMLSSchema (Document doc)
1.14      cvs        43: {
                     44:   SSchema      XHTMLSSchema;
                     45: 
1.43      cvs        46:    XHTMLSSchema = TtaGetSSchema ("HTML", doc);
1.14      cvs        47:    if (XHTMLSSchema == NULL)
1.17      cvs        48:        XHTMLSSchema = TtaNewNature(doc, TtaGetDocumentSSchema(doc),
1.46      cvs        49:                                   "HTML", "HTMLP");
1.14      cvs        50:    return (XHTMLSSchema);
                     51: }
                     52: 
1.54      cvs        53: /*----------------------------------------------------------------------
1.1       cvs        54:    GetMathMLSSchema returns the MathML Thot schema for document doc.
                     55:   ----------------------------------------------------------------------*/
1.55      vatton     56: SSchema GetMathMLSSchema (Document doc)
1.1       cvs        57: {
                     58:   SSchema      MathMLSSchema;
                     59: 
1.43      cvs        60:   MathMLSSchema = TtaGetSSchema ("MathML", doc);
1.13      cvs        61:   if (MathMLSSchema == NULL)
1.17      cvs        62:      MathMLSSchema = TtaNewNature(doc, 
1.46      cvs        63:                                  TtaGetDocumentSSchema(doc),
                     64:                                  "MathML", "MathMLP");
1.13      cvs        65:   return (MathMLSSchema);
1.1       cvs        66: }
                     67: 
                     68: /*----------------------------------------------------------------------
1.49      vatton     69:    GetSVGSSchema returns the SVG Thot schema for document doc.
1.1       cvs        70:   ----------------------------------------------------------------------*/
1.55      vatton     71: SSchema GetSVGSSchema (Document doc)
1.1       cvs        72: 
                     73: {
1.49      vatton     74:   SSchema      SVGSSchema;
1.1       cvs        75: 
1.49      vatton     76:   SVGSSchema = TtaGetSSchema ("SVG", doc);
                     77:   if (SVGSSchema == NULL)
                     78:     SVGSSchema = TtaNewNature(doc,
1.46      cvs        79:                                  TtaGetDocumentSSchema(doc),
1.49      vatton     80:                                  "SVG", "SVGP");
                     81:   return (SVGSSchema);
1.1       cvs        82: }
                     83: 
                     84: /*----------------------------------------------------------------------
1.13      cvs        85:    GetXLinkSSchema returns the XLink Thot schema for document doc.
                     86:   ----------------------------------------------------------------------*/
1.55      vatton     87: SSchema GetXLinkSSchema (Document doc)
1.13      cvs        88: 
                     89: {
                     90:   SSchema      XLinkSSchema;
                     91: 
1.43      cvs        92:   XLinkSSchema = TtaGetSSchema ("XLink", doc);
1.13      cvs        93:   if (XLinkSSchema == NULL)
1.46      cvs        94:     XLinkSSchema = TtaNewNature(doc, TtaGetDocumentSSchema(doc),
                     95:                                "XLink", "XLinkP");
1.13      cvs        96:   return (XLinkSSchema);
                     97: }
                     98: 
                     99: /*----------------------------------------------------------------------
1.46      cvs       100:    GetGenericXMLSSchema returns the XML Thot schema for the document doc.
                    101:   ----------------------------------------------------------------------*/
1.55      vatton    102: SSchema GetGenericXMLSSchema (Document doc)
1.46      cvs       103: 
                    104: {
                    105:   SSchema      XMLSSchema;
                    106: 
                    107:   XMLSSchema = TtaGetSSchema ("XML", doc);
                    108:   if (XMLSSchema == NULL)
                    109:       XMLSSchema = TtaNewNature(doc, TtaGetDocumentSSchema(doc),
                    110:                                "XML", "XMLP");
                    111:   return (XMLSSchema);
                    112: }
                    113: 
                    114: /*----------------------------------------------------------------------
1.57      kahan     115:    GetTextSSchema returns the TextFile Thot schema for document doc.
                    116:    (this is not XML, but its useful to have this function here).
                    117:   ----------------------------------------------------------------------*/
                    118: SSchema         GetTextSSchema (Document doc)
                    119: 
                    120: {
                    121:   SSchema      XLinkSSchema;
                    122: 
                    123:   XLinkSSchema = TtaGetSSchema ("TextFile", doc);
                    124:   if (XLinkSSchema == NULL)
                    125:     XLinkSSchema = TtaNewNature(doc, TtaGetDocumentSSchema(doc),
                    126:                                "TextFile", "TextFileP");
                    127:   return (XLinkSSchema);
                    128: }
                    129: 
                    130: 
                    131: /*----------------------------------------------------------------------
1.46      cvs       132:    GetGenericXMLSSchemaByUri returns the XML Thot schema for the document doc.
                    133:   ----------------------------------------------------------------------*/
1.55      vatton    134: SSchema GetGenericXMLSSchemaByUri (char *uriName, Document doc, ThotBool *isnew)
1.46      cvs       135: 
                    136: {
                    137:   SSchema      XMLSSchema;
                    138: 
                    139:   XMLSSchema = TtaGetSSchemaByUri (uriName, doc);
                    140:   if (XMLSSchema == NULL)
                    141:     {
                    142:       XMLSSchema = TtaNewNature(doc, TtaGetDocumentSSchema(doc),
                    143:                                "XML", "XMLP");
                    144:       *isnew = TRUE;
                    145:     }
                    146:   return (XMLSSchema);
                    147: }
                    148: 
                    149: /*----------------------------------------------------------------------
1.13      cvs       150:    GetXMLSSchema returns the XML Thot schema for document doc.
1.1       cvs       151:   ----------------------------------------------------------------------*/
1.45      cvs       152: SSchema GetXMLSSchema (int XMLtype, Document doc)
1.38      cvs       153: 
1.1       cvs       154: {
1.14      cvs       155:   if (XMLtype == XHTML_TYPE)
                    156:     return GetXHTMLSSchema (doc);
                    157:   else if (XMLtype == MATH_TYPE)
1.13      cvs       158:     return GetMathMLSSchema (doc);
1.51      cvs       159:   else if (XMLtype == SVG_TYPE)
1.49      vatton    160:     return GetSVGSSchema (doc);
1.13      cvs       161:   else if (XMLtype == XLINK_TYPE)
1.15      cvs       162:     return GetXLinkSSchema (doc);
1.13      cvs       163:   else
                    164:     return NULL;
1.1       cvs       165: }
                    166: 
                    167: /*----------------------------------------------------------------------
1.22      cvs       168:   MapXMLElementType
                    169:   Generic function which searchs in the Element Mapping table, selected
                    170:   by the parameter XMLtype, the entry XMLname and returns the corresponding
                    171:   Thot element type.
1.55      vatton    172:   Returns:
1.22      cvs       173:     - ElTypeNum and ElSSchema into elType  ElTypeNum = 0 if not found.
1.55      vatton    174:     - content information about this entry
                    175:     - checkProfile TRUE if the entry is valid for the current Doc profile.
1.1       cvs       176:   ----------------------------------------------------------------------*/
1.55      vatton    177: void MapXMLElementType (int XMLtype, char *XMLname, ElementType *elType,
                    178:                        char **mappedName, char *content,
                    179:                        ThotBool *checkProfile, Document doc)
1.1       cvs       180: {
1.56      vatton    181:   ElemMapping        *ptr;
                    182:   char                c;
                    183:   int                 i;
1.1       cvs       184: 
1.56      vatton    185:   /* Initialize variables */
                    186:   *mappedName = NULL;
                    187:   *checkProfile = TRUE;
                    188:   elType->ElTypeNum = 0;
                    189: 
                    190:   /* Select the right table */
                    191:   if (XMLtype == XHTML_TYPE)
                    192:     ptr = XHTMLElemMappingTable;
                    193:   else if (XMLtype == MATH_TYPE)
                    194:     {
                    195:       if (TtaGetDocumentProfile(doc) == L_Basic &&
                    196:          DocumentTypes[doc] == docHTML)
                    197:        {
                    198:          /* Maths are not allowed in this document */
                    199:          ptr = NULL;
                    200:          *checkProfile = FALSE;
                    201:        }
                    202:       else
                    203:        ptr = MathMLElemMappingTable;
                    204:     }
                    205:   else if (XMLtype == SVG_TYPE)
                    206:     {
                    207:       if (TtaGetDocumentProfile(doc) == L_Basic &&
                    208:          DocumentTypes[doc] == docHTML)
                    209:        {
                    210:          /* Graphics are not allowed in this document */
                    211:          ptr = NULL;
                    212:          *checkProfile = FALSE;
                    213:        }
                    214:       else
                    215:        ptr = SVGElemMappingTable;
                    216:     }
                    217:   else
                    218:     ptr = NULL;
1.32      cvs       219:    
1.56      vatton    220:   if (ptr)
                    221:     {
                    222:       /* search in the ElemMappingTable */
                    223:       i = 0;
                    224:       /* case insensitive for HTML */
                    225:       if (ptr == XHTMLElemMappingTable)
                    226:        c = tolower (XMLname[0]);
                    227:       else
                    228:        c = XMLname[0];
                    229:       /* look for the first concerned entry in the table */
                    230:       while (ptr[i].XMLname[0] < c && ptr[i].XMLname[0] != EOS)
                    231:        i++;     
                    232:       /* look at all entries starting with the right character */
                    233:       do
                    234:        if (ptr == XHTMLElemMappingTable && strcasecmp (ptr[i].XMLname, XMLname))
                    235:          /* it's not the tag */
                    236:          i++;
                    237:        else if (ptr != XHTMLElemMappingTable && strcmp (ptr[i].XMLname, XMLname))
                    238:          /* it's not the tag */
                    239:          i++;
                    240:        else if (TtaGetDocumentProfile(doc) != L_Other &&
                    241:                 !(ptr[i].Level & TtaGetDocumentProfile(doc)))
                    242:          {
                    243:            /* this tag is not valid in the document profile */
                    244:            *checkProfile = FALSE;
                    245:            i++;
                    246:          }
                    247:        else
                    248:          {
                    249:            elType->ElTypeNum = ptr[i].ThotType;
                    250:            if (elType->ElSSchema == NULL)
                    251:              elType->ElSSchema = GetXMLSSchema (XMLtype, doc);
                    252:            *mappedName = ptr[i].XMLname;
                    253:            *content = ptr[i].XMLcontents;
                    254:          }
                    255:       while (elType->ElTypeNum <= 0 && ptr[i].XMLname[0] == c);
                    256:     }
1.1       cvs       257: }
                    258: 
                    259: 
                    260: /*----------------------------------------------------------------------
                    261:    GetXMLElementName
1.52      cvs       262:    Generic function which searchs in the mapping tables the XML name for
1.22      cvs       263:    a given Thot type.
1.1       cvs       264:   ----------------------------------------------------------------------*/
1.55      vatton    265: char *GetXMLElementName (ElementType elType, Document doc)
1.1       cvs       266: {
1.46      cvs       267:   ElemMapping  *ptr;
                    268:   char         *name;
                    269:   int           i;
                    270:   ThotBool      invalid = FALSE;
1.27      cvs       271: 
                    272:   if (elType.ElTypeNum > 0)
                    273:     {
                    274:       i = 0;
                    275:       /* Select the table which matches with the element schema */
                    276:       name = TtaGetSSchemaName (elType.ElSSchema);
1.44      cvs       277:       if (strcmp ("MathML", name) == 0)
1.27      cvs       278:        ptr = MathMLElemMappingTable;
1.49      vatton    279:       else if (strcmp ("SVG", name) == 0)
                    280:        ptr = SVGElemMappingTable;
1.27      cvs       281:       else
                    282:        ptr = XHTMLElemMappingTable;
                    283:       
                    284:       if (ptr)
                    285:        do
                    286:          {
                    287:            if (ptr[i].ThotType == elType.ElTypeNum)
                    288:              {
1.37      cvs       289:                if (doc == 0 || 
1.53      cvs       290:                    TtaGetDocumentProfile(doc) == L_Other ||
                    291:                    (ptr[i].Level & TtaGetDocumentProfile(doc)))
1.27      cvs       292:                  return ptr[i].XMLname;
                    293:                else
                    294:                  invalid = TRUE;
                    295:              }
                    296:            i++;
                    297:          }
1.44      cvs       298:        while (ptr[i].XMLname[0] != EOS);         
1.27      cvs       299:     }
                    300:   if (invalid)
1.43      cvs       301:     return "";
1.27      cvs       302:   else
1.43      cvs       303:     return "???";
1.22      cvs       304: }
1.25      cvs       305: 
                    306: 
1.29      cvs       307: 
                    308: /*----------------------------------------------------------------------
1.30      cvs       309:    IsXMLElementInline
1.52      cvs       310:    Generic function which searchs in the mapping tables if a given
1.29      cvs       311:    Thot type is an inline character or not
                    312:   ----------------------------------------------------------------------*/
1.55      vatton    313: ThotBool IsXMLElementInline (ElementType elType, Document doc)
1.29      cvs       314: {
1.34      cvs       315:   int            i;
                    316:   ThotBool       ret = FALSE;
1.45      cvs       317:   char          *name;
1.34      cvs       318:   ElemMapping   *ptr;
1.29      cvs       319: 
                    320:   if (elType.ElTypeNum > 0)
                    321:     {
                    322:       i = 0;
                    323:       /* Select the table which matches with the element schema */
                    324:       name = TtaGetSSchemaName (elType.ElSSchema);
1.44      cvs       325:       if (strcmp ("MathML", name) == 0)
1.29      cvs       326:        ptr = MathMLElemMappingTable;
1.49      vatton    327:       else if (strcmp ("SVG", name) == 0)
                    328:        ptr = SVGElemMappingTable;
1.52      cvs       329:       else if (strcmp ("HTML", name) == 0)
                    330:        ptr = XHTMLElemMappingTable;
1.29      cvs       331:       else
1.52      cvs       332:        ptr = NULL;
1.29      cvs       333:       
                    334:       if (ptr)
                    335:        {
1.44      cvs       336:          while (ptr[i].XMLname[0] != EOS &&
1.29      cvs       337:                 ptr[i].ThotType != elType.ElTypeNum)
                    338:            i++;
                    339:          if (ptr[i].ThotType == elType.ElTypeNum)
1.58      quint     340:            ret = ptr[i].InlineElem;
1.29      cvs       341:        }
                    342:     }
                    343:   return ret;
                    344: }
                    345: 
1.22      cvs       346: /*----------------------------------------------------------------------
1.59      vatton    347:    MapMathMLAttributeValue
                    348:    Search in the Attribute Value Mapping Table the entry for the attribute
                    349:    ThotAtt and its value attVal. Returns the corresponding Thot value.
                    350:   ----------------------------------------------------------------------*/
                    351: void MapXMLAttributeValue (int XMLtype, char *attVal, AttributeType attrType,
                    352:                              int *value)
                    353: {
                    354:   AttrValueMapping   *ptr;
                    355:   int                 i;
                    356: 
                    357:   /* Select the right table */
                    358:   if (XMLtype == XHTML_TYPE)
1.60    ! vatton    359:     ptr = XhtmlAttrValueMappingTable;
1.59      vatton    360:   else if (XMLtype == MATH_TYPE)
                    361:     ptr = MathMLAttrValueMappingTable;
                    362:   else if (XMLtype == SVG_TYPE)
1.60    ! vatton    363:     ptr = SVGAttrValueMappingTable;
1.59      vatton    364:   else
                    365:     ptr = NULL;
                    366:   
                    367:   *value = 0;
                    368:   i = 0;
                    369:   if (ptr == NULL)
                    370:     return;
                    371:   while (ptr[i].ThotAttr != attrType.AttrTypeNum && ptr[i].ThotAttr != 0)
                    372:     i++;
                    373:   if (ptr[i].ThotAttr == attrType.AttrTypeNum)
                    374:     do
                    375:       if (!strcmp (ptr[i].XMLattrValue, attVal))
                    376:        *value = ptr[i].ThotAttrValue;
                    377:       else
                    378:        i++;
                    379:     while (*value == 0 && ptr[i].ThotAttr == attrType.AttrTypeNum);
                    380: }
                    381: 
                    382: 
                    383: /*----------------------------------------------------------------------
1.24      cvs       384:    MapXMLAttribute
1.22      cvs       385:    Generic function which searchs in the Attribute Mapping Table (table)
                    386:    the entry attrName associated to the element elementName.
1.24      cvs       387:    Returns the corresponding entry or -1.
1.22      cvs       388:   ----------------------------------------------------------------------*/
1.55      vatton    389: int MapXMLAttribute (int XMLtype, char *attrName, char *elementName,
                    390:                     ThotBool *checkProfile, Document doc, int *thotType)
1.22      cvs       391: {
1.56      vatton    392:   AttributeMapping   *ptr;
                    393:   char                c;
                    394:   int                 i;
1.24      cvs       395: 
1.33      cvs       396:   /* Initialization */
1.55      vatton    397:   *checkProfile = TRUE;
1.33      cvs       398:   i = 1;
                    399:   *thotType = 0;
1.50      cvs       400:   
                    401:   /* Select the right table */
                    402:   if (XMLtype == XHTML_TYPE)
                    403:     ptr = XHTMLAttributeMappingTable;
                    404:   else if (XMLtype == MATH_TYPE)
                    405:     ptr = MathMLAttributeMappingTable;
1.51      cvs       406:   else if (XMLtype == SVG_TYPE)
1.50      cvs       407:     ptr = SVGAttributeMappingTable;
                    408:   else if (XMLtype == XLINK_TYPE)
                    409:     ptr = XLinkAttributeMappingTable;
                    410:   else
                    411:     ptr = NULL;
                    412:   
1.24      cvs       413:   if (ptr == NULL)
                    414:     return -1;
1.50      cvs       415:   
                    416:   if (strcmp (attrName, "unknown_attr") == 0)
                    417:     {
                    418:       *thotType = ptr[0].ThotAttribute;
                    419:       return 0;
                    420:     }
1.22      cvs       421: 
1.56      vatton    422:   /* case insensitive for HTML */
                    423:   if (ptr == XHTMLAttributeMappingTable)
                    424:     c = tolower (attrName[0]);
                    425:   else
                    426:     c = attrName[0];
1.22      cvs       427:   /* look for the first concerned entry in the table */
1.56      vatton    428:   while (ptr[i].XMLattribute[0] < c &&  ptr[i].XMLattribute[0] != EOS)
1.22      cvs       429:     i++;
1.56      vatton    430:   while (ptr[i].XMLattribute[0] == c)
1.22      cvs       431:     {
1.56      vatton    432:       if (ptr == XHTMLAttributeMappingTable &&
                    433:          (strcasecmp (ptr[i].XMLattribute, attrName) ||
                    434:           (ptr[i].XMLelement[0] != EOS &&
                    435:            strcasecmp (ptr[i].XMLelement, elementName))))
                    436:        i++;
                    437:       else if (ptr != XHTMLAttributeMappingTable &&
                    438:          (strcmp (ptr[i].XMLattribute, attrName) ||
                    439:           (ptr[i].XMLelement[0] != EOS &&
                    440:            strcmp (ptr[i].XMLelement, elementName))))
1.22      cvs       441:        i++;
1.53      cvs       442:       else if (TtaGetDocumentProfile(doc) != L_Other &&
                    443:               !(ptr[i].Level & TtaGetDocumentProfile(doc)))
1.33      cvs       444:        {
1.55      vatton    445:          *checkProfile = FALSE;
1.33      cvs       446:          i++;
                    447:        }
1.22      cvs       448:       else
1.24      cvs       449:        {
                    450:          *thotType = ptr[i].ThotAttribute;
                    451:          return (i);
                    452:        }
1.22      cvs       453:     }
1.24      cvs       454:   return (-1);
1.25      cvs       455: }
                    456: 
                    457: 
                    458: /*----------------------------------------------------------------------
1.37      cvs       459:    GetXMLAttributeName
1.52      cvs       460:    Generic function which searchs in the mapping tables the XML name for
1.25      cvs       461:    a given Thot type.
                    462:   ----------------------------------------------------------------------*/
1.55      vatton    463: char *GetXMLAttributeName (AttributeType attrType, ElementType elType,
                    464:                           Document doc)
1.25      cvs       465: {
                    466:   AttributeMapping   *ptr;
1.45      cvs       467:   char               *name, *tag;
1.25      cvs       468:   int                 i;
1.27      cvs       469:   ThotBool            invalid = FALSE;
1.25      cvs       470: 
1.27      cvs       471:   if (attrType.AttrTypeNum > 0)
                    472:     {
1.28      cvs       473:       /* get the specific element tag */
                    474:       if (elType.ElTypeNum > 0)
                    475:        tag = GetXMLElementName (elType, doc);
                    476:       else
1.43      cvs       477:        tag = "";
1.28      cvs       478: 
1.27      cvs       479:       i = 0;
                    480:       /* Select the table which matches with the element schema */
                    481:       name = TtaGetSSchemaName (attrType.AttrSSchema);
1.44      cvs       482:       if (strcmp ("MathML", name) == 0)
1.27      cvs       483:        ptr = MathMLAttributeMappingTable;
1.49      vatton    484:       else if (strcmp ("SVG", name) == 0)
                    485:        ptr = SVGAttributeMappingTable;
1.44      cvs       486:       else if (strcmp ("XLink", name) == 0)
1.27      cvs       487:        ptr = XLinkAttributeMappingTable;
                    488:       else
                    489:        ptr = XHTMLAttributeMappingTable;
                    490:       
                    491:       if (ptr)
                    492:        do
                    493:          {
1.28      cvs       494:            if (ptr[i].ThotAttribute == attrType.AttrTypeNum &&
1.44      cvs       495:                (ptr[i].XMLelement[0] == EOS ||
                    496:                 !strcmp (ptr[i].XMLelement, tag)))
1.25      cvs       497:              {
1.48      cvs       498:                if (doc != 0 &&
1.53      cvs       499:                    TtaGetDocumentProfile(doc) != L_Other &&
                    500:                    !(ptr[i].Level & TtaGetDocumentProfile(doc)))
1.48      cvs       501:                  invalid = TRUE;
                    502:                else
1.25      cvs       503:                  return ptr[i].XMLattribute;
                    504:              }
                    505:            i++;
1.27      cvs       506:          }
1.44      cvs       507:        while (ptr[i].XMLattribute[0] != EOS);    
1.27      cvs       508:     }
                    509:   if (invalid)
1.43      cvs       510:     return "";
1.27      cvs       511:   else
1.43      cvs       512:     return "???";
1.36      cvs       513: }
                    514: 
                    515: /*----------------------------------------------------------------------
                    516:    MapXMLEntity
                    517:    Generic function which searchs in the Entity Mapping Table (table)
                    518:    the entry entityName and give the corresponding decimal value.
                    519:    Returns FALSE if entityName is not found.
                    520:   ----------------------------------------------------------------------*/
1.46      cvs       521: ThotBool      MapXMLEntity (int XMLtype, char *entityName, int *entityValue)
1.36      cvs       522: {
                    523:   XmlEntity  *ptr;
                    524:   ThotBool    found;
1.38      cvs       525:   int         inf, sup, med, rescomp;
1.36      cvs       526: 
1.38      cvs       527:   /* Initialization */
1.36      cvs       528:   found = FALSE;
1.42      cvs       529:   sup = 0;
1.36      cvs       530: 
                    531:   /* Select the right table */
                    532:   if (XMLtype == XHTML_TYPE)
1.39      cvs       533:     {
                    534:       ptr = XhtmlEntityTable;
                    535:       if (XHTMLSup == 0)
                    536:        for (XHTMLSup = 0; ptr[XHTMLSup].charCode > 0; XHTMLSup++);
                    537:       sup = XHTMLSup;
                    538:     }
1.36      cvs       539:   else if (XMLtype == MATH_TYPE)
1.39      cvs       540:     {
                    541:       ptr = MathEntityTable;
                    542:       if (MathSup == 0)
                    543:        for (MathSup = 0; ptr[MathSup].charCode > 0; MathSup++);
                    544:       sup = MathSup;
                    545:     }
1.36      cvs       546:   else
                    547:     ptr = NULL;
                    548:   
                    549:   if (ptr == NULL)
1.38      cvs       550:     return found;
                    551: 
                    552:   inf = 0;
                    553:   while (sup >= inf && !found)
                    554:     /* Dichotomic research */
1.36      cvs       555:     {
1.38      cvs       556:       med = (sup + inf) / 2;
1.44      cvs       557:       rescomp = strcmp (ptr[med].charName, entityName);
1.38      cvs       558:       if (rescomp == 0)
                    559:        {
                    560:          /* entity found */
                    561:          *entityValue = ptr[med].charCode;
                    562:          found = TRUE;
                    563:        }
                    564:       else
                    565:        {
                    566:          if (rescomp > 0)
                    567:            sup = med - 1;
                    568:          else
                    569:            inf = med + 1;
                    570:        }
1.36      cvs       571:     }
1.38      cvs       572:   return found;
                    573:  }
1.39      cvs       574: 
                    575: /*----------------------------------------------------------------------
                    576:    MapEntityByCode
                    577:    Generic function which searchs in the Entity Mapping Table (table)
                    578:    the entry with code entityValue and give the corresponding name.
                    579:    Returns FALSE if entityValue is not found.
                    580:   ----------------------------------------------------------------------*/
1.40      cvs       581: void MapEntityByCode (int entityValue, char **entityName)
1.39      cvs       582: {
                    583:   XmlEntity  *ptr;
                    584:   ThotBool    found;
                    585:   int         i;
                    586: 
                    587:   /* Select the right table */
1.40      cvs       588:   ptr = XhtmlEntityTable;
                    589:   if (ptr)
                    590:     {
1.41      cvs       591:       /* look for in the HTML entities table */
1.40      cvs       592:       found = FALSE;
1.41      cvs       593:       while (ptr && !found)
                    594:        {
                    595:          for (i = 0; ptr[i].charCode >= 0 && !found; i++)
                    596:            found = (ptr[i].charCode == entityValue);
1.39      cvs       597:   
1.41      cvs       598:          if (found)
                    599:            {
                    600:              /* entity value found */
                    601:              i--;
                    602:              *entityName = (char *) (ptr[i].charName);
                    603:            }
                    604:          else if (ptr != MathEntityTable)
                    605:            /* look for in the Math entities table */
                    606:            ptr = MathEntityTable;
                    607:          else
                    608:            {
                    609:              *entityName = NULL;
                    610:              ptr = NULL;
                    611:            }
1.40      cvs       612:        }
                    613:     }
1.39      cvs       614:   else
1.40      cvs       615:     *entityName = NULL;
1.39      cvs       616: }

Webmaster