File: csdbheader.cpp

package info (click to toggle)
codequery 0.26.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,332 kB
  • sloc: cpp: 106,043; xml: 16,576; python: 4,187; perl: 244; makefile: 11
file content (150 lines) | stat: -rw-r--r-- 3,035 bytes parent folder | download | duplicates (5)
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

/*
 * CodeQuery
 * Copyright (C) 2013-2017 ruben2020 https://github.com/ruben2020/
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */


// 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");
}