Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / MFC

Be Sweet - a set of visual source code browsers

Rate me:
Please Sign up or sign in to vote.
4.85/5 (35 votes)
1 Jul 20038 min read 183.7K   4.9K   122  
A set of source code and project browsers to compliment Visual Studio.
#ifndef STL_EXT_H
#define STL_EXT_H

#include <cctype>
#include <cstring>
#include <algorithm>
#include <functional>
#include <string>
#include <sstream>
#include <typeinfo>
#include <list>

#include <smart_ptr.h>

template<class LHS, class RHS>
LHS lexical_cast(const RHS &rhs)
{
  LHS lhs;
  std::stringstream ss;
  ss << rhs;
  ss >> lhs;

  if(ss.fail()) {
    throw std::invalid_argument(std::string("lexical_cast(): fail to convert ") + typeid(RHS).name() + ss.str() + " to " + typeid(LHS).name());
  }

  return lhs;
}

template<typename ForwardIter, typename OutputIter, typename UnaryPred>
OutputIter copy_if(ForwardIter begin, ForwardIter end, OutputIter dest, UnaryPred f) 
{
  while(begin != end) {
    if(f(*begin))
      *dest++ = *begin;
    begin++;
  }
  return dest;
}

template <typename T>
void rm_dup_space (T &s) {
    typename T::size_type pos = 0;
    typename T::size_type size = s.size();

    while ((pos+1) < size) {
       if (isspace(s[pos]) && isspace(s[pos + 1])) {
           s.erase(pos+1, 1);
           size = s.size();
           continue;
       }

       pos++;
    }
}
/*
template <typename T>
void rm_space_newline(T &s) {
  typename T::size_type pos;
  
  while ( (pos = s.find('\n')) != T::npos) {
    s[pos] = ' ';
    rm_space_forward(s, pos+1);
    if (pos != 0) rm_space_reverse(s, pos-1);
    
    if ((pos + 1) == s.size()) {
      s.erase(pos); break;
    }
    
  }
}*/

template<typename CharType>
struct make_lower_case
{
  CharType operator() (CharType c) const
  { using namespace std; return tolower(c); }
};

inline char is_cntrl(char c)
{
  using namespace std;
  return iscntrl(c);
}

template <typename T>
T to_lower(const T& in)
{
  std::string out = in;
  std::transform(in.begin(), in.end(), out.begin(), make_lower_case<T::value_type>());
  return out;
}

struct select1st
{
  template<class T1, class T2>
  const T1& operator()(const std::pair<T1, T2> & p) const
  { return p.first; }
};

struct select2nd
{
  template<class T1, class T2>
  const T2& operator()(const std::pair<T1, T2> & p) const
  { return p.second; }
};

struct front_extractor
{
  template<class container_type>
  const container_type::value_type& operator()(const container_type &container) const
  { return container.front(); }

};

template<class container_type>
struct container_creator  : std::unary_function<container_type::value_type, container_type>

{
  result_type operator()(const argument_type &front) const
  { 
    container_type container;
    container.insert(container.begin(), front);
    return container;
  }
};

template<class T>
std::list<T> make_list(const T &front)
{
  std::list<T> li;
  li.push_front(front);
  return li
}

template<class T>
class tree_node
{
public:
  typedef T value_type;

private:
  typedef smart_ptr<tree_node<value_type> > smart_node;
  
  smart_node mParent;
  std::list<tree_node<value_type> > mChildren;
  T mUserObject;

public:
  tree_node(const T& value) 
  : mUserObject(value) {}


  bool isRoot() { mParent.isNull(); }
  bool isLeafe() { return children().empty(); }
  
  const tree_node& parent() { return mParent; }
  const std::list<tree_node<T> >& children() const { return mChildren; }
  
  value_type& object() { return mUserObject; }
  const value_type& object() const { return mUserObject; }

  void addChild(const tree_node &child)
  {
    mChildren.push_back(child);
  }
};


struct file_name_is : public std::binary_function<std::string,std::string,bool>
{
  result_type operator()(const std::string &full, const std::string &name) const
  { return nameof(full) == name; }

  static std::string nameof(const std::string &full) 
  {
    size_t start = full.find_last_of("\\/");
    size_t end = full.rfind(".");

    start = start == std::string::npos ? 0 : start + 1;
    return full.substr(start, end-start);
  }
};

struct file_suffix_is : public std::binary_function<std::string,std::string,bool>
{
  result_type operator()(const std::string &full, const std::string &suffix) const
  { return suffixof(full) == suffix; }

  static std::string suffixof(const std::string &full)
  {
    size_t start = full.rfind(".");
    return start != std::string::npos ? full.substr(start+1, std::string::npos) : "";
  }
};

struct file_path_is : public std::binary_function<std::string,std::string,bool>
{
  result_type operator()(const std::string &full, const std::string &path) const
  { return pathof(full) == path; }

  static std::string pathof(const std::string &full)
  {
    size_t start = full.find_last_of("\\/");
    return start != std::string::npos ? full.substr(0, start+1) : "";
  }
};

#endif

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions