view examples/rot13/main.cpp @ 81:f8715c806d1c

[rot13] Fixed comments about cast in the rot13 example
author Zoltan K.
date Sun, 04 Apr 2010 19:31:02 +0200
parents 7c8cd74023ce
children
line wrap: on
line source

#include <sstream>
#include <iostream>
#include <vector>

#include "Rot13Module.hpp"
#include "Rot13Handler.hpp"

/**
 * @brief Function that "emulates" the behavior of a server by instanciating a
 * IModule and by calling one of its handlers.
 *
 * @param [in] is The stream to read from.
 * @param [in] os The stream to write to.
 */
void    process_stream(std::istream & is, std::ostream & os)
{
    ModRot13                                            module;
    const std::vector<zia::api::handler::IHandler *> &  handlers = module.getHandlers();

    std::cout << "Running: " << module.getName()
        << " v" << module.getVersion() << std::endl;

    if (!handlers.empty()
        && handlers[0]->getHook() == zia::api::handler::ResponseBodyModifier)
    {
        /*
         * We should cast to zia::api::handler::IBodyHandler, but to call the
         * functor we would need to implement zia::api::http::ITransaction,
         * which isn't the purpose of this example.
         */
        Rot13Handler * bodyHandler = dynamic_cast<Rot13Handler *>(handlers[0]);

        if (bodyHandler != NULL)
            (*bodyHandler)(is, os);
    }
}

/**
 * @brief This function print the content of a stream on the standard output
 *
 * @param [in] is the stream to dump
 */
void    dump_stream(std::istream & is)
{
    while (!is.eof())
    {
        char c;
        is.get(c);
        std::cout << c;
    }
    std::cout << "---" << std::endl;
}

int     main(void)
{
    std::stringbuf  buffer1;
    std::stringbuf  buffer2;
    std::ostream    os(NULL);
    std::istream    is(NULL);

    os.rdbuf(&buffer1);
    os << "This is an example for the StreamIt api !" << std::endl;

    is.rdbuf(&buffer1);
    os.rdbuf(&buffer2);

    process_stream(is, os);

    is.rdbuf(&buffer2);

    dump_stream(is);

    std::cout << "The correct rot13 is: " << std::endl
        << "Guvf vf na rknzcyr sbe gur FgernzVg ncv !" << std::endl;

    return (0);
}