Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

RMI for C++

, 6 Aug 2009 CPOL
User-friendly remote method invocation in C++.
rcf.zip
RCF
demo
vs2003
RCF
Client
Server
doc
include
RCF
Connection.inl
Marshal.inl
Multiplexer.inl
RCF.inl
RcfServer.inl
ServerStub.inl
Protocol
util
Platform
Compiler
bcc55
cw80
gcc32
icl70
msvc71
Library
bcc55
cw80
gcc32
msvc60
msvc71
Machine
SPARC
x86
OS
Cygwin
Unix
Windows
System
NonWindows
Windows
Threads
SF
src
RCF
Protocol
util
SF
test
Jamfile
Jamrules
borland
Jamfile
Jamrules
vs2003
RCF
RCF
RCFTest
//*****************************************************************************
// Copyright (c) 2003. All rights reserved.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//*****************************************************************************

#ifndef _UTIL_SCAN_HPP_
#define _UTIL_SCAN_HPP_

#include <sstream>
#include <string>
#include <utility>
#include <vector>

namespace util {

    class Scan {
    public:
        Scan( std::string s, std::string format ) : i_(0), ok_(true) 
        {
            int n = 0;
            std::string::size_type pos = 0;
            std::string::size_type prev_pos = 0;
            std::string::size_type npos = std::string::npos;

            // Extract format string literals
            std::vector< std::pair<std::string, std::string> > literals;
            while(pos != npos) {
                n++;
                std::string specifier1 = makeSpecifier(n);
                std::string specifier2 = makeSpecifier(n+1);
                pos = format.find(specifier1, prev_pos);
                std::string literal1 = (pos == npos) ? format.substr(prev_pos) : format.substr(prev_pos, pos - prev_pos);
                if (pos != npos) pos += specifier1.length();
                prev_pos = pos;
                pos = format.find(specifier2, prev_pos);
                std::string literal2 = (pos == npos) ? format.substr(prev_pos) : format.substr(prev_pos, pos - prev_pos);
                literals.push_back( std::make_pair( literal1, literal2 ) );
            }

            // Extract arg string values
            pos = 0;
            for (std::vector< std::pair<std::string, std::string> >::iterator it = literals.begin(); it != literals.end(); it++) {
                if (pos != npos && pos == s.find( (*it).first, pos)) {
                    std::string::size_type a = pos + (*it).first.length();
                    std::string::size_type b = s.find( (*it).second, a );
                    values_.push_back( s.substr( a , (b == a) ? npos : b-a ) );
                    pos = b;
                }
                else {
                    ok_ = false;
                }
                    
            }
            
        }

        template<typename T> Scan &operator()(T &t)
        { 
            std::istringstream istr( getNextValue() );
            istr >> t;
            return *this;
        }
        
        operator bool() const
        { 
            return ok(); 
        }

        bool ok() const
        { 
            return ok_; 
        }

    private:

        std::string makeSpecifier( int n )
        {
            std::ostringstream ostr;
            ostr << "%" << n;
            return ostr.str();
        }

        std::string getNextValue()
        { 
            if (i_<values_.size()) 
                return values_[ i_++ ]; 
            else {
                ok_ = false;
                return "";
            }
        }

        std::vector<std::string> values_;
        unsigned int i_;
        bool ok_;
    };

} // namespace util


#endif // ! _UTIL_SCAN_HPP_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jarl Lindrud

Australia Australia
Software developer, ex-resident of Sweden and now living in Canberra, Australia, working on distributed C++ applications. Jarl enjoys programming, but prefers skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 6 Aug 2009
Article Copyright 2005 by Jarl Lindrud
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid