summaryrefslogtreecommitdiffstats
path: root/kmymoney2/mymoney/mymoneyutils.cpp
blob: 08ffff4eb558bbd579173af829fd2cffb1f7daaa (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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/***************************************************************************
                          mymoneyutils.cpp  -  description
                             -------------------
    begin                : Tue Jan 29 2002
    copyright            : (C) 2000-2002 by Michael Edwardes
    email                : mte@users.sourceforge.net
                           Javier Campos Morales <javi_c@users.sourceforge.net>
                           Felix Rodriguez <frodriguez@users.sourceforge.net>
                           John C <thetacoturtle@users.sourceforge.net>
                           Thomas Baumgart <ipwizard@users.sourceforge.net>
                           Kevin Tambascio <ktambascio@users.sourceforge.net>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <iostream>

#include "mymoneyutils.h"
#include "mymoneyaccount.h"

#include <cstdio>
#include <cstdarg>
#include <cstdlib>

#include <tqregexp.h>

#ifdef _CHECK_MEMORY

#undef new
#undef _CheckMemory_Leak
#undef _CheckMemory_FreeAll

_CheckMemory chkmem;
bool enable=false;

_CheckMemoryEntry::_CheckMemoryEntry(void *p, int line, size_t size, const char *file)
  : m_p(p), m_line(line), m_size(size), m_file(file)
{
}

_CheckMemoryEntry::_CheckMemoryEntry()
  : m_p(0), m_line(0), m_size(0)
{
}


/////////////////////////////////////////////////////////////////////////////////////////
_CheckMemory::_CheckMemory()
{
  outfunc = (_CheckMemoryOutFunc *)NULL;
}

_CheckMemory::_CheckMemory(_CheckMemoryOutFunc *out)
{
  outfunc = out;
}

_CheckMemory::~_CheckMemory()
{
}

_CheckMemoryOutFunc *_CheckMemory::SetOutFunc(_CheckMemoryOutFunc *out)
{
  _CheckMemoryOutFunc *old;
  old = outfunc;
  outfunc = out;
  return old;
}

void _CheckMemory::Output(const char *fmt,...)
{
  va_list args;
  char buf[128];
  va_start(args,fmt);
  if(outfunc) {
    vsprintf(buf,fmt,args);
    outfunc(buf);
  }
  else {
    vfprintf(stderr,fmt,args);
    putc('\n', stderr);
  }
  va_end(args);
}

int _CheckMemory::TableCount(void)
{
  return table.size();
}

bool _CheckMemory::CheckMemoryLeak(bool freeall)
{
  bool d = false;
  size_t total = 0;
  int freec = 0;
  CheckMemoryTable::ConstIterator it;

  for(it = table.begin(); it != table.end(); ++it) {
    if((*it).pointer() != 0) {
      total += (*it).size();
      freec++;
      if(d == false) {
        Output("CheckMemory++: CheckMemoryLeak: Memory leak detected!");
        Output("Position  |Size(bytes)  |Allocated at");
        d=true;
      }
      if(d==true)
        Output("%p |%-13d|%s:%d",(*it).pointer(),(int)(*it).size(),(*it).file(),(*it).line());
    }
  }
  if(d == true)
    Output("You have forgotten to free %d object(s), %d bytes of memory.",freec, (int)total);
  else
    Output("CheckMemory++: CheckMemoryLeak: No memory leak detected.");
  if(freeall == true)
    FreeAll();
  return true;
}

void _CheckMemory::FreeAll()
{
  size_t total=0;
  int freec=0;
  CheckMemoryTable::Iterator it;

  for(it = table.begin(); it != table.end(); it = table.begin()) {
    if((*it).pointer() != 0) {
      total += (*it).size();
      freec++;
      Output("CheckMemory++: FreeAll: freed %d bytes of memory at %p.",(int)(*it).size(),(*it).pointer());
      free((*it).pointer());
    }
    table.remove(it);
  }
  Output("CheckMemory++: FreeAll: Totally freed %d objects, %d bytes of memory.",freec,(int)total);
}

void _CheckMemory_Init(_CheckMemoryOutFunc *out)
{
  if(enable!=true) {
    chkmem.Restart();
    chkmem.SetOutFunc(out);
    enable=true;
  }
}

void _CheckMemory_End()
{
  if(enable!=false) {
    chkmem.Restart();
    chkmem.SetOutFunc(NULL);
    enable=false;
  }
}

/////////////////////////////////////////////////////////////////////////////////////////
void *operator new(size_t s,const char *file,int line) throw()
{
  void *p = malloc(s);

  if(p == NULL) throw;
  if(enable==true) {
    _CheckMemoryEntry entry(p, line, s, file);
    chkmem.table[p] = entry;
  }
  return p;
}

void * operator new [] (size_t s,const char *file,int line) throw()
{
  void *p = malloc(s);

  if(p == NULL) throw;
  if(enable==true) {
    _CheckMemoryEntry entry(p, line, s, file);
    chkmem.table[p] = entry;
  }
  return p;
}

void operator delete(void *p) throw()
{
  if(enable==true) {
    CheckMemoryTable::Iterator it;
    it = chkmem.table.find(p);
    if(it != chkmem.table.end()) {
      chkmem.table.remove(it);
    }
  }
  free(p);
}

void operator delete [] (void *p) throw()
{
  if(enable==true) {
    CheckMemoryTable::Iterator it;
    it = chkmem.table.find(p);
    if(it != chkmem.table.end()) {
      chkmem.table.remove(it);
    }
  }
  free(p);
}

#endif // _CHECK_MEMORY

TQString MyMoneyUtils::getFileExtension(TQString strFileName)
{
  TQString strTemp;
  if(!strFileName.isEmpty())
  {
    //find last . delminator
    int nLoc = strFileName.findRev('.');
    if(nLoc != -1)
    {
      strTemp = strFileName.right(strFileName.length() - (nLoc + 1));
      return strTemp.upper();
    }
  }
  return strTemp;
}

int MyMoneyTracer::m_indentLevel = 0;
int MyMoneyTracer::m_onoff = 0;

MyMoneyTracer::MyMoneyTracer(const char* name)
{
  if(m_onoff) {
    TQRegExp exp("(.*)::(.*)");
    if(exp.search(name) != -1) {
      m_className = exp.cap(1);
      m_memberName = exp.cap(2);
    } else {
      m_className = TQString(name);
      m_memberName = TQString();
    }
    TQString indent;
    indent.fill(' ', m_indentLevel);
    std::cerr << indent.latin1() << "ENTER: " << m_className.latin1() << "::" << m_memberName.latin1() << std::endl;
  }
  m_indentLevel += 2;
}

MyMoneyTracer::MyMoneyTracer(const TQString& className, const TQString& memberName) :
  m_className(className),
  m_memberName(memberName)
{
  if(m_onoff) {
    TQString indent;
    indent.fill(' ', m_indentLevel);
    std::cerr << indent.latin1() << "ENTER: " << m_className.latin1() << "::" << m_memberName.latin1() << std::endl;
  }
  m_indentLevel += 2;
}

MyMoneyTracer::~MyMoneyTracer()
{
  m_indentLevel -= 2;
  if(m_onoff) {
    TQString indent;
    indent.fill(' ', m_indentLevel);
    std::cerr << indent.latin1() << "LEAVE: " << m_className.latin1() << "::" << m_memberName.latin1() << std::endl;
  }
}

void MyMoneyTracer::printf(const char *format, ...) const
{
  if(m_onoff) {
    va_list args;
    va_start(args, format);
    TQString indent;
    indent.fill(' ', m_indentLevel);
    std::cerr << indent.latin1();

    vfprintf(stderr,format,args);
    putc('\n', stderr);
    va_end(args);
  }
}

void MyMoneyTracer::onOff(int onOff)
{
  m_onoff = onOff;
}

void MyMoneyTracer::on(void)
{
  m_onoff = 1;
}

void MyMoneyTracer::off(void)
{
  m_onoff = 0;
}

TQString dateToString(const TQDate& date)
{
  if(!date.isNull() && date.isValid())
    return date.toString(Qt::ISODate);

  return TQString();
}

TQDate stringToDate(const TQString& str)
{
  if(str.length()) {
    TQDate date = TQDate::fromString(str, Qt::ISODate);
    if(!date.isNull() && date.isValid())
      return date;
  }
  return TQDate();
}

TQString TQStringEmpty(const TQString& val)
{
  if(!val.isEmpty())
    return TQString(val);

  return TQString();
}

unsigned long extractId(const TQString& txt)
{
  int pos;
  unsigned long rc = 0;

  pos = txt.find(TQRegExp("\\d+"), 0);
  if(pos != -1) {
    rc = atol(txt.mid(pos));
  }
  return rc;
}