VPR-6.0

libvpr/read_xml_util.c File Reference

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "ezxml.h"
#include "read_xml_util.h"
Include dependency graph for read_xml_util.c:

Go to the source code of this file.

Functions

ezxml_t FindElement (INP ezxml_t Parent, INP const char *Name, INP boolean Required)
ezxml_t FindFirstElement (INP ezxml_t Parent, INP const char *Name, INP boolean Required)
void CheckElement (INP ezxml_t Node, INP const char *Name)
void FreeNode (INOUTP ezxml_t Node)
boolean IsWhitespace (char c)
const char * FindProperty (INP ezxml_t Parent, INP const char *Name, INP boolean Required)
void CountTokensInString (INP const char *Str, OUTP int *Num, OUTP int *Len)
char ** GetNodeTokens (INP ezxml_t Node)
char ** LookaheadNodeTokens (INP ezxml_t Node)
int GetIntProperty (INP ezxml_t Parent, INP const char *Name, INP boolean Required, INP int default_value)
float GetFloatProperty (INP ezxml_t Parent, INP const char *Name, INP boolean Required, INP float default_value)
boolean GetBooleanProperty (INP ezxml_t Parent, INP const char *Name, INP boolean Required, INP boolean default_value)
int CountChildren (INP ezxml_t Node, INP const char *Name, INP int min_count)

Function Documentation

void CheckElement ( INP ezxml_t  Node,
INP const char *  Name 
)

Checks the node is an element with name equal to one given

Definition at line 67 of file read_xml_util.c.

{
        assert(Node != NULL && Name != NULL);
    if(0 != strcmp(Node->name, Name))
        {
            printf(ERRTAG
                                "[LINE %d] Element '%s' within element '%s' does match expected "
                                "element type of '%s'\n", Node->line, Node->name, Node->parent->name,
                    Name);
            exit(1);
        }
}

Here is the caller graph for this function:

int CountChildren ( INP ezxml_t  Node,
INP const char *  Name,
INP int  min_count 
)

Counts number of child elements in a container element. Name is the name of the child element. Errors if no occurances found.

Definition at line 407 of file read_xml_util.c.

{
    ezxml_t Cur, sibling;
    int Count;

    Count = 0;
    Cur = Node->child;
        sibling = NULL;

        if(Cur)
        {
                sibling = Cur->sibling;
        }
            
    while(Cur)
        {
                if(strcmp(Cur->name, Name) == 0)
                {
                    ++Count;
                }
            Cur = Cur->next;
            if(Cur == NULL)
                {
                    Cur = sibling;
                        if(Cur != NULL)
                        {
                                sibling = Cur->sibling;
                        }
                }
        }   
        /* Error if no occurances found */ 
        if(Count < min_count)
        {
                printf(ERRTAG "[Line %d] Expected node '%s' to have %d "
                    "child elements, but none found.\n", Node->line, Node->name, min_count);
            exit(1);
        }
    return Count;
}

Here is the caller graph for this function:

void CountTokensInString ( INP const char *  Str,
OUTP int *  Num,
OUTP int *  Len 
)

Count tokens and length in all the Text children nodes of current node.

Definition at line 165 of file read_xml_util.c.

{
    boolean InToken;
    *Num = 0;
    *Len = 0;
    InToken = FALSE;
    while(*Str)
        {
            if(IsWhitespace(*Str))
                {
                    InToken = FALSE;
                }
            
            else
                
                {
                    if(!InToken)
                        {
                            ++(*Num);
                        }
                    ++(*Len);
                    InToken = TRUE;
                }
            ++Str;              /* Advance pointer */
        }
}

Here is the call graph for this function:

Here is the caller graph for this function:

ezxml_t FindElement ( INP ezxml_t  Parent,
INP const char *  Name,
INP boolean  Required 
)

Finds child element with given name and returns it. Errors out if more than one instance exists.

Definition at line 12 of file read_xml_util.c.

