Click here to Skip to main content
15,884,085 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.
#include "stdafx.h"

#include "VSWorkspaceLoader.h"
#include <Utilities/StringTokenizer.h>
#include <Utilities/stl_ext.h>

#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <iterator>

using namespace std;

string VSProjectLoader::extractPath(const std::string &full) const
{
	string path = full.substr(0, full.rfind("\\"));
	if(path.size() >= 1 && path[0] == '.' ) {
		
    if(path.find("..") == string::npos) {
      path = path.substr(1, string::npos);
    }
  }

	return path;
}

string mergePaths(const string &abs, const  string &rel)
{
  vector<string> absolute = StringVectorTokenizer(abs, "\\/").tokens();
  vector<string> relative = StringVectorTokenizer(rel, "\\/").tokens();

  int dotdotCnt = count(relative.begin(), relative.end(), "..");

  stringstream ss;
  vector<string> full(absolute.begin(), absolute.end()-dotdotCnt);
  full.insert(full.end(), relative.begin()+dotdotCnt, relative.end());
  
  copy(full.begin(), full.end(), ostream_iterator<string>(ss, "\\"));
  return ss.str();
}

string VSProjectLoader::extractFileName(const std::string &full) const
{
	size_t pos = full.rfind("\\");
	if(pos == string::npos) {
		return full;
	} 

	return full.substr(pos+1, string::npos);
}

bool VSProjectLoader::setup(Package &package)
{
  package = load(package.getName());
  return true;
}

Package VSProjectLoader::load(const std::string &dsp) const
{
  ifstream file(dsp.c_str());
	Package::FileSet files;
  if(!file) {
    throw invalid_argument("project file " + dsp + " not found");
  }

	string projectPath = extractPath(dsp);

  string line;
  size_t pos;
  while(!file.eof()) {
    getline(file, line);
    if((pos = line.find("SOURCE=")) != string::npos && isValidFile(line)){
      string full = line.substr(pos+7, string::npos);
			if(full[0] != '\\') {
        string relativePath = extractPath(full);
				full = mergePaths(projectPath, relativePath) + extractFileName(full);
			}
      //visual studio sometimes fucks up the case of the file-path (converts all chars to upper !)
			files.insert(to_lower(extractPath(full)) + "\\" + extractFileName(full));
    }
  }

  return Package(dsp, files);
}

bool VSProjectLoader::isValidFile(const std::string &file) const
{
	return	file.find(".h") != string::npos || 
					file.find(".hpp") != string::npos || 
					file.find(".cpp") != string::npos;
}


smart_ptr<Workspace> VisualStudioWorkspaceLoader::load(const list<string> &projects)
{
	list<Package> packages;
	transform(projects.begin(), projects.end(), inserter(packages, packages.begin()),
						VSProjectLoader());

	return smart_ptr<Workspace>(new Workspace(mName, packages));
}

smart_ptr<Workspace> VisualStudioWorkspaceLoader::load(const list<Package> &projects)
{
	list<Package> packages = projects;
  for_each(packages.begin(), packages.end(), VSProjectLoader());

	return smart_ptr<Workspace>(new Workspace(mName, packages));
}


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