Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / C++

Useful Managers

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Aug 2001 80.4K   1.2K   16  
An add-in for Devstudio that provides tag indexing and search, window, bookmarks, session and other managers
// Commands.cpp : implementation file
// COPYRIGHT (C) 1999-2000 by Mike Melnikov zmike@andnow.ru
//
#include "stdafx.h"

#include "Commands.h"
#include "RegularExpressions.h"
#include "tags\\entry.h"
#include <vector>
#include <algorithm>

using namespace std;

bool find(RegularExpression& begin,RegularExpression& exp,char*& findStr,CString& result)
{
  int rstart,rlength;
  if(begin.index(findStr,rstart,rlength))
  {
    char* buffer1 = findStr+rstart+rlength;
    if(exp.index(buffer1,rstart,rlength))
    {
      result = CString(buffer1+rstart,rlength);
      findStr = buffer1+rstart+rlength;
      return true;
    }
    else
      ASSERT(false);
  }
  return false;
}


bool insert(vector<CString>& vec,CString& str)
{
  str.MakeLower();
  CString* found = lower_bound(vec.begin(),vec.end(),str);
  if(found && found!=vec.end() && !(str<*found))
  {
    return false;
  }
  vec.insert(found,str);
  return true;
}

STDMETHODIMP CCommands::Test()
{
  LockState lock(m_pApplication);

  CFileDialog dlg(TRUE,"*.dsw",NULL,OFN_FILEMUSTEXIST,"WorkSpace Files (*.dsw)|*.dsw|All Files (*.*)|*.*||");
  if(dlg.DoModal()!=IDOK )
    return S_OK;
  CString fileDSW = dlg.GetPathName();

  //CString fileDSW = "D:\\life spring src\\nestor.dsw";

  RegularExpression findDSPBegin("^Project:[\"a-zA-Z_0-9& ]*=");
  RegularExpression findDSP("(\"[a-zA-Z_0-9\\\\&.- ]*\")|([a-zA-Z_0-9\\\\&.- ]* -)");

  RegularExpression findSourceBegin("^SOURCE=");
  RegularExpression findSource("[a-zA-Z_0-9\\\\&.- ]*");

  RegularExpression findIncludeDirBegin("/I \"");
  RegularExpression findIncludeDir("[a-zA-Z_0-9\\\\&:.- ]*\"");

  RegularExpression findIncludeBegin("#include[ ]*");
  RegularExpression findInclude("(<|\")[/a-zA-Z_0-9\\\\&.- ]*(>|\")");

  CWaitCursor wait;

	CString strFile,pathDSW,dspPath;
  const int max = 2000;
  char* buffer = new char[max];
  vector<CString> directoriesMain;
  vector<CString> allFiles;
  CFileFind finder;

  FILE* fpOut = fopen("files.txt","w");

  FILE* fpDSW = fopen(fileDSW,"r");
  insert(allFiles,fileDSW);
  baseFilename(fileDSW,strFile,pathDSW);

  char* findStr;
  while(fpDSW)
  {
    if(fgets(buffer,max,fpDSW) == NULL)
      break;
    CString fileDSP;
    findStr = buffer;
    if(find(findDSPBegin,findDSP,findStr,fileDSP))
    {
      fileDSP.TrimLeft("\"");
      fileDSP.TrimRight(" \"-");
      if(fileDSP[0]=='.' && fileDSP[1]=='\\')
      {
        fileDSP.TrimLeft(".\\");
      }
      else
        ASSERT(false);
      CString fullDSPFile = pathDSW + "\\" + fileDSP;
      if(finder.FindFile(fullDSPFile))
      {
        finder.FindNextFile();
        fullDSPFile = finder.GetFilePath();
        insert(allFiles,fullDSPFile);
      }
      else
      {
        ASSERT(false);
        continue;
      }

      FILE* fpDSP = fopen(fullDSPFile,"r");
      baseFilename(fullDSPFile,strFile,dspPath);

      vector<CString> directories;
      vector<CString> includes;
      vector<CString> files;

      insert(directories,CString(".\\"));

      while(fpDSP)
      {
        if(fgets(buffer,max,fpDSP) == NULL)
          break;
        findStr = buffer;
        while(true)
        {
          // find Source file
          CString source,includeDir;
          if(find(findSourceBegin,findSource,findStr,source))
          {
            if(source[0]=='.' && source[1]=='\\')
              source.TrimLeft(".\\");
            insert(files,source);
          }
          // find new include directory
          else if(find(findIncludeDirBegin,findIncludeDir,findStr,includeDir))
          {
            includeDir.TrimRight("\"\\");
            CString fullDir;
            if(includeDir[0]=='.')
              fullDir = dspPath + "\\" + includeDir + "\\";
            else
              fullDir = includeDir + "\\";
            insert(directories,fullDir);
          }
          else
           break;
        }
      }

      // search includes
      for(int f=0;f<files.size();f++)
      {
        CString fullFile = dspPath + "\\" + files[f];
        if(finder.FindFile(fullFile))
        {
          finder.FindNextFile();
          fullFile = finder.GetFilePath();
        }
        else
        {
          ASSERT(false);
          continue;
        }
        if(!insert(allFiles,fullFile))
        {
          // already searched
          continue;
        }
        FILE* fpSource = fopen(fullFile,"r");
        while(fpSource)
        {
          if(fgets(buffer,max,fpSource) == false)
            break;
          CString include;
          findStr = buffer;
          if(find(findIncludeBegin,findInclude,findStr,include))
          {
            include.TrimLeft (" \"<");
            include.TrimRight(" \">");
            if(insert(includes,include))
            {
              for(int d=0;d<directories.size();d++)
              {
                if(finder.FindFile(directories[d]+include))
                {
                  finder.FindNextFile();
                  CString str = finder.GetFilePath();
                  insert(allFiles,str);
                  break;
                }
              }
            }
          }
        }
        if(fpSource)
          fclose(fpSource);
      }
    }
  }

  for(int f=0;f<allFiles.size();f++)
    fprintf(fpOut,"%s\n",allFiles[f]);

  fclose(fpOut);
  delete buffer;
	return S_OK;
}

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
Web Developer
Russian Federation Russian Federation
Mike has been programming in C/C++ for 11 years and Visual C++/MFC for 4 years. His background includes pure and applied mathematics, engineering and physics, and he is currently based in Moscow, Russia.

Comments and Discussions