summaryrefslogtreecommitdiff
path: root/support/dktools/DkWxBmepsThread.cpp
blob: f79a017cda2ef2ed88d32a21bf87ea59e099c050 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: DkWxBmepsThread.cpt
*/

/*
Copyright (C) 2011-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file DkWxBmepsThread.cpp The DkWxBmepsThread module.
*/


#line 127 "DkWxBmepsThread.cpt"

#include "wxbmeps.h"



#line 131 "DkWxBmepsThread.cpt"



/**	File name separator.
*/
static wxChar const	dkwxbmeps_fnsep[] = {
DK3_WX_CHAR_SEP , wxT('\0')
};



static wxChar const * const	dkwxbmeps_output_suffixes[] = {
wxT(".ps"),
wxT(".eps"),
wxT(".pdf"),
wxT(".bb")
};




/**	Non-localized  keywords used by this module.
*/
static wxChar const * const	dkwxbmeps_thread_kw[] = {
/* 0 */
wxT("*.*"),

NULL


#line 161 "DkWxBmepsThread.cpt"
};



/**	File suffixes we can handle.
*/
static wxChar const * const	dkwxbmeps_thread_suffixes[] = {
#if DK3_HAVE_PNG_H
wxT(".png"),
#endif
#if DK3_HAVE_JPEGLIB_H
wxT(".jpg"),
wxT(".jpeg"),
#endif
#if DK3_HAVE_TIFF_H
wxT(".tif"),
wxT(".tiff"),
#endif
NULL
};



DkWxBmepsThread::DkWxBmepsThread(
      DkWxCommunicator		*pc,
      wxChar const * const	*st,
      dkChar const * const	*m,
      wxChar const		*sd,
      DkWxAppHelper		*ph,
      int			 wxe,
      int			 dke,
      dk3_bm_eps_options_t	*bmeoptr
)
{
  

#line 196 "DkWxBmepsThread.cpt"
  pComm = pc;
  sTexts = st;
  msg = m;
  sDirectory = dk3wxs_dup_app(sd, ph->getApp());
  pHelper = ph;
  wxenc = wxe;
  dkenc = dke;
  dk3mem_cpy((void *)(&bmeo), (void *)bmeoptr, sizeof(dk3_bm_eps_options_t));
  bmeo.to_stdout = 0;
  

#line 206 "DkWxBmepsThread.cpt"
}



bool
DkWxBmepsThread::checkSetup()
{
  bool		back = false;
  

#line 215 "DkWxBmepsThread.cpt"
  if(sDirectory) {
    back = true;
  } 

#line 218 "DkWxBmepsThread.cpt"
  return back;
}



DkWxBmepsThread::~DkWxBmepsThread()
{
  

#line 226 "DkWxBmepsThread.cpt"
  dk3_release(sDirectory);
  

#line 228 "DkWxBmepsThread.cpt"
}



void *
DkWxBmepsThread::Entry()
{
#if 0
  /*
	Simple test thread without practical use.
  */
  int	i = 0;
  int	cc = 1;
  int	numbreak = -1;
  do {
    this->Sleep(100);
    i++;
    if(i >= 10) {
      cc = 0;
    }
    pComm->setUpdates(wxT("test-file"), (100*i));
    if(pComm->getCanContinue() == 0) {
      if(numbreak == -1) {
        numbreak = i;
      } else {
        if(i >= numbreak + 15) {
	  cc = 0;
	}
      }
    }
    pComm->addWxText(wxT("Test mit einer ganz ganz ganz ganz langen Zeile\n"));
  } while(cc);
  pComm->setRunning(0);
#else
  

#line 263 "DkWxBmepsThread.cpt"
  if(wxFileName::DirExists(sDirectory)) {	

#line 264 "DkWxBmepsThread.cpt"
    runForDirectory(sDirectory);
  } else {					

#line 266 "DkWxBmepsThread.cpt"
    if(wxFileName::FileExists(sDirectory)) {	

#line 267 "DkWxBmepsThread.cpt"
      runForFile(sDirectory, 0, 1000);
    } else {					

#line 269 "DkWxBmepsThread.cpt"
      /* ##### ERROR: No such file or directory! */
    }
  }
  pComm->setRunning(0); 

#line 273 "DkWxBmepsThread.cpt"
#endif
  return NULL;
}



void
DkWxBmepsThread::OnExit()
{
  /* Doing nothing. */
  

#line 284 "DkWxBmepsThread.cpt"
}



/**	Compare two wxChar file names.
	@param	l	Left file name.
	@param	r	Right file name.
	@param	cr	Comparison criteria (ignored).
	@return	Comparison result.
*/
static
int
dkwxbmeps_compare_filenames(void const *l, void const *r, int cr)
{
  int		back	= 0;
  if(l) {
    if(r) {
#if DK3_HAVE_FNCASEINS
      back = dk3wxs_casecmp((wxChar const *)l, (wxChar const *)r);
#else
      back = dk3wxs_cmp((wxChar const *)l, (wxChar const *)r);
#endif
      if(back < -1) back = -1;
      if(back >  1) back =  1;
    } else {
      back = 1;
    }
  } else {
    if(r) {
      back = -1;
    }
  }
  return back;
}



void
DkWxBmepsThread::runForFile(wxChar const *fn, int ming, int maxg)
{
  dkChar	 sfn[DK3_MAX_PATH];
  wxChar const	*shortname;
  

#line 327 "DkWxBmepsThread.cpt"
  shortname = shortFileName(fn);
  if(dk3wxs_to_dkstr(
      sfn,
      DK3_SIZEOF(sfn,dkChar),
      pHelper->getDkEncoding(),
      fn,
      pHelper->getWxEncoding()
    )
  )
  {
    /* PROGRESS MESSAGE Start */
    pComm->addWxText(sTexts[114]);
    pComm->addWxText(shortname);
    pComm->addWxText(sTexts[115]);
    pComm->addWxText(sTexts[110]);
    (void)dk3bm_process_file_name(
      &bmeo,
      sfn,
      DK3_BIF_IMAGE_TYPE_UNKNOWN,
      ming,
      maxg,
      (void *)pComm
    );
    /* PROGRESS MESSAGE Finished */
#if 0
    pComm->addWxText(sTexts[116]);
    pComm->addWxText(shortname);
    pComm->addWxText(sTexts[117]);
    pComm->addWxText(sTexts[110]);
#endif
    pComm->addWxText(sTexts[110]);
  } else { 

#line 359 "DkWxBmepsThread.cpt"
    /* ERROR: Failed to convert file name! */
    pComm->addWxText(sTexts[118]);
    pComm->addWxText(shortname);
    pComm->addWxText(sTexts[119]);
    pComm->addWxText(sTexts[110]);
    pComm->setLogLevel(DK3_LL_ERROR);
  }
  pComm->setUpdates(NULL, maxg);
  

#line 368 "DkWxBmepsThread.cpt"
}






void
DkWxBmepsThread::runForDirectory(wxChar const *dn)
{
  wxChar		namein[DK3_MAX_PATH];
  wxChar const		*currentfn;
  wxChar const		*cfilename;
  wxChar		*suffix;
  dk3_sto_t		*sFiles;
  dk3_sto_it_t		*iFiles;
  size_t		fnlgt;
  int			namesFound;
  int			i;
  int			promille;
  int			cc;

  

#line 391 "DkWxBmepsThread.cpt"
  pComm->addWxText(sTexts[106]);
  pComm->addWxText(dn);
  pComm->addWxText(sTexts[107]);
  pComm->addWxText(sTexts[110]);
  pComm->addWxText(sTexts[110]);
  sFiles = dk3sto_open_app(pHelper->getApp());
  if(sFiles) {
    dk3sto_set_comp(sFiles, dkwxbmeps_compare_filenames, 0);
    iFiles = dk3sto_it_open(sFiles);
    if(iFiles) {
      /*
      	Find all input filename candidates.
      */
      namesFound = 0;
      {
        wxLogNull	lognull;
	bool		canContinue;
        wxDir directory(sDirectory);
	if(directory.IsOpened()) {
	  wxString	filename;
	  canContinue = directory.GetFirst(
	    &filename, dkwxbmeps_thread_kw[0], wxDIR_FILES
	  );
	  while(canContinue) {
	    cfilename = filename.c_str();
	    if(cfilename) {
	      if(dk3wxs_len(cfilename) < DK3_SIZEOF(namein,wxChar)) {
	        dk3wxs_cpy(namein, cfilename);
		suffix = dk3wxs_get_suffix(namein);
		if(suffix) {
		  if(dk3wxs_array_index(dkwxbmeps_thread_suffixes,suffix,0)
		     > -1
		  )
		  {
		    suffix = dk3wxs_dup_app(namein, pHelper->getApp());
		    if(suffix) {
		      if(dk3sto_add(sFiles, suffix)) {
		        namesFound++; 

#line 429 "DkWxBmepsThread.cpt"
		      } else {
		        /* ERROR: Memory */
			dk3_release(suffix);
			pComm->addWxText(sTexts[105]);
			pComm->addWxText(sTexts[110]);
			pComm->setLogLevel(DK3_LL_ERROR);
		      }
		    } else {
		      /* ERROR: Memory */
		      pComm->addWxText(sTexts[105]);
		      pComm->addWxText(sTexts[110]);
		      pComm->setLogLevel(DK3_LL_ERROR);
		    }
		  }
		}
	      } else {
	        /* ERROR: Name too long! */
		pComm->addWxText(sTexts[111]);
		pComm->addWxText(cfilename);
		pComm->addWxText(sTexts[112]);
		pComm->addWxText(sTexts[110]);
		pComm->setLogLevel(DK3_LL_ERROR);
	      }
	    }
	    canContinue = directory.GetNext(&filename);
	  }
	}
      }
      pComm->setUpdates(NULL, 50);
      /*
      	Process all input file name candidates.
      */
      if(namesFound > 0) {
        dk3sto_it_reset(iFiles);
	i = 0;
#if VERSION_BEFORE_20150821
	/* Immediately set to 0 at loop begin */
	cc = 1;
#endif
	do {
	  cc = 0;
	  cfilename = (wxChar const *)dk3sto_it_next(iFiles);
	  if(cfilename) {
	    cc = 1;
	    promille = 50 + (950 * i )/namesFound;
	    if(promille >= 1000) { promille = 999; }
	    pComm->setUpdates(cfilename, promille);
	    

#line 477 "DkWxBmepsThread.cpt"
	    fnlgt = dk3wxs_len(sDirectory);
	    fnlgt += dk3wxs_len(cfilename);
	    fnlgt += 1;
	    if(fnlgt < DK3_SIZEOF(namein,wxChar)) {
	      dk3wxs_cpy(namein, sDirectory);
	      dk3wxs_cat(namein, dkwxbmeps_fnsep);
	      dk3wxs_cat(namein, cfilename);
	      if(checkMustRun(namein)) {
	        runForFile(
	          namein,
		  50 + (950 * i )/namesFound,
		  50 + (950 * (i + 1))/namesFound
	        );
	      }
	    } else {
	      /* ERROR: File name too long for buffer */
	      pComm->addWxText(sTexts[111]);
	      pComm->addWxText(sDirectory);
	      pComm->addWxText(dkwxbmeps_fnsep);
	      pComm->addWxText(cfilename);
	      pComm->addWxText(sTexts[112]);
	      pComm->addWxText(sTexts[110]);
	      pComm->setLogLevel(DK3_LL_ERROR);
	    }
	    i++;
	    promille = 50 + (950 * i )/namesFound;
	    if(promille >= 1000) { promille = 999; }
	    pComm->setUpdates(cfilename, promille);
	    if(!(pComm->getCanContinue())) {
	      cc = 0;
	    }
	  }
	} while(cc);
	pComm->setUpdates(cfilename, 1000);
      } else {
        /* ERROR: No names found! */
	pComm->addWxText(sTexts[113]);
	pComm->addWxText(sTexts[110]);
	pComm->setLogLevel(DK3_LL_ERROR);
      }
      /*
      	Release the file names.
      */
      dk3sto_it_reset(iFiles);
      while((currentfn = (wxChar const *)dk3sto_it_next(iFiles)) != NULL) {
        dk3_release(currentfn);
      }
      dk3sto_it_close(iFiles);
    } else {
      pComm->addWxText(sTexts[105]);
      pComm->addWxText(sTexts[110]);
      pComm->setLogLevel(DK3_LL_ERROR);
    }
    dk3sto_close(sFiles);
  } else {
    pComm->addWxText(sTexts[105]);
    pComm->addWxText(sTexts[110]);
    pComm->setLogLevel(DK3_LL_ERROR);
  }
  pComm->addWxText(sTexts[108]);
  pComm->addWxText(dn);
  pComm->addWxText(sTexts[109]);
  pComm->addWxText(sTexts[110]);
  

#line 541 "DkWxBmepsThread.cpt"
}



bool
DkWxBmepsThread::checkMustRun(wxChar const *sfn)
{
  wxChar	nameout[DK3_MAX_PATH];
  wxChar	*suffix;
  wxChar const	*nsuffix;
  bool		back	= true;
  if(bmeo.make) {
    dk3wxs_cpy(nameout, sfn);
    suffix = dk3wxs_get_suffix(nameout);
    if(suffix) {
      *suffix = wxT('\0');
      nsuffix = NULL;
      switch(bmeo.dr) {
        case DK3_BMEPS_DRIVER_PS: {
	  nsuffix = dkwxbmeps_output_suffixes[0];
	} break;
	case DK3_BMEPS_DRIVER_EPS: {
	  nsuffix = dkwxbmeps_output_suffixes[1];
	} break;
	case DK3_BMEPS_DRIVER_PDF: {
	  nsuffix = dkwxbmeps_output_suffixes[2];
	} break;
	case DK3_BMEPS_DRIVER_BB: {
	  nsuffix = dkwxbmeps_output_suffixes[3];
	} break;
      }
      if(nsuffix) {
        if((dk3wxs_len(nameout) + dk3wxs_len(nsuffix))
	   < DK3_SIZEOF(nameout,wxChar)
	)
	{
	  dk3wxs_cat(nameout, nsuffix);
	  if(wxFileName::FileExists(sfn)) {
	    if(wxFileName::FileExists(nameout)) {
	      wxFileName	fni(sfn);
	      wxFileName	fno(nameout);
	      if(fno.GetModificationTime() > fni.GetModificationTime()) {
	        back = false;
	      }
	    }
	  } else {
	    /* ##### BUG: Source file does not exist, should not happen. */
	  }
	} else {
	  /* ##### ERROR: Name too long! */
	}
      } else {		

#line 593 "DkWxBmepsThread.cpt"
        /* BUG: Illegal output driver, should not happen. */
      }
    }
  }
  return back;
}



wxChar const *
DkWxBmepsThread::shortFileName(wxChar const *longName)
{
  wxChar const	*back	= NULL;
  wxChar const	*ptr;

  if(longName) {
    ptr = longName;
    while(*ptr) {
      if(*ptr == DK3_WX_CHAR_SEP) {
        back = ptr;
      }
      ptr++;
    }
    if(back) { back++; }
    else { back = longName; }
  }
  return back;
}