{
    ezxml_t Cur;

        /* Find the first node of correct name */
        Cur = ezxml_child(Parent, Name);

        /* Error out if node isn't found but they required it */
        if(Required)
        {
            if(NULL == Cur)
                {
                    printf(ERRTAG
                            "[LINE %d] Element '%s' not found within element '%s'.\n", Parent->line,
                                                        Name, Parent->name);
                    exit(1);
                }
        }

        /* Look at next tag with same name and error out if exists */
        if(Cur != NULL && Cur->next)
        {
            printf(ERRTAG "[LINE %d] Element '%s' found twice within element '%s'.\n", Parent->line,
                    Name, Parent->name);
            exit(1);
        }
    return Cur;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ezxml_t FindFirstElement ( INP ezxml_t  Parent,
INP const char *  Name,
INP boolean  Required 
)

Finds child element with given name and returns it.

Definition at line 42 of file read_xml_util.c.

{
    ezxml_t Cur;

        /* Find the first node of correct name */
        Cur = ezxml_child(Parent, Name);

        /* Error out if node isn't found but they required it */
        if(Required)
        {
            if(NULL == Cur)
                {
                    printf(ERRTAG
                                                "[LINE %d] Element '%s' not found within element '%s'.\n", Parent->line,
                            Name, Parent->name);
                    exit(1);
                }
        }

    return Cur;
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* FindProperty ( INP ezxml_t  Parent,
INP const char *  Name,
INP boolean  Required 
)

Definition at line 143 of file read_xml_util.c.

{
    const char *Res;

    Res = ezxml_attr(Parent, Name);
    if(Required)
        {
            if(NULL == Res)
                {
                    printf(ERRTAG
                                                "[Line %d] Required property '%s' not found for element '%s'.\n", Parent->line,
                            Name, Parent->name);
                    exit(1);
                }
        }
    return Res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void FreeNode ( INOUTP ezxml_t  Node)

Checks that the node has no remaining child nodes or property nodes, then unlinks the node from the tree which updates pointers and frees the memory

Definition at line 85 of file read_xml_util.c.

{
    ezxml_t Cur;
    char *Txt;


        /* Shouldn't have unprocessed properties */
        if(Node->attr[0])
        {
                        printf(ERRTAG "[LINE %d] Node '%s' has invalid property %s=\"%s\".\n", Node->line,
                    Node->name, Node->attr[0], Node->attr[1]);
            exit(1);
        }

        /* Shouldn't have non-whitespace text */
        Txt = Node->txt;
    while(*Txt)
        {
            if(!IsWhitespace(*Txt))
                {
                    printf(ERRTAG
                                                "[LINE %d] Node '%s' has unexpected text '%s' within it.\n", Node->line,
                            Node->name, Node->txt);
                    exit(1);
                }
            ++Txt;
        }

        /* We shouldn't have child items left */
        Cur = Node->child;
    if(Cur)
        {
                        printf(ERRTAG "[LINE %d] Node '%s' has invalid child node '%s'.\n", Node->line,
                    Node->name, Cur->name);
            exit(1);
        }

        /* Now actually unlink and free the node */
        ezxml_remove(Node);
}

Here is the call graph for this function:

Here is the caller graph for this function:

boolean GetBooleanProperty ( INP ezxml_t  Parent,
INP const char *  Name,
INP boolean  Required,
INP boolean  default_value 
)

Find boolean attribute matching Name in XML tag Parent and return it if exists. Removes attribute from Parent

Definition at line 371 of file read_xml_util.c.

{
        
        const char * Prop;
        boolean property_value;

        property_value = default_value;
        Prop = FindProperty(Parent, Name, Required);
    if(Prop)
        {
            if((strcmp(Prop, "false") == 0) ||
                        (strcmp(Prop, "FALSE") == 0) ||
                        (strcmp(Prop, "False") == 0)) {
                        property_value = FALSE;
                } else if ((strcmp(Prop, "true") == 0) ||
                        (strcmp(Prop, "TRUE") == 0) ||
                        (strcmp(Prop, "True") == 0)) {
                        property_value = TRUE;
                } else {
                        printf(ERRTAG "[LINE %d] Unknown value %s for boolean attribute %s in %s", Parent->line,
                                Prop, Name, Parent->name);
                        exit(1);
                }
            ezxml_set_attr(Parent, Name, NULL);
        }
        return property_value;
}

Here is the call graph for this function:

Here is the caller graph for this function:

float GetFloatProperty ( INP ezxml_t  Parent,
INP const char *  Name,
INP boolean  Required,
INP float  default_value 
)

Find floating-point attribute matching Name in XML tag Parent and return it if exists. Removes attribute from Parent

Definition at line 350 of file read_xml_util.c.

{
        
        const char * Prop;
        float property_value;

        property_value = default_value;
        Prop = FindProperty(Parent, Name, Required);
    if(Prop)
        {
            property_value = atof(Prop);
            ezxml_set_attr(Parent, Name, NULL);
        }
        return property_value;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int GetIntProperty ( INP ezxml_t  Parent,
INP const char *  Name,
INP boolean  Required,
INP int  default_value 
)

Find integer attribute matching Name in XML tag Parent and return it if exists. Removes attribute from Parent

Definition at line 330 of file read_xml_util.c.

{
        const char * Prop;
        int property_value;

        property_value = default_value;
        Prop = FindProperty(Parent, Name, Required);
    if(Prop)
        {
            property_value = my_atoi(Prop);
            ezxml_set_attr(Parent, Name, NULL);
        }
        return property_value;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char** GetNodeTokens ( INP ezxml_t  Node)

Returns a token list of the text nodes of a given node. This unlinks all the text nodes from the document

Definition at line 195 of file read_xml_util.c.

{
        int Count, Len;
        char *Cur, *Dst;
        boolean InToken;
        char **Tokens;

    
        /* Count the tokens and find length of token data */ 
        CountTokensInString(Node->txt, &Count, &Len);
    
        /* Error out if no tokens found */ 
        if(Count < 1)
        {
            return NULL;
        }
    Len = (sizeof(char) * Len) + /* Length of actual data */ 
        (sizeof(char) * Count); /* Null terminators */
    
        /* Alloc the pointer list and data list. Count the final 
         * empty string we will use as list terminator */ 
        Tokens = (char **)my_malloc(sizeof(char *) * (Count + 1));
    Dst = (char *)my_malloc(sizeof(char) * Len);
    Count = 0;
    
        /* Copy data to tokens */ 
        Cur = Node->txt;
    InToken = FALSE;
    while(*Cur)
        {
            if(IsWhitespace(*Cur))
                {
                    if(InToken)
                        {
                            *Dst = '\0';
                            ++Dst;
                        }
                    InToken = FALSE;
                }
            
            else
                {
                    if(!InToken)
                        {
                            Tokens[Count] = Dst;
                            ++Count;
                        }
                    *Dst = *Cur;
                    ++Dst;
                    InToken = TRUE;
                }
            ++Cur;
        }
    if(InToken)
        {                       /* Null term final token */
            *Dst = '\0';
            ++Dst;
        }
    ezxml_set_txt(Node, "");
    Tokens[Count] = NULL;       /* End of tokens marker is a NULL */
    
        /* Return the list */ 
        return Tokens;
}

Here is the call graph for this function:

Here is the caller graph for this function:

boolean IsWhitespace ( char  c)

Returns TRUE if character is whatspace between tokens

Definition at line 128 of file read_xml_util.c.

{
    switch (c)
        {
        case ' ':
        case '\t':
        case '\r':
        case '\n':
            return TRUE;
        default:
            return FALSE;
        }
}

Here is the caller graph for this function:

char** LookaheadNodeTokens ( INP ezxml_t  Node)

Returns a token list of the text nodes of a given node. This does not unlink the text nodes from the document

Definition at line 264 of file read_xml_util.c.

{
        int Count, Len;
        char *Cur, *Dst;
        boolean InToken;
        char **Tokens;

    
        /* Count the tokens and find length of token data */ 
        CountTokensInString(Node->txt, &Count, &Len);
    
        /* Error out if no tokens found */ 
        if(Count < 1)
        {
            return NULL;
        }
    Len = (sizeof(char) * Len) + /* Length of actual data */ 
        (sizeof(char) * Count); /* Null terminators */
    
        /* Alloc the pointer list and data list. Count the final 
         * empty string we will use as list terminator */ 
        Tokens = (char **)my_malloc(sizeof(char *) * (Count + 1));
    Dst = (char *)my_malloc(sizeof(char) * Len);
    Count = 0;
    
        /* Copy data to tokens */ 
        Cur = Node->txt;
    InToken = FALSE;
    while(*Cur)
        {
            if(IsWhitespace(*Cur))
                {
                    if(InToken)
                        {
                            *Dst = '\0';
                            ++Dst;
                        }
                    InToken = FALSE;
                }
            
            else
                {
                    if(!InToken)
                        {
                            Tokens[Count] = Dst;
                            ++Count;
                        }
                    *Dst = *Cur;
                    ++Dst;
                    InToken = TRUE;
                }
            ++Cur;
        }
    if(InToken)
        {                       /* Null term final token */
            *Dst = '\0';
            ++Dst;
        }
    Tokens[Count] = NULL;       /* End of tokens marker is a NULL */
    
        /* Return the list */ 
        return Tokens;
}

Here is the call graph for this function: