parameterhandler.cc 3.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*! \file parameterhandler.cc
 * \brief handle a parameter configuration string (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
 * \author Thomas Forbriger
 * \date 06/05/2011
 * 
 * handle a parameter configuration string (implementation)
 * 
 * Copyright (c) 2011 by Thomas Forbriger (BFO Schiltach) 
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 *
 * 
 * REVISIONS and CHANGES 
 *  - 06/05/2011   V1.0   Thomas Forbriger
 * 
 * ============================================================================
 */
#define STFINV_PARAMETERHANDLER_CC_VERSION \
36
  "STFINV_PARAMETERHANDLER_CC   V1.0"
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

#include <algorithm>
#include <stfinv/parameterhandler.h>

std::string stfinv::tools::clipstring(std::string& s, const std::string& delim)
{
  std::string::size_type i=s.find(delim);
  std::string result;
  if ((i>=0) && (i<s.length())) {
    result=s.substr(0,i);
    s.erase(0,i+delim.length());
  } else {
    result=s;
    s.erase();
  }
  return(result);
} // std::string stfinv::tools::clipstring^

/*----------------------------------------------------------------------*/

stfinv::tools::Tparamap stfinv::tools::makeparamap(const std::string& p,
                                                   const std::string& delimiter,
                                                   const std::string& assign)
{
  std::string pstring=p;
  stfinv::tools::Tparamap retval;
  while (pstring.length()>0)
  {
    std::string para=stfinv::tools::clipstring(pstring, delimiter);
    std::string key=stfinv::tools::clipstring(para, assign);
    retval.insert(make_pair(key, para));
  }
  return retval;
} // stfinv::tools::Tparamap stfinv::tools::makeparamap()

/*----------------------------------------------------------------------*/

std::string stfinv::tools::commatospace(std::string s)
{
  std::replace(s.begin(), s.end(), ',', ' ');
  return(s);
} // std::string stfinv::tools::commatospace(const std::string& s)

/*----------------------------------------------------------------------*/

// \brief remove leading and trailing whitespace
std::string stfinv::tools::trimws(std::string s)
{
  if (s.length()>0)
  {
    std::string::size_type ib=s.find_first_not_of(" ", 0);
    if (ib==std::string::npos)
    {
      s="";
    }
    else
    {
      std::string::size_type il=s.find_last_not_of(" \r", s.length());
      std::string::size_type n=il>=ib ? il-ib+1 : 0;
      if (n==0) { ib = 0; }
      if ((ib!=0) || (n!=s.length())) { s=s.substr(ib,n); }
    }
  }
  return(s);
} // std::string trimws(std::string s)

/* ----- END OF parameterhandler.cc ----- */