File: csdbheader.cpp

package info (click to toggle)
codequery 0.18.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,312 kB
  • ctags: 2,215
  • sloc: cpp: 18,496; xml: 16,576; perl: 244; makefile: 11
file content (158 lines) | stat: -rw-r--r-- 3,481 bytes parent folder | download
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

/*
 * CodeQuery
 * Copyright (C) 2013-2016 ruben2020 https://github.com/ruben2020/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

// Cscope database header


#include "csdbheader.h"
#include <stdlib.h>


csdbheader::csdbheader()
:m_csdbver(0)
,m_trailer_start(0)
{

}

csdbheader::csdbheader(tStr hdr)
:m_csdbver(0)
,m_trailer_start(0)
{
m_header = hdr;
}

void csdbheader::set_header(tStr hdr)
{
m_header = hdr;
}

void csdbheader::set_header(const char* hdr)
{
m_header = hdr;
}

bool csdbheader::parse(void)
{

size_t pos;

m_param_list.clear();
if (m_header.empty()) return false;

// Header should be at least 19 bytes long and starts with "cscope "
if ((m_header.length() <19)||(m_header.compare(0, strlen("cscope "), "cscope ") != 0))
	{return false;}

// Get the cscope version used to build the database
// Assuming it is 2 digits always
m_csdbver = strtol(m_header.substr(7, 2).c_str(), NULL, 10);

// Get the trailer start position
pos = m_header.find_last_of((char) ' ');
m_trailer_start = strtol(m_header.substr(pos + 1).c_str(), NULL, 10);

// Get rid of "cscope nn " and trailer start pos
m_header = m_header.substr(10, pos - 10);

// Get the first position of a param
pos = m_header.find((const char*) " -");

// if no params, then whole string is db path
if (pos == std::string::npos)
{
	m_base_path = m_header.substr(0);

	// Trim db path
	pos = m_base_path.find_last_not_of((char) ' ') + 1;
	m_base_path = m_base_path.substr(0, pos);

	// If the last char of base path is \", remove it
	// Cannot remember why I needed to do this
	if (*(m_base_path.rbegin()) == '\"') m_base_path.erase(m_base_path.end() - 1);
	return true;
}

// Get db path
m_base_path = m_header.substr(0, pos);

// Get rid of db path
m_header = m_header.substr(pos);

// Trim db path
pos = m_base_path.find_last_not_of((char) ' ') + 1;
m_base_path = m_base_path.substr(0, pos);

// If the last char of base path is \", remove it
// Cannot remember why I needed to do this
if (*(m_base_path.rbegin()) == '\"') m_base_path.erase(m_base_path.end() - 1);

// Prepare a list of params
pos = 0;
tStr s;
while ((pos != std::string::npos) && (pos < m_header.length()))
{
	pos = m_header.find((char) '-', pos);
	if (pos != std::string::npos)
	{
		pos++;
		s = m_header.at(pos);
		m_param_list.push_back(s);
	}
}

return true;

}

long int csdbheader::get_version(void)
{
return m_csdbver;
}

tStr csdbheader::get_base_path(void)
{
return m_base_path;
}

tVecStr csdbheader::get_param_list(void)
{
return m_param_list;
}

long int csdbheader::get_trailer_start(void)
{
return m_trailer_start;
}

void csdbheader::print_contents(void)
{
	unsigned int i;
	printf("Ver= %lu\nPath= \"%s\"\nTrailer start= %lu\nParam list= ", m_csdbver, m_base_path.c_str(), m_trailer_start);
	for (i=0; i< m_param_list.size(); i++)
	{
		printf("\"%s\", ", m_param_list[i].c_str());
	}
	printf("\n\n");
}