Changeset 13667 for trunk/FACT++


Ignore:
Timestamp:
05/12/12 12:29:54 (12 years ago)
Author:
tbretz
Message:
Added a split function able to parse an escaped command line
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/tools.cc

    r13221 r13667  
    1111
    1212#include <stdarg.h>
     13
     14#include <boost/tokenizer.hpp>
    1315
    1416using namespace std;
     
    140142    return rc;
    141143}
     144
     145// --------------------------------------------------------------------------
     146//
     147//! Splits a string into a filename and command line arguments, like:
     148//!
     149//!    file.txt arg1=argument1 arg2="argument 2" arg3="argument \"3\""
     150//!
     151//! 'file.txt' will be returned on opt, the arguments will be returned in
     152//! the returned map.
     153//!
     154//! If the returned file name is empty, an error has occured:
     155//!   If the map is also empty the file name was empty, if the map has
     156//!   one entry then for this entry the euqal sign was missing.
     157//!
     158map<string,string> Tools::Split(string &opt)
     159{
     160    using namespace boost;
     161    typedef escaped_list_separator<char> separator;
     162
     163    const string data(opt);
     164
     165    const tokenizer<separator> tok(data, separator('\\', ' ', '\"'));
     166
     167    auto it=tok.begin();
     168    if (it==tok.end())
     169    {
     170        opt = "";
     171        return map<string,string>();
     172    }
     173
     174    opt = *it++;
     175
     176    map<string,string> rc;
     177
     178    for (; it!=tok.end(); it++)
     179    {
     180        const size_t pos = it->find_first_of('=');
     181        if (pos==string::npos)
     182        {
     183            opt = "";
     184            rc.clear();
     185            rc[*it] = "";
     186            return rc;
     187        }
     188
     189        rc[it->substr(0, pos)] = it->substr(pos+1);
     190    }
     191
     192    return rc;
     193}
  • trunk/FACT++/src/tools.h

    r13221 r13667  
    22#define FACT_Tools
    33
     4#include <map>
    45#include <string>
    56
     
    1112    std::string TrimQuotes(const std::string &str);
    1213    std::string Wrap(std::string &str, size_t width=78);
     14
     15    std::map<std::string,std::string> Split(std::string &);
    1316}
    1417
Note: See TracChangeset for help on using the changeset viewer.