File: info.cpp

package info (click to toggle)
guymager 0.8.8-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,832 kB
  • sloc: cpp: 17,293; makefile: 30; sh: 18
file content (283 lines) | stat: -rw-r--r-- 7,302 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
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
// ****************************************************************************
//  Project:        GUYMAGER
// ****************************************************************************
//  Programmer:     Guy Voncken
//                  Police Grand-Ducale
//                  Service de Police Judiciaire
//                  Section Nouvelles Technologies
// ****************************************************************************
//  Module:         Information about the acquisition, creates the info file
// ****************************************************************************

// Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
// Guy Voncken
//
// This file is part of Guymager.
//
// Guymager 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.
//
// Guymager 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 Guymager. If not, see <http://www.gnu.org/licenses/>.

#include <errno.h>

#include <QtCore>

#include "common.h"

#include "qtutil.h"
#include "util.h"
#include "device.h"
#include "config.h"

const char *pInfoDefaultColSep = "::";
const char *pInfoDefaultColSepReplace = ":";


class t_InfoLocal
{
   public:
      t_pcAcquisition pAcquisition;
      QMutex           Mutex;
      QString          ColSep;
      QString          ColSepReplace;
      QStringList      TableRows;
};


t_Info::t_Info (t_pcAcquisition pAcquisition)
{
   static bool Initialised = false;

   if (!Initialised)
   {
      CHK_EXIT (TOOL_ERROR_REGISTER_CODE (ERROR_INFO_FILE_CREATE_FAILED))
      CHK_EXIT (TOOL_ERROR_REGISTER_CODE (ERROR_INFO_FILE_OPEN_FAILED  ))
      CHK_EXIT (TOOL_ERROR_REGISTER_CODE (ERROR_INFO_FILE_WRITE_FAILED ))
      CHK_EXIT (TOOL_ERROR_REGISTER_CODE (ERROR_INFO_FILE_CLOSE_FAILED ))

      Initialised = true;
   }
   pOwn = new t_InfoLocal;
   pOwn->pAcquisition = pAcquisition;
   CHK_EXIT (InitTable (pInfoDefaultColSep, pInfoDefaultColSepReplace))
}

t_Info::~t_Info ()
{
   delete pOwn;
}

QString t_Info::FullFileName ()
{
   return pOwn->pAcquisition->InfoPath + pOwn->pAcquisition->InfoFilename + t_File::pExtensionInfo;
}


APIRET t_Info::Create (void)
{
   FILE *pFile;

   pOwn->Mutex.lock();
      pFile = fopen (QSTR_TO_PSZ(FullFileName()), "w");
      if (pFile == NULL)
      {
          pOwn->Mutex.unlock();
          LOG_ERROR ("File %s cannot be opened. errno=%d '%s'", QSTR_TO_PSZ(FullFileName()), errno, ToolErrorTranslateErrno (errno))
          return ERROR_INFO_FILE_CREATE_FAILED;
      }
      if (UtilClose (pFile))
      {
          pOwn->Mutex.unlock();
          return ERROR_INFO_FILE_CLOSE_FAILED;
      }
   pOwn->Mutex.unlock();

   return NO_ERROR;
}

APIRET t_Info::vWrite (const char *pFormat, va_list pArguments)
{
   FILE *pFile;
   int    Ret;

   pOwn->Mutex.lock();
      pFile = fopen (QSTR_TO_PSZ(FullFileName()), "a");

      if (pFile == NULL)
      {
          pOwn->Mutex.unlock();
          return ERROR_INFO_FILE_OPEN_FAILED;
      }

      Ret = vfprintf (pFile, pFormat, pArguments);
      if (Ret < 0)
      {
          pOwn->Mutex.unlock();
          return ERROR_INFO_FILE_WRITE_FAILED;
      }

      Ret = fclose (pFile);
   pOwn->Mutex.unlock();

   if (Ret < 0)
       return ERROR_INFO_FILE_CLOSE_FAILED;

   return NO_ERROR;
}

APIRET t_Info::Write (const char *pFormat, ...)
{
   va_list VaList;  //lint -esym(530,VaList) not initialised

   va_start (VaList, pFormat);
   CHK (vWrite (pFormat, VaList))
   va_end (VaList);

   return NO_ERROR;
}

