view src/examples/rot13/main.cpp @ 5:aa427c18ffe2

Separation of Rot13Module and Rot13Handler into 2 separate files
author Zoltan K
date Sat, 20 Mar 2010 00:06:08 +0100
parents e0a7028df8bf
children e39c4397764c
line wrap: on
line source

#include <iostream>
#include <vector>

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

void            process_stream(std::istream & is, std::ostream & os)
{
    ModRot13                                    module;
    std::vector<zia::api::handler::IHandler *>  handlers = module.getHandlers();

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

    if (!handlers.empty() && handlers[0]->getHook() == zia::api::handler::ResponseBodyModifier)
    {
        /*
        ** We should cast to zia::api::handler::IHttpHandler, 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);
    }
}

void        dump_stream(std::istream & is)
{
    char        c;

    while (!is.eof())
    {
        is.get(c);
        std::cout << c;
    }
    std::cout << "---" << std::endl;
}

int                     main(void)
{
    Buffer              buffer1;
    Buffer              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);
}