File: DevStudioFileStore.cpp

package info (click to toggle)
cccc 3.pre84-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 3,816 kB
  • ctags: 4,969
  • sloc: ansic: 33,244; cpp: 10,694; java: 622; makefile: 164; sh: 11
file content (308 lines) | stat: -rw-r--r-- 8,701 bytes parent folder | download | duplicates (10)
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
36
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// DevStudioFileStore.cpp: implementation of the DevStudioFileStore class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

// Turn off warnings due to long names arising out of STL use
#pragma warning ( disable : 4786 4503 )

#include "CcccDevStudioAddIn.h"
#include "DevStudioFileStore.h"

// Use of the COM interface to the Visual Studio workspace is
// localised to this file.
#include <ObjModel\addguid.h>
#include <ObjModel\appguid.h>
#include <ObjModel\bldguid.h>
#include <ObjModel\textguid.h>
#include <ObjModel\dbgguid.h>

#include <fstream>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

// This file contains logic shamelessly copied from Joshua C. Jensen's 
// Workspace Utilities package.

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DevStudioFileStore::DevStudioFileStore(IApplication *pApp, 
									   const std::string& wsDir, const std::string& wsName)
									   : m_pApplication(pApp)
									   , m_WorkspaceDirectory(wsDir)
									   , m_WorkspaceName(wsName)
{
	CComBSTR bszWSPath, bszWSName, bszWSFullName;
	
	VERIFY_OK(m_pApplication->get_Path(&bszWSPath));
	VERIFY_OK(m_pApplication->get_Name(&bszWSName));
	VERIFY_OK(m_pApplication->get_FullName(&bszWSFullName));
	
	VERIFY_OK(m_pApplication->PrintToOutputWindow(bszWSPath));
	VERIFY_OK(m_pApplication->PrintToOutputWindow(bszWSName));
	VERIFY_OK(m_pApplication->PrintToOutputWindow(bszWSFullName));
	
	// First, get a pointer to the dispatch for the Projects collection
	CComPtr<IDispatch> pDispProjects;
	VERIFY_OK(m_pApplication->get_Projects(&pDispProjects));
	CComQIPtr<IProjects, &IID_IProjects> pProjects(pDispProjects);
	
	// Get the number of projects in the collection
	long workspaceProjectCount;
	VERIFY_OK(pProjects->get_Count(&workspaceProjectCount));
	
	// get information on the projects
	for (long i = 1; i < workspaceProjectCount + 1; i++)
	{
		CComVariant Vari = i;
		
		// Get the next project
		CComPtr<IGenericProject> pGenProject;
		VERIFY_OK(pProjects->Item(Vari, &pGenProject));
		CComQIPtr<IGenericProject, &IID_IGenericProject> pProject(pGenProject);
		
		CComBSTR bszStr;
		VERIFY_OK(pProject->get_FullName(&bszStr));
		CString projectName = bszStr;
		
		ProjectFileStore::value_type pfsPair;
		std::string pfsName=projectName.GetBuffer(0);
		FilenameList pfsList;
		collectFilenames(pfsName,"SOURCE=",pfsList);
		m_ProjectFileStore.insert(std::make_pair(pfsName,pfsList));
	}  // end for [1..workspaceProjectCount]
	
	// dumpProjectFileList();
}

DevStudioFileStore::~DevStudioFileStore()
{
}



void DevStudioFileStore::listWorkspaceFiles(FilenameList& fileList)
{
	ProjectFileStore::const_iterator pfsIter;
	for(pfsIter=m_ProjectFileStore.begin();
	pfsIter!=m_ProjectFileStore.end();
	++pfsIter)
	{
		FilenameList::const_iterator flIter;
		for(flIter=pfsIter->second.begin();
		flIter!=pfsIter->second.end();
		++flIter)
		{
			fileList.insert(*flIter);
		}
	}
	
}

void DevStudioFileStore::listProjectFiles(std::string projectName, FilenameList& fileList)
{
	ProjectFileStore::const_iterator pfsIter;
	pfsIter=m_ProjectFileStore.find(projectName);
	std::string fileNames;
	if(pfsIter!=m_ProjectFileStore.end())
	{
		FilenameList::const_iterator flIter;
		for(flIter=pfsIter->second.begin();
		flIter!=pfsIter->second.end();
		++flIter)
		{
			fileList.insert(*flIter);
			fileNames+=*flIter+" ";
		}	
		dump(fileNames);
	}
	else
	{
		std::string msg = "Project ";
		msg.append(projectName);
		msg.append(" not found");
		AfxMessageBox(msg.c_str());
	}
}

