Back to index

lightning-sunbird  0.9+nobinonly
Classes | Public Member Functions | Private Attributes
nsCacheMetaData Class Reference

#include <nsCacheMetaData.h>

Collaboration diagram for nsCacheMetaData:
Collaboration graph
[legend]

List of all members.

Classes

struct  MetaElement

Public Member Functions

 nsCacheMetaData ()
 ~nsCacheMetaData ()
void Clear ()
PRBool IsEmpty ()
const char * GetElement (const char *key)
nsresult SetElement (const char *key, const char *value)
PRUint32 Size (void)
nsresult FlattenMetaData (char *buffer, PRUint32 bufSize)
nsresult UnflattenMetaData (const char *buffer, PRUint32 bufSize)
nsresult VisitElements (nsICacheMetaDataVisitor *visitor)

Private Attributes

MetaElementmData
PRUint32 mMetaSize

Detailed Description

Definition at line 51 of file nsCacheMetaData.h.


Constructor & Destructor Documentation

Definition at line 47 of file nsCacheMetaData.cpp.

    : mData(nsnull), mMetaSize(0)
{
}

Definition at line 54 of file nsCacheMetaData.h.

{ Clear(); }

Here is the call graph for this function:


Member Function Documentation

Definition at line 53 of file nsCacheMetaData.cpp.

{
    mMetaSize = 0;
    MetaElement * elem;
    while (mData) {
        elem = mData->mNext;
        delete mData;
        mData = elem;
    }
}

Here is the caller graph for this function:

nsresult nsCacheMetaData::FlattenMetaData ( char *  buffer,
PRUint32  bufSize 
)

Definition at line 143 of file nsCacheMetaData.cpp.

{
    const char *key;

    if (mMetaSize > bufSize) {
        NS_ERROR("buffer size too small for meta data.");
        return NS_ERROR_OUT_OF_MEMORY;
    }

    MetaElement * elem = mData;
    while (elem) {
        elem->mKey->GetUTF8String(&key);

        PRUint32 keySize = 1 + strlen(key);
        memcpy(buffer, key, keySize);
        buffer += keySize;

        PRUint32 valSize = 1 + strlen(elem->mValue);
        memcpy(buffer, elem->mValue, valSize);
        buffer += valSize;

        elem = elem->mNext;
    }
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char * nsCacheMetaData::GetElement ( const char *  key)

Definition at line 65 of file nsCacheMetaData.cpp.

{
    // We assume the number of meta data elements will be very small, so
    // we keep it real simple.  Singly-linked list, linearly searched.

    nsCOMPtr<nsIAtom> keyAtom = do_GetAtom(key);

    MetaElement * elem = mData;
    while (elem) {
        if (elem->mKey == keyAtom)
            return elem->mValue;
        elem = elem->mNext;
    }
    return nsnull;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 57 of file nsCacheMetaData.h.

{ return (mData == nsnull); }
nsresult nsCacheMetaData::SetElement ( const char *  key,
const char *  value 
)

Definition at line 83 of file nsCacheMetaData.cpp.

{
    nsCOMPtr<nsIAtom> keyAtom = do_GetAtom(key);
    if (!keyAtom)
        return NS_ERROR_OUT_OF_MEMORY;

    PRUint32 keySize = strlen(key);
    PRUint32 valueSize = value ? strlen(value) : 0;

    // find and remove or update old meta data element
    MetaElement * elem = mData, * last = nsnull;
    while (elem) {
        if (elem->mKey == keyAtom) {
            // Get length of old value
            PRUint32 oldValueLen = strlen(elem->mValue);
            if (valueSize == oldValueLen) {
                // Just replace value
                memcpy(elem->mValue, value, valueSize);
                return NS_OK;
            }
            // remove elem
            if (last)
                last->mNext = elem->mNext;
            else
                mData = elem->mNext;
            // 2 for the zero bytes of both strings
            mMetaSize -= 2 + keySize + oldValueLen;
            delete elem;
            break;
        }
        last = elem;
        elem = elem->mNext;
    }

    // allocate new meta data element
    if (value) {
        elem = new (value, valueSize) MetaElement;
        if (!elem)
            return NS_ERROR_OUT_OF_MEMORY;
        elem->mKey = keyAtom;

        // insert after last or as first element...
        if (last) {
            elem->mNext = last->mNext;
            last->mNext = elem;
        }
        else {
            elem->mNext = mData;
            mData = elem;
        }

        // Adjust CacheMetaData size, 2 for the zero bytes of both strings
        mMetaSize += 2 + keySize + valueSize;
    }

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 64 of file nsCacheMetaData.h.

{ return mMetaSize; }

Here is the caller graph for this function:

nsresult nsCacheMetaData::UnflattenMetaData ( const char *  buffer,
PRUint32  bufSize 
)

Definition at line 170 of file nsCacheMetaData.cpp.

{
    if (size == 0) return NS_OK;

    const char* limit = data + size;
    MetaElement * last = nsnull;

    while (data < limit) {
        const char* key = data;
        PRUint32 keySize = strlen(key);
        data += 1 + keySize;
        if (data < limit) {
            nsCOMPtr<nsIAtom> keyAtom = do_GetAtom(key);
            if (!keyAtom)
                return NS_ERROR_OUT_OF_MEMORY;

            PRUint32 valueSize = strlen(data);
            MetaElement *elem = new (data, valueSize) MetaElement;
            if (!elem)
                 return NS_ERROR_OUT_OF_MEMORY;
            elem->mKey = keyAtom;

            // insert after last or as first element...
            if (last) {
                elem->mNext = last->mNext;
                last->mNext = elem;
            }
            else {
                elem->mNext = mData;
                mData = elem;
            }

            last = elem;
            data += 1 + valueSize;

            // Adjust CacheMetaData size, 2 for the zero bytes of both strings
            mMetaSize += 2 + keySize + valueSize;
        }
    }
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 213 of file nsCacheMetaData.cpp.

{
    const char *key;

    MetaElement * elem = mData;
    while (elem) {
        elem->mKey->GetUTF8String(&key);

        PRBool keepGoing;
        nsresult rv = visitor->VisitMetaDataElement(key, elem->mValue, &keepGoing);

        if (NS_FAILED(rv) || !keepGoing)
            break;

        elem = elem->mNext;
    }

    return NS_OK;
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 86 of file nsCacheMetaData.h.

Definition at line 87 of file nsCacheMetaData.h.


The documentation for this class was generated from the following files: