summaryrefslogtreecommitdiffstats
path: root/src/projectbase.cpp
blob: 603ba44a687b1a1d775ac15b92b499637a201532 (plain)
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
/***************************************************************************
 *
 * Copyright (C) 2007 Elad Lahav (elad_lahav@users.sourceforge.net)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ***************************************************************************/

#include "projectbase.h"
#include "kscopeconfig.h"
#include "cscopefrontend.h"

ProjectBase::ProjectBase()
{
}

ProjectBase::~ProjectBase()
{
}

bool ProjectBase::open(const TQString& sPath)
{
	TQFileInfo fi(sPath);
		
	// Make sure the file exists, and that is is a cross-reference file
	if (!fi.exists() || !isCscopeOut(fi.absFilePath()))
		return false;
		
	// Set the project's directory
	m_dir = fi.dirPath(true);
	
	// Set the name of the project to be the full path of the file
	m_sName = fi.absFilePath();
	
	// Initialise project options (assume source root is the folder holding the
	// cscope.out file)
	getDefOptions(m_opt);
	m_opt.sSrcRootPath = m_dir.absPath();
	
	return true;
}

/**
 * Determines if the cscope.out file for this project exists.
 * @return	true if the database exists, false otherwise
 */
bool ProjectBase::dbExists()
{
	return m_dir.exists("cscope.out");
}

void ProjectBase::getOptions(Options& opt) const
{
	getDefOptions(opt);
}

void ProjectBase::getMakeParams(TQString& sCmd, TQString& sDir) const
{
	sCmd = "make";
	sDir = getSourceRoot();
}

/**
 * Fills a structure with default properties for a new project.
 * Default properties are partly based on the system profile.
 * @param	opt	The structure to fill
 */
void ProjectBase::getDefOptions(Options& opt)
{
	// Set default source path to file-system root
	opt.sSrcRootPath = "/";
	
	// Initialise MIME-type list
	opt.slFileTypes.append("*.c");
	opt.slFileTypes.append("*.h");

	// Set other options
	opt.bKernel = DEF_IS_KERNEL;
	opt.bInvIndex = DEF_INV_INDEX;
	opt.bNoCompress = DEF_NO_COMPRESS;
	opt.bSlowPathDef = DEF_SLOW_PATH;
	opt.nACMinChars = DEF_AC_MIN_CHARS;
	opt.nACDelay = DEF_AC_DELAY;
	opt.nACMaxEntries = DEF_AC_MAX_ENTRIES;
	opt.nTabWidth = DEF_TAB_WIDTH;
	
	// Set profile-dependant options
	if (Config().getSysProfile() == KScopeConfig::Fast) {
		opt.nAutoRebuildTime = 10;
		opt.bACEnabled = true;
	}
	else {
		opt.nAutoRebuildTime = -1;
		opt.bACEnabled = false;
	}
}
	
void ProjectBase::initOptions()
{
	// Load the options
	getOptions(m_opt);

	// Create the argument list for invoking Cscope
	m_nArgs = 0;
	if (m_opt.bKernel)
		m_nArgs |= CscopeFrontend::Kernel;
	if (m_opt.bInvIndex)
		m_nArgs |= CscopeFrontend::InvIndex;
	if (m_opt.bNoCompress)
		m_nArgs |= CscopeFrontend::NoCompression;
	if (m_opt.bSlowPathDef)
		m_nArgs |= CscopeFrontend::SlowPathDef;
}

/**
 * Determines if the given file is a Cscope cross-reference database.
 * @param	sPath	The full path of the file to check
 * @return	true if the given file is a cscope.out file, false otherwise
 */
bool ProjectBase::isCscopeOut(const TQString& sPath)
{
	TQFile file(sPath);
	TQString sLine;
	int nVer;
	char szDir[PATH_MAX];

	// Try to open the file
	if (!file.open(IO_ReadOnly))
		return false;
		
	// Check if the first line matches the expected format
	sLine = TQTextStream(&file).readLine();
	return sscanf(sLine.latin1(), "cscope %d %s", &nVer, szDir) == 2;
}

/**
 * Fills a list object with all files in the project.
 * List items are created by reading and parsing all file name entries from
 * the project's 'cscope.files' file.
 * Note that the file may contain option lines, beginning with a dash. These
 * should be ignored.
 * @param	pList	Pointer to the object to fill
 */
bool ProjectBase::loadFileList(FileListTarget* pList)
{
	TQString sFilePath;
	TQFile file;
	
	// Make sure the file exists
	if (!m_dir.exists("cscope.files"))
		return false;
	
	// Open the file
	file.setName(m_dir.absPath() + "/cscope.files");
	if (!file.open(IO_ReadOnly))
		return false;

	// Read all file names from the file
	TQTextStream str(&file);
	while ((sFilePath = str.readLine()) != TQString::null) {
		// Skip option lines
		if (sFilePath.at(0) == '-')
			continue;

		// Set the new list item
		pList->addItem(sFilePath);
	}

	file.close();
	return true;
}