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
|
/***************************************************************************
* Copyright (C) 2004 by Daniel Rocher *
* daniel.rocher@adella.org *
* *
* 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, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QtCore>
#include "linecore.h"
/**
\class LineCore
\brief Parse lines of %smbstatus reply.
\date 2008-11-11
\version 1.2
\author Daniel Rocher
\sa smbstatus
*/
int LineCore::compteur_objet=0;
// tolerance in the research of the beginning or the end of a field - advised: 2
const short int LineCore::back=2;
LineCore::LineCore(QObject *parent)
: QObject(parent)
{
debugQt("Object LineCore : "+QString::number(++compteur_objet));
initHead=false; // true if InitHeader has been processed
// true if Analysis has been processed
analysisProc=false;
}
LineCore::~LineCore()
{
debugQt("Object LineCore : "+QString::number(--compteur_objet));
listElement.clear();
}
/**
Init header. Received header for one section.
\param OneHeader header of one section (connections, shares, locked files, ...).
Example:
\verbatim
Pid DenyMode Access R/W Oplock Name
\endverbatim
*/
void LineCore::InitHeader(const QString & OneHeader)
{
debugQt("LineCore::InitHeader("+OneHeader+")");
initHead=true;
header=OneHeader;
analysisProc=false;
// Clear valuelist
listElement.clear();
}
/**
Find field on header
Example: "Pid", "DenyMode", .
\param element field
\return index or -1 if field doesn't exist
\sa InitHeader
*/
int LineCore::InitElement(const QString & element)
{
debugQt("LineCore::InitElement("+element+")");
if (!initHead) return false;
record mon_record;
mon_record.Begin=header.indexOf (element,0,Qt::CaseInsensitive);
// field doesn't exist
if (mon_record.Begin==-1) return -1;
// find first space+characters after 'element'
mon_record.End=header.indexOf(QRegExp("[\\s][\\S]"),mon_record.Begin+element.length());
mon_record.New_Begin=0;
mon_record.New_End=0;
mon_record.Value="";
mon_record.Ident=element;
// add field characteristics on a record
listElement.append(mon_record);
return listElement.size()-1;
}
/**
Analysis one line
\param OneLine one line of %smbstatus (without the header).
Example:
\verbatim
3456 DENY_NONE 0x1 RDONLY NONE /home/villou/Utilitaires/qtsmbstatus.pdf Fri Sep 10 14:14:12 2004
\endverbatim
*/
void LineCore::Analysis(const QString & OneLine)
{
debugQt("LineCore::Analysis()");
if (!initHead) return;
// if first time in this section, sort
if (!analysisProc) SortElements();
analysisProc=true;
line=OneLine;
int resu=0;
int CumulDelta=0;
QList<record>::iterator itI;
for (itI = listElement.begin(); itI != listElement.end(); ++itI )
{
(*itI).New_Begin=(*itI).Begin;
(*itI).New_End=(*itI).End;
// if not first field
if ((*itI).Begin!=0)
{
resu=line.indexOf(QRegExp("[\\s][\\S]"),-back+(*itI).Begin+CumulDelta);
(*itI).New_Begin=resu+1;
CumulDelta=(*itI).New_Begin-(*itI).Begin;
}
// if last field
if ((*itI).End==-1)
{
(*itI).New_End=line.length();
}
else
{
resu=line.indexOf(QRegExp("[\\s][\\S]"),-back+(*itI).End+CumulDelta);
(*itI).New_End=resu;
CumulDelta=(*itI).New_End-(*itI).End;
}
(*itI).Value=(line.mid((*itI).New_Begin,(*itI).New_End-(*itI).New_Begin)).trimmed();
/*debugQt( " Begin:"+QString::number((*itI).Begin)+
" End:"+QString::number((*itI).End)+
" New_Begin:"+QString::number((*itI).New_Begin)+
" New_End:"+QString::number((*itI).New_End)+
" CumulDelta:"+QString::number(CumulDelta)+
" Ident:"+(*itI).Ident+
" Value:"+(*itI).Value); */
}
}
/**
Return one field contents
\param element field name
\return one field contents
*/
QString LineCore::ReturnElement(const QString & element)
{
debugQt("LineCore::ReturnElement("+element+")");
if (!initHead || !analysisProc) return "";
QList<record>::iterator itI;
for (itI = listElement.begin(); itI != listElement.end(); ++itI )
{
if (((*itI).Ident).toLower()==element.toLower ()) return ((*itI).Value);
}
// if doesn't exist
return "";
}
/**
Return one field contents
\param index index of field
\return one field contents
*/
QString LineCore::ReturnElement(int index)
{
debugQt("LineCore::ReturnElement("+QString::number(index)+")");
if (!initHead || !analysisProc) return "";
if ( index >= 0 && index < listElement.size()) {
return listElement.at(index).Value;
} else {
// if doesn't exist
return "";
}
}
/**
Sort records
**/
void LineCore::SortElements()
{
debugQt("LineCore::SortElements()");
if (!initHead) return;
int plus_petit;
QList<record>::iterator itI;
QList<record>::iterator itJ;
QList<record>::iterator position;
record mon_record;
for (itI = listElement.begin(); itI != listElement.end(); ++itI )
{
plus_petit=(*itI).Begin;
position=itI;
for (itJ = itI; itJ != listElement.end(); ++itJ)
{
if ((*itJ).Begin < plus_petit) {
plus_petit=(*itJ).Begin;
position=itJ;
}
}
if (position!=itI)
{
mon_record=*itI;
*itI=*position;
*position=mon_record;
}
}
}
|