Click here to Skip to main content
15,886,578 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.8K   4.9K   122  
A set of source code and project browsers to compliment Visual Studio.
#ifndef Properties_H
#define Properties_H

#include <string>
#include <set>
#include <map>
#include <stdexcept>
#include <fstream>
#include <iterator>

#pragma warning( disable: 4290 )

template<char Delimiter='='>
class Property : public std::pair<std::string, std::string>
{

public:
  Property(){}
  Property(const std::pair<std::string, std::string> &p)
  : std::pair<std::string, std::string>(p) {}

  explicit Property(const std::string &line)
  {
    size_t pos = line.find(Delimiter);
    if(pos != std::string::npos) {
      first = line.substr(0, pos);
      second = line.substr(pos+1, std::string::npos);
      std::replace_if(second.begin(), second.end(), std::bind2nd(std::equal_to<char>(), '\t'), '\n');
    }
  }

  Property(const std::string &name, const std::string &value)
   : std::pair<std::string, std::string>(name, value){ }

  ~Property(){}

  Property& operator=(const std::pair<std::string, std::string> &p)
  { std::pair<std::string, std::string>::operator=(p); }

  const std::string& getName() const { return first; }
  const std::string& getValue() const { return second; }

  friend std::ostream& operator<<(std::ostream &os, const Property& p)
  { 
    if(!p.getName().empty()) {
      std::string value = p.getValue();
      std::replace_if(value.begin(), value.end(), std::bind2nd(std::equal_to<char>(), '\n'), '\t');
      os << p.getName() << Delimiter << value << std::endl;
    }
    return os;
  }

  friend std::istream& operator>>(std::istream &is, Property& p)
  {
    char buffer[2048];
    is.getline(buffer, sizeof(buffer));
    p = Property(buffer);
    return is;
  }
};

class Properties
{
private:
  struct PropertyComperator
  {
    bool operator()(const Property<'='> &lhs, const Property<'='> &rhs) const
    { return lhs.getName() < rhs.getName(); }
  };

  typedef Property<'='> PropertyType;
  typedef std::set<PropertyType , PropertyComperator> DatastoreType;

  std::string mFilename;
  DatastoreType mDatastore;

public:
  Properties(const std::string &filename = "");
  virtual ~Properties();

  void load(const std::string &filename = "");
  void dump(const std::string &filename = "");

  std::string getProperty(const std::string &name) const throw(std::invalid_argument);
  std::string getProperty(const std::string &name, const std::string &defaultValue) const throw();
  
  void setProperty(const std::string &name, const std::string &value);
  
  void removeProperty(const std::string &name);

  std::map<std::string, std::string> asMap() const;
  operator std::map<std::string, std::string> () const { return asMap(); }
  
private:
  const DatastoreType& getDatastore() const;
  DatastoreType& getDatastore();

  template<class StreamType>
  StreamType& openfile(StreamType &file, const std::string& filename)
  {
    std::string fn = filename.empty() ? mFilename : filename;
    file.open(fn.c_str());
    if(!file) {
      throw std::runtime_error("could not open file " + fn);
    }

    return file;
  }

};

class PropertiesManager : public Properties
{
public:
  static PropertiesManager& instance();

private:
  PropertiesManager(const std::string &file) 
  :Properties(file) {}
};

#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