std::string DevStudioFileStore::composeFilename(const std::string& baseDir, const std::string& relName)
{
	std::string retval;
	
	// baseDir can either be a directory specification ending in a '\' or '/'
	// or a file specification.
	// Either way
	// Now resolve relative paths.
	
	// We want to arrive at a canonical name for the file, that is a 
	// name which gives the drive letter and full path, with no use of 
	// ".."  .  We are provided with the name of the referring file 
	// and a filename (possibly) relative to that location.
	
	// Our algorithm is to compose a non-canonical name by examining
	// the potentially relative name and prepending the directory path
	// of the base location unless the relative part is seen to be
	// absolute.
	
	std::string nonCanonicalName;
	// the classes of strings which are absolute in their
	// own right are
	// 1) those which start with / or \ indicating the root directory
	//    on the current drive; and
	// 2) those which start with X: or X: indicating the root directory
	//    on the specified drive
	if( (relName[0]=='\\') || (relName[0] == '/') || (relName[1]==':') )
	{
		nonCanonicalName=relName;
	}
	else 
	{
		std::string::size_type endOfPath=baseDir.find_last_of("\\/");
		if(endOfPath!=std::string::npos)
		{
			nonCanonicalName=baseDir.substr(0,endOfPath+1)+relName;
		}
		else
		{
			// we needed a path separator and didn't find one,
			// so we are basically stuffed.
			std::string msg;
			msg.append("Couldn't compose name from ");
			msg.append(baseDir);
			msg.append(" and ");
			msg.append(relName);
			AfxMessageBox(msg.c_str());
		}
	}
	
	// Hopefully, nonCanonicalName now contains _one_ path to the 
	// file.  We use the following logic to convert this to the 
	// preferred name.
	if(nonCanonicalName.size()>0)
	{
		// The logic below has in the past contained experimentation
		// with various ways of accessing a canonical name for the 
		// file.
		// There are still two different APIs covered, anyone who knows
		// of a reason why one is better than another or why anything else
		// would be better (remember I want to support Win95/98/NT4/2000
		// in a single set of source), please mail me.
		CFileStatus fileStatus;
		CFile::GetStatus(nonCanonicalName.c_str(), fileStatus);
		
		char getFullPathNameBuf[1024],*shortNamePtr;
		unsigned long len=1024;
		memset(getFullPathNameBuf,0,len);
		int gcode=GetFullPathName(const_cast<char*>(nonCanonicalName.c_str()),
			len,getFullPathNameBuf,&shortNamePtr);
		
		
		char msgbuf[1024];
		sprintf(msgbuf,"fs=%s, "
            "GetFull returned %d name 3=%s short=%s", 
			fileStatus.m_szFullName,gcode,getFullPathNameBuf,shortNamePtr
			);
		
		// AfxMessageBox(msgbuf);
		dump(msgbuf);
		
		retval = fileStatus.m_szFullName;
	}
	
	return retval;
}

void DevStudioFileStore::collectFilenames(const std::string& dsFile, const std::string& pfx, 
										  FilenameList& fileList)
{
	std::ifstream prjStr(dsFile.c_str());
	
	const int pfxWidth=pfx.size();
	
	while(prjStr.good())
	{
		char linebuf[1024];
		prjStr.getline(linebuf,1023);
		if(!prjStr.good())
		{
			// the stream has gone bad (hopefully due to simple exhaustion)
			// no action here and the loop should terminate
		}
		else if(strncmp(linebuf,pfx.c_str(),pfxWidth)==0)
		{
			std::string relname=linebuf+pfxWidth;
			std::string absName=composeFilename(dsFile,relname);
			fileList.insert(absName);
		}
	}
}

void DevStudioFileStore::dumpProjectFileList()
{
	ProjectFileStore::const_iterator pfsIter;
	for(pfsIter=m_ProjectFileStore.begin();
	pfsIter!=m_ProjectFileStore.end();
	++pfsIter)
	{
		dump(pfsIter->first);
		dump(": ");
		
		FilenameList::const_iterator flIter;
		for(flIter=pfsIter->second.begin();
		flIter!=pfsIter->second.end();
		++flIter)
		{
			dump(flIter->c_str());
		}
		dump("");
	}
}

void DevStudioFileStore::dump(const std::string& str)
{
	VERIFY_OK(m_pApplication->PrintToOutputWindow(CComBSTR(str.c_str())));
}

std::string DevStudioFileStore::suggestedWorkingDirectory()
{
	// It would be nice to be able to make the working directory take on a constant
	// relationship to the .dsw file of the workspace, but there is no programmatic
	// way of accessing this item.
	// If we just use the current working directory of the 
	// project, we seem to start off in the .dsw file directory,
	// until a file is opened, at which point the current working
	// directory reflects the directory in which the file open
	// dialog most recently displayed.  This is not very satisfactory,
	// so for the moment we just try to use a constant directory,
	// defined either by the environment variable CCCCTEMP, with fallbacks
	// to TEMP and to the hardcoded directory c:\ (the main virtue of the latter
	// being that we are reasonably confident it will exist).
	std::string retval;
	
	char * envdir = getenv("CCCCTEMP");
	if(envdir!=0)
	{
		retval=envdir;
	}
	else
	{
		envdir=getenv("TEMP");
		if(envdir!=0)
		{
			retval=envdir;
		}
		else
		{
			retval="c:\\";
		}
	}
	return retval;
}