APIRET t_Info::Write (const QString &Text)
{
   CHK (Write ("%s", QSTR_TO_PSZ(Text)))
   return NO_ERROR;
}

APIRET t_Info::WriteLn (const QString &Text)
{
   CHK (Write ("\r\n%s", QSTR_TO_PSZ(Text)))
   return NO_ERROR;
}

APIRET t_Info::Title (const QString &Text)
{
   int Len;

   CHK (WriteLn (Text))
   CHK (WriteLn ())
   Len = Text.length();
   for (int i=0; i<Len; i++)
      CHK (Write ("="))

   return NO_ERROR;
}

APIRET t_Info::WriteDeviceInfo (void)
{
   QString DeviceInfo;

   CHK (GetDeviceInfo (pOwn->pAcquisition->pDevice, false, DeviceInfo))

   CHK (Write (DeviceInfo))

   return NO_ERROR;
}

APIRET t_Info::GetDeviceInfo (t_pcDevice pDevice, bool RichText, QString &Info)
{
   QStringList *pDeviceInfoCommands;
   QString       Command;
   QString       Result;
   APIRET        rc;
   int           i;

   CHK (CfgGetDeviceInfoCommands (&pDeviceInfoCommands))
   for (i = 0; i < pDeviceInfoCommands->size(); i++)
   {
      Command = pDeviceInfoCommands->at(i);

      Command.replace ("%dev", pDevice->LinuxDevice, Qt::CaseInsensitive);
      if (i>0)
         Info += "\r\n\r\n";
      if (RichText)
         Info += "<b>";
      Info += tr("Command executed: %1") .arg(Command);

      rc = QtUtilProcessCommand (Command, &Result);
      if (rc == ERROR_QTUTIL_COMMAND_TIMEOUT)
      {
         Info += "\r\n" + tr("No information can be displayed as a timeout occurred while executing the command.");
         if (RichText)
            Info += "</b>";
      }
      else
      {
         CHK(rc)
         Info += "\r\n" + tr("Information returned:");
         if (RichText)
              Info += "</b>";
         else Info += "\r\n----------------------------------------------------------------------------------------------------";
         Result.replace ("\n\r", "\n");      // Make sure that every new line of the result
         Result.replace ("\r\n", "\n");      // has a CRLF (for Wind compatibility) and that
         Result.replace ("\n", "\r\n   ");   // the result lines are indented by 3 spaces.
         Result = Result.trimmed();
         Info += "\r\n   "  + Result;
         CHK (rc)
      }
   }

   return NO_ERROR;
}

APIRET t_Info::InitTable (const QString &ColSep, const QString &ColSepReplace)
{
   pOwn->ColSep        = ColSep;
   pOwn->ColSepReplace = ColSepReplace;
   pOwn->TableRows.clear();

   return NO_ERROR;
}

APIRET t_Info::AddRow (const QString &Row)
{
   pOwn->TableRows.append (Row);

   return NO_ERROR;
}

APIRET t_Info::WriteTable (void)
{
   QStringList ColList;
   QList<int>  ColWidth;
   int         r, c;

   // Calculate column widths
   // -----------------------
   for (r=0; r<pOwn->TableRows.count(); r++)
   {
      ColList = pOwn->TableRows[r].split (pOwn->ColSep);
      for (c=0; c<ColList.count(); c++)
      {
         if (c >= ColWidth.count())
            ColWidth.append (0);
         ColWidth[c] = GETMAX (ColWidth[c], ColList[c].length());
      }
   }

   // Write to info
   // -------------
   for (r=0; r<pOwn->TableRows.count(); r++)
   {
      CHK (WriteLn ())
      ColList = pOwn->TableRows[r].split (pOwn->ColSep);
      for (c=0; c<ColList.count(); c++)
      {
         CHK (Write (ColList[c].leftJustified (ColWidth[c])))
         if (c<ColList.count()-1)
            CHK (Write (pOwn->ColSepReplace))
      }
   }
   pOwn->TableRows.clear();

   return NO_ERROR;
}