Exemplo n.º 1
0
//--------------------------------------------------------------------------------------------------
le_result_t ni_GetNodeName
(
    ni_IteratorRef_t iteratorRef,  ///< [IN]  The iterator object to access.
    const char* pathPtr,           ///< [IN]  Optional path to another node in the tree.
    char* destBufferPtr,           ///< [OUT] The buffer to copy string data into.
    size_t bufferMax               ///< [IN]  The maximum size of the string buffer.
)
//--------------------------------------------------------------------------------------------------
{
    // Make sure we were given a buffer.
    if (bufferMax == 0)
    {
        return LE_OVERFLOW;
    }

    // If we have a current node, get it's name.  Otherwise we'll have to get the name from the
    // path.
    tdb_NodeRef_t nodeRef = ni_GetNode(iteratorRef, pathPtr);

    // If the iterator was closed during the GetNode, then that means there was a fatal problem
    // encountered.
    if (iteratorRef->isTerminated)
    {
        // At this point we know the client has been disconnected.  So just return fault so that the
        // calling code can know this.
        return LE_FAULT;
    }

    if (nodeRef != NULL)
    {
        return tdb_GetNodeName(nodeRef, destBufferPtr, bufferMax);
    }

    // Looks like a node wasn't found.  So, try to get the name of the node from the sub-path.  Or
    // if a sub-path was not specified, get the name from the iterator's base path.
    *destBufferPtr = '\0';

    if (strcmp(pathPtr, "") != 0)
    {
        le_pathIter_Ref_t subPathIter = le_pathIter_CreateForUnix(pathPtr);
        le_result_t result = le_pathIter_GoToEnd(iteratorRef->pathIterRef);

        if (result == LE_OK)
        {
            result = le_pathIter_GetCurrentNode(subPathIter, destBufferPtr, bufferMax);
        }

        le_pathIter_Delete(subPathIter);
        return result;
    }

    LE_ASSERT(le_pathIter_GoToEnd(iteratorRef->pathIterRef) == LE_OK);
    return le_pathIter_GetCurrentNode(iteratorRef->pathIterRef, destBufferPtr, bufferMax);
}
Exemplo n.º 2
0
static void IteratePath
(
    le_pathIter_Ref_t iteratorRef,
    const char* originalPath,
    const char* nodes[]
)
{
    char fullPath[LARGE_BUFFER_SIZE] = { 0 };

    le_pathIter_GetPath(iteratorRef, fullPath, LARGE_BUFFER_SIZE);

    LE_INFO("Iterating path %s.", fullPath);
    LE_TEST(strcmp(fullPath, originalPath) == 0);

    le_pathIter_GoToStart(iteratorRef);
    ssize_t index = 0;

    LE_INFO(">>>> Forward Iteration >>>>");

    do
    {
        char buffer[LARGE_BUFFER_SIZE] = { 0 };

        LE_TEST(le_pathIter_GetCurrentNode(iteratorRef,
                                            buffer,
                                            LARGE_BUFFER_SIZE) != LE_OVERFLOW);
        LE_INFO("> Found: %s, Expect: %s", buffer, nodes[index]);
        LE_TEST(strcmp(buffer, nodes[index]) == 0);
        index++;
    }
    while (le_pathIter_GoToNext(iteratorRef) != LE_NOT_FOUND);

    --index;

    LE_INFO("<<<< Reverse Iteration <<<<");

    le_pathIter_GoToEnd(iteratorRef);

    do
    {
        char buffer[LARGE_BUFFER_SIZE] = { 0 };

        LE_TEST(le_pathIter_GetCurrentNode(iteratorRef,
                                            buffer,
                                            LARGE_BUFFER_SIZE) != LE_OVERFLOW);
        LE_INFO("< Found: %s, Expect: %s", buffer, nodes[index]);
        LE_TEST(strcmp(buffer, nodes[index]) == 0);
        index--;
    }
    while (le_pathIter_GoToPrev(iteratorRef) != LE_NOT_FOUND);

    LE_TEST(index == -1);
}
Exemplo n.º 3
0
//--------------------------------------------------------------------------------------------------
le_result_t ni_GetNodeName
(
    ni_IteratorRef_t iteratorRef,  ///< The iterator object to access.
    const char* pathPtr,           ///< Optional path to another node in the tree.
    char* destBufferPtr,           ///< The buffer to copy string data into.
    size_t bufferMax               ///< The maximum size of the string buffer.
)
//--------------------------------------------------------------------------------------------------
{
    // Make sure we were given a buffer.
    if (bufferMax == 0)
    {
        return LE_OVERFLOW;
    }

    // If we have a current node, get it's name.  Otherwise we'll have to get the name from the
    // path.
    tdb_NodeRef_t nodeRef = ni_GetNode(iteratorRef, pathPtr);

    if (nodeRef != NULL)
    {
        return tdb_GetNodeName(nodeRef, destBufferPtr, bufferMax);
    }

    // Clear out the target buffer and attempt get the last named segment on our path string.  If
    // that fails then we're on the root node and just return an empty string.
    *destBufferPtr = 0;

    if (le_pathIter_GoToEnd(iteratorRef->pathIterRef) != LE_OK)
    {
        return le_pathIter_GetCurrentNode(iteratorRef->pathIterRef, destBufferPtr, bufferMax);
    }

    return LE_OK;
}
Exemplo n.º 4
0
static void TestUnixStyleIterator(void)
{
    LE_INFO("======== Test Unix Style Iterator.");

    static const char* nodes[] = { "a", "path", "to", "some", "end" };
    static const char* nodes2[] = { "a", "b", "c", "d", "e" };

    {
        static const char path[] = "/a/path/to/some/end";

        le_pathIter_Ref_t iteratorRef = le_pathIter_CreateForUnix(path);
        IteratePath(iteratorRef, path, nodes);
        le_pathIter_Delete(iteratorRef);
    }

    {
        static const char path[] = "::a::path::to::some::end";

        le_pathIter_Ref_t iteratorRef = le_pathIter_Create(path, "::", "..", ".");
        IteratePath(iteratorRef, path, nodes);
        le_pathIter_Delete(iteratorRef);
    }

    {
        static const char path[] = "/a/b/c/d/e";

        le_pathIter_Ref_t iteratorRef = le_pathIter_CreateForUnix(path);
        IteratePath(iteratorRef, path, nodes2);
        le_pathIter_Delete(iteratorRef);
    }

    {
        static const char path[] = "::a::b::c::d::e";

        le_pathIter_Ref_t iteratorRef = le_pathIter_Create(path, "::", "..", ".");
        IteratePath(iteratorRef, path, nodes2);
        le_pathIter_Delete(iteratorRef);
    }

    {
        char buffer[LARGE_BUFFER_SIZE] = { 0 };
        le_pathIter_Ref_t iteratorRef = le_pathIter_CreateForUnix("");

        LE_TEST(le_pathIter_GetCurrentNode(iteratorRef,
                                            buffer,
                                            LARGE_BUFFER_SIZE) == LE_NOT_FOUND);
        LE_TEST(strcmp(buffer, "") == 0);
    }

    {
        char buffer[LARGE_BUFFER_SIZE] = { 0 };
        le_pathIter_Ref_t iteratorRef = le_pathIter_CreateForUnix("/");

        le_pathIter_GoToStart(iteratorRef);
        LE_TEST(le_pathIter_GetCurrentNode(iteratorRef,
                                            buffer,
                                            LARGE_BUFFER_SIZE) == LE_NOT_FOUND);
        LE_TEST(strcmp(buffer, "") == 0);

        le_pathIter_GoToEnd(iteratorRef);
        LE_TEST(le_pathIter_GetCurrentNode(iteratorRef,
                                            buffer,
                                            LARGE_BUFFER_SIZE) == LE_NOT_FOUND);
        LE_TEST(strcmp(buffer, "") == 0);
    }

    {
        char buffer[LARGE_BUFFER_SIZE] = { 0 };
        le_pathIter_Ref_t iteratorRef = le_pathIter_CreateForUnix("/some/path/somewhere");

        le_pathIter_GoToStart(iteratorRef);
        LE_TEST(le_pathIter_GetCurrentNode(iteratorRef,
                                            buffer,
                                            LARGE_BUFFER_SIZE) == LE_OK);
        LE_TEST(strcmp(buffer, "some") == 0);

        le_pathIter_GoToEnd(iteratorRef);
        LE_TEST(le_pathIter_GetCurrentNode(iteratorRef,
                                            buffer,
                                            LARGE_BUFFER_SIZE) != LE_NOT_FOUND);
        LE_TEST(strcmp(buffer, "somewhere") == 0);
    